Use GameInformationDto for FieldUpdated events

Send a GameInformationDto from the GameManager when broadcasting FieldUpdated (includes players, current field, state and current turn) and move currentTurn toggle before the broadcast. Update client code (GameConnection and LocalGame) to accept GameInformationDto for FieldUpdated, rename handler to updateState and apply the full game state payload. This ensures clients receive consistent game metadata (including current turn) with each field update.
This commit is contained in:
Jonas
2026-03-05 22:07:48 +01:00
parent 0eed8020b8
commit 34f683854b
3 changed files with 17 additions and 10 deletions
+10 -3
View File
@@ -77,7 +77,16 @@ public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSock
return; return;
} }
await hubContext.Clients.Group(game.Id).SendAsync("FieldUpdated", ConvertField(game.Field.CurrentField)); game.CurrentTurn = game.CurrentTurn == 1 ? 2 : 1;
var gameInfoDto = new GameInformationDto
{
Players = game?.Players,
CurrentField = ConvertField(game?.Field.CurrentField),
State = game?.State,
CurrentTurn = game?.CurrentTurn ?? 0
};
await hubContext.Clients.Group(game.Id).SendAsync("FieldUpdated", gameInfoDto);
var winResult = game.Field.CheckForWin(); var winResult = game.Field.CheckForWin();
if (winResult != 0) if (winResult != 0)
@@ -104,8 +113,6 @@ public class GameManager(IGameRepository gameRepository, IHubContext<GameHubSock
ScheduleGameDeletion(game.Id, TimeSpan.FromSeconds(5)); ScheduleGameDeletion(game.Id, TimeSpan.FromSeconds(5));
} }
game.CurrentTurn = game.CurrentTurn == 1 ? 2 : 1;
} }
public Task DisconnectedPlayer(string playerConnectionId) public Task DisconnectedPlayer(string playerConnectionId)
+4 -4
View File
@@ -21,12 +21,12 @@ class LocalGame {
this.player1.onGameStarted = (gameInfo: GameInformationDto) => { this.player1.onGameStarted = (gameInfo: GameInformationDto) => {
console.log('Game started for player 1', gameInfo); console.log('Game started for player 1', gameInfo);
this.gameStarted(gameInfo); this.updateState(gameInfo);
}; };
this.player1.onFieldUpdated = (currentField: number[][]) => { this.player1.onFieldUpdated = (currentState: GameInformationDto) => {
if (this.gameState) { if (this.gameState) {
this.gameState.currentField = currentField; this.gameState = currentState;
this.onGameStateChanged?.(this.gameState); this.onGameStateChanged?.(this.gameState);
} }
}; };
@@ -48,7 +48,7 @@ class LocalGame {
await this.player1.createGame(this._settings.fieldSize); await this.player1.createGame(this._settings.fieldSize);
} }
async gameStarted(gameInfo: GameInformationDto) { async updateState(gameInfo: GameInformationDto) {
this.gameState = gameInfo; this.gameState = gameInfo;
this.onGameStateChanged?.(this.gameState); this.onGameStateChanged?.(this.gameState);
} }
@@ -33,7 +33,7 @@ class GameConnection {
public onGameJoined?: (gameIdentifier: GameIdentifier) => void; public onGameJoined?: (gameIdentifier: GameIdentifier) => void;
public onGameStarted?: (gameInfo: GameInformationDto) => void; public onGameStarted?: (gameInfo: GameInformationDto) => void;
public onGameInformation?: (gameInfo: GameInformationDto) => void; public onGameInformation?: (gameInfo: GameInformationDto) => void;
public onFieldUpdated?: (currentField: number[][]) => void; public onFieldUpdated?: (currentField: GameInformationDto) => void;
public onGameEnded?: (gameEndedInfo: GameEndedDto) => void; public onGameEnded?: (gameEndedInfo: GameEndedDto) => void;
public onError?: (error: string) => void; public onError?: (error: string) => void;
public onGameDestroyed?: () => void; public onGameDestroyed?: () => void;
@@ -61,8 +61,8 @@ class GameConnection {
this.onGameInformation?.(payload); this.onGameInformation?.(payload);
}); });
this.connection.on('FieldUpdated', (currentField: number[][]) => { this.connection.on('FieldUpdated', (payload: GameInformationDto) => {
this.onFieldUpdated?.(currentField); this.onFieldUpdated?.(payload);
}); });
this.connection.on('GameEnded', (payload: GameEndedDto) => { this.connection.on('GameEnded', (payload: GameEndedDto) => {