add: tictactoe: timer and afk handling
This commit is contained in:
@ -37,7 +37,6 @@ class TicTacToe
|
||||
|
||||
async onReceive(messageData)
|
||||
{
|
||||
console.log(messageData)
|
||||
switch (messageData.detail)
|
||||
{
|
||||
case 'x':
|
||||
@ -48,16 +47,20 @@ class TicTacToe
|
||||
|
||||
case 'game_start':
|
||||
this.game.started = true;
|
||||
this.game.finished = false;
|
||||
if (this.turn)
|
||||
this.setOutline(4, false);
|
||||
this.printTimer();
|
||||
break;
|
||||
|
||||
case 'game_move':
|
||||
if (messageData.targetMorpion === undefined || messageData.targetCase === undefined)
|
||||
this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0;
|
||||
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x");
|
||||
this.setOutline(this.currentMorpion, false);
|
||||
this.printTimer();
|
||||
break;
|
||||
|
||||
|
||||
case 'game_end':
|
||||
this.game.finished = true;
|
||||
this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
|
||||
@ -89,7 +92,37 @@ class TicTacToe
|
||||
this.context.beginPath();
|
||||
this.context.fillStyle = (winning_sign == "o") ? "red" : "green";
|
||||
this.context.fillText((winning_sign == "o") ? lang.get("morpionWin") + "O" : lang.get("morpionWin") + "X", this.width / 2 - 30, this.height - this.gap / 2, 140);
|
||||
this.context.closePath();
|
||||
}
|
||||
|
||||
printTimer()
|
||||
{
|
||||
let sec = 20;
|
||||
let turn = this.turn
|
||||
let id = setInterval(() =>
|
||||
{
|
||||
this.context.beginPath();
|
||||
this.context.fillStyle = "#1a1a1d";
|
||||
this.context.fillRect(this.width / 2 - 40, 0, this.width / 2 + 40, this.gap - 10)
|
||||
this.context.closePath();
|
||||
if (sec == 0 || turn != this.turn || this.game.finished)
|
||||
{
|
||||
clearInterval(id);
|
||||
if (sec == 0 && !this.turn && this.game.finished == false)
|
||||
this.game.send(JSON.stringify({"timerIsDue" : this.sign}))
|
||||
return;
|
||||
}
|
||||
if (this.turn)
|
||||
{
|
||||
this.context.beginPath();
|
||||
this.context.fillStyle = "white";
|
||||
this.context.font = `40px Verdana`;
|
||||
this.context.fillText(sec, this.width / 2, this.gap / 2);
|
||||
this.context.font = `inherit`;
|
||||
this.context.closePath();
|
||||
}
|
||||
sec--;
|
||||
}, 1000
|
||||
)
|
||||
}
|
||||
checkWin()
|
||||
{
|
||||
@ -131,6 +164,7 @@ class TicTacToe
|
||||
{
|
||||
morpion.setOutline(this.currentMorpion, true);
|
||||
morpion.sendCase(targetMorpion, targetCase);
|
||||
morpion.printTimer()
|
||||
}
|
||||
else
|
||||
morpion.incorrectCase();
|
||||
|
Reference in New Issue
Block a user