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;
}

View File

@ -39,8 +39,8 @@ export class TicTacToeOnlineView extends AbstractView
<canvas id="Morpion" width="${this.width}" height="${this.height}"></canvas>
</div>
<h1>Work in progress bozo</h1>
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824821680275576/cat-thumbs-up-middle-finger-cat.png?ex=6613fc09&is=66018709&hm=d901ea182d58ce0ab80fbcc119ae0b769f318c448ae55a6500607ddb8b3f6ff8&">
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824792148316180/thumbs-up-cat.png?ex=6613fc02&is=66018702&hm=16748a2df0cb8e8f111c0817ea456289333aa0f8047821e7ae4ea2e57720e843&">
<img src="https://cdn.discordapp.com/attachments/447859443867189250/1221824753686548580/catthumbsup-cat.png?ex=6613fbf9&is=660186f9&hm=94e93b8db9f72fc146942dedf167a3d7476b3dced95ed5c3e80a4ccce8e27f9e&">`;
<img src="https://i.imgur.com/hh4yjO9.jpeg&">
<img src="https://i.imgur.com/TpOtHY6.png&">
<img src="https://i.imgur.com/O8F314O.jpeg&">`;
}
}