Skip to content

Commit

Permalink
Merge pull request #1 from Panxuc/new-ship
Browse files Browse the repository at this point in the history
New ship
  • Loading branch information
panxuc authored Apr 7, 2024
2 parents b078c8c + 3abf6ea commit 45e1079
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
3 changes: 1 addition & 2 deletions dependency/proto/Message2Server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
42 changes: 35 additions & 7 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,42 @@ public struct PlayerInitInfo(long teamID, long playerID, ShipType shipType)
public List<Team> TeamList => teamList;
private readonly Map gameMap;
public Map GameMap => gameMap;
private Random random = new();
public long AddPlayer(PlayerInitInfo playerInitInfo)
{
if (!gameMap.TeamExists(playerInitInfo.teamID))
{
return GameObj.invalidID;
}
if (playerInitInfo.shipType != ShipType.Null)
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)
Expand All @@ -46,21 +73,22 @@ 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, 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)
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);
return shipManager.ActivateShip(ship, pos);
if (shipManager.ActivateShip(ship, pos))
{
return ship.PlayerID;
}
return GameObj.invalidID;
}
public bool StartGame(int milliSeconds)
{
Expand Down
2 changes: 1 addition & 1 deletion logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public override Task<BoolRes> 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, Transformation.ShipTypeFromProto(request.ShipType), request.BirthpointIndex) != GameObj.invalidID;
#if DEBUG
Console.WriteLine("END BuildShip");
#endif
Expand Down

0 comments on commit 45e1079

Please sign in to comment.