Skip to content

Commit

Permalink
Merge pull request eesast#125 from shangfengh/dev
Browse files Browse the repository at this point in the history
refactor: 🐛 use the LockedClassList
  • Loading branch information
shangfengh authored Mar 12, 2024
2 parents e0923e1 + 8d69451 commit 42d0da0
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 367 deletions.
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GameClass.GameObj.Areas;
public class Wormhole : Immovable, IWormhole
{
public LongInTheVariableRange HP = new LongInTheVariableRange(GameData.WormholeHP);
private List<XY> grids = new();
private readonly List<XY> grids = new();
public List<XY> Grids => grids;
public override bool IsRigid => HP > GameData.WormholeHP / 2;
public override ShapeType Shape => ShapeType.Square;
Expand Down
241 changes: 45 additions & 196 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace GameClass.GameObj
{
public partial class Map : IMap
{
public Dictionary<GameObjType, IList<IGameObj>> GameObjDict { get; }
public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict { get; }
private Dictionary<GameObjType, LockedClassList<IGameObj>> gameObjDict;
public Dictionary<GameObjType, LockedClassList<IGameObj>> GameObjDict => gameObjDict;
private readonly uint height;
public uint Height => height;
private readonly uint width;
Expand Down Expand Up @@ -62,151 +62,45 @@ public IOutOfBound GetOutOfBound(XY pos)
{
return new OutOfBoundBlock(pos);
}

public Ship? FindShipInID(long ID)
{
Ship? ship = null;
GameObjLockDict[GameObjType.Ship].EnterReadLock();
try
{
foreach (Ship s in GameObjDict[GameObjType.Ship].Cast<Ship>())
{
if (s.ID == ID)
{
ship = s;
break;
}
}
}
finally
{
GameObjLockDict[GameObjType.Ship].ExitReadLock();
}
return ship;
return (Ship?)GameObjDict[GameObjType.Ship].Find(gameObj => (ID == ((Ship)gameObj).ID));
}
public Ship? FindShipInShipID(long shipID)
{
Ship? ship = null;
GameObjLockDict[GameObjType.Ship].EnterReadLock();
try
{
foreach (Ship s in GameObjDict[GameObjType.Ship].Cast<Ship>())
{
if (s.ShipID == shipID)
{
ship = s;
break;
}
}
}
finally
return (Ship?)GameObjDict[GameObjType.Ship].Find(gameObj => (shipID == ((Ship)gameObj).ShipID));
}

public bool WormholeInteract(Wormhole gameObj, XY Pos)
{
foreach (XY xy in gameObj.Grids)
{
GameObjLockDict[GameObjType.Ship].ExitReadLock();
if (GameData.ApproachToInteract(xy, Pos))
return true;
}
return ship;
return false;
}
public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
{
GameObj? GameObjForInteract = null;
GameObjLockDict[gameObjType].EnterReadLock();
try
{
foreach (GameObj gameObj in GameObjDict[gameObjType].Cast<GameObj>())
{
if (gameObjType == GameObjType.Wormhole)
{
bool flag = false;
foreach (XY xy in ((Wormhole)gameObj).Grids)
{
if (GameData.ApproachToInteract(xy, Pos))
{
GameObjForInteract = gameObj;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
else
{
if (GameData.ApproachToInteract(gameObj.Position, Pos))
{
GameObjForInteract = gameObj;
break;
}
}
}
}
finally
{
GameObjLockDict[gameObjType].ExitReadLock();
}
return GameObjForInteract;
return (GameObj?)GameObjDict[gameObjType].Find(gameObj =>
((GameData.ApproachToInteract(gameObj.Position, Pos)) ||
(gameObjType == GameObjType.Wormhole && WormholeInteract((Wormhole)gameObj, Pos)))
);
}

public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
{
GameObj? GameObjForInteract = null;
GameObjLockDict[gameObjType].EnterReadLock();
try
{
foreach (GameObj gameObj in GameObjDict[gameObjType].Cast<GameObj>())
{
if (GameData.IsInTheSameCell(gameObj.Position, Pos))
{
GameObjForInteract = gameObj;
break;
}
}
}
finally
{
GameObjLockDict[gameObjType].ExitReadLock();
}
return GameObjForInteract;
return (GameObj?)GameObjDict[gameObjType].Find(gameObj => (GameData.IsInTheSameCell(gameObj.Position, Pos)));
}
public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
{
GameObj? GameObjForInteract = null;
GameObjLockDict[gameObjType].EnterReadLock();
try
{
foreach (GameObj gameObj in GameObjDict[gameObjType].Cast<GameObj>())
{
if (GameData.PartInTheSameCell(gameObj.Position, Pos))
{
GameObjForInteract = gameObj;
break;
}
}
}
finally
{
GameObjLockDict[gameObjType].ExitReadLock();
}
return GameObjForInteract;
return (GameObj?)GameObjDict[gameObjType].Find(gameObj => (GameData.PartInTheSameCell(gameObj.Position, Pos)));
}
public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
{
GameObj? GameObjForInteract = null;
GameObjLockDict[gameObjType].EnterReadLock();
try
{
foreach (GameObj gameObj in GameObjDict[gameObjType].Cast<GameObj>())
{
if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
{
GameObjForInteract = gameObj;
break;
}
}
}
finally
{
GameObjLockDict[gameObjType].ExitReadLock();
}
return GameObjForInteract;
return (GameObj?)GameObjDict[gameObjType].Find(gameObj =>
GameData.ApproachToInteractInACross(gameObj.Position, Pos));
}
public bool CanSee(Ship ship, GameObj gameObj)
{
Expand Down Expand Up @@ -257,70 +151,34 @@ public bool CanSee(Ship ship, GameObj gameObj)
}
public bool Remove(GameObj gameObj)
{
GameObj? ToDel = null;
GameObjLockDict[gameObj.Type].EnterWriteLock();
try
{
foreach (GameObj obj in GameObjDict[gameObj.Type].Cast<GameObj>())
{
if (gameObj.ID == obj.ID)
{
ToDel = obj;
break;
}
}
if (ToDel != null)
{
GameObjDict[gameObj.Type].Remove(ToDel);
ToDel.TryToRemove();
}
}
finally
GameObj? ans = (GameObj?)GameObjDict[gameObj.Type].RemoveOne(obj => gameObj.ID == obj.ID);
if (ans != null)
{
GameObjLockDict[gameObj.Type].ExitWriteLock();
ans.TryToRemove();
return true;
}
return ToDel != null;
return false;
}
public bool RemoveJustFromMap(GameObj gameObj)
{
GameObjLockDict[gameObj.Type].EnterWriteLock();
try
{
if (GameObjDict[gameObj.Type].Remove(gameObj))
{
gameObj.TryToRemove();
return true;
}
return false;
}
finally
if (GameObjDict[gameObj.Type].Remove(gameObj))
{
GameObjLockDict[gameObj.Type].ExitWriteLock();
gameObj.TryToRemove();
return true;
}
return false;
}
public void Add(IGameObj gameObj)
{
GameObjLockDict[gameObj.Type].EnterWriteLock();
try
{
GameObjDict[gameObj.Type].Add(gameObj);
}
finally
{
GameObjLockDict[gameObj.Type].ExitWriteLock();
}
GameObjDict[gameObj.Type].Add(gameObj);
}
public Map(MapStruct mapResource)
{
GameObjDict = [];
GameObjLockDict = [];
gameObjDict = new Dictionary<GameObjType, LockedClassList<IGameObj>>();
foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
{
if (idx != GameObjType.Null)
{
GameObjDict.Add(idx, new List<IGameObj>());
GameObjLockDict.Add(idx, new ReaderWriterLockSlim());
}
gameObjDict.TryAdd(idx, new LockedClassList<IGameObj>());
}
height = mapResource.height;
width = mapResource.width;
Expand All @@ -339,31 +197,22 @@ public Map(MapStruct mapResource)
Add(new Construction(GameData.GetCellCenterPos(i, j)));
break;
case PlaceType.Wormhole:
foreach (Wormhole wormhole in GameObjDict[GameObjType.Wormhole].Cast<Wormhole>())
Func<Wormhole, bool> HasWormhole = (Wormhole wormhole) =>
{
if (wormhole.Grids.Contains(new XY(i, j)))
return true;
foreach (XY xy in wormhole.Grids)
{
hasWormhole = true;
break;
}
else
{
foreach (XY xy in wormhole.Grids)
{
if (Math.Abs(xy.x - i) <= 1 && Math.Abs(xy.y - j) <= 1)
{
wormhole.Grids.Add(new XY(i, j));
hasWormhole = true;
break;
}
}
if (hasWormhole)
if (Math.Abs(xy.x - i) <= 1 && Math.Abs(xy.y - j) <= 1)
{
break;
wormhole.Grids.Add(new XY(i, j));
return true;
}
}
}
if (!hasWormhole)
return false;
};

if (GameObjDict[GameObjType.Wormhole].Cast<Wormhole>().Find(wormhole => HasWormhole(wormhole)) == null)
{
List<XY> grids = [new XY(i, j)];
Add(new Wormhole(GameData.GetCellCenterPos(i, j), grids));
Expand All @@ -376,7 +225,7 @@ public Map(MapStruct mapResource)
}
}
}
Homes = GameObjDict[GameObjType.Home].Cast<Home>().ToList();
Homes = GameObjDict[GameObjType.Home].Cast<Home>().ToNewList();
}
}
}
Loading

0 comments on commit 42d0da0

Please sign in to comment.