From ced838e5e29226571e035e8417b27d8e75a68b39 Mon Sep 17 00:00:00 2001 From: Xuc Pan Date: Sat, 6 Apr 2024 21:50:12 +0800 Subject: [PATCH 1/2] fix: :bug: ActivateShip always false --- logic/Gaming/Game.cs | 14 ++++++++------ logic/Server/RpcServices.cs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index ccfd44d7..6e29ef98 100755 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -28,7 +28,7 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) { return GameObj.invalidID; } - if (playerInitInfo.shipType != ShipType.Null) + if (playerInitInfo.playerID != 0) { // Add a ship Ship? newShip = shipManager.AddShip(playerInitInfo.teamID, playerInitInfo.playerID, @@ -46,13 +46,11 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) return playerInitInfo.playerID; } } - public bool ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0) + public long ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0) { Ship? ship = teamList[(int)teamID].ShipPool.GetObj(shipType); if (ship == null) - return false; - else if (ship.IsRemoved == false) - return false; + return GameObj.invalidID; if (birthPointIndex < 0) birthPointIndex = 0; if (birthPointIndex >= teamList[(int)teamID].BirthPointList.Count) @@ -60,7 +58,11 @@ public bool ActivateShip(long teamID, long playerID, ShipType shipType, int birt XY pos = teamList[(int)teamID].BirthPointList[birthPointIndex]; Random random = new(); pos += new XY(((random.Next() & 2) - 1) * 1000, ((random.Next() & 2) - 1) * 1000); - return shipManager.ActivateShip(ship, pos); + if (shipManager.ActivateShip(ship, pos)) + { + return ship.PlayerID; + } + return GameObj.invalidID; } public bool StartGame(int milliSeconds) { diff --git a/logic/Server/RpcServices.cs b/logic/Server/RpcServices.cs index 917bb74d..6eea14fb 100755 --- a/logic/Server/RpcServices.cs +++ b/logic/Server/RpcServices.cs @@ -519,7 +519,7 @@ public override Task BuildShip(BuildShipMsg request, ServerCallContext boolRes.ActSuccess = false; return Task.FromResult(boolRes); } - boolRes.ActSuccess = game.ActivateShip(request.TeamId, request.PlayerId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex); + boolRes.ActSuccess = game.ActivateShip(request.TeamId, request.PlayerId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex) != GameObj.invalidID; #if DEBUG Console.WriteLine("END BuildShip"); #endif From 3abf6ea3904408db8f160353beb234de2c50c5ba Mon Sep 17 00:00:00 2001 From: Xuc Pan Date: Sun, 7 Apr 2024 19:04:58 +0800 Subject: [PATCH 2/2] feat: :sparkles: ship type validation check --- dependency/proto/Message2Server.proto | 3 +-- logic/Gaming/Game.cs | 30 +++++++++++++++++++++++++-- logic/Server/RpcServices.cs | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/dependency/proto/Message2Server.proto b/dependency/proto/Message2Server.proto index 0c32e16f..90c1d45b 100755 --- a/dependency/proto/Message2Server.proto +++ b/dependency/proto/Message2Server.proto @@ -70,6 +70,5 @@ message BuildShipMsg { ShipType ship_type = 1; int64 team_id = 2; - int64 player_id = 3; - int32 birthpoint_index = 4; + int32 birthpoint_index = 3; } \ No newline at end of file diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index 6f033292..12fb7167 100755 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -22,6 +22,7 @@ public struct PlayerInitInfo(long teamID, long playerID, ShipType shipType) public List TeamList => teamList; private readonly Map gameMap; public Map GameMap => gameMap; + private Random random = new(); public long AddPlayer(PlayerInitInfo playerInitInfo) { if (!gameMap.TeamExists(playerInitInfo.teamID)) @@ -31,6 +32,32 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) if (playerInitInfo.playerID != 0) { // Add a ship + var shipType = playerInitInfo.shipType; + switch (shipType) + { + case ShipType.Null: + return GameObj.invalidID; + case ShipType.CivilShip: + if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.CivilShip) >= GameData.MaxCivilShipNum) + { + return GameObj.invalidID; + } + break; + case ShipType.WarShip: + if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.WarShip) >= GameData.MaxWarShipNum) + { + return GameObj.invalidID; + } + break; + case ShipType.FlagShip: + if (teamList[(int)playerInitInfo.teamID].ShipPool.GetNum(ShipType.FlagShip) >= GameData.MaxFlagShipNum) + { + return GameObj.invalidID; + } + break; + default: + return GameObj.invalidID; + } Ship? newShip = shipManager.AddShip(playerInitInfo.teamID, playerInitInfo.playerID, playerInitInfo.shipType, teamList[(int)playerInitInfo.teamID].MoneyPool); if (newShip == null) @@ -46,7 +73,7 @@ public long AddPlayer(PlayerInitInfo playerInitInfo) return playerInitInfo.playerID; } } - public long ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0) + public long ActivateShip(long teamID, ShipType shipType, int birthPointIndex = 0) { Ship? ship = teamList[(int)teamID].ShipPool.GetObj(shipType); if (ship == null) @@ -56,7 +83,6 @@ public long ActivateShip(long teamID, long playerID, ShipType shipType, int birt if (birthPointIndex >= teamList[(int)teamID].BirthPointList.Count) birthPointIndex = teamList[(int)teamID].BirthPointList.Count - 1; XY pos = teamList[(int)teamID].BirthPointList[birthPointIndex]; - Random random = new(); pos += new XY(((random.Next() & 2) - 1) * 1000, ((random.Next() & 2) - 1) * 1000); if (shipManager.ActivateShip(ship, pos)) { diff --git a/logic/Server/RpcServices.cs b/logic/Server/RpcServices.cs index 6eea14fb..1fff2abb 100755 --- a/logic/Server/RpcServices.cs +++ b/logic/Server/RpcServices.cs @@ -519,7 +519,7 @@ public override Task BuildShip(BuildShipMsg request, ServerCallContext boolRes.ActSuccess = false; return Task.FromResult(boolRes); } - boolRes.ActSuccess = game.ActivateShip(request.TeamId, request.PlayerId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex) != GameObj.invalidID; + boolRes.ActSuccess = game.ActivateShip(request.TeamId, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex) != GameObj.invalidID; #if DEBUG Console.WriteLine("END BuildShip"); #endif