From c397619b935f369b4963de7233ffb0368d1b0df2 Mon Sep 17 00:00:00 2001 From: Xuc Pan Date: Sat, 11 May 2024 21:40:07 +0800 Subject: [PATCH] feat: :sparkles: add Repair --- dependency/proto/Services.proto | 2 + logic/GameClass/GameObj/Areas/Home.cs | 16 +++++++- logic/Gaming/ActionManager.cs | 57 ++++++++++++++++++++++++++- logic/Gaming/Game.cs | 13 +++++- logic/Server/RpcServices.cs | 32 +++++++++++++++ 5 files changed, 116 insertions(+), 4 deletions(-) diff --git a/dependency/proto/Services.proto b/dependency/proto/Services.proto index dbc18505..21c2becb 100755 --- a/dependency/proto/Services.proto +++ b/dependency/proto/Services.proto @@ -17,6 +17,8 @@ service AvailableService rpc Produce(IDMsg) returns (BoolRes); // 开采 rpc Rebuild(ConstructMsg) returns (BoolRes); // 给建筑回血 rpc Construct(ConstructMsg) returns (BoolRes); // 修建建筑 + rpc RepairHome(IDMsg) returns (BoolRes); // 修理大本营 + rpc RepairWormhole(IDMsg) returns (BoolRes); // 修理虫洞 rpc Attack(AttackMsg) returns (BoolRes); // 攻击 rpc Send(SendMsg) returns (BoolRes); // 传递信息 // 大本营 diff --git a/logic/GameClass/GameObj/Areas/Home.cs b/logic/GameClass/GameObj/Areas/Home.cs index 3746ca96..c91baffa 100755 --- a/logic/GameClass/GameObj/Areas/Home.cs +++ b/logic/GameClass/GameObj/Areas/Home.cs @@ -1,6 +1,7 @@ using Preparation.Interface; using Preparation.Utility; using Preparation.Utility.Value; +using Preparation.Utility.Value.SafeValue.Atomic; using Preparation.Utility.Value.SafeValue.LockedValue; namespace GameClass.GameObj.Areas; @@ -12,10 +13,23 @@ public class Home(XY initPos, long id) public InVariableRange HP { get; } = new(GameData.HomeHP); public override bool IsRigid => true; public override ShapeType Shape => ShapeType.Square; - + public AtomicInt RepairNum { get; } = new AtomicInt(0); + public bool Repair(int constructSpeed, Ship ship) + { + return HP.AddVUseOtherRChange(constructSpeed, ship.MoneyPool.Money, 1) > 0; + } public void BeAttacked(Bullet bullet) { if (bullet!.Parent!.TeamID != TeamID) HP.SubPositiveV(bullet.AP); } + public void AddRepairNum(int add = 1) + { + RepairNum.Add(add); + } + public void SubRepairNum(int sub = 1) + { + RepairNum.Sub(sub); + } + } diff --git a/logic/Gaming/ActionManager.cs b/logic/Gaming/ActionManager.cs index 37c3d021..25bdcfd3 100755 --- a/logic/Gaming/ActionManager.cs +++ b/logic/Gaming/ActionManager.cs @@ -240,7 +240,62 @@ public bool Construct(Ship ship, ConstructionType constructionType) { IsBackground = true }.Start(); return false; } - public bool Repair(Ship ship) + public bool RepairHome(Ship ship) + { + Home? home = ((Home?)gameMap.OneForInteract(ship.Position, GameObjType.Home)); + if (home == null) + { + return false; + } + if (home.HP.IsMaxV()) + { + return false; + } + long stateNum = ship.SetShipState(RunningStateType.Waiting, ShipStateType.Constructing); + if (stateNum == -1) + { + return false; + } + new Thread + ( + () => + { + ship.ThreadNum.WaitOne(); + if (!ship.StartThread(stateNum, RunningStateType.RunningActively)) + { + ship.ThreadNum.Release(); + return; + } + home.AddRepairNum(); + Thread.Sleep(GameData.CheckInterval); + new FrameRateTaskExecutor + ( + loopCondition: () => stateNum == ship.StateNum && gameMap.Timer.IsGaming, + loopToDo: () => + { + if (!home.Repair(ship.ConstructSpeed / GameData.NumOfStepPerSecond, ship)) + { + ship.ResetShipState(stateNum); + return false; + } + if (home.HP == home.HP.GetMaxV()) + { + ship.ResetShipState(stateNum); + return false; + } + return true; + }, + timeInterval: GameData.CheckInterval, + finallyReturn: () => 0 + ).Start(); + ship.ThreadNum.Release(); + home.SubRepairNum(); + } + ) + { IsBackground = true }.Start(); + return false; + } + public bool RepairWormhole(Ship ship) { Wormhole? wormhole = ((WormholeCell?)gameMap.OneForInteract(ship.Position, GameObjType.Wormhole))?.Wormhole; if (wormhole == null) diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index 7d9f26ca..5ae9c04c 100755 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -220,13 +220,22 @@ public bool Recycle(long teamID, long shipID) } return false; } - public bool Repair(long teamID, long shipID) + public bool RepairHome(long teamID, long shipID) { if (!gameMap.Timer.IsGaming) return false; Ship? ship = gameMap.FindShipInPlayerID(teamID, shipID); if (ship != null) - return actionManager.Repair(ship); + return actionManager.RepairHome(ship); + return false; + } + public bool RepairWormhole(long teamID, long shipID) + { + if (!gameMap.Timer.IsGaming) + return false; + Ship? ship = gameMap.FindShipInPlayerID(teamID, shipID); + if (ship != null) + return actionManager.RepairWormhole(ship); return false; } public bool Stop(long teamID, long shipID) diff --git a/logic/Server/RpcServices.cs b/logic/Server/RpcServices.cs index 2b8d21d8..4cd9f19f 100755 --- a/logic/Server/RpcServices.cs +++ b/logic/Server/RpcServices.cs @@ -334,6 +334,38 @@ public override Task Construct(ConstructMsg request, ServerCallContext return Task.FromResult(boolRes); } + public override Task RepairHome(IDMsg request, ServerCallContext context) + { + GameServerLogging.logger.ConsoleLogDebug( + $"TRY RepairHome: Player {request.PlayerId} from Team {request.TeamId}"); + BoolRes boolRes = new(); + if (request.PlayerId >= spectatorMinPlayerID) + { + boolRes.ActSuccess = false; + return Task.FromResult(boolRes); + } + // var gameID = communicationToGameID[request.TeamId][request.PlayerId]; + boolRes.ActSuccess = game.RepairHome(request.TeamId, request.PlayerId); + GameServerLogging.logger.ConsoleLogDebug("END RepairHome"); + return Task.FromResult(boolRes); + } + + public override Task RepairWormhole(IDMsg request, ServerCallContext context) + { + GameServerLogging.logger.ConsoleLogDebug( + $"TRY RepairWormhole: Player {request.PlayerId} from Team {request.TeamId}"); + BoolRes boolRes = new(); + if (request.PlayerId >= spectatorMinPlayerID) + { + boolRes.ActSuccess = false; + return Task.FromResult(boolRes); + } + // var gameID = communicationToGameID[request.TeamId][request.PlayerId]; + boolRes.ActSuccess = game.RepairWormhole(request.TeamId, request.PlayerId); + GameServerLogging.logger.ConsoleLogDebug("END RepairWormhole"); + return Task.FromResult(boolRes); + } + public override Task Attack(AttackMsg request, ServerCallContext context) { GameServerLogging.logger.ConsoleLogDebug(