diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index a1e553d..0a1643f 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -30,6 +30,11 @@ export class Profile extends AExchangeable */ this.avatar = avatar; + /** + * @type {Boolean} + **/ + this.online = null; + /** * @type {Boolean} */ @@ -58,6 +63,7 @@ export class Profile extends AExchangeable this.id = response_data.id; this.username = response_data.username; this.avatar = response_data.avatar; + this.online = response_data.online if (!this.client.me || this.client.me.id === this.id) return; diff --git a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js index 49d6d18..ffbaad5 100644 --- a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js +++ b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js @@ -43,13 +43,12 @@ class TicTacToe case 'o': this.sign = messageData.detail; this.turn = messageData.detail == "x"; - if (this.turn) - this.setOutline(4, false); break; case 'game_start': this.game.started = true; - this.game.finished = false; + if (this.turn) + this.setOutline(4, false); break; case 'game_move': @@ -63,6 +62,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 +111,6 @@ class TicTacToe return -1 } } - - printWin() - { - - } onClick(event, morpion) { let x = event.offsetX; @@ -142,9 +150,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..08ba4d4 100644 --- a/games/consumers.py +++ b/games/consumers.py @@ -38,11 +38,16 @@ class TicTacToeWebSocket(WebsocketConsumer): self.game: TicTacToeGame = game_manager.get(self.game_id, "tictactoe") + if (self.game is None): + return self.member = self.game.join(self.user.pk, self) 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,18 +60,18 @@ 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.model.finish(self.user) + 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") + try: + self.member.socket = None + except: + pass 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] diff --git a/profiles/serializers.py b/profiles/serializers.py index 5d0a8be..217b45a 100644 --- a/profiles/serializers.py +++ b/profiles/serializers.py @@ -25,12 +25,9 @@ class ProfileSerializer(serializers.ModelSerializer): user = request.user if user is None: - return False + return None - if user.pk == obj.pk: - return True - - if not user.profilemodel.is_friend(obj): + if not user.profilemodel.is_friend(obj) and user.pk != obj.pk: return None return notice_manager.get_consumer_by_user(obj.user) is not None diff --git a/profiles/viewsets/MyProfileViewSet.py b/profiles/viewsets/MyProfileViewSet.py index 97a7b6a..5ac6cdb 100644 --- a/profiles/viewsets/MyProfileViewSet.py +++ b/profiles/viewsets/MyProfileViewSet.py @@ -35,4 +35,4 @@ class MyProfileViewSet(viewsets.ModelViewSet): return Response(ProfileSerializer(profile).data) def retrieve(self, pk=None): - return Response(self.serializer_class(self.get_object()).data) + return Response(self.serializer_class(self.get_object(), context={'request': self.request}).data)