Skip to content

Commit

Permalink
Merge pull request #358 from Panxuc/dev
Browse files Browse the repository at this point in the history
feat: ✨ add Repair
  • Loading branch information
DreamEnderKing authored May 11, 2024
2 parents 66140a3 + 3488243 commit ac09459
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dependency/proto/Services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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); // 传递信息
// 大本营
Expand Down
16 changes: 15 additions & 1 deletion logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,10 +13,23 @@ public class Home(XY initPos, long id)
public InVariableRange<long> 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<long>(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);
}

}
57 changes: 56 additions & 1 deletion logic/Gaming/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>
(
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)
Expand Down
13 changes: 11 additions & 2 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,38 @@ public override Task<BoolRes> Construct(ConstructMsg request, ServerCallContext
return Task.FromResult(boolRes);
}

public override Task<BoolRes> 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<BoolRes> 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<BoolRes> Attack(AttackMsg request, ServerCallContext context)
{
GameServerLogging.logger.ConsoleLogDebug(
Expand Down

0 comments on commit ac09459

Please sign in to comment.