Skip to content

Commit

Permalink
Merge pull request #146 from shangfengh/dev
Browse files Browse the repository at this point in the history
refactor: ⚡ delete the ObjOfShipLock
  • Loading branch information
panxuc authored Mar 23, 2024
2 parents dad1d65 + c2330fa commit 541bbf6
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions logic/GameClass/GameObj/GameObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public abstract class GameObj(XY initPos, int initRadius, GameObjType initType)
private static long currentMaxID = 0; // 目前游戏对象的最大ID
public const long invalidID = long.MaxValue; // 无效的ID
public long ID { get; } = Interlocked.Increment(ref currentMaxID);

protected XY position = initPos;
public abstract XY Position { get; }
public abstract bool IsRigid { get; }
Expand Down
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public Map(MapStruct mapResource)
return false;
};

if (GameObjDict[GameObjType.Wormhole].Cast<Wormhole>().Find(wormhole => HasWormhole(wormhole)) == null)
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 @@ -233,7 +233,7 @@ public Map(MapStruct mapResource)
}
}
}
Homes = GameObjDict[GameObjType.Home].Cast<Home>().ToNewList();
Homes = GameObjDict[GameObjType.Home].Cast<Home>()?.ToNewList()!;
}
}
}
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Movable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class Movable(XY initPos, int initRadius, GameObjType initType)
public object ActionLock => actionLock;
private readonly ReaderWriterLockSlim moveReaderWriterLock = new();
/// <summary>
/// 规定MoveReaderWriterLock < ActionLock
/// 规定ActionLock > MoveReaderWriterLock
/// </summary>
public ReaderWriterLockSlim MoveReaderWriterLock => moveReaderWriterLock;
public Semaphore ThreadNum { get; } = new(1, 1);
Expand Down
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/ObjOfShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ namespace GameClass.GameObj
public abstract class ObjOfShip(XY initPos, int initRadius, GameObjType initType)
: Movable(initPos, initRadius, initType), IObjOfShip
{
public object ObjOfShipLock { get; } = new();
private IShip? parent = null;
public IShip? Parent
{
get
{
lock (ObjOfShipLock)
return parent;
return Interlocked.CompareExchange(ref parent, null, null);
}
set
{
lock (ObjOfShipLock)
parent = value;
Interlocked.Exchange(ref parent, value);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
{
if (bullet.CanBeBombed(kvp.Key))
{
beAttackedList.AddRange(gameMap.GameObjDict[kvp.Key].FindAll(gameObj => bullet.CanAttack((GameObj)gameObj)));
var thisList = gameMap.GameObjDict[kvp.Key].FindAll(gameObj => bullet.CanAttack((GameObj)gameObj));
if (thisList != null) beAttackedList.AddRange(thisList);
}
}
foreach (GameObj beAttackedObj in beAttackedList.Cast<GameObj>())
Expand Down
9 changes: 5 additions & 4 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,15 @@ public List<IGameObj> GetGameObj()
{
if (GameData.NeedCopy(keyValuePair.Key))
{
gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key].ToNewList());
var thisList = gameMap.GameObjDict[keyValuePair.Key].ToNewList();
if (thisList != null) gameObjList.AddRange(thisList);
}
}
return gameObjList;
}
public void UpdateBirthPoint()
{
gameMap.GameObjDict[GameObjType.Construction].Cast<Construction>().ForEach(
gameMap.GameObjDict[GameObjType.Construction].Cast<Construction>()?.ForEach(
delegate (Construction construction)
{
if (construction.ConstructionType == ConstructionType.Community)
Expand All @@ -254,7 +255,7 @@ public void UpdateBirthPoint()
{
foreach (XY birthPoint in team.BirthPointList)
{
gameMap.GameObjDict[GameObjType.Construction].Cast<Construction>().ForEach(
gameMap.GameObjDict[GameObjType.Construction].Cast<Construction>()?.ForEach(
delegate (Construction construction)
{
if (construction.Position == birthPoint)
Expand All @@ -277,7 +278,7 @@ public Game(MapStruct mapResource, int numOfTeam)
actionManager = new(gameMap, shipManager);
attackManager = new(gameMap, shipManager);
teamList = [];
gameMap.GameObjDict[GameObjType.Home].Cast<GameObj>().ForEach(
gameMap.GameObjDict[GameObjType.Home].Cast<GameObj>()?.ForEach(
delegate (GameObj gameObj)
{
if (gameObj.Type == GameObjType.Home)
Expand Down
14 changes: 14 additions & 0 deletions logic/Preparation/Utility/SafeValue/Atomic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ public abstract class Atomic
{
}


public class AtomicT<T>(T? x) : Atomic where T : class?
{
protected T? v = x;

public override string ToString() => Interlocked.CompareExchange(ref v, null, null)?.ToString() ?? "NULL";
public T? Get() => Interlocked.CompareExchange(ref v, null, null);
public static implicit operator T?(AtomicT<T> aint) => Interlocked.CompareExchange(ref aint.v, null, null);
/// <returns>返回操作前的值</returns>
public virtual T? SetReturnOri(T? value) => Interlocked.Exchange(ref v, value);
/// <returns>返回操作前的值</returns>
public virtual T? CompareExReturnOri(T? newV, T? compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicInt(int x) : Atomic
{
protected int v = x;
Expand Down
14 changes: 7 additions & 7 deletions logic/Preparation/Utility/SafeValue/ListLocked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Preparation.Utility
{
public class LockedClassList<T> where T : class
public class LockedClassList<T> where T : class?
{
private readonly ReaderWriterLockSlim listLock = new();
private List<T> list;
Expand Down Expand Up @@ -104,7 +104,7 @@ public bool RemoveAt(int index)
#endregion

#region 读取与对类操作
public TResult ReadLock<TResult>(Func<TResult> func)
public TResult? ReadLock<TResult>(Func<TResult?> func)
{
listLock.EnterReadLock();
try
Expand All @@ -130,7 +130,7 @@ public void ReadLock(Action func)

}

public T this[int index]
public T? this[int index]
{
get
{
Expand Down Expand Up @@ -158,7 +158,7 @@ public bool Contains(T item)
return ReadLock(() => { return list.Find(match); });
}

public List<T> FindAll(Predicate<T> match)
public List<T>? FindAll(Predicate<T> match)
{
return ReadLock(() => { return list.FindAll(match); });
}
Expand All @@ -167,17 +167,17 @@ public List<T> FindAll(Predicate<T> match)

public void ForEach(Action<T> action) => ReadLock(() => { list.ForEach(action); });

public Array ToArray()
public Array? ToArray()
{
return ReadLock(() => { return list.ToArray(); });
}
public List<T> ToNewList()
public List<T>? ToNewList()
{
List<T> lt = new();
return ReadLock(() => { lt.AddRange(list); return lt; });
}

public LockedClassList<TResult> Cast<TResult>() where TResult : class
public LockedClassList<TResult>? Cast<TResult>() where TResult : class?
{
LockedClassList<TResult> lt = new();
return ReadLock(() => { lt.AddRange(list.Cast<TResult>()); return lt; });
Expand Down
2 changes: 1 addition & 1 deletion logic/Server/GameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void ReportGame(GameState gameState, bool requiredGaming = true)

private bool PlayerDeceased(int playerID) //# 这里需要判断大本营deceased吗?
{
return game.GameMap.GameObjDict[GameObjType.Ship].Cast<Ship>().Find(
return game.GameMap.GameObjDict[GameObjType.Ship].Cast<Ship>()?.Find(
ship => ship.PlayerID == playerID && ship.ShipState == ShipStateType.Deceased
) != null;
}
Expand Down

0 comments on commit 541bbf6

Please sign in to comment.