Reformat
This commit is contained in:
parent
1f19022d45
commit
742bd4f106
8 changed files with 49 additions and 62 deletions
|
|
@ -12,7 +12,7 @@ interface MakeMoveMessage {
|
|||
col: number;
|
||||
}
|
||||
|
||||
type WS = ElysiaWS<{query: {playerId: string, gameId: string}}>;
|
||||
type WS = ElysiaWS<{ query: { playerId: string; gameId: string } }>;
|
||||
export class WebSocketHandler {
|
||||
private connections: Map<string, Array<WS>>;
|
||||
private games: Map<string, GameInstance>;
|
||||
|
|
@ -23,7 +23,7 @@ export class WebSocketHandler {
|
|||
}
|
||||
|
||||
public handleConnection(ws: WS): void {
|
||||
const {gameId, playerId} = ws.data.query;
|
||||
const { gameId, playerId } = ws.data.query;
|
||||
|
||||
if (!this.connections.has(gameId)) {
|
||||
this.connections.set(gameId, []);
|
||||
|
|
@ -53,14 +53,13 @@ export class WebSocketHandler {
|
|||
|
||||
private handleMakeMove(ws: WS, message: MakeMoveMessage): void {
|
||||
const { row, col } = message;
|
||||
const {gameId, playerId} = ws.data.query;
|
||||
console.log(`Handling make_move message in game ${gameId} from player ${playerId}: ${{message}}`);
|
||||
const { gameId, playerId } = ws.data.query;
|
||||
console.log(
|
||||
`Handling make_move message in game ${gameId} from player ${playerId}: ${{ message }}`,
|
||||
);
|
||||
|
||||
if (!gameId || !playerId || row === undefined || col === undefined) {
|
||||
this.sendMessage(
|
||||
ws,
|
||||
'Error: missing gameId, playerId, row, or col',
|
||||
);
|
||||
this.sendMessage(ws, 'Error: missing gameId, playerId, row, or col');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -74,10 +73,7 @@ export class WebSocketHandler {
|
|||
([_, id]) => id === playerId,
|
||||
)?.[0] as ('black' | 'white') | undefined;
|
||||
if (!playerColor) {
|
||||
this.sendMessage(
|
||||
ws,
|
||||
'Error: you are not a player in this game',
|
||||
);
|
||||
this.sendMessage(ws, 'Error: you are not a player in this game');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -102,11 +98,13 @@ export class WebSocketHandler {
|
|||
}
|
||||
|
||||
public handleDisconnect(ws: WS): void {
|
||||
const {gameId, playerId} = ws.data.query;
|
||||
const { gameId, playerId } = ws.data.query;
|
||||
|
||||
const connectionsInGame = this.connections.get(gameId);
|
||||
if (!connectionsInGame) {
|
||||
console.error(`Disconnecting WebSocket for player ${playerId} from game ${gameId}, but that game has no connections!`);
|
||||
console.error(
|
||||
`Disconnecting WebSocket for player ${playerId} from game ${gameId}, but that game has no connections!`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.connections.set(
|
||||
|
|
@ -127,28 +125,30 @@ export class WebSocketHandler {
|
|||
public broadcastGameState(gameId: string): void {
|
||||
const game = this.games.get(gameId);
|
||||
if (!game) {
|
||||
console.warn('Attempted to broadcast state of game ${gameId}, which is not loaded.');
|
||||
console.warn(
|
||||
'Attempted to broadcast state of game ${gameId}, which is not loaded.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const connectionsToUpdate = this.connections.get(gameId);
|
||||
if (connectionsToUpdate) {
|
||||
connectionsToUpdate.forEach((ws) => {
|
||||
const {gameId, playerId} = ws.data.query;
|
||||
const { gameId, playerId } = ws.data.query;
|
||||
|
||||
const updatedBoardHtml = renderGameBoardHtml(game, playerId);
|
||||
ws.send(updatedBoardHtml);
|
||||
const updatedPlayerInfoHtml = renderPlayerInfoHtml(
|
||||
game.id,
|
||||
playerId,
|
||||
);
|
||||
const updatedPlayerInfoHtml = renderPlayerInfoHtml(game.id, playerId);
|
||||
ws.send(updatedPlayerInfoHtml);
|
||||
|
||||
if (game.status === 'finished') {
|
||||
if (game.winner === 'draw') {
|
||||
this.sendMessageToGame(gameId, 'Game ended in draw.');
|
||||
} else if (game.winner) {
|
||||
this.sendMessageToGame(gameId, `${game.winner.toUpperCase()} wins!`);
|
||||
this.sendMessageToGame(
|
||||
gameId,
|
||||
`${game.winner.toUpperCase()} wins!`,
|
||||
);
|
||||
}
|
||||
} else if (game.status === 'playing') {
|
||||
const clientPlayerColor = Object.entries(game.players).find(
|
||||
|
|
@ -168,27 +168,21 @@ export class WebSocketHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public sendMessageToGame(
|
||||
gameId: string,
|
||||
message: string,
|
||||
): void {
|
||||
public sendMessageToGame(gameId: string, message: string): void {
|
||||
const connections = this.connections.get(gameId);
|
||||
if (connections) {
|
||||
connections.forEach((ws) => {
|
||||
ws.send('<div id="messages">' + message + '</div>')
|
||||
ws.send('<div id="messages">' + message + '</div>');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public sendMessage(
|
||||
targetWs: WS,
|
||||
message: string,
|
||||
): void {
|
||||
targetWs.send('<div id="messages">' + message + '</div>')
|
||||
public sendMessage(targetWs: WS, message: string): void {
|
||||
targetWs.send('<div id="messages">' + message + '</div>');
|
||||
}
|
||||
|
||||
public getGame(gameId: string): GameInstance | undefined {
|
||||
return this.games.get(gameId)
|
||||
return this.games.get(gameId);
|
||||
}
|
||||
|
||||
createGame(gameId?: string): GameInstance {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue