diff --git a/CAPI/python/PyAPI/DebugAPI.py b/CAPI/python/PyAPI/DebugAPI.py index ab457e26..b70b6878 100644 --- a/CAPI/python/PyAPI/DebugAPI.py +++ b/CAPI/python/PyAPI/DebugAPI.py @@ -260,6 +260,9 @@ def PrintSweeper(self) -> None: ) self.__logger.info('************************\n') + def PrintTeam(self) -> None: + pass + def PrintSelfInfo(self) -> None: sweeper = self.__logic.GetSelfInfo() self.__logger.info('******sweeper Info******') diff --git a/CAPI/python/PyAPI/Interface.py b/CAPI/python/PyAPI/Interface.py index 37745f22..b407dc66 100644 --- a/CAPI/python/PyAPI/Interface.py +++ b/CAPI/python/PyAPI/Interface.py @@ -59,7 +59,7 @@ def GetHomeHp(self) -> int: pass @abstractmethod - def GetEnergy(self) -> int: + def GetMoney(self) -> int: pass @abstractmethod @@ -214,7 +214,7 @@ def GetHomeHp(self) -> int: pass @abstractmethod - def GetEnergy(self) -> int: + def GetMoney(self) -> int: pass @abstractmethod diff --git a/CAPI/python/PyAPI/logic.py b/CAPI/python/PyAPI/logic.py index 4e6badae..b4d9cb9c 100644 --- a/CAPI/python/PyAPI/logic.py +++ b/CAPI/python/PyAPI/logic.py @@ -11,6 +11,7 @@ import PyAPI.structures as THUAI7 from PyAPI.utils import Proto2THUAI7, AssistFunction from PyAPI.API import SweeperAPI, TeamAPI +from PyAPI.DebugAPI import SweeperDebugAPI, TeamDebugAPI from PyAPI.AI import Setting from PyAPI.Communication import Communication from PyAPI.State import State @@ -851,11 +852,11 @@ def Main( if platform.system().lower() == 'windows': os.system( - f'mkdir '{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}\\logs'' + f'mkdir "{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}\\logs"' ) else: os.system( - f'mkdir -p '{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))} / logs'' + f'mkdir -p "{os.path.dirname(os.path.dirname(os.path.realpath(__file__)))}/logs"' ) fileHandler = logging.FileHandler( diff --git a/logic/Client/Interact/JsonInteractor.cs b/logic/Client/Interact/JsonInteractor.cs index 562bd974..091069c8 100644 --- a/logic/Client/Interact/JsonInteractor.cs +++ b/logic/Client/Interact/JsonInteractor.cs @@ -19,9 +19,9 @@ static string[] JsonInteract() if (File.Exists(filepath)) { - FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite); - StreamReader file = new StreamReader(fs, System.Text.Encoding.Default); - JsonTextReader reader = new JsonTextReader(file);//解析json模块 + FileStream fs = new(filepath, FileMode.Open, FileAccess.ReadWrite); + StreamReader file = new(fs, System.Text.Encoding.Default); + JsonTextReader reader = new(file);//解析json模块 JObject o = (JObject)JToken.ReadFrom(reader); string ip = o["ip"].ToString(); string port = o["port"].ToString(); @@ -51,7 +51,7 @@ static string[] JsonInteract() public static string[] FileInteract() { string[] cominfo = new string[10]; - ConfigData d = new ConfigData(); + ConfigData d = new(); if (d.Commands.Launched == false) { cominfo[0] = d.Commands.IP; diff --git a/logic/GameClass/GameObj/Areas/Asteroid.cs b/logic/GameClass/GameObj/Areas/Asteroid.cs index 8d9776f2..841c0966 100755 --- a/logic/GameClass/GameObj/Areas/Asteroid.cs +++ b/logic/GameClass/GameObj/Areas/Asteroid.cs @@ -1,5 +1,4 @@ using Preparation.Utility; -using System; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Areas/Construction.cs b/logic/GameClass/GameObj/Areas/Construction.cs index 0c2b3072..9cfde48d 100755 --- a/logic/GameClass/GameObj/Areas/Construction.cs +++ b/logic/GameClass/GameObj/Areas/Construction.cs @@ -1,5 +1,4 @@ using Preparation.Utility; -using System; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Areas/Home.cs b/logic/GameClass/GameObj/Areas/Home.cs index 9dfb4093..68c1f812 100755 --- a/logic/GameClass/GameObj/Areas/Home.cs +++ b/logic/GameClass/GameObj/Areas/Home.cs @@ -1,6 +1,5 @@ using Preparation.Interface; using Preparation.Utility; -using System; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Areas/Resource.cs b/logic/GameClass/GameObj/Areas/Resource.cs index 4b0ad354..2cd32dc1 100755 --- a/logic/GameClass/GameObj/Areas/Resource.cs +++ b/logic/GameClass/GameObj/Areas/Resource.cs @@ -1,6 +1,4 @@ using Preparation.Utility; -using System; -using System.Threading; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Areas/Shadow.cs b/logic/GameClass/GameObj/Areas/Shadow.cs index 921d82db..36f1e3e4 100755 --- a/logic/GameClass/GameObj/Areas/Shadow.cs +++ b/logic/GameClass/GameObj/Areas/Shadow.cs @@ -1,5 +1,4 @@ using Preparation.Utility; -using System; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Areas/Wormhole.cs b/logic/GameClass/GameObj/Areas/Wormhole.cs index 04f0d015..fcc48dd3 100755 --- a/logic/GameClass/GameObj/Areas/Wormhole.cs +++ b/logic/GameClass/GameObj/Areas/Wormhole.cs @@ -1,6 +1,5 @@ using Preparation.Interface; using Preparation.Utility; -using System; using System.Collections.Generic; namespace GameClass.GameObj.Areas; diff --git a/logic/GameClass/GameObj/Bullets/Laser.cs b/logic/GameClass/GameObj/Bullets/Laser.cs index 043aadaf..fb20bea9 100755 --- a/logic/GameClass/GameObj/Bullets/Laser.cs +++ b/logic/GameClass/GameObj/Bullets/Laser.cs @@ -1,7 +1,4 @@ -using Preparation.Interface; using Preparation.Utility; -using System; -using System.Threading; namespace GameClass.GameObj.Bullets; diff --git a/logic/GameClass/GameObj/Bullets/Missile.cs b/logic/GameClass/GameObj/Bullets/Missile.cs index 8cd428c1..c1d4895d 100755 --- a/logic/GameClass/GameObj/Bullets/Missile.cs +++ b/logic/GameClass/GameObj/Bullets/Missile.cs @@ -1,5 +1,5 @@ -using System; -using Preparation.Utility; +using Preparation.Utility; +using System; namespace GameClass.GameObj.Bullets; diff --git a/logic/GameClass/GameObj/Map/Map.cs b/logic/GameClass/GameObj/Map/Map.cs index a368f255..73b24cf3 100755 --- a/logic/GameClass/GameObj/Map/Map.cs +++ b/logic/GameClass/GameObj/Map/Map.cs @@ -1,11 +1,9 @@ -using System.Collections.Generic; -using System.Threading; +using GameClass.GameObj.Areas; +using GameClass.MapGenerator; using Preparation.Interface; using Preparation.Utility; using System; -using GameClass.GameObj.Areas; -using System.Linq; -using GameClass.MapGenerator; +using System.Collections.Generic; namespace GameClass.GameObj { diff --git a/logic/GameClass/GameObj/Map/MapGameTimer.cs b/logic/GameClass/GameObj/Map/MapGameTimer.cs index cb800d01..f9a92db6 100755 --- a/logic/GameClass/GameObj/Map/MapGameTimer.cs +++ b/logic/GameClass/GameObj/Map/MapGameTimer.cs @@ -1,6 +1,6 @@ -using System; +using Preparation.Utility; +using System; using System.Threading; -using Preparation.Utility; using ITimer = Preparation.Interface.ITimer; namespace GameClass.GameObj diff --git a/logic/GameClass/GameObj/Ship.cs b/logic/GameClass/GameObj/Ship.cs index c1705019..79e0b207 100755 --- a/logic/GameClass/GameObj/Ship.cs +++ b/logic/GameClass/GameObj/Ship.cs @@ -1,9 +1,8 @@ -using System; -using GameClass.GameObj.Bullets; -using Preparation.Interface; -using Preparation.Utility; +using GameClass.GameObj.Bullets; using GameClass.GameObj.Modules; using GameClass.GameObj.Occupations; +using Preparation.Interface; +using Preparation.Utility; namespace GameClass.GameObj; @@ -18,7 +17,9 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj) { if (IsRemoved) return true; - if (targetObj.Type == GameObjType.Ship && XY.DistanceCeil3(targetObj.Position, this.Position) < this.Radius + targetObj.Radius - GameData.AdjustLength) + if (targetObj.Type == GameObjType.Ship + && XY.DistanceCeil3(targetObj.Position, Position) + < Radius + targetObj.Radius - GameData.AdjustLength) return true; return false; } @@ -355,7 +356,7 @@ public bool ResetShipState(long state, RunningStateType running = RunningStateTy lock (actionLock) { if (state != stateNum) return false; - this.runningState = running; + runningState = running; whatInteractingWith = (GameObj?)obj; shipState = value; ++stateNum; @@ -367,7 +368,7 @@ public bool ResetShipStateInOneThread(long state, RunningStateType running = Run lock (actionLock) { if (state != stateNum) return false; - this.runningState = running; + runningState = running; whatInteractingWith = (GameObj?)obj; shipState = value; return true; @@ -377,7 +378,7 @@ public bool StartThread(long stateNum, RunningStateType runningState) { lock (actionLock) { - if (this.StateNum == stateNum) + if (StateNum == stateNum) { this.runningState = runningState; return true; @@ -408,16 +409,16 @@ public bool Commandable() public Ship(int initRadius, ShipType shipType, MoneyPool moneyPool) : base(GameData.PosNotInGame, initRadius, GameObjType.Ship) { - this.CanMove.SetROri(false); - this.IsRemoved.SetROri(true); - this.Occupation = OccupationFactory.FindIOccupation(this.ShipType = shipType); - this.ViewRange = this.Occupation.ViewRange; - this.HP = new(this.Occupation.MaxHp); - this.Armor = new(this.Occupation.BaseArmor); - this.Shield = new(this.Occupation.BaseShield); - this.MoveSpeed.SetROri(this.orgMoveSpeed = Occupation.MoveSpeed); - this.MoneyPool = moneyPool; - (this.producerType, this.constructorType, this.armorType, this.shieldType, this.weaponType) = this.ShipType switch + CanMove.SetROri(false); + IsRemoved.SetROri(true); + Occupation = OccupationFactory.FindIOccupation(ShipType = shipType); + ViewRange = Occupation.ViewRange; + HP = new(Occupation.MaxHp); + Armor = new(Occupation.BaseArmor); + Shield = new(Occupation.BaseShield); + MoveSpeed.SetROri(orgMoveSpeed = Occupation.MoveSpeed); + MoneyPool = moneyPool; + (producerType, constructorType, armorType, shieldType, weaponType) = ShipType switch { ShipType.CivilShip => ( ProducerType.Producer1, @@ -442,12 +443,12 @@ public Ship(int initRadius, ShipType shipType, MoneyPool moneyPool) : ), _ => (ProducerType.Null, ConstructorType.Null, ArmorType.Null, ShieldType.Null, WeaponType.Null) }; - (this.producer, this.constructor, this.armor, this.shield, this.weapon) = ( - ModuleFactory.FindIProducer(this.ShipType, this.producerType), - ModuleFactory.FindIConstructor(this.ShipType, this.constructorType), - ModuleFactory.FindIArmor(this.ShipType, this.armorType), - ModuleFactory.FindIShield(this.ShipType, this.shieldType), - ModuleFactory.FindIWeapon(this.ShipType, this.weaponType) + (producer, constructor, armor, shield, weapon) = ( + ModuleFactory.FindIProducer(ShipType, producerType), + ModuleFactory.FindIConstructor(ShipType, constructorType), + ModuleFactory.FindIArmor(ShipType, armorType), + ModuleFactory.FindIShield(ShipType, shieldType), + ModuleFactory.FindIWeapon(ShipType, weaponType) ); } } diff --git a/logic/GameClass/GameObj/Team.cs b/logic/GameClass/GameObj/Team.cs index a0d76426..f2e52c53 100755 --- a/logic/GameClass/GameObj/Team.cs +++ b/logic/GameClass/GameObj/Team.cs @@ -1,50 +1,48 @@ using GameClass.GameObj.Areas; +using Preparation.Interface; using Preparation.Utility; using System.Collections.Generic; -using System.Data.SqlTypes; -using System.Threading; -using Preparation.Interface; namespace GameClass.GameObj { - public class Team(Home home) : IPlayer + public class Team : IPlayer { - public AtomicLong TeamID { get; } = new(home.TeamID); + public AtomicLong TeamID { get; } public AtomicLong PlayerID { get; } = new(0); public const long invalidTeamID = long.MaxValue; public const long noneTeamID = long.MinValue; - private readonly List shipList = new(GameData.MaxShipNum); private readonly List birthPointList = []; + public ObjPool ShipPool { get; } public List BirthPointList => birthPointList; - private Home home = home; - public MoneyPool MoneyPool { get; } = new MoneyPool(); - public Ship? GetShip(long shipID) + public Home Home { get; set; } + public MoneyPool MoneyPool { get; } = new(); + + public Team(Home home) { - foreach (Ship ship in shipList) - { - if (ship.PlayerID == shipID) - return ship; - } - return null; + TeamID = new(home.TeamID); + Home = home; + ShipPool = new( + classfier: (ship) => ship.ShipType, + idleChecker: (ship) => ship.IsRemoved, + activator: (ship) => + { + ship.CanMove.SetROri(true); + ship.IsRemoved.SetROri(false); + }, + inactivator: (ship) => + { + ship.CanMove.SetROri(false); + ship.IsRemoved.SetROri(true); + }); + ShipPool.Initiate(ShipType.CivilShip, GameData.MaxCivilShipNum, + () => new(GameData.ShipRadius, ShipType.CivilShip, MoneyPool)); + ShipPool.Initiate(ShipType.WarShip, GameData.MaxWarShipNum, + () => new(GameData.ShipRadius, ShipType.WarShip, MoneyPool)); + ShipPool.Initiate(ShipType.FlagShip, GameData.MaxFlagShipNum, + () => new(GameData.ShipRadius, ShipType.FlagShip, MoneyPool)); } - public Ship? GetNewShip(ShipType shipType) - { - switch (shipType) - { - case ShipType.CivilShip: - var civilIndex = GetNewCivilShipIndex(); - return civilIndex == -1 ? null : shipList[civilIndex]; - case ShipType.WarShip: - var warIndex = GetNewWarShipIndex(); - return warIndex == -1 ? null : shipList[warIndex]; - case ShipType.FlagShip: - var flagIndex = GetNewFlagShipIndex(); - return flagIndex == -1 ? null : shipList[flagIndex]; - default: - return null; - } - } - public bool AddShip(Ship ship) + + /*public bool AddShip(Ship ship) { switch (ship.ShipType) { @@ -65,7 +63,8 @@ public bool AddShip(Ship ship) } //shipList.Add(ship); return true; - } + }*/ + public void AddMoney(long add) { MoneyPool.Money.Add(add); @@ -75,82 +74,5 @@ public void SubMoney(long sub) { MoneyPool.Money.Sub(sub); } - public void SetHome(Home home) - { - this.home = home; - } - public int GetShipNum() - { - int num = 0; - foreach (Ship ship in shipList) - { - if (!ship.IsRemoved) - num++; - } - return num; - } - public int GetCivilShipNum() - { - int num = 0; - foreach (Ship ship in shipList) - { - if (ship.ShipType == ShipType.CivilShip && !ship.IsRemoved) - num++; - } - return num; - } - public int GetNewCivilShipIndex() - { - var from = 1; - var to = 1 + GameData.MaxCivilShipNum; - for (int index = from; index < to; index++) - { - if (shipList[index].IsRemoved) - return index; - } - return -1; - } - public int GetWarShipNum() - { - int num = 0; - foreach (Ship ship in shipList) - { - if (ship.ShipType == ShipType.WarShip && !ship.IsRemoved) - num++; - } - return num; - } - public int GetNewWarShipIndex() - { - var from = 1 + GameData.MaxCivilShipNum; - var to = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum; - for (int index = from; index < to; index++) - { - if (shipList[index].IsRemoved) - return index; - } - return -1; - } - public int GetFlagShipNum() - { - int num = 0; - foreach (Ship ship in shipList) - { - if (ship.ShipType == ShipType.FlagShip && !ship.IsRemoved) - num++; - } - return num; - } - public int GetNewFlagShipIndex() - { - var from = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum; - var to = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum + GameData.MaxFlagShipNum; - for (int index = from; index < to; index++) - { - if (shipList[index].IsRemoved) - return index; - } - return -1; - } } } diff --git a/logic/GameEngine/CollisionChecker.cs b/logic/GameEngine/CollisionChecker.cs index 7c1e2010..78f5a937 100755 --- a/logic/GameEngine/CollisionChecker.cs +++ b/logic/GameEngine/CollisionChecker.cs @@ -1,6 +1,6 @@ -using System; -using Preparation.Interface; +using Preparation.Interface; using Preparation.Utility; +using System; namespace GameEngine { diff --git a/logic/GameEngine/MoveEngine.cs b/logic/GameEngine/MoveEngine.cs index 73018975..4ca74c7e 100755 --- a/logic/GameEngine/MoveEngine.cs +++ b/logic/GameEngine/MoveEngine.cs @@ -1,7 +1,7 @@ -using System; -using System.Threading; -using Preparation.Interface; +using Preparation.Interface; using Preparation.Utility; +using System; +using System.Threading; using Timothy.FrameRateTask; using ITimer = Preparation.Interface.ITimer; diff --git a/logic/Gaming/AttackManager.cs b/logic/Gaming/AttackManager.cs index f8440a53..277c8775 100755 --- a/logic/Gaming/AttackManager.cs +++ b/logic/Gaming/AttackManager.cs @@ -1,13 +1,13 @@ -using System.Collections.Generic; -using System.Threading; -using GameClass.GameObj; +using GameClass.GameObj; using GameClass.GameObj.Areas; using GameClass.GameObj.Bullets; using GameEngine; -using Preparation.Utility; using Preparation.Interface; -using Timothy.FrameRateTask; +using Preparation.Utility; +using System.Collections.Generic; using System.Linq; +using System.Threading; +using Timothy.FrameRateTask; namespace Gaming { diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index a5a57806..e763c5b1 100755 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -37,7 +37,7 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) { return GameObj.invalidID; } - teamList[(int)playerInitInfo.teamID].AddShip(newShip); + //teamList[(int)playerInitInfo.teamID].AddShip(newShip); return newShip.PlayerID; } else @@ -48,7 +48,7 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) } public bool ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0) { - Ship? ship = teamList[(int)teamID].GetShip(playerID); + Ship? ship = teamList[(int)teamID].ShipPool.GetObj(shipType); if (ship == null) return false; else if (ship.IsRemoved == false) diff --git a/logic/Gaming/ShipManager.cs b/logic/Gaming/ShipManager.cs index d0e7e871..d584584e 100755 --- a/logic/Gaming/ShipManager.cs +++ b/logic/Gaming/ShipManager.cs @@ -1,6 +1,6 @@ -using System.Threading; -using GameClass.GameObj; +using GameClass.GameObj; using Preparation.Utility; +using System.Threading; namespace Gaming { diff --git a/logic/Preparation/Interface/IMap.cs b/logic/Preparation/Interface/IMap.cs index 1a6c6329..6a4cf884 100755 --- a/logic/Preparation/Interface/IMap.cs +++ b/logic/Preparation/Interface/IMap.cs @@ -1,6 +1,5 @@ -using System.Collections.Generic; -using System.Threading; -using Preparation.Utility; +using Preparation.Utility; +using System.Collections.Generic; namespace Preparation.Interface { diff --git a/logic/Preparation/Interface/IMovable.cs b/logic/Preparation/Interface/IMovable.cs index ab9b35e9..1a636b7f 100755 --- a/logic/Preparation/Interface/IMovable.cs +++ b/logic/Preparation/Interface/IMovable.cs @@ -1,6 +1,6 @@ -using System; +using Preparation.Utility; +using System; using System.Threading; -using Preparation.Utility; namespace Preparation.Interface { diff --git a/logic/Preparation/Interface/IObjPool.cs b/logic/Preparation/Interface/IObjPool.cs new file mode 100644 index 00000000..9e4de764 --- /dev/null +++ b/logic/Preparation/Interface/IObjPool.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; + +namespace Preparation.Interface; + +public interface IObjPool + where T : class + where TType : notnull +{ + public int Size { get; } + public bool IsEmpty { get; } + public int IdleNum { get; } + + public void Initiate(TType tp, int num, Func func); + + public T? GetObj(TType tp); + public void ReturnObj(T obj); + + public void Append(T obj); + public void Append(IEnumerable objs); + public void AppendNoCheck(TType tp, IEnumerable objs); + + public int GetNum(TType tp); + public bool CheckEmpty(TType tp); + public int GetIdleNum(TType tp); + + public List Travel(Func func); + public void Travel(Action func); + public List? Travel(TType tp, Func func); + public void Travel(TType tp, Action func); + + public T? Find(Func cond); + public T? Find(TType tp, Func cond); + + public (List results, T? target) Until(Func func, Func cond); + public T? Until(Action func, Func cond); + public (List? results, T? target) Until(TType tp, Func func, Func cond); + public T? Until(TType tp, Action func, Func cond); + + public void Clear(); + public void Clear(TType tp); +} diff --git a/logic/Preparation/Utility/MapEncoder.cs b/logic/Preparation/Utility/MapEncoder.cs index d8e2c4e4..e26f34c4 100755 --- a/logic/Preparation/Utility/MapEncoder.cs +++ b/logic/Preparation/Utility/MapEncoder.cs @@ -1,6 +1,4 @@ -using System; - -namespace Preparation.Utility +namespace Preparation.Utility { public class MapEncoder { diff --git a/logic/Preparation/Utility/Value/ObjPool.cs b/logic/Preparation/Utility/Value/ObjPool.cs new file mode 100644 index 00000000..d1153d0f --- /dev/null +++ b/logic/Preparation/Utility/Value/ObjPool.cs @@ -0,0 +1,356 @@ +using Preparation.Interface; +using System; +using System.Collections.Generic; + +namespace Preparation.Utility; + +public class ObjPool(Func classfier, + Func idleChecker, + Action activator, + Action inactivator) + : IObjPool + where T : class + where TType : notnull +{ + private readonly object dictLock = new(); + private readonly Dictionary> objs = []; + private readonly Func classfier = classfier; + private readonly Func idleChecker = idleChecker; + private readonly Action activator = activator; + private readonly Action inactivator = inactivator; + + #region 属性 + + public int Size + { + get + { + lock (dictLock) + { + int ret = 0; + foreach (var ls in objs.Values) + { + ret += ls.Count; + } + return ret; + } + } + } + public bool IsEmpty + { + get + { + lock (dictLock) + { + foreach (var ls in objs.Values) + { + if (ls.Count != 0) return false; + } + return true; + } + } + } + public int IdleNum + { + get + { + int sum = 0; + Travel((o) => { if (idleChecker(o)) sum++; }); + return sum; + } + } + + #endregion + + #region 主功能 + + public void Initiate(TType tp, int num, Func func) + { + if (objs.ContainsKey(tp)) Clear(tp); + lock (dictLock) + { + objs[tp] = new(num); + for (int i = 0; i < num; i++) + { + var obj = func(); + inactivator(obj); + objs[tp].Add(obj); + } + } + } + public T? GetObj(TType tp) + { + lock (dictLock) + { + if (CheckEmpty(tp) || GetIdleNum(tp) == 0) return null; + var ret = Find(idleChecker); + if (ret is null) return null; + activator(ret); + return ret; + } + } + public void ReturnObj(T obj) + { + lock (dictLock) inactivator(obj); + } + + #endregion + + #region Append + + public void Append(T obj) + { + TType tp = classfier(obj); + if (!objs.TryGetValue(tp, out var ls)) + { + LockedClassList temp = new(); + temp.Add(obj); + lock (dictLock) objs[tp] = temp; + } + else + { + lock (dictLock) ls.Add(obj); + } + } + public void Append(IEnumerable objs) + { + foreach (var obj in objs) + { + Append(obj); + } + } + public void AppendNoCheck(TType tp, IEnumerable objs) + { + if (!this.objs.TryGetValue(tp, out var ls)) + { + LockedClassList temp = new(); + temp.AddRange(objs); + lock (dictLock) this.objs[tp] = temp; + } + else + { + lock (dictLock) ls.AddRange(objs); + } + } + + #endregion + + #region 子属性 + + public int GetNum(TType tp) + => objs[tp].Count; + public bool CheckEmpty(TType tp) + => GetNum(tp) == 0; + public int GetIdleNum(TType tp) + { + int sum = 0; + Travel(tp, (o) => { if (idleChecker(o)) sum++; }); + return sum; + } + + #endregion + + #region 遍历 + + #region Travel + + public List Travel(Func func) + { + List ret = []; + lock (dictLock) + { + foreach (var ls in objs.Values) + { + var len = ls.Count; + for (int i = 0; i < len; i++) + { + ret.Add(func(ls[i])); + } + } + } + return ret; + } + public void Travel(Action func) + { + lock (dictLock) + { + foreach (var ls in objs.Values) + { + var len = ls.Count; + for (int i = 0; i < len; i++) + { + func(ls[i]); + } + } + } + } + public List? Travel(TType tp, Func func) + { + if (!objs.TryGetValue(tp, out var ls)) return null; + var len = ls.Count; + List ret = new(len); + lock (dictLock) + { + for (int i = 0; i < len; i++) + { + ret.Add(func(ls[i])); + } + } + return ret; + } + public void Travel(TType tp, Action func) + { + if (!objs.TryGetValue(tp, out var ls)) return; + var len = ls.Count; + lock (dictLock) + { + for (int i = 0; i < len; i++) + { + func(ls[i]); + } + } + } + + #endregion + + #region Find + + public T? Find(Func cond) + { + lock (dictLock) + { + foreach (var ls in objs.Values) + { + var len = ls.Count; + for (int i = 0; i < len; i++) + { + if (cond(ls[i])) return ls[i]; + } + } + } + return null; + } + public T? Find(TType tp, Func cond) + { + if (!objs.TryGetValue(tp, out var ls)) return null; + var len = ls.Count; + lock (dictLock) + { + for (int i = 0; i < len; i++) + { + if (cond(ls[i])) return ls[i]; + } + } + return null; + } + + #endregion + + #region Until + + public (List results, T? target) Until(Func func, Func cond) + { + List ret = []; + T? retObj = null; + lock (dictLock) + { + bool flag = false; + foreach (var ls in objs.Values) + { + var len = ls.Count; + for (int i = 0; i < len; i++) + { + ret.Add(func(ls[i])); + if (cond(ls[i])) + { + retObj = ls[i]; + flag = true; + break; + } + } + if (flag) break; + } + } + return (ret, retObj); + } + public T? Until(Action func, Func cond) + { + lock (dictLock) + { + foreach (var ls in objs.Values) + { + var len = ls.Count; + for (int i = 0; i < len; i++) + { + func(ls[i]); + if (cond(ls[i])) return ls[i]; + } + } + } + return null; + } + public (List? results, T? target) Until(TType tp, Func func, Func cond) + { + if (!objs.TryGetValue(tp, out var ls)) return (null, null); + var len = ls.Count; + List ret = new(len); + T? retObj = null; + lock (dictLock) + { + for (int i = 0; i < len; i++) + { + ret.Add(func(ls[i])); + if (cond(ls[i])) + { + retObj = ls[i]; + break; + } + } + } + return (ret, retObj); + } + public T? Until(TType tp, Action func, Func cond) + { + if (!objs.TryGetValue(tp, out var ls)) return null; + var len = ls.Count; + lock (dictLock) + { + for (int i = 0; i < len; i++) + { + func(ls[i]); + if (cond(ls[i])) return ls[i]; + } + } + return null; + } + + #endregion + + #endregion + + #region Clear + + public void Clear() + { + lock (dictLock) + { + foreach (var k in objs.Keys) + { + objs[k].Clear(); + objs.Remove(k); + } + } + } + public void Clear(TType tp) + { + if (!objs.TryGetValue(tp, out var ls)) return; + lock (dictLock) + { + ls.Clear(); + objs.Remove(tp); + } + } + + #endregion +} + diff --git a/logic/Preparation/Utility/Value/SafeValue/Atomic.cs b/logic/Preparation/Utility/Value/SafeValue/Atomic.cs index db0c5701..5442a57c 100644 --- a/logic/Preparation/Utility/Value/SafeValue/Atomic.cs +++ b/logic/Preparation/Utility/Value/SafeValue/Atomic.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading; +using System.Threading; namespace Preparation.Utility { diff --git a/logic/Preparation/Utility/Value/SafeValue/ISafeAddable.cs b/logic/Preparation/Utility/Value/SafeValue/ISafeAddable.cs index 2d631dc0..f0431f5c 100644 --- a/logic/Preparation/Utility/Value/SafeValue/ISafeAddable.cs +++ b/logic/Preparation/Utility/Value/SafeValue/ISafeAddable.cs @@ -1,7 +1,4 @@ -using System; -using System.Threading; - -namespace Preparation.Utility +namespace Preparation.Utility { public interface ISafeAddable { diff --git a/logic/Preparation/Utility/Value/SafeValue/ListLocked.cs b/logic/Preparation/Utility/Value/SafeValue/ListLocked.cs index 66d2d111..18ee9fcc 100644 --- a/logic/Preparation/Utility/Value/SafeValue/ListLocked.cs +++ b/logic/Preparation/Utility/Value/SafeValue/ListLocked.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -9,20 +8,20 @@ namespace Preparation.Utility public class LockedClassList where T : class? { private readonly ReaderWriterLockSlim listLock = new(); - private List list; + private readonly List list; #region 构造 public LockedClassList() { - list = new List(); + list = []; } public LockedClassList(int capacity) { - list = new List(capacity); + list = new(capacity); } public LockedClassList(IEnumerable collection) { - list = new List(collection); + list = new(collection); } #endregion @@ -70,15 +69,16 @@ public void Insert(int index, T item) public void Clear() { - WriteLock(() => { list.Clear(); }); + WriteLock(list.Clear); } public bool Remove(T item) { - return WriteLock(() => { return list.Remove(item); }); + return WriteLock(() => list.Remove(item)); } - public int RemoveAll(T item) => WriteLock(() => { return list.RemoveAll((t) => { return t == item; }); }); + public int RemoveAll(T item) + => WriteLock(() => list.RemoveAll((t) => t == item)); public T? RemoveOne(Predicate match) => WriteLock(() => @@ -90,7 +90,8 @@ public bool Remove(T item) return ans; }); - public int RemoveAll(Predicate match) => WriteLock(() => { return list.RemoveAll(match); }); + public int RemoveAll(Predicate match) + => WriteLock(() => list.RemoveAll(match)); public bool RemoveAt(int index) { @@ -134,46 +135,48 @@ public T? this[int index] { get { - return ReadLock(() => { return list[index]; }); + return ReadLock(() => { return list[index]; }); } set { ReadLock(() => { list[index] = value!; }); } } - public int Count => ReadLock(() => { return list.Count; }); + public int Count => ReadLock(() => list.Count); public int IndexOf(T item) { - return ReadLock(() => { return list.IndexOf(item); }); + return ReadLock(() => list.IndexOf(item)); } public bool Contains(T item) { - return ReadLock(() => { return list.Contains(item); }); + return ReadLock(() => list.Contains(item)); } public T? Find(Predicate match) { - return ReadLock(() => { return list.Find(match); }); + return ReadLock(() => list.Find(match)); } public List? FindAll(Predicate match) { - return ReadLock(() => { return list.FindAll(match); }); + return ReadLock(() => list.FindAll(match)); } - public int FindIndex(Predicate match) => ReadLock(() => { return list.FindIndex(match); }); + public int FindIndex(Predicate match) + => ReadLock(() => list.FindIndex(match)); - public void ForEach(Action action) => ReadLock(() => { list.ForEach(action); }); + public void ForEach(Action action) + => ReadLock(() => { list.ForEach(action); }); public Array? ToArray() { - return ReadLock(() => { return list.ToArray(); }); + return ReadLock(list.ToArray); } public List? ToNewList() { - List lt = new(); + List lt = []; return ReadLock(() => { lt.AddRange(list); return lt; }); } diff --git a/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs b/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs index 43ac7078..f67680f7 100644 --- a/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs +++ b/logic/Preparation/Utility/Value/SafeValue/LockedValue/InTheRange.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; namespace Preparation.Utility { diff --git a/logic/Preparation/Utility/Value/SafeValue/TimeBased.cs b/logic/Preparation/Utility/Value/SafeValue/TimeBased.cs index 402595a1..544762ae 100644 --- a/logic/Preparation/Utility/Value/SafeValue/TimeBased.cs +++ b/logic/Preparation/Utility/Value/SafeValue/TimeBased.cs @@ -134,7 +134,7 @@ public void SetAndStop(long value = 0) public class TimeBasedProgressAtVariableSpeed { - private LongInTheVariableRangeWithStartTime progress; + private readonly LongInTheVariableRangeWithStartTime progress; public AtomicDouble speed; #region 构造 diff --git a/logic/Server/ArgumentOptions.cs b/logic/Server/ArgumentOptions.cs index 03fdfe58..4d97803f 100755 --- a/logic/Server/ArgumentOptions.cs +++ b/logic/Server/ArgumentOptions.cs @@ -12,7 +12,7 @@ public class DefaultArgumentOptions public class ArgumentOptions { [Option("ip", Required = false, HelpText = "Server listening ip")] - public string ServerIP { get; set; } = "172.28.192.1"; + public string ServerIP { get; set; } = "localhost"; [Option('p', "port", Required = true, HelpText = "Server listening port")] public ushort ServerPort { get; set; } = 8888; diff --git a/logic/Server/CopyInfo.cs b/logic/Server/CopyInfo.cs index 4516cf43..1dcc11d4 100755 --- a/logic/Server/CopyInfo.cs +++ b/logic/Server/CopyInfo.cs @@ -1,8 +1,8 @@ using GameClass.GameObj; using GameClass.GameObj.Areas; using Preparation.Utility; -using Utility = Preparation.Utility; using Protobuf; +using Utility = Preparation.Utility; namespace Server { diff --git a/logic/Server/GameServer.cs b/logic/Server/GameServer.cs index acc74943..596a612b 100755 --- a/logic/Server/GameServer.cs +++ b/logic/Server/GameServer.cs @@ -1,13 +1,12 @@ using GameClass.GameObj; -using Gaming; using GameClass.MapGenerator; +using Gaming; using Newtonsoft.Json; using Playback; using Preparation.Utility; using Protobuf; using System.Collections.Concurrent; using Timothy.FrameRateTask; -using Utility = Preparation.Utility; namespace Server { diff --git a/logic/Server/RpcServices.cs b/logic/Server/RpcServices.cs index e9a6b4ab..f6bfbbb0 100755 --- a/logic/Server/RpcServices.cs +++ b/logic/Server/RpcServices.cs @@ -3,7 +3,6 @@ using Grpc.Core; using Preparation.Utility; using Protobuf; -using System.Runtime.CompilerServices; namespace Server { @@ -508,13 +507,14 @@ public override Task BuildSweeper(BuildSweeperMsg request, ServerCallCo Console.WriteLine($"TRY BuildSweeper: SweeperType {request.SweeperType} from Team {request.TeamId}"); #endif BoolRes boolRes = new(); - - if (game.TeamList[(int)request.TeamId].GetShip(request.PlayerId) == null) + var ship = game.TeamList[(int)request.TeamId].ShipPool.Find( + (ship) => ship.PlayerID == request.PlayerId); + if (ship == null) { boolRes.ActSuccess = false; return Task.FromResult(boolRes); } - else if (game.TeamList[(int)request.TeamId].GetShip(request.PlayerId).IsRemoved == false) + else if (ship.IsRemoved == false) { boolRes.ActSuccess = false; return Task.FromResult(boolRes);