Add resign button
This commit is contained in:
parent
10a4bd64d2
commit
3093754bd4
6 changed files with 79 additions and 28 deletions
|
|
@ -76,6 +76,12 @@ export class GomokuGame {
|
|||
return { success: true };
|
||||
}
|
||||
|
||||
public resign(resigningPlayerColor: PlayerColor) {
|
||||
this.winnerColor = resigningPlayerColor === 'white' ? 'black' : 'white';
|
||||
this.status = 'finished';
|
||||
this.currentPlayerColor = null;
|
||||
}
|
||||
|
||||
private checkWin(row: number, col: number, color: PlayerColor): boolean {
|
||||
const directions = [
|
||||
[1, 0], // vertical
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
export interface Message {
|
||||
type: 'make_move' | 'resign';
|
||||
gameId: string;
|
||||
playerId: string;
|
||||
}
|
||||
|
||||
export interface MakeMoveMessage extends Message {
|
||||
|
|
|
|||
|
|
@ -118,8 +118,12 @@ class GameServer {
|
|||
}
|
||||
|
||||
public broadcastButtonsToPlayer(conn: PlayerConnection) {
|
||||
// TODO
|
||||
console.log(`Sent buttons for game ${this.id} to player ${conn.id}`);
|
||||
let buttonsHtml;
|
||||
if (this.gomoku.status == 'playing' && this.getPlayerColor(conn)) {
|
||||
buttonsHtml = <button id="resign-button" ws-send="click">Resign</button>;
|
||||
}
|
||||
conn.ws.send(<div id="button-box">{buttonsHtml}</div>);
|
||||
console.log(`Sent buttons for game ${this.id} to player ${conn.id}`);
|
||||
}
|
||||
|
||||
private playerTag(playerId: string, color: PlayerColor) {
|
||||
|
|
@ -136,6 +140,9 @@ class GameServer {
|
|||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Handling ${message.type} message in game ${this.id} from player ${conn.id}: ${JSON.stringify(message)}`,
|
||||
);
|
||||
switch (message.type) {
|
||||
case 'make_move': {
|
||||
this.handleMakeMove(conn, message as MakeMoveMessage);
|
||||
|
|
@ -159,9 +166,6 @@ class GameServer {
|
|||
}
|
||||
|
||||
private handleMakeMove(conn: PlayerConnection, message: MakeMoveMessage): void {
|
||||
console.log(
|
||||
`Handling make_move message in game ${this.id} from player ${conn.id}: ${{ message }}`,
|
||||
);
|
||||
const { row, col } = message;
|
||||
|
||||
var playerColor;
|
||||
|
|
@ -180,7 +184,6 @@ class GameServer {
|
|||
|
||||
const stateBeforeMove = this.gomoku.status;
|
||||
const result = this.gomoku.makeMove(playerColor, row, col);
|
||||
console.log(result);
|
||||
if (result.success) {
|
||||
this.broadcastBoard();
|
||||
this.broadcastTitle();
|
||||
|
|
@ -196,8 +199,28 @@ class GameServer {
|
|||
}
|
||||
}
|
||||
|
||||
private handleResignation(conn: PlayerConnection, _message: ResignationMessage): void {
|
||||
// TODO
|
||||
private handleResignation(conn: PlayerConnection, message: ResignationMessage): void {
|
||||
console.log(
|
||||
`Handling resign message in game ${this.id} from player ${conn.id}: ${{ message }}`,
|
||||
);
|
||||
|
||||
if (this.gomoku.status !== 'playing') {
|
||||
conn.sendMessage('error', 'You can only resign from an active game.');
|
||||
return;
|
||||
}
|
||||
|
||||
const resigningPlayerColor = this.getPlayerColor(conn);
|
||||
if (!resigningPlayerColor) {
|
||||
conn.sendMessage('error', 'You are not a player in this game.');
|
||||
return;
|
||||
}
|
||||
|
||||
this.gomoku.resign(resigningPlayerColor);
|
||||
this.broadcastBoard();
|
||||
this.broadcastTitle();
|
||||
this.broadcastButtons();
|
||||
|
||||
console.log(`Player ${conn.id} resigned from game ${this.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue