game: add: backend input verification

This commit is contained in:
Namonay
2024-04-10 19:41:49 +02:00
parent 8b9c4d7c1c
commit a7af70f1f6
5 changed files with 112 additions and 21 deletions

View File

@ -17,7 +17,7 @@ class TicTacToe
this.game = new AGame(client, game_id, this.onReceive.bind(this), this.uninit.bind(this), "tictactoe")
this.canvas = canvas
this.context = this.canvas.getContext("2d");
this.sign = "x";
this.sign;
this.turn;
}
@ -31,6 +31,7 @@ class TicTacToe
async uninit()
{
this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
this.game.leave()
}
async onReceive(messageData)
@ -46,10 +47,38 @@ class TicTacToe
else
{
this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0;
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x")
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x");
if (this.checkWin() != -1)
printWin();
}
}
checkWin()
{
for (let i = 0; i < 9; i++)
{
for (let j = 0; j < 3; j++)
{
if (this.map[i][j] == this.map[i][j + 3] && this.map[i][j + 3] == this.map[i][j + 6])
return (this.map[i][j])
}
for (let j = 0; i < 9; i += 3)
{
if (this.map[i][j] == this.map[i][j + 1] && this.map[i][j + 1] == this.map[i][j + 2])
return (this.map[i][j])
}
if (this.map[i][0] == this.map[i][4] && this.map[i][4] == this.map[i][8])
return (this.map[i][0]);
if (this.map[i][6] == this.map[i][4] && this.map[i][4] == this.map[i][2])
return (this.map[i][6]);
return -1
}
}
printWin()
{
}
onClick(event, morpion)
{
let x = event.offsetX;
@ -72,7 +101,6 @@ class TicTacToe
checkCase(targetMorpion, targetCase)
{
console.log("TargetMorpion :" + targetMorpion + " targetCase :" + targetCase);
return (this.map[targetMorpion][targetCase] == -1 && this.turn == true);
}
@ -83,10 +111,10 @@ class TicTacToe
sendCase(targetMorpion, targetCase)
{
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1; // soit pas un bozo relis
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1;
this.printSign(targetMorpion, targetCase, this.sign);
console.log(this.game.send, targetMorpion, targetCase)
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase}));
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign}));
console.log(this.turn);
this.turn = !this.turn;
}