Add turn tracking and enforce turn order
Introduce CurrentTurn to Game (default 1) and expose it via GameInformationDto. Update GameManager to include CurrentTurn in the GameInformation payload, validate that a player can only Drop when it is their turn, and toggle CurrentTurn between 1 and 2 after a successful move. Also use a safe fallback (0) when game is null.
This commit is contained in:
@@ -43,7 +43,8 @@ public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSock
|
||||
{
|
||||
Players = game?.Players,
|
||||
CurrentField = game?.Field.CurrentField,
|
||||
State = game?.State
|
||||
State = game?.State,
|
||||
CurrentTurn = game?.CurrentTurn ?? 0
|
||||
};
|
||||
|
||||
await hubContext.Clients.Client(playerConnectionId).SendAsync("GameInformation", gameInfoDto);
|
||||
@@ -57,6 +58,9 @@ public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSock
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
if(game.CurrentTurn != player.PlayerTag)
|
||||
return;
|
||||
|
||||
var result = game.Field.Drop(column, player.PlayerTag);
|
||||
|
||||
if (result != DropResult.Placed)
|
||||
@@ -92,6 +96,8 @@ public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSock
|
||||
|
||||
ScheduleGameDeletion(game.Id, TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
game.CurrentTurn = game.CurrentTurn == 1 ? 2 : 1;
|
||||
}
|
||||
|
||||
public Task DisconnectedPlayer(string playerConnectionId)
|
||||
|
||||
Reference in New Issue
Block a user