mirror of
https://github.com/thib8956/tic-tac-toe-ws.git
synced 2024-11-16 17:06:33 +00:00
fix endgame condition
This commit is contained in:
parent
8333df9580
commit
f56fc0a65c
16
client.mts
16
client.mts
@ -22,11 +22,11 @@ interface Shape {
|
||||
time: number;
|
||||
}
|
||||
|
||||
let grid = [0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
let circle = true;
|
||||
let grid = [0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||
let pendingEvts: Point[] = [];
|
||||
let shapes: Shape[] = [];
|
||||
let myId: number | null = null;
|
||||
let mySymbol: "x" | "o" | null = null;
|
||||
let canvasMsg: string = "Offline";
|
||||
|
||||
function drawGridBackground(ctx: CanvasRenderingContext2D, origin: Point) {
|
||||
@ -105,18 +105,17 @@ function updateGridState(ctx: CanvasRenderingContext2D, gridOrigin: Point) {
|
||||
for (let x = 0; x < 3; ++x) {
|
||||
switch (grid[y*3+x]) {
|
||||
case 0: break;
|
||||
case 1: {
|
||||
case myId: {
|
||||
const p = gridIndexToCoords(gridOrigin, x, y);
|
||||
drawCircle(ctx, p);
|
||||
mySymbol == "o" ? drawCircle(ctx, p) : drawCross(ctx, p);
|
||||
break;
|
||||
|
||||
}
|
||||
case 2: {
|
||||
default: {
|
||||
const p = gridIndexToCoords(gridOrigin, x, y);
|
||||
drawCross(ctx, p);
|
||||
mySymbol == "o" ? drawCross(ctx, p) : drawCircle(ctx, p);
|
||||
break;
|
||||
}
|
||||
default: throw new Error(`unhandled grid state ${grid[y*3+x]}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,7 +164,8 @@ function init() {
|
||||
switch (msg.kind) {
|
||||
case "hello": {
|
||||
myId = (msg.data as Hello).id;
|
||||
canvasMsg = `connected to server with id ${myId}`;
|
||||
mySymbol = (msg.data as Hello).symbol;
|
||||
canvasMsg = `connected to server with id ${myId}, ${mySymbol}`;
|
||||
console.log(canvasMsg);
|
||||
break;
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ export interface Response {
|
||||
}
|
||||
|
||||
export interface Hello {
|
||||
id: number
|
||||
id: number,
|
||||
symbol: "x" | "o"
|
||||
}
|
||||
|
||||
export interface EndGame {
|
||||
|
18
server.mts
18
server.mts
@ -1,5 +1,5 @@
|
||||
import { Message, Response, Hello, EndGame } from "common.mjs"
|
||||
import { WebSocket, WebSocketServer } from "ws";
|
||||
import { WebSocket, WebSocketServer, MessageEvent } from "ws";
|
||||
|
||||
const port = 1234
|
||||
const wss = new WebSocketServer({ port });
|
||||
@ -28,27 +28,31 @@ wss.on("connection", (ws) => {
|
||||
}
|
||||
|
||||
const symbol = clients.length == 0 ? "o" : "x";
|
||||
const helloMsg: Message = {
|
||||
kind: "hello",
|
||||
data: { id, symbol } as Hello
|
||||
}
|
||||
clients.push({id, ws, symbol});
|
||||
ws.send(JSON.stringify({kind: "hello", data: { id } as Hello}));
|
||||
ws.send(JSON.stringify(helloMsg));
|
||||
console.log(`player #${id} connected`);
|
||||
|
||||
ws.addEventListener("message", (event: any) => {
|
||||
const message = JSON.parse(event.data);
|
||||
ws.addEventListener("message", (event: MessageEvent) => {
|
||||
const message = JSON.parse(event.data as string);
|
||||
const {x, y} = message;
|
||||
const player = clients.find(x => x.ws === ws);
|
||||
console.assert(player !== undefined);
|
||||
if (!player) throw new Error("player not found");
|
||||
console.log(message, player!.id, currentPlayer?.id);
|
||||
|
||||
if (!currentPlayer) {
|
||||
currentPlayer = player;
|
||||
}
|
||||
|
||||
if (clients.length < 2 || player!.id != currentPlayer?.id || endGame) {
|
||||
if (clients.length < 2 || player.id != currentPlayer?.id || endGame) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (grid[y*3+x] === 0) {
|
||||
grid[y*3+x] = player!.symbol == "o" ? 1 : 2;
|
||||
grid[y*3+x] = player.id;
|
||||
for (const c of clients) {
|
||||
const msg: Message = {
|
||||
kind: "update",
|
||||
|
Loading…
Reference in New Issue
Block a user