diff --git a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js index 49d6d18..cc29116 100644 --- a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js +++ b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js @@ -36,20 +36,18 @@ class TicTacToe async onReceive(messageData) { - console.log(messageData) switch (messageData.detail) { case 'x': case 'o': this.sign = messageData.detail; this.turn = messageData.detail == "x"; - if (this.turn) + if (this.turn && this.game.started == false) this.setOutline(4, false); break; case 'game_start': this.game.started = true; - this.game.finished = false; break; case 'game_move': @@ -63,6 +61,20 @@ class TicTacToe this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion)); this.printWin(messageData.winning_sign); break; + case 'catchup': + this.map = messageData.morpion; + for (let i = 0; i < 9; i++) + { + for (let j = 0; j < 9; j++) + { + if (this.map[i][j] != -1) + this.printSign(i, j, this.map[i][j]) + } + } + this.turn = (messageData.turn == this.sign); + this.currentMorpion = messageData.currentMorpion; + if (this.turn) + this.setOutline(this.currentMorpion, false); } } @@ -98,11 +110,6 @@ class TicTacToe return -1 } } - - printWin() - { - - } onClick(event, morpion) { let x = event.offsetX; @@ -142,9 +149,7 @@ class TicTacToe this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1; this.currentMorpion = targetCase; this.printSign(targetMorpion, targetCase, this.sign); - console.log(targetMorpion, targetCase) this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign})); - console.log(this.turn); this.turn = !this.turn; } diff --git a/games/consumers.py b/games/consumers.py index 5bac2ef..5e56e89 100644 --- a/games/consumers.py +++ b/games/consumers.py @@ -40,9 +40,13 @@ class TicTacToeWebSocket(WebsocketConsumer): self.member = self.game.join(self.user.pk, self) + self.lastmovetimer = time.time() if (isinstance(self.member, TicTacToePlayer)): self.member.send(self.member.sign) + if (self.game._everbody_is_here() and self.game.model.started == True and self.game.model.finished == False): + self.member.send("catchup", {"morpion" : self.game._map, "turn" : self.game.turn, "currentMorpion": self.member.currentMorpion}) + return if (self.game._everbody_is_here() and self.game.model.started == False): if (self.game.time != -1): self.game.broadcast("opponent_joined") @@ -55,17 +59,14 @@ class TicTacToeWebSocket(WebsocketConsumer): if (data.get("targetMorpion") is not None and data.get("targetCase") is not None): if (self.game.add(data, self.member) == False): return - if (data.get("catchup") is not None and self.game.model.finished == False and self.game.model.finished == True): - self.member.send("catchup", {"Morpion": self.game._map, "turn": self.game.turn}) if (self.game.checkWin() != False): - print(self.game.checkWin()) - self.game.broadcast("game_end", {"winning_sign": self.member.sign}) + self.winner = self.game.checkWin() + self.game.broadcast("game_end", {"winning_sign": self.winner}) self.game.broadcast("game_move", data, [self.member]) pass def disconnect(self, event): self.member.socket = None - self.game.time = time.time() self.game.broadcast("opponent_leave_timer") class PongWebSocket(WebsocketConsumer): diff --git a/games/objects/tictactoe/TicTacToeGame.py b/games/objects/tictactoe/TicTacToeGame.py index 7befea5..769d51c 100644 --- a/games/objects/tictactoe/TicTacToeGame.py +++ b/games/objects/tictactoe/TicTacToeGame.py @@ -24,6 +24,8 @@ class TicTacToeGame(AGame): self._map = [[-1 for _ in range(9)] for _ in range(9)] + self.winner = None + def _everbody_is_here(self): return len(self.players) == len(self.get_players_connected()) @@ -41,21 +43,18 @@ class TicTacToeGame(AGame): return player def add(self, newmove, player): - for i in self._map: - print(i) - - print(newmove.get("targetMorpion"), newmove.get("targetCase"), newmove.get("sign")) - - if (self.checkMove(newmove, player)): + if (self.checkMove(newmove, player) and self.checkWin() == False): self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign") player.currentMorpion = int(newmove.get("targetCase")) - self.turn = newmove.get("sign") + self.changeTurn() return True return False + def changeTurn(self): + self.turn = 'x' if (self.turn == 'o') else 'o' + def checkMove(self, newmove, player): - print(int(newmove.get("targetMorpion")), player.currentMorpion) if (int(newmove.get("targetMorpion")) != player.currentMorpion or newmove.get("sign") != self.turn): return False @@ -68,7 +67,7 @@ class TicTacToeGame(AGame): for tab in self._map: for i in range(3): if tab[i] != -1 and tab[i] == tab[i + 3] and tab[i + 3] == tab[i + 6]: - return tab[i] + return for i in range(0, 9, 3): if tab[i] != -1 and tab[i] == tab[i + 1] and tab[i + 1] == tab[i + 2]: return tab[i]