From 5040f35c04be9d29b3d37f4767b82f29028e2cb4 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 21 Apr 2024 19:29:20 +0200 Subject: [PATCH 01/16] fix: matchmaking --- matchmaking/models.py | 2 +- transcendence/abstract/AbstractRoom.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/matchmaking/models.py b/matchmaking/models.py index f1261f0..58a0af0 100644 --- a/matchmaking/models.py +++ b/matchmaking/models.py @@ -23,7 +23,7 @@ class WaitingRoom(AbstractRoom): if (tmp is not None): tmp.send("Connection close: Another connection open with the same user id.") self.remove(tmp) - waiter.accept() + waiter.socket.accept() self._member_list.append(waiter) class WaitingRoomManager(AbstractRoomManager): diff --git a/transcendence/abstract/AbstractRoom.py b/transcendence/abstract/AbstractRoom.py index afdad42..620d037 100644 --- a/transcendence/abstract/AbstractRoom.py +++ b/transcendence/abstract/AbstractRoom.py @@ -37,7 +37,7 @@ class AbstractRoom: def remove(self, member: AbstractRoomMember, code: int = 1000): self._member_list.remove(member) - member.disconnect(code) + member.socket.disconnect(code) def empty(self): for _ in self._member_list: From e125eb16c78a4e3a179696d38b5dae8355e7c675 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Apr 2024 11:37:08 +0200 Subject: [PATCH 02/16] core: fix: matchmaking and game --- frontend/static/js/api/Matchmaking.js | 4 +- frontend/static/js/views/MatchMakingView.js | 26 +++---- games/models.py | 18 +++-- games/objects/AGame.py | 10 ++- games/objects/APlayer.py | 3 - games/objects/ASpectator.py | 6 +- games/objects/pong/PongGame.py | 10 +-- games/objects/pong/PongPlayer.py | 12 ++- games/routine.py | 1 - games/serializers.py | 20 +---- matchmaking/consumers.py | 34 ++++---- matchmaking/models.py | 33 +++++--- matchmaking/routing.py | 2 +- transcendence/abstract/AbstractRoom.py | 78 ++++++++++--------- transcendence/abstract/AbstractRoomManager.py | 4 +- transcendence/abstract/AbstractRoomMember.py | 8 +- 16 files changed, 135 insertions(+), 134 deletions(-) diff --git a/frontend/static/js/api/Matchmaking.js b/frontend/static/js/api/Matchmaking.js index b0e2240..bcf4173 100644 --- a/frontend/static/js/api/Matchmaking.js +++ b/frontend/static/js/api/Matchmaking.js @@ -21,12 +21,12 @@ class MatchMaking * @param {Number} mode The number of players in a game * @returns {Promise} */ - async start(receive_func, disconnect_func, gamemode, mode) + async start(receive_func, disconnect_func, game_type, mode) { if (!await this.client.isAuthenticated()) return null; - let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${gamemode}/${mode}`; + let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${game_type}/${mode}`; this._socket = new WebSocket(url); diff --git a/frontend/static/js/views/MatchMakingView.js b/frontend/static/js/views/MatchMakingView.js index 2327053..b8bce29 100644 --- a/frontend/static/js/views/MatchMakingView.js +++ b/frontend/static/js/views/MatchMakingView.js @@ -19,7 +19,7 @@ export default class extends AbstractAuthenticatedView { } else { - await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), this.gamemode_input.value, this.nb_players_input.value); + await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), this.game_type_input.value, this.nb_players_input.value); this.button.innerHTML = lang.get("matchmakingStopSearch"); } @@ -34,10 +34,10 @@ export default class extends AbstractAuthenticatedView { { if (data.detail === "game_found") { - if (this.gamemode_input.value == "pong") - navigateTo(`/games/${data.gamemode}/${data.game_id}`); + if (this.game_type_input.value == "pong") + navigateTo(`/games/${data.game_type}/${data.game_id}`); else - navigateTo(`/games/${data.gamemode}/${data.game_id}`); + navigateTo(`/games/${data.game_type}/${data.game_id}`); return; } this.display_data(data); @@ -51,7 +51,7 @@ export default class extends AbstractAuthenticatedView { addEnterEvent() { - [this.nb_players_input, this.gamemode_input].forEach((input) => { + [this.nb_players_input, this.game_type_input].forEach((input) => { input.addEventListener('keydown', async ev => { @@ -74,13 +74,13 @@ export default class extends AbstractAuthenticatedView { }); } - addChangeGameModeEvent() + addChangegame_typeEvent() { let nb_players_div = document.getElementById("nb-players-div"); - this.gamemode_input.addEventListener("change", () => { + this.game_type_input.addEventListener("change", () => { - if (this.gamemode_input.value === "tictactoe") + if (this.game_type_input.value === "tictactoe") { nb_players_div.style.display = 'none'; this.nb_players_input.value = 2; @@ -96,7 +96,7 @@ export default class extends AbstractAuthenticatedView { addEvents() { this.addEnterEvent(); - this.addChangeGameModeEvent(); + this.addChangegame_typeEvent(); this.addChangeNbPlayersEvent(); } @@ -104,7 +104,7 @@ export default class extends AbstractAuthenticatedView { { this.button = document.getElementById("toggle-search"); this.nb_players_input = document.getElementById("nb-players-input"); - this.gamemode_input = document.getElementById("gamemode-input"); + this.game_type_input = document.getElementById("game-type-input"); this.button.onclick = this.toggle_search.bind(this); @@ -117,12 +117,12 @@ export default class extends AbstractAuthenticatedView {

${lang.get("matchmakingTitle")}

-
- - +
diff --git a/games/models.py b/games/models.py index 3fca337..37bfcce 100644 --- a/games/models.py +++ b/games/models.py @@ -3,25 +3,24 @@ from __future__ import annotations from django.db import models from django.db.models import QuerySet, CASCADE -from profiles.models import ProfileModel +from django.contrib.auth.models import User import time -# Create your models here. class GameModel(models.Model): finished = models.BooleanField(default = False) started = models.BooleanField(default = False) - winner = models.ForeignKey(ProfileModel, on_delete=CASCADE, null=True, blank=True) + winner = models.ForeignKey(User, on_delete=CASCADE, null=True, blank=True) start_timestamp = models.BigIntegerField(null = True, blank = True) stop_timestamp = models.BigIntegerField(null = True, blank = True) game_type = models.CharField(max_length = 60, default = "pong") - def create(self, players: list[ProfileModel]): + def create(self, players: list[User]): self.save() for player in players: - GameMembersModel(game = self.pk, player=player).save() + GameMembersModel(game = self, player=player).save() return self.pk def start(self): @@ -35,9 +34,12 @@ class GameModel(models.Model): self.stop_timestamp = round(time.time() * 1000, 1) self.save() - def get_players(self) -> list[ProfileModel]: + def get_players(self) -> list[User]: return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)] + def get_players_profiles(self) -> list[User]: + return [game_player.player.profilemodel for game_player in GameMembersModel.objects.filter(game = self)] + def get_score_by_player_id(self, player_id: int) -> list[int]: query: QuerySet = GameGoalModel.objects.filter(game_id = self.pk, player_id = player_id) score_data: list[int] = [game_goal.timestamp for game_goal in query] @@ -56,10 +58,10 @@ class GameModel(models.Model): class GameMembersModel(models.Model): game = models.ForeignKey(GameModel, on_delete=CASCADE) - player = models.ForeignKey(ProfileModel, on_delete=CASCADE) + player = models.ForeignKey(User, on_delete=CASCADE) class GameGoalModel(models.Model): game = models.ForeignKey(GameModel, on_delete=CASCADE) - player = models.ForeignKey(ProfileModel, on_delete=CASCADE) + player = models.ForeignKey(User, on_delete=CASCADE) timestamp = models.IntegerField() \ No newline at end of file diff --git a/games/objects/AGame.py b/games/objects/AGame.py index b0ff08c..6839a61 100644 --- a/games/objects/AGame.py +++ b/games/objects/AGame.py @@ -6,6 +6,8 @@ from .ASpectator import ASpectator from ..models import GameModel +from django.contrib.auth.models import User + class AGame(AbstractRoom): def __init__(self, game_type: str, game_id: int, game_manager): @@ -16,23 +18,23 @@ class AGame(AbstractRoom): self.model: GameModel = GameModel.objects.get(pk = game_id, game_type = game_type) - players_id: list[int] = self.model.get_players_id() + players: list[User] = self.model.get_players() - self.players: list[APlayer] = [APlayer(player_id, None, self) for player_id in players_id] + self.players: list[APlayer] = [APlayer(player.pk, None, self) for player in players] self.spectators: list[ASpectator] = [] self.game_id: int = game_id def get_players_id(self) -> list[int]: - return [player.user_id for player in self.players] + return [player.pk for player in self.players] def get_players_connected(self) -> list[APlayer]: return [player for player in self.players if player.is_connected()] def get_player_by_user_id(self, user_id: int) -> APlayer: for player in self.players: - if (player.user_id == user_id): + if (player.user.pk == user_id): return player return None diff --git a/games/objects/APlayer.py b/games/objects/APlayer.py index 20edb67..1d4bcca 100644 --- a/games/objects/APlayer.py +++ b/games/objects/APlayer.py @@ -10,9 +10,6 @@ if TYPE_CHECKING: class APlayer(ASpectator): - def __init__(self, user_id: int, socket: WebsocketConsumer, game: AGame): - super().__init__(user_id, socket, game) - def is_connected(self) -> bool: return self.socket != None diff --git a/games/objects/ASpectator.py b/games/objects/ASpectator.py index f5a34e5..f7f2fd6 100644 --- a/games/objects/ASpectator.py +++ b/games/objects/ASpectator.py @@ -3,6 +3,8 @@ from channels.generic.websocket import WebsocketConsumer from transcendence.abstract.AbstractRoomMember import AbstractRoomMember +from django.contrib.auth.models import User + from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -10,8 +12,8 @@ if TYPE_CHECKING: class ASpectator(AbstractRoomMember): - def __init__(self, user_id: int, socket: WebsocketConsumer, game): + def __init__(self, user: User, socket: WebsocketConsumer, game): - super().__init__(user_id, socket) + super().__init__(user, socket) self.game: AGame = game \ No newline at end of file diff --git a/games/objects/pong/PongGame.py b/games/objects/pong/PongGame.py index 91f2de4..76296c7 100644 --- a/games/objects/pong/PongGame.py +++ b/games/objects/pong/PongGame.py @@ -22,7 +22,7 @@ import threading from typing import TYPE_CHECKING if TYPE_CHECKING: - pass + from profiles.models import ProfileModel class PongGame(AGame): @@ -36,7 +36,7 @@ class PongGame(AGame): radius: float = min(config.MAP_SIZE_X, config.MAP_SIZE_Y) / 2 - 10 - players_id: list[int] = self.model.get_players_id() + players: list[ProfileModel] = self.model.get_players() nb_sides = 4 @@ -58,16 +58,16 @@ class PongGame(AGame): self.walls: list[Wall] self.players: list[PongPlayer] - nb_players: int = len(players_id) + nb_players: int = len(players) if (nb_players == 2): - self.players = [PongPlayer(self, players_id[0], None, segments[0]), PongPlayer(self, players_id[1], None, segments[2])] + self.players = [PongPlayer(self, players[0], None, segments[0]), PongPlayer(self, players[1], None, segments[2])] self.walls = [Wall(segments[1].start, segments[1].stop), Wall(segments[3].start, segments[3].stop)] else: self.players = [] self.walls = [] for i in range(4): if (i < nb_players): - self.players.append(PongPlayer(self, players_id[i], None, segments[i])) + self.players.append(PongPlayer(self, players[i], None, segments[i])) else: self.walls.append(Wall(segments[i])) diff --git a/games/objects/pong/PongPlayer.py b/games/objects/pong/PongPlayer.py index 14db6e7..aeaa5de 100644 --- a/games/objects/pong/PongPlayer.py +++ b/games/objects/pong/PongPlayer.py @@ -17,9 +17,9 @@ if TYPE_CHECKING: class PongPlayer(APlayer): - def __init__(self, game: PongGame, user_id: int, socket: WebsocketConsumer, rail: Segment) -> None: + def __init__(self, game: PongGame, user: User, socket: WebsocketConsumer, rail: Segment) -> None: - super().__init__(user_id, socket, game) + super().__init__(user, socket, game) self.position: Position = Position(0.5, 0) @@ -29,8 +29,6 @@ class PongPlayer(APlayer): self.game: PongGame - self.username: str = User.objects.get(pk = self.user_id).username - def eliminate(self): self.disconnect(1000) @@ -43,7 +41,7 @@ class PongPlayer(APlayer): if (detail is None): return - + if (detail == "update_my_paddle_pos"): self.update_position(data) @@ -119,8 +117,8 @@ class PongPlayer(APlayer): def to_dict(self) -> dict: data = { - "username": self.username, - "id": self.user_id, + "username": self.user.username, + "id": self.user.pk, "position": self.position.to_dict(), "score": self.score, diff --git a/games/routine.py b/games/routine.py index 2bb3cba..6b1a0a5 100644 --- a/games/routine.py +++ b/games/routine.py @@ -270,7 +270,6 @@ async def render_ball(game: PongGame): async def render_players(game: PongGame): while True: - for player in game._updated_players: await SyncToAsync(game.broadcast)("update_player", player.to_dict(), [player]) diff --git a/games/serializers.py b/games/serializers.py index 6321d65..114c82d 100644 --- a/games/serializers.py +++ b/games/serializers.py @@ -3,7 +3,8 @@ from rest_framework import serializers from django.contrib.auth.models import User from django.db.models import QuerySet -from .models import GameModel, GameMembersModel +from .models import GameModel +from profiles.serializers.ProfileSerializer import ProfileSerializer class GameSerializer(serializers.ModelSerializer): @@ -28,19 +29,4 @@ class GameSerializer(serializers.ModelSerializer): return "waiting" def get_players(self, instance: GameModel): - players_data: list = [] - for player_id in instance.get_players_id(): - query: QuerySet = User.objects.filter(pk = player_id) - username: str = "Deleted User" - if (query.exists()): - username = query[0].username - - data: dict = { - "id": player_id, - "username": username, - "score": instance.get_score_by_player_id(player_id) - } - - players_data.append(data) - - return players_data \ No newline at end of file + return ProfileSerializer(instance.get_players_profiles(), many=True).data \ No newline at end of file diff --git a/matchmaking/consumers.py b/matchmaking/consumers.py index 80bceeb..331d391 100644 --- a/matchmaking/consumers.py +++ b/matchmaking/consumers.py @@ -6,15 +6,10 @@ from games.models import GameModel import json -from .models import Waiter, WaitingRoom, WaitingRoomManager, normal +from .models import Waiter, WaitingRoom, waiting_room_manager class MatchMaking(WebsocketConsumer): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.channel_name = "matchmaking" - self.group_name = "matchmaking" - def connect(self): user: User = self.scope["user"] @@ -22,14 +17,12 @@ class MatchMaking(WebsocketConsumer): if (user.is_anonymous or not user.is_authenticated): return - self.channel_layer.group_add(self.group_name, self.channel_name) - self.mode: int = int(self.scope['url_route']['kwargs']['mode']) - self.gamemode: str = self.scope['url_route']['kwargs']['gamemode'] + self.game_type: str = self.scope['url_route']['kwargs']['game_type'] self.group_name = self.mode - waiting_room: WaitingRoom = normal.get(self.gamemode, self.mode) - waiting_room.append(Waiter(user.pk, self)) + self.waiting_room: WaitingRoom = waiting_room_manager.get(self.game_type, self.mode) + self.waiting_room.append(Waiter(user, self)) if (self.mode < 2 or self.mode > 4): data: dict = { @@ -39,23 +32,22 @@ class MatchMaking(WebsocketConsumer): self.disconnect(1000) return - if (self.gamemode not in ["tictactoe", "pong"]): + if (self.game_type not in ["tictactoe", "pong"]): data: dict = { - "detail": "The gamemode must 'pong' or 'tictactoe'.", + "detail": "The game_type must 'pong' or 'tictactoe'.", } self.send(json.dumps(data)) self.disconnect(1000) return - waiting_room.broadcast(f"{len(waiting_room)} / {waiting_room.mode}") - if (len(waiting_room) == waiting_room.mode): - game_id: int = GameModel().create(self.gamemode, waiting_room.get_users_id()) - waiting_room.broadcast("game_found", {"game_id": game_id, "gamemode": self.gamemode}) - waiting_room.clear() + self.waiting_room.broadcast(f"{len(self.waiting_room)} / {self.waiting_room.mode}") + if (len(self.waiting_room) == self.waiting_room.mode): + game_id: int = GameModel(game_type=self.game_type).create(self.waiting_room.get_members()) + self.waiting_room.broadcast("game_found", {"game_id": game_id, "game_type": self.game_type}) def disconnect(self, close_code): - super().close(close_code) - waiting_room: WaitingRoom = normal.get(self.gamemode, self.mode) + super().disconnect(close_code) + waiting_room: WaitingRoom = waiting_room_manager.get(self.game_type, self.mode) waiter: Waiter = waiting_room.get_member_by_socket(self) if (waiter is not None): - waiting_room.remove(waiter, close_code) \ No newline at end of file + waiting_room.remove(waiter) \ No newline at end of file diff --git a/matchmaking/models.py b/matchmaking/models.py index 58a0af0..ac9e014 100644 --- a/matchmaking/models.py +++ b/matchmaking/models.py @@ -13,29 +13,44 @@ class Waiter(AbstractRoomMember): class WaitingRoom(AbstractRoom): - def __init__(self, room_manager, gamemode: str, mode: int): + def __init__(self, room_manager, game_type: str, mode: int): + super().__init__(room_manager) - self.mode = mode - self.gamemode = gamemode + self._member_list: set[Waiter] + + self.mode: int = mode + self.game_type: str = game_type def append(self, waiter: Waiter): - tmp: Waiter = self.get_member_by_user_id(waiter.user_id) + + tmp: Waiter = self.get_member_by_user(waiter.user) if (tmp is not None): tmp.send("Connection close: Another connection open with the same user id.") self.remove(tmp) + waiter.socket.accept() - self._member_list.append(waiter) + + super().append(waiter) class WaitingRoomManager(AbstractRoomManager): - def get(self, gamemode: str, mode: int): + def __init__(self): + super().__init__() + self._room_list: set[WaitingRoom] + + + def get(self, game_type: str, mode: int) -> WaitingRoom: + for waiting_room in self._room_list: waiting_room: WaitingRoom - if (waiting_room.mode == mode and waiting_room.gamemode == gamemode): + if (waiting_room.mode == mode and waiting_room.game_type == game_type): return waiting_room - tmp: WaitingRoom = WaitingRoom(self, gamemode, mode) + + tmp: WaitingRoom = WaitingRoom(self, game_type, mode) + super().append(tmp) + return tmp -normal: WaitingRoomManager = WaitingRoomManager() \ No newline at end of file +waiting_room_manager: WaitingRoomManager = WaitingRoomManager() \ No newline at end of file diff --git a/matchmaking/routing.py b/matchmaking/routing.py index 2ae19b1..58d2178 100644 --- a/matchmaking/routing.py +++ b/matchmaking/routing.py @@ -2,5 +2,5 @@ from django.urls import re_path from . import consumers websocket_urlpatterns = [ - re_path(r'ws/matchmaking/(?P\w+)/(?P\d+)$', consumers.MatchMaking.as_asgi()) + re_path(r'ws/matchmaking/(?P\w+)/(?P\d+)$', consumers.MatchMaking.as_asgi()) ] diff --git a/transcendence/abstract/AbstractRoom.py b/transcendence/abstract/AbstractRoom.py index 620d037..5d568ff 100644 --- a/transcendence/abstract/AbstractRoom.py +++ b/transcendence/abstract/AbstractRoom.py @@ -1,51 +1,57 @@ +from __future__ import annotations + from channels.generic.websocket import WebsocketConsumer from .AbstractRoomMember import AbstractRoomMember +from django.contrib.auth.models import User + +from profiles.models import ProfileModel + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from .AbstractRoomManager import AbstractRoomManager + class AbstractRoom: - def __init__(self, room_manager): - self._member_list: list[AbstractRoomMember] = [] - self.room_manager = room_manager + def __init__(self, room_manager: AbstractRoomManager): + self._member_list: set[AbstractRoomMember] = set() + self._room_manager: AbstractRoomManager = room_manager - def broadcast(self, detail: str, data: dict = {}): - for member in self._member_list: - member: AbstractRoomMember + def broadcast(self, detail: str, data: dict = {}, excludes: set[AbstractRoomMember] = set()) -> None: + + members: set[AbstractRoomMember] = self._member_list - excludes + + for member in members: member.send(detail, data) + + def get_member_by_socket(self, socket: WebsocketConsumer) -> AbstractRoomMember | None: + + for member in self._member_list: + if member.socket is socket: + return member + + def get_member_by_user(self, user: User) -> AbstractRoomMember: + + for member in self._member_list: + if member.user == user: + return member - def clear(self): - self._member_list.clear() + def get_members_profiles(self) -> set[ProfileModel]: + return set(member.user.profilemodel for member in self._member_list) + + def get_members(self) -> set[ProfileModel]: + return set(member.user for member in self._member_list) - def get_member_by_socket(self, socket: WebsocketConsumer): - for member in self._member_list: - member: AbstractRoomMember - if (member.socket is socket): - return member - return None + def append(self, member: AbstractRoomMember) -> None: + self._member_list.add(member) - def get_member_by_user_id(self, user_id: int): - for member in self._member_list: - member: AbstractRoomMember - if (member.user_id == user_id): - return member - return None - - - def append(self, member: AbstractRoomMember): - self._member_list.append(member) - member.accept() - - def remove(self, member: AbstractRoomMember, code: int = 1000): + def remove(self, member: AbstractRoomMember) -> None: self._member_list.remove(member) - member.socket.disconnect(code) - def empty(self): - for _ in self._member_list: - return False - return True + def get_users(self) -> set[User]: + return set(member.user for member in self._member_list) - def get_users_id(self): - return [member.user_id for member in self._member_list] - - def __len__(self): + def __len__(self) -> int: return len(self._member_list) diff --git a/transcendence/abstract/AbstractRoomManager.py b/transcendence/abstract/AbstractRoomManager.py index 5b12ef6..8c1f370 100644 --- a/transcendence/abstract/AbstractRoomManager.py +++ b/transcendence/abstract/AbstractRoomManager.py @@ -5,8 +5,8 @@ class AbstractRoomManager: def __init__(self): self._room_list: list[AbstractRoom] = [] - def remove(self, room: AbstractRoom): + def remove(self, room: AbstractRoom) -> None: self._room_list.remove(room) - def append(self, room: AbstractRoom): + def append(self, room: AbstractRoom) -> None: self._room_list.append(room) diff --git a/transcendence/abstract/AbstractRoomMember.py b/transcendence/abstract/AbstractRoomMember.py index e0e701e..570a110 100644 --- a/transcendence/abstract/AbstractRoomMember.py +++ b/transcendence/abstract/AbstractRoomMember.py @@ -1,14 +1,16 @@ from channels.generic.websocket import WebsocketConsumer +from django.contrib.auth.models import User + import json class AbstractRoomMember: - def __init__(self, user_id: int, socket: WebsocketConsumer): - self.user_id: int = user_id + def __init__(self, user: User, socket: WebsocketConsumer): + self.user: User = user self.socket: WebsocketConsumer = socket - def send(self, detail: str, data: dict = {}): + def send(self, detail: str, data: dict = {}) -> None: raw_data: dict = {"detail": detail} raw_data.update(data) self.socket.send(text_data=json.dumps(raw_data)) \ No newline at end of file From 1d8c2c633adf09518121df894ea16b04a01c7d83 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Apr 2024 11:41:16 +0200 Subject: [PATCH 03/16] fux --- games/objects/tictactoe/TicTacToeGame.py | 4 ++-- games/objects/tictactoe/TicTacToePlayer.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/games/objects/tictactoe/TicTacToeGame.py b/games/objects/tictactoe/TicTacToeGame.py index 8cd496e..fe123e5 100644 --- a/games/objects/tictactoe/TicTacToeGame.py +++ b/games/objects/tictactoe/TicTacToeGame.py @@ -14,9 +14,9 @@ class TicTacToeGame(AGame): def __init__(self, game_id: int, game_manager): super().__init__("tictactoe", game_id, game_manager) - players_id: list[int] = self.model.get_players_id() + players: list[int] = self.model.get_players() - self.players: list[TicTacToePlayer] = [TicTacToePlayer(player_id, None, self, ["x", "o"][i]) for i, player_id in enumerate(players_id)] + self.players: list[TicTacToePlayer] = [TicTacToePlayer(player, None, self, ["x", "o"][i]) for i, player in enumerate(players)] self._map = [[-1 for _ in range(9)] for _ in range(9)] diff --git a/games/objects/tictactoe/TicTacToePlayer.py b/games/objects/tictactoe/TicTacToePlayer.py index ba34b21..5f61db0 100644 --- a/games/objects/tictactoe/TicTacToePlayer.py +++ b/games/objects/tictactoe/TicTacToePlayer.py @@ -2,11 +2,13 @@ from games.objects.AGame import AGame from ..APlayer import APlayer +from django.contrib.auth.models import User + from channels.generic.websocket import WebsocketConsumer class TicTacToePlayer(APlayer): - def __init__(self, user_id: int, socket: WebsocketConsumer, game: AGame, sign): - super().__init__(user_id, socket, game) + def __init__(self, user: User, socket: WebsocketConsumer, game: AGame, sign): + super().__init__(user, socket, game) self.sign = sign self.currentMorpion = 4 self.timestamp = None \ No newline at end of file From aafc61d40aecb0bf70e19f6566be3b5f67168e41 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Apr 2024 17:03:45 +0200 Subject: [PATCH 04/16] tournament: add: les crampte --- .../static/js/api/tournament/Tournament.js | 85 ++++++++--- .../views/tournament/TournamentCreateView.js | 20 ++- .../js/views/tournament/TournamentPageView.js | 133 +++++++----------- tournament/consumers.py | 52 +++++-- tournament/models.py | 33 ++--- tournament/serializers.py | 20 +-- 6 files changed, 191 insertions(+), 152 deletions(-) diff --git a/frontend/static/js/api/tournament/Tournament.js b/frontend/static/js/api/tournament/Tournament.js index 634ee11..caee57c 100644 --- a/frontend/static/js/api/tournament/Tournament.js +++ b/frontend/static/js/api/tournament/Tournament.js @@ -42,14 +42,33 @@ class Tourmanent extends AExchangeable * @type {Number} */ this.finished; - /** * @type {"finished" | "started" | "waiting"} must be "finished", or "started", or "waiting". Any other return all elements */ this.state; + + /** + * @type {Boolean} the client is a participant of the tournament + */ + this.is_participating; } + /** + * @param {Boolean} newParticipation + */ + async setParticipation(newParticipation) + { + if (this.isParticipating == newParticipation) + return; + + this.isParticipating = newParticipation; + + this._socket.send(JSON.stringify({"detail": "update_participating", + "is_participating": newParticipation}) + ); + + } /** * * @returns {Promise} @@ -72,18 +91,35 @@ class Tourmanent extends AExchangeable return; this.connected = false; this._socket.close(); - this.disconnectHandler(event); + this._disconnectHandler(event); } /** * @param {Object} data */ - async _receiveParticipantUpdate(data) + async _receiveAddParticipant(data) { - this.par + const participant = new Profile(this.client, undefined, data.participant.user_id); + participant.import(data.participant) + + this.participantList.push(participant); + + await this._addParticipantHandler(this.participantList.length) } - async onError(data) + /** + * @param {Object} data + */ + async _receiveDelParticipant(data) + { + const index = this.participantList.indexOf((profile) => profile.id === data.profile.user_id) + + this.participantList.splice(index, 1); + + await this._delParticipantHandler(this.participantList.length); + } + + async _receiveError(data) { await this.errorHandler(data); } @@ -92,14 +128,26 @@ class Tourmanent extends AExchangeable * * @param {MessageEvent} event */ - onReceive(event) + async onReceive(event) { const data = JSON.parse(event.data); - if (data.detail === "error") - this.onError(data); - else if (["del_participant", "add_participant"].includes(data.detail)) - this._receiveParticipantUpdate(data); + switch (data.detail) { + case "error": + await this._receiveError(data) + break; + + case "add_participant": + await this._receiveAddParticipant(data); + break; + + case "del_participant": + await this._receiveDelParticipant(data); + break; + + default: + break; + } } /** @@ -109,9 +157,11 @@ class Tourmanent extends AExchangeable * @param {CallableFunction} delParticipantHandler called when a participants leave the tournament * @param {CallableFunction} disconnectHandler * @param {CallableFunction} goToHandler called when the next game will start - * @returns {?} + * @param {CallableFunction} startHandler called when tournament start + * @param {CallableFunction} finishHandler called when tournament finish + * @returns {Promise} */ - async join(participantsUpdateHandler, errorHandler, goToHandler, disconnectHandler) + async join(addParticipantHandler, delParticipantHandler, startHandler, finishHandler, errorHandler, goToHandler, disconnectHandler) { if (!await this.client.isAuthenticated()) return null; @@ -123,10 +173,13 @@ class Tourmanent extends AExchangeable this.connected = true; this.isParticipating = false; - this.participantsUpdateHandler = participantsUpdateHandler; - this.errorHandler = errorHandler; - this.disconnectHandler = disconnectHandler; - this.goToHandler = goToHandler; + this._startHandler = startHandler; + this._finishHandler = finishHandler; + this._addParticipantHandler = addParticipantHandler; + this._delParticipantHandler = delParticipantHandler; + this._errorHandler = errorHandler; + this._disconnectHandler = disconnectHandler; + this._goToHandler = goToHandler; this._socket.onmessage = this.onReceive.bind(this); diff --git a/frontend/static/js/views/tournament/TournamentCreateView.js b/frontend/static/js/views/tournament/TournamentCreateView.js index 6565520..24752f1 100644 --- a/frontend/static/js/views/tournament/TournamentCreateView.js +++ b/frontend/static/js/views/tournament/TournamentCreateView.js @@ -16,10 +16,9 @@ export default class extends AbstractAuthenticatedView if (name.length == 0) name = lang.get("TournamentCreateTournamentName"); - console.log(name); - let nb_players = document.getElementById("nb-players-input").value; + let nb_participant = document.getElementById("nb-participant-input").value; - let response = await client.tournaments.createTournament(nb_players, name); + let response = await client.tournaments.createTournament(nb_participant, name); let response_data = await response.json(); let id = response_data.id; @@ -29,7 +28,7 @@ export default class extends AbstractAuthenticatedView return; } - clearIds("innerHTML", ["name", "nb_players"]); + clearIds("innerHTML", ["name", "nb_participants"]); fill_errors(response_data, "innerHTML"); } @@ -50,9 +49,16 @@ export default class extends AbstractAuthenticatedView
- - - + + +
diff --git a/frontend/static/js/views/tournament/TournamentPageView.js b/frontend/static/js/views/tournament/TournamentPageView.js index a43c84e..e7a5833 100644 --- a/frontend/static/js/views/tournament/TournamentPageView.js +++ b/frontend/static/js/views/tournament/TournamentPageView.js @@ -3,6 +3,10 @@ import { Tourmanent } from "../../api/tournament/Tournament.js"; import {client, navigateTo} from "../../index.js"; import AbstractAuthenticatedView from "../abstracts/AbstractAuthenticatedView.js"; +const TEXT_CONVENTION = { + "error": "[ERROR]" +} + export default class extends AbstractAuthenticatedView { constructor(params) @@ -13,95 +17,49 @@ export default class extends AbstractAuthenticatedView pressButton() { - this.tournament.toggle_participation(); + this.tournament.setParticipation(!this.tournament.isParticipating); + this.updateParticipating() } - createGraph() + updateParticipating() { - console.log(this.tournament); - let tournament_tree = document.createElement("div"); - - tournament_tree.id = "tournament-tree"; - - document.getElementById("app").appendChild(tournament_tree); - - for (let round_id = 0; round_id < this.tournament.round.length; round_id++) - { - let current_round = document.createElement("ul"); - - tournament_tree.appendChild(current_round); - - current_round.className = `round round-${round_id}`; - - for (let participant_i = 0; participant_i < this.tournament.round[round_id].length; participant_i += 2) - { - let spacer = document.createElement("li"); - - spacer.className = "spacer"; - spacer.innerText = " "; - - current_round.appendChild(spacer); - - let game_top = document.createElement("li"); - - game_top.className = "game game-top"; - game_top.innerText = `${this.tournament.round[round_id][participant_i]}`; - - current_round.appendChild(game_top); - - let game_spacer = document.createElement("li"); - - spacer.className = "game game-spacer"; - spacer.innerText = " "; - - current_round.appendChild(game_spacer); - - let game_bottom = document.createElement("li"); - - game_bottom.className = "game game-bottom"; - game_bottom.innerText = `${this.tournament.round[round_id][participant_i + 1]}`; - - current_round.appendChild(game_bottom); - } - } - - } - - async receive(data) - { - if (data.detail === "update_participants") - document.getElementById("nb_participants").innerText = `${data.participants} / ${this.tournament.nb_participants}`; - if (data.detail === "go_to") - navigateTo(data.url); - if (data.detail === "is_participant") - this.updateParticipating(data.is_participant); - if (data.detail === "error") - document.getElementById("display").innerText = data.error_message; - } - - async updateParticipating(state) - { - document.getElementById("button").value = state ? `Leave ${this.tournament.name}` : `Join ${this.tournament.name}`; - document.getElementById("display").innerText = state ? "You are a particpant" : "You are not a participant"; - } - - /** - * - * @param {[Profile]} oldParticipantsList - * @param {[Profile]} currentParticipantsList - */ - async onParticipantsUpdate(oldParticipantsList, currentParticipantsList) - { - + document.getElementById("button").value = this.tournament.isParticipating ? `Leave ${this.tournament.name}` : `Join ${this.tournament.name}`; + document.getElementById("display").innerText = this.tournament.isParticipating ? "You are a particpant" : "You are not a participant"; } async onDisconnect(event) { + } async onError(data) { + this.addChatMessage(`${TEXT_CONVENTION} data.error_message`); + } + async onFinish() + { + document.getElementById("state").innerText = "finished" + } + + async onStart() + { + document.getElementById("state").innerText = "started" + } + + async onGoTo(data) + { + await navigateTo(`/games/pong/${data.game_id}`) + } + + async onAddParticipant(nb_participants) + { + document.getElementById("nb_participants").innerText = nb_participants; + } + + async onDelParticipant(nb_participants) + { + document.getElementById("nb_participants").innerText = nb_participants; } async postInit() @@ -114,20 +72,27 @@ export default class extends AbstractAuthenticatedView if (this.tournament === null) return 404; - this.tournament.join(this.onParticipantsUpdate.bind(this), this.onError.bind(this), this.onDisconnect.bind(this)); + this.tournament.join(this.onAddParticipant, this.onDelParticipant, this.onStart, this.onFinish, this.onError, this.onGoTo, this.onDisconnect); let button = document.getElementById("button"); button.onclick = this.pressButton.bind(this); document.getElementById("name").innerText = this.tournament.name; - document.getElementById("nb_participants").innerText = this.tournament.nb_participants; + document.getElementById("nb_participants").innerText = this.tournament.participantList.length; + document.getElementById("expected_nb_participants").innerText = this.tournament.nb_participants; document.getElementById("round").innerText = this.tournament.round; document.getElementById("state").innerText = this.tournament.state; if (this.tournament.started === false) button.disabled = false; - this.createGraph(); + + this.chat = document.getElementById("chat"); + } + + addChatMessage(message) + { + this.chat.innerText += message; } async getHtml() @@ -150,13 +115,19 @@ export default class extends AbstractAuthenticatedView Loading... - status + Expected number of participants + Loading... + + + state Loading... + `; } } diff --git a/tournament/consumers.py b/tournament/consumers.py index cb99d9c..78aa59f 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -6,7 +6,7 @@ from django.contrib.auth.models import User from django.db.models import QuerySet from django.utils.translation import gettext as _ - +from games.models import GameModel from profiles.models import ProfileModel from profiles.serializers.ProfileSerializer import ProfileSerializer from .models import TournamentModel @@ -31,6 +31,9 @@ class TournamentMember: data_to_send.update(data) self.send("error", data_to_send) + + def send_goto(self, game: GameModel): + self.send("go_to", {"game_id": game.pk}) def _receive_participating(self, data: dict) -> None: @@ -38,20 +41,18 @@ class TournamentMember: if (is_participating is None): self.send_error(_("Missing is_participating statement.")) return - self._room.set_participation() + + self._room.set_participation(self, is_participating) def receive(self, data: dict): - if self.is_participating == False: - return - detail: str | None = data.get("detail") if (detail is None): return match(detail): - case "update_particapating": - self._receive_participating() + case "update_participating": + self._receive_participating(data) case _: print("bozo_send") @@ -80,16 +81,35 @@ class TournamentRoom: def __init__(self, room_manager: TournamentRoomManager, tournament: TournamentModel): self._room_manager: TournamentRoomManager = room_manager - self._member_list: list[TournamentMember] = [] + self._member_list: set[TournamentMember] = set() self._model: TournamentModel = tournament def join(self, socket: TournamentWebConsumer) -> TournamentMember: member: TournamentMember = TournamentMember(socket, self) - self._member_list.append(member) + self._member_list.add(member) return member + + def get_participants_profiles(self) -> list[ProfileModel]: + return [participant._socket.user.profilemodel for participant in self.get_participants()] + def start(self) -> None: + + games: list[GameModel] = self._model.start() + + self.broadcast("start") + + for game in games: + for player in game.get_players(): + participant: TournamentMember = self.get_participant_by_profile(player) + participant.send_goto(game) + + def get_participant_by_profile(self, profile: ProfileModel): + for participant in self.get_participants(): + if (participant._socket.user.profilemodel == profile): + return participant + def leave(self, member: TournamentMember) -> None: # Delete room if nobody connected, no cringe memory leak @@ -100,10 +120,13 @@ class TournamentRoom: self._member_list.remove(member) self.set_participation(member, False) + + def everybody_is_here(self): + return len(self.get_participants()) == self._model.nb_participants - def broadcast(self, detail: str, data: dict, excludes: list[TournamentMember] = []) -> None: + def broadcast(self, detail: str, data: dict, excludes: set[TournamentMember] = set()) -> None: - member_list: list[TournamentMember] = [member for member in self._member_list if member not in excludes] + member_list: list[TournamentMember] = self._member_list - excludes for member in member_list: member.send(detail, data) @@ -120,11 +143,14 @@ class TournamentRoom: return if (is_participating == True): - self.broadcast("add_participant", {"profile", ProfileSerializer(member._socket.user.profilemodel).data}) + self.broadcast("add_participant", {"participant": ProfileSerializer(member._socket.user.profilemodel).data}) else: - self.broadcast("del_participant", {"profile", ProfileSerializer(member._socket.user.profilemodel).data}) + self.broadcast("del_participant", {"participant": ProfileSerializer(member._socket.user.profilemodel).data}) member.is_participating = is_participating + + if self.everybody_is_here(): + self.start() tournament_manager: TournamentRoomManager = TournamentRoomManager() diff --git a/tournament/models.py b/tournament/models.py index 176fdfc..4f0fe28 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -1,21 +1,12 @@ from __future__ import annotations -from transcendence.abstract.AbstractRoomMember import AbstractRoomMember -from transcendence.abstract.AbstractRoom import AbstractRoom -from transcendence.abstract.AbstractRoomManager import AbstractRoomManager - -from profiles.models import ProfileModel from games.models import GameModel from django.contrib.auth.models import User from django.db.models import CASCADE -from channels.generic.websocket import WebsocketConsumer from django.db import models -import json - -# Create your models here.tu class TournamentModel(models.Model): name = models.CharField(max_length = 100) @@ -23,12 +14,14 @@ class TournamentModel(models.Model): round = models.IntegerField() started = models.BooleanField(default = False) finished = models.BooleanField(default = False) - winner = models.ForeignKey(ProfileModel, on_delete=CASCADE, blank=True, null=True) + winner = models.ForeignKey(User, on_delete=CASCADE, blank=True, null=True) - def _register_participant(self, participant: ProfileModel) -> None: + def _register_participant(self, participant: User) -> None: TournamentParticipantModel(participant=participant, tournament=self).save() - def start(self, participants: list[ProfileModel]) -> None: + def start(self, participants: list[User]) -> None: + + games: list[GameModel] = [] self.started = True @@ -36,11 +29,14 @@ class TournamentModel(models.Model): self._register_participant(player) for (participant1, participant2) in zip(participants[0::2], participants[1::2]): - self.create_game([participant1, participant2], round=1) + game: GameModel = self.create_game([participant1, participant2], round=1) + games.append(game) self.save() - def create_game(self, participants: list[ProfileModel], round: int) -> GameModel: + return games + + def create_game(self, participants: list[User], round: int) -> GameModel: if (self.started == False): return None @@ -62,10 +58,10 @@ class TournamentModel(models.Model): def get_games_by_round(self, round: int) -> list[GameModel]: return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)] - def get_players_by_round(self, round: int) -> list[ProfileModel]: + def get_players_by_round(self, round: int) -> list[User]: return [game.get_players() for game in self.get_games_by_round(round)] - def get_winners_by_round(self, round: int) -> list[ProfileModel]: + def get_winners_by_round(self, round: int) -> list[User]: return [game.winner for game in self.get_games_by_round(round)] def get_participants(self) -> list[TournamentParticipantModel]: @@ -74,13 +70,12 @@ class TournamentModel(models.Model): def get_state(self) -> str: return ("waiting to start", "in progress", "finish")[self.started + self.finished] - def is_participanting(self, profile: ProfileModel) -> bool: + def is_participanting(self, profile: User) -> bool: return TournamentParticipantModel.objects.filter(participant=profile, tournament=self).exists() class TournamentParticipantModel(models.Model): - participant = models.ForeignKey(ProfileModel, on_delete=CASCADE) + participant = models.ForeignKey(User, on_delete=CASCADE) tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE) - #prout à encore frappé class TournamentGameModel(models.Model): diff --git a/tournament/serializers.py b/tournament/serializers.py index 18b4f13..abf2cfb 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -1,15 +1,13 @@ from rest_framework import serializers -from django.db.models.query import QuerySet - -from django.contrib.auth.models import User - from .models import TournamentModel from profiles.models import ProfileModel from profiles.serializers.ProfileSerializer import ProfileSerializer from games.serializers import GameSerializer +nb_participants = [2 ** i for i in range(2, 6)] + class TournamentSerializer(serializers.ModelSerializer): state = serializers.SerializerMethodField(read_only=True, required=False) @@ -30,16 +28,6 @@ class TournamentSerializer(serializers.ModelSerializer): return ["waiting", "started", "finished"][instance.started + instance.finished] def validate_nb_participants(self, value: int): - if (value < 2): - raise serializers.ValidationError("The numbers of participants must be greather than 2.") - return value - - def validate_nb_participants_by_game(self, value: int): - if (value < 2): - raise serializers.ValidationError("The numbers of participants by game must be greather than 2.") - nb_participants: str = self.initial_data.get("nb_participants") - if (nb_participants is not None and nb_participants.isnumeric()): - nb_participants: int = int(nb_participants) - if (value > nb_participants): - raise serializers.ValidationError("The numbers of participants by game must be smaller than the numbers of participants.") + if (value not in nb_participants): + raise serializers.ValidationError(f"The numbers of participants must be {str(nb_participants)}.") return value \ No newline at end of file From ea9d86b5a318af611ebd3fe150910a9d010ba6e4 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Apr 2024 17:03:53 +0200 Subject: [PATCH 05/16] pong: fix --- games/objects/pong/PongPlayer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/games/objects/pong/PongPlayer.py b/games/objects/pong/PongPlayer.py index aeaa5de..9efe82d 100644 --- a/games/objects/pong/PongPlayer.py +++ b/games/objects/pong/PongPlayer.py @@ -66,7 +66,7 @@ class PongPlayer(APlayer): return if (self.position.time > new_position.time): - self.game_member.send_error("time error") + self.send_error("time error") return distance: float = abs(self.position.location - new_position.location) From 2a63edf73965bbbbed0039d030233ac78ef6186a Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Apr 2024 17:32:26 +0200 Subject: [PATCH 06/16] tournament: add: goto --- frontend/static/js/api/tournament/Tournament.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/frontend/static/js/api/tournament/Tournament.js b/frontend/static/js/api/tournament/Tournament.js index caee57c..f9d163c 100644 --- a/frontend/static/js/api/tournament/Tournament.js +++ b/frontend/static/js/api/tournament/Tournament.js @@ -124,6 +124,11 @@ class Tourmanent extends AExchangeable await this.errorHandler(data); } + async _receiveGoTo(data) + { + await this._goToHandler(data) + } + /** * * @param {MessageEvent} event @@ -144,6 +149,10 @@ class Tourmanent extends AExchangeable case "del_participant": await this._receiveDelParticipant(data); break; + + case "go_to": + await this._receiveGoTo(data); + break default: break; From 8c8847cdd8f1eaee12a9c074d4cfca6c9f0d80c6 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Mon, 22 Apr 2024 18:05:09 +0200 Subject: [PATCH 07/16] profiles: friend and block through profile page :)) --- frontend/static/js/api/MyProfile.js | 50 ++++- frontend/static/js/api/Profile.js | 13 +- frontend/static/js/api/Profiles.js | 34 --- frontend/static/js/views/ProfilePageView.js | 217 ++++++++++---------- profiles/views/friends.py | 8 +- 5 files changed, 175 insertions(+), 147 deletions(-) diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index 549f116..ef2e694 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -18,7 +18,7 @@ class MyProfile extends Profile /** * @type {[Profile]} */ - this.friends = []; + this.friendList = []; /** * @type {[Profile]} */ @@ -46,7 +46,7 @@ class MyProfile extends Profile async getFriends() { const response = await this.client._get('/api/profiles/friends'); const data = await response.json(); - data.forEach(profileData => this.friends.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); + data.forEach(profileData => this.friendList.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); } async getIncomingFriendRequests() { const response = await this.client._get('/api/profiles/incoming_friend_requests'); @@ -62,6 +62,52 @@ class MyProfile extends Profile new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar) )); } + + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _isFriend(profile) { + for (const user of this.friendList) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _isBlocked(profile) { + for (const user of this.blockedUsers) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _hasIncomingRequestFrom(profile) { + for (const user of this.incomingFriendRequests) { + if (user.id === profile.id) + return true; + } + return false; + } + /** + * @param {Profile} profile + * @returns {Boolean} + */ + _hasOutgoingRequestTo(profile) { + for (const user of this.outgoingFriendRequests) { + if (user.id === profile.id) + return true; + } + return false; + } + /** * * @param {File} selectedFile diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index bd38d9f..feb9570 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -33,8 +33,10 @@ export class Profile extends AExchangeable /** * @type {Boolean} */ - this.isBlocked = false; - this.isFriend = false; + this.isFriend; + this.isBlocked; + this.hasIncomingRequest; + this.hasOutgoingRequest; } /** @@ -57,6 +59,13 @@ export class Profile extends AExchangeable this.username = response_data.username; this.avatar = response_data.avatar; + if (!this.client.me || this.client.me.id === this.id) + return; + + this.isFriend = this.client.me._isFriend(this); + this.isBlocked = this.client.me._isBlocked(this); + this.hasIncomingRequest = this.client.me._hasIncomingRequestFrom(this); + this.hasOutgoingRequest = this.client.me._hasOutgoingRequestTo(this); } /** diff --git a/frontend/static/js/api/Profiles.js b/frontend/static/js/api/Profiles.js index 7c0de7e..796abc7 100644 --- a/frontend/static/js/api/Profiles.js +++ b/frontend/static/js/api/Profiles.js @@ -54,40 +54,6 @@ class Profiles return null; return profile; } - - /** - * Block a user - * @param {Number} user_id - * @returns {Promise} - */ - async block(user_id) { - - // blocker & blocked - let response = await this.client._post("/api/profiles/block", { - users_id:[this.client.me.id, user_id], - }); - - let data = await response.json(); - return data; - - } - - /** - * Unblock a user - * @param {Number} user_id - * @returns {Promise} - */ - async deblock(user_id) { - - // blocker & blocked - let response = await this.client._delete("/api/profiles/block", { - users_id:[this.client.me.id, user_id], - }); - - let data = await response.json(); - return data; - - } } export {Profiles}; diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 930e206..8b27e81 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -8,7 +8,7 @@ export default class extends AbstractView { } setTitle() { - document.title = `${this.username} - Profile`; + document.title = this.titleKey; } async postInit() @@ -16,100 +16,32 @@ export default class extends AbstractView { if (!this.profile) return 404; - this.userId = this.profile.id; + const addFriendButton = document.getElementById('addFriendButton'), + removeFriendButton = document.getElementById('removeFriendButton'), + blockButton = document.getElementById('blockButton'), + unblockButton = document.getElementById('unblockButton'); - await this.blockButton(); - await this.friendButton(); + if (this.profile.hasIncomingRequest) { + addFriendButton.classList.remove('d-none'); + addFriendButton.innerHTML = 'Accept Request'; + } else if (this.profile.hasOutgoingRequest) { + removeFriendButton.classList.remove('d-none'); + removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); + removeFriendButton.innerHTML = 'Cancel Request' + } else if (this.profile.isFriend) + removeFriendButton.classList.remove('d-none'); + else + addFriendButton.classList.remove('d-none'); - client.notice.rewrite_profile = async () => { - await this.profile.getFriend(); - await this.profile.getBlock(); - await this.friendButton(); - }; - } + if (this.profile.isBlocked) + unblockButton.classList.remove('d-none'); + else + blockButton.classList.remove('d-none'); - async blockButton() { - // Block option - if (await client.isAuthenticated() === false) - return; - - if (client.me.id != this.userId) { - let block = document.getElementById("block"); - if (block == undefined) { - block = document.createElement("p"); - // this.info.appendChild(block); - } - - block.id = "block"; - block.onclick = async () => { - if (!this.profile.isBlocked) - await client.profiles.block(this.userId); - else - await client.profiles.deblock(this.userId); - this.profile = await client.profiles.getProfile(this.username); - - this.blockButton(); - }; - if (this.profile.isBlocked) - block.textContent = lang.get('profileUnblock', 'Unblock'); - else - block.textContent = lang.get('profileBlock', 'Block'); - } - } - - async friendButton() { - if (await client.isAuthenticated() === false) - return; - - if (client.me.id != this.userId) { - let yes = document.getElementById("yes") || document.createElement("p"); - let no = document.getElementById("no") || document.createElement("p"); - let friend = document.getElementById("friend") || document.createElement("p"); - - if (client.notice.data.asker.includes(this.userId)) { - - if (friend) - friend.remove(); - - yes.id = "yes"; - yes.textContent = lang.get('profileAcceptRequest', 'Accept Friend'); - yes.onclick = async () => { - client.notice.accept_friend(this.userId); - }; - - no.id = "no"; - no.textContent = lang.get('profileDenyRequest', 'Decline Friend'); - no.onclick = async () => { - client.notice.refuse_friend(this.userId); - }; - - // this.info.appendChild(yes); - // this.info.appendChild(document.createTextNode(" ")); - // this.info.appendChild(no); - - } - else { - - if (yes && no) - yes.remove(); no.remove(); - - friend.id = "friend"; - friend.onclick = async () => { - if (this.profile.isFriend) - await client.notice.remove_friend(this.userId); - else - await client.notice.ask_friend(this.userId); - await client.profiles.getProfile(this.username); - this.friendButton(); - }; - if (this.profile.isFriend) - friend.textContent = lang.get('profileRemoveFriend', 'Remove Friend'); - else { - friend.textContent = lang.get('profileAddFriend', 'Ask Friend'); - } - // this.info.appendChild(friend); - } - } + addFriendButton.onclick = _ => this.addFriend(); + removeFriendButton.onclick = _ => this.removeFriend(); + unblockButton.onclick = _ => this.unblockUser(); + blockButton.onclick = _ => this.blockUser(); } async getHtml() { @@ -118,19 +50,94 @@ export default class extends AbstractView { if (!this.profile) return ''; - const logged = await client.isAuthenticated(); - return ` -
-

${this.username}

- - - -
-
- - -
- `; +
+
+

${this.username}

+ + + +
+
+ + + + +
+
+ `; + } + + async addFriend() { + const removeFriendButton = document.getElementById('removeFriendButton'); + + const response = await client._post(`/api/profiles/friends/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + removeFriendButton.classList.remove('d-none'); + document.getElementById('addFriendButton').classList.add('d-none'); + } + if (response.status === 200) { + removeFriendButton.innerHTML = 'Cancel Request'; + removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); + client.me.outgoingFriendRequests.push(this.profile); + this.profile.hasOutgoingRequest = true; + } else if (response.status === 201) { + removeFriendButton.innerHTML = 'Remove Friend'; + removeFriendButton.classList.replace('btn-secondary', 'btn-danger'); + this.profile.friend = true; + this.profile.hasIncomingRequest = false; + client.me.incomingFriendRequests = client.me.incomingFriendRequests.filter(profile => profile.id !== this.profile.id); + client.me.friendList.push(this.profile); + } + } + + async removeFriend() { + const addFriendButton = document.getElementById('addFriendButton'); + + const response = await client._delete(`/api/profiles/friends/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + addFriendButton.innerHTML = 'Add Friend'; + addFriendButton.classList.remove('d-none'); + document.getElementById('removeFriendButton').classList.add('d-none'); + } + if (response.status === 200) { + this.profile.hasOutgoingRequest = false; + client.me.outgoingFriendRequests = client.me.outgoingFriendRequests.filter(profile => profile.id !== this.profile.id); + } else if (response.status === 201) { + this.profile.isFriend = false; + client.me.friendList = client.me.friendList.client.me.incomingFriendRequests = filter(friend => friend.id !== this.profile.id); + } + } + + async blockUser() { + const response = await client._post(`/api/profiles/block/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + document.getElementById('blockButton').classList.add('d-none'); + document.getElementById('unblockButton').classList.remove('d-none'); + client.me.blockedUsers.push(this.profile); + this.profile.isBlocked = true; + } + } + + async unblockUser() { + const response = await client._delete(`/api/profiles/block/${this.profile.id}`); + const body = await response.json(); + console.log(body); + + if (response.ok) { + document.getElementById('unblockButton').classList.add('d-none'); + document.getElementById('blockButton').classList.remove('d-none'); + client.me.blockedUsers = client.me.blockedUsers.filter(profile => profile.id !== this.profile.id); + this.profile.isBlocked = false; + } } } diff --git a/profiles/views/friends.py b/profiles/views/friends.py index 00d76b3..125b061 100644 --- a/profiles/views/friends.py +++ b/profiles/views/friends.py @@ -50,16 +50,16 @@ class EditFriendView(APIView): user_profile = self.get_object() friend_profile = get_object_or_404(ProfileModel, pk=pk) - if not user_profile.is_friend(friend_profile): - return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) - outgoing_request = user_profile.get_outgoing_friend_request_to(friend_profile) if outgoing_request: outgoing_request.delete() return Response(_('Friend request cancelled.')) + if not user_profile.is_friend(friend_profile): + return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) + user_profile.delete_friend(friend_profile) - return Response(_('Friendship succssfully deleted.')) + return Response(_('Friendship succssfully deleted.'), status.HTTP_201_CREATED) class GetIncomingFriendRequestView(APIView): From 91cdc969721225355875861858c40c99f78d497a Mon Sep 17 00:00:00 2001 From: Namonay Date: Mon, 22 Apr 2024 19:13:08 +0200 Subject: [PATCH 08/16] morpion: add: Frontend checks, QOL changes and winning conditions --- .../js/api/game/tictactoe/TicTacToeGame.js | 105 +++++++++--------- .../static/js/sound/tictactoe/play-move.mp3 | Bin 0 -> 5224 bytes .../static/js/views/TicTacToeOnlineView.js | 1 - games/consumers.py | 9 +- games/objects/tictactoe/TicTacToeGame.py | 19 ++-- 5 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 frontend/static/js/sound/tictactoe/play-move.mp3 diff --git a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js index 73b5a56..49d6d18 100644 --- a/frontend/static/js/api/game/tictactoe/TicTacToeGame.js +++ b/frontend/static/js/api/game/tictactoe/TicTacToeGame.js @@ -18,12 +18,12 @@ class TicTacToe this.canvas = canvas this.context = this.canvas.getContext("2d"); this.sign; + this.currentMorpion = 4; this.turn; } async init() { - console.log(this.game_id); await this.game.join(); this.canvas.addEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion)); } @@ -37,22 +37,46 @@ class TicTacToe async onReceive(messageData) { console.log(messageData) - if (messageData.detail == "x" || messageData.detail == "o") + switch (messageData.detail) { - this.sign = messageData.detail; - this.turn = messageData.detail == "x"; - } - else if (messageData.detail == "game_start") - this.game.started = true; - else if (messageData.targetMorpion && messageData.targetCase) - { - this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0; - this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x"); - if (this.checkWin() != -1) - printWin(); + case 'x': + 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; + break; + + case 'game_move': + 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); + break; + + case 'game_end': + this.game.finished = true; + this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion)); + this.printWin(messageData.winning_sign); + break; } } + printWin(winning_sign) + { + this.context.beginPath(); + this.context.fillStyle = "white"; + this.context.fillRect(this.width / 2 - 200, this.height - this.gap + 10, 400, 80); + this.context.closePath(); + this.context.beginPath(); + this.context.fillStyle = (winning_sign == "o") ? "red" : "green"; + this.context.fillText((winning_sign == "o") ? "Winner is : O" : "Winner is : X", this.width / 2 - 30, this.height - this.gap / 2, 140); + this.context.closePath(); + } checkWin() { for (let i = 0; i < 9; i++) @@ -85,6 +109,10 @@ class TicTacToe let y = event.offsetY; let targetMorpion, targetCase; + if (this.game.finished) + { + return; + } targetMorpion = morpion.findPlace(x, this) + morpion.findPlace(y, this) * 3; if (morpion.findPlace(x, this) < 0 || morpion.findPlace(y, this) < 0) return -1; @@ -92,8 +120,8 @@ class TicTacToe if (morpion.checkCase(targetMorpion, targetCase)) { + morpion.setOutline(this.currentMorpion, true); morpion.sendCase(targetMorpion, targetCase); - morpion.setOutline(); } else morpion.incorrectCase(); @@ -101,19 +129,20 @@ class TicTacToe checkCase(targetMorpion, targetCase) { - return (this.map[targetMorpion][targetCase] == -1 && this.turn == true); + return (this.map[targetMorpion][targetCase] == -1 && this.turn == true && targetMorpion == this.currentMorpion); } incorrectCase() { - console.log("bozo"); + } sendCase(targetMorpion, targetCase) { this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1; + this.currentMorpion = targetCase; this.printSign(targetMorpion, targetCase, this.sign); - console.log(this.game.send, targetMorpion, targetCase) + console.log(targetMorpion, targetCase) this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign})); console.log(this.turn); this.turn = !this.turn; @@ -172,21 +201,25 @@ class TicTacToe return -1; } - setOutline() + setOutline(targetMorpion, clear) { - if (this.turn) + let targetX = (this.gap + targetMorpion % 3 * this.rectsize * 3); + let targetY = (this.gap + Math.floor(targetMorpion / 3) * this.rectsize * 3); + if (this.game.finished) + return; + if (!clear) { this.context.beginPath(); this.context.strokeStyle = (this.sign == "x") ? "green" : "red"; - this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25); + this.context.rect(targetX, targetY, this.rectsize * 3, this.rectsize * 3) this.context.stroke(); this.context.closePath(); } else { this.context.beginPath(); - this.context.strokeStyle = "#1a1a1d"; - this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25); + this.context.strokeStyle = `rgb(230 230 230)`; + this.context.rect(targetX, targetY, this.rectsize * 3, this.rectsize * 3) this.context.stroke(); this.context.closePath(); } @@ -244,34 +277,6 @@ class TicTacToe } this.context.closePath(); } - - selectCase(x, y) - { - case_morpion = Math.floor(x / this.rectsize) + Math.floor((y / this.rectsize)) * 3 - case_square = Math.floor(x / Math.floor(this.rectsize / 3)) % this.rectsize + Math.floor(y / this.rectsize) % this.rectsize - // ask server if case_morpion == playing_case && case_square == empty - - } - - setOutline() - { - if (this.turn) - { - this.context.beginPath(); - this.context.strokeStyle = (this.sign == "x") ? "green" : "red"; - this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25); - this.context.stroke(); - this.context.closePath(); - } - else - { - this.context.beginPath(); - this.context.strokeStyle = "#1a1a1d"; - this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25); - this.context.stroke(); - this.context.closePath(); - } - } } export { TicTacToe }; \ No newline at end of file diff --git a/frontend/static/js/sound/tictactoe/play-move.mp3 b/frontend/static/js/sound/tictactoe/play-move.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..1713db075b05221ed24cdd14b0024b31633016d8 GIT binary patch literal 5224 zcmeI0c{tQ<+sCgN`xyJaWiVr>*{mVM3?oZ}!PsgrBP3K5QjI0M46;_TrEK>tLdZ=b zq6Lv6mBL+UbKl)9)I8%&f86hJJkPuQ^&IcvV=XD*w<9Ce%5yuO- zK*Omd7hCQ?1OOhV$bF$f2+E;I1PxG$ZksZkD`U|ZBLg(f0Br`SG#ZVoN^sSnNJePH zzP%x#hod78#ettDMXo72^Z;}3A!ac15Hli(2{1;cCdOzkTRspWu20N?PzFH9Zpxbw zaU_h52@Y$7CD@?Rrg#%H!4zv^M6k0nvD>th&{z@yw>dJ#Y<~Y|Y>lyaGd$WDy=65) zo1%%vCZ<2HC!h)Fzr`ci8Jn4yk^t4+oy4t3jGK~Ez>#1hQ$rJDLv9dYOWnQM02lz# zkwJh55){JK$OH;k(+6O4_5%o#vjx?Gisfpe+)z~Jfv8Ax1O|&X{OPyp%~{{P{9i-k zc9rYFO=xpXB-en=4}!Kx+2X%r`UQRv*Nz9)sJQ z`~=Al{V|kWYY@J@{8Rq<%UOTU;O?P7*rNj|gdmdO&{UZY(dysf>OGrsqxF1nFUDFB zKmpYF%!y}|Bwj(a@`{QIaRBJ#5(>KSoE1e?;Kus|WnKud78iJy2oX3=e~yrUe|+&i z2f|6Be?Iv^-b(Dkm#73)v5jk1VrgSM05a?F3Pikp8#ng^4u|{k$i5%w{F(^3=94!* zaaMNDXBz>a**UG^E6fjcB_R;^V~Ot*D;_?|9=Jm3ClLFuH0!Ds@Y_N7ZjtzFrIS;V z%g_oI5haRR3Zj}2pkv)v2yuc7u`XK=DT|=tFl$#Z_!>#$XZflIU4>hW>00+;!7v;k zAbmpw5f&Ws9s&AzfVf#o)S$`@?qd5vt&yNPqK^)JE&>FVXn|}ALT2Uj=oFTuptTSW z$mRtH5HO%k>35M$^3l%1cadNz)OzEbQ~hd;bo7+kqOBy|lsO)$3>|SmB_hOF=@3LZ z$h(!0tdz(S2PJok$?^MK3W&6?=c=5|plXZm?zPA;7g?M|!*ukWtP!mG@Hx#qQ`~TQ z5US<0PH_yjvB)~xMO;H#sD?_)cBW?b?ESv(nc4Lpi@(t~{Z*@@*WRtzXUOZLI06U) zYwq$wc%-7Wq5wddAii~}iV%I%M?;9HL)If_2$8C3Y8YUH8svr6k);t((8#>40tkw5joV@UFQ9{>XaXHuAsWUL%DP9# zunePJYNWe72wFb5>`9900n(Wq4G&euZe*xwU^^45fA;Rve5o^8!>^Uya=Xh<4-+e^C@bcQ@uhgv}Ny7rK%@ z)3ax7)00fC6?xfyMAv$qqurJ+1$BB(Z2ughkbrfPW~scP8caqPZ>0r}Ap)Sb5wwia zz%XfxE1t+Nnrolo#R;+a&dUI@^Td_*%k`JGl(nV;vtWPt8BaX5NG1IEu;Z)Pk5-ewag zP6Qq1R_zeBa1|ool&kdy{aE*qCZnfTbPmHW!U#$*mFI)~&Rpw?oohQWr$RolH!=rS zF}K)8lS-SXeec672?afZODgRN;H{^f99Mtjrtfy*==4Kb%G6s4Fj!L0VRYMT9il0Q z3+A+D+lQE#9MeTM6SR0H93NJvhgqduNEQ9HWcP5}qE<{0hf~mNZd%HHTJe9%t8tod zha;PZRM4r;uHpgW%cf_lu6 zORrbFBsr?llvf)*tl2erSArFIu~F$a_LsCfUV4-xse5*7xZG>yEz(EmPiP@q2zC$l z?7-Y?&^-M(+83E!H8|=%f4H>(_kPuWqrNG=%Avr43%qek zPaGKZ&IZ}t@JSXyV+8tPWsuiZfu~9!p`HQI#&ij@0SH`>wCc;IEc(%t%Jv8MqJ_~q zNaN-)pd@CUI}|ro}^b8ZC96IsfDFK|I10=38P|ZZ1{`as+r`q$~K?w2Jh<1 zS8o(vo3FTJQQF$8kGmVC1q4f8!%OFu@|pH$CCnyGVZCLa%`hgN57Xbp?HRt~(#moE zkfA6dsPlY4XFVD+HtH5OjB?=)+9;gI{?PVNF~HpfKZ4szN*Il>Og#!W;W>x zzQmev;j9Hs_uAQHE8kz5e#O6V2+@2A53aps(PBvZrRGLzG$Br<$pDu?_Whh_xV_PB z=86ApiRMf*y(5CT?;G>|2(`8CF(>^wf1J#D_p~{^RPL;8(l=F(Gj+CvyW@x}EIu9g z;dLkca9x=9&d}5b@tPI2Hvzu~3dm`EE7J610f*~#;`fqPd7iwnd%q zc2(YE#k~gIkB)fHdHN&X3(mYqWbh#GOf2=2+%jCT_d_e2l*4a#dgkjP>SY5wn~aLn zjl_XSBpbkNdYdFtFH*yy3VM4W@<>snPlZ}p5ASW^WY4-@?>UYNSPGy3e*aZ#Xoudd>ZU{_zwCKQ#67h!HIj488}}bn`F-@@85(&?En+KQTNSV$C~-+xd7<0alN#3^=(~2HX~bM8 zElq@45yeQhE3;B_SVC0rbEbLO-Y)k8p56~Eh-0BIG9Y0`rh+6~`4(RCD;)FC zJnQ+ST~a8x=v|e>?-}=m$#0=?ZAxi6?Qk>OVdkt9LRol9TlH5h$E@+n8hpd4%W6I_ zlFp>`sfe1q$!9Xhjjt8FdLE{Ga`sr0jIx>4kt~T{iv5naAA9zyKzkW73RIf4u|v4?mN(CuZ>QiXQQB+2H~-~;*}pbfUhR5@9Ao3e7cotF zqj0hG6r?4H;1JS213lu^NSk@}GILzb7ZE1<>s8;3rLl7%=c6*B0uVUl0B5~|_&bYd z0)6WkZC7j*s^lFrwN!MnRbjPxVt&2orDY>}LrQ~;vEfwoM;ms%EeXD`cQU=CSR9!3 zUI8%j`D9t?HUhmsr=W<)IEtpta>*}9Cd8zWq%qqSi*FM4RjL4 zgv+hQgCAxo2XR(8oVTg5Oa$23o6Ul)-fJ7yRo{h@Ax&ya&c{f09;=ziZ9(1%IW>j^ zk8;Ls?Jg>*Br?*U)3c$2If)A2Ap}Yh9Nx}SCMTqXl)X5s_#uckfvVI`y?L?Vbx*Bj zxcM5iSY1B;(Nfk0VQCW!FXnpSRoZC>Y%k|e+a29%KyKX*@GcJ-?6x+<^LEcZyiQka z%*Dbl%_QbR-ym-3@zSruP9aM!H9vEnZFmr4H?OhyqLR>oQaRnfASMAZqgSDAccJ7m z^&Btxxc?EM8u7@SW@f`SKrsn)D)t?21BooE#y@SpM7}akX4zP3;}{^dTSV5+bAVt zW9I52lm)rj#W#E9S`4+Ps@3GdMDlNc9qUESao?(RH>BzDMH6*C0a=2mSPI$IEZE(RakrqEs+sJi z!8gW`D>Ka@dfNzwTLlYmGp-`+OH1ijbbD{{xMS`d=ldJO7+=+I{%34kTM;Zn4%Wom zwoa}a+`%Xn)ayURO>U%Ij=x*B51Yvxk-xA~Mp@Nsuv?VYkHaLCe-KFt%^W+g_GoRn z$W11*JTAyACNgS5$9i9FW88VvK&h$xj`$PyxhUuF>oF(yz2-dDeIfW<~pESA?70Cza7+8#dZiEJmk7ZsOqksK4+ax{G7r03qda`*RQml;jZ5&74!PClg8@(Et1XKZ{f0mh3_@-`XPR%9SEruk*{PCAzN|gA zcd-9AS@YSZbmvAQke6CoZ7r4^G{W=mS?E7e#0zk01amg-E>fFa#&`R;h-YPLM%C-| zgSh;>*LHQzGQzGkRfXo2`p}F%bx)EMIqznM`@QR5FX6}hQlHgwKI^p9I}w~oxXn{- z{eQ{-!1bTT`uB{%S*H^x#nyHx4TjvuEc*<^<^(M5w+4{!+~W=cfSr8+@cz@1{-xpH zrQUy;b%7 literal 0 HcmV?d00001 diff --git a/frontend/static/js/views/TicTacToeOnlineView.js b/frontend/static/js/views/TicTacToeOnlineView.js index 129e6a5..2379cd8 100644 --- a/frontend/static/js/views/TicTacToeOnlineView.js +++ b/frontend/static/js/views/TicTacToeOnlineView.js @@ -19,7 +19,6 @@ export class TicTacToeOnlineView extends AbstractView this.Morpion = new TicTacToe(this.height, this.width, 60, 60, document.getElementById("Morpion"), this.game_id); this.Morpion.DrawSuperMorpion(); await this.Morpion.init(); - this.Morpion.setOutline(); } async leavePage() diff --git a/games/consumers.py b/games/consumers.py index 4fd1094..825ffe5 100644 --- a/games/consumers.py +++ b/games/consumers.py @@ -45,6 +45,8 @@ class TicTacToeWebSocket(WebsocketConsumer): self.member.send(self.member.sign) if (self.game._everbody_is_here() and self.game.model.started == False): + if (self.game.time != -1): + self.game.broadcast("opponent_joined") self.game.broadcast("game_start") self.game.model.start() @@ -54,7 +56,12 @@ 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 - self.game.broadcast("", data, [self.member]) + 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.game.broadcast("game_move", data, [self.member]) pass def disconnect(self, event): diff --git a/games/objects/tictactoe/TicTacToeGame.py b/games/objects/tictactoe/TicTacToeGame.py index fe123e5..7befea5 100644 --- a/games/objects/tictactoe/TicTacToeGame.py +++ b/games/objects/tictactoe/TicTacToeGame.py @@ -18,6 +18,10 @@ class TicTacToeGame(AGame): self.players: list[TicTacToePlayer] = [TicTacToePlayer(player, None, self, ["x", "o"][i]) for i, player in enumerate(players)] + self.time = -1 + + self.turn = 'x' + self._map = [[-1 for _ in range(9)] for _ in range(9)] def _everbody_is_here(self): @@ -44,14 +48,15 @@ class TicTacToeGame(AGame): if (self.checkMove(newmove, player)): self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign") - player.currentMorpion = int(newmove.get("targetMorpion")) + player.currentMorpion = int(newmove.get("targetCase")) + self.turn = newmove.get("sign") return True return False def checkMove(self, newmove, player): print(int(newmove.get("targetMorpion")), player.currentMorpion) - if (int(newmove.get("targetMorpion")) != player.currentMorpion): + if (int(newmove.get("targetMorpion")) != player.currentMorpion or newmove.get("sign") != self.turn): return False if (self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] != -1): @@ -62,16 +67,16 @@ class TicTacToeGame(AGame): def checkWin(self): for tab in self._map: for i in range(3): - if tab[i] == tab[i + 3] == tab[i + 6]: + if tab[i] != -1 and tab[i] == tab[i + 3] and tab[i + 3] == tab[i + 6]: return tab[i] for i in range(0, 9, 3): - if tab[i] == tab[i + 1] == tab[i + 2]: + if tab[i] != -1 and tab[i] == tab[i + 1] and tab[i + 1] == tab[i + 2]: return tab[i] - if tab[0] == tab[4] == tab[8]: + if tab[0] != -1 and tab[0] == tab[4] and tab[4] == tab[8]: return tab[0] - if tab[6] == tab[4] == tab[2]: + if tab[6] != -1 and tab[6] == tab[4] and tab[4] == tab[2]: return tab[6] - return None + return False def _spectator_join(self, user_id: int, socket: WebsocketConsumer): From 743bb7edeb6885e91019399f6f55fca4adfab47b Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 23 Apr 2024 15:41:18 +0200 Subject: [PATCH 09/16] tournament: add: game call func when finished --- games/models.py | 21 ++++++++++++++++++++- tournament/consumers.py | 3 +++ tournament/models.py | 2 +- tournament/serializers.py | 6 +++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/games/models.py b/games/models.py index 37bfcce..573a891 100644 --- a/games/models.py +++ b/games/models.py @@ -7,6 +7,10 @@ from django.contrib.auth.models import User import time +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from tournament.models import TournamentGameModel class GameModel(models.Model): @@ -27,13 +31,28 @@ class GameModel(models.Model): self.start_timestamp = round(time.time() * 1000, 1) self.started = True self.save() + + def get_tournament(self) -> None | TournamentGameModel: + + from tournament.models import TournamentGameModel + + query = TournamentGameModel.objects.filter(game=self) + if (not query.exists()): + return None + return query[0] def finish(self, winner_id): self.winner_id = winner_id self.finished = True self.stop_timestamp = round(time.time() * 1000, 1) self.save() - + + tournament = self.get_tournament() + if tournament is not None: + from tournament.consumers import tournament_manager + room = tournament_manager.get(tournament) + room.set_game_as_finished(self) + def get_players(self) -> list[User]: return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)] diff --git a/tournament/consumers.py b/tournament/consumers.py index 78aa59f..953b082 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -90,6 +90,9 @@ class TournamentRoom: self._member_list.add(member) return member + + def set_game_as_finished(self, game: GameModel): + raise NotImplemented() def get_participants_profiles(self) -> list[ProfileModel]: return [participant._socket.user.profilemodel for participant in self.get_participants()] diff --git a/tournament/models.py b/tournament/models.py index 4f0fe28..6a91526 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -53,7 +53,7 @@ class TournamentModel(models.Model): return game def get_games(self) -> list[GameModel]: - return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self)] + return [games for games in self.get_games_by_round(i for i in range(1, self.round))] def get_games_by_round(self, round: int) -> list[GameModel]: return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)] diff --git a/tournament/serializers.py b/tournament/serializers.py index abf2cfb..cabb773 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -13,16 +13,20 @@ class TournamentSerializer(serializers.ModelSerializer): state = serializers.SerializerMethodField(read_only=True, required=False) participants = serializers.SerializerMethodField(read_only=True, required=False) round = serializers.ReadOnlyField() + games = serializers.SerializerMethodField(read_only=True, required=False) started = serializers.ReadOnlyField() finished = serializers.ReadOnlyField() name = serializers.CharField(default="") class Meta: model = TournamentModel - fields = ["name", "nb_participants", "round", "started", "finished", "id", "state", "participants"] + fields = ["name", "nb_participants", "round", "started", "finished", "id", "state", "participants", "games"] def get_participants(self, instance: TournamentModel): return ProfileSerializer(instance.get_participants(), many=True).data + + def get_games(self, instance: TournamentModel): + return GameSerializer(instance.get_games(), many=True).data def get_state(self, instance: TournamentModel): return ["waiting", "started", "finished"][instance.started + instance.finished] From 02bbaa6d9f501f162a257d612315697970e9f516 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Tue, 23 Apr 2024 15:54:16 +0200 Subject: [PATCH 10/16] hotfix: profilepage remove friend function --- frontend/static/js/views/ProfilePageView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 8b27e81..65e94fa 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -111,7 +111,7 @@ export default class extends AbstractView { client.me.outgoingFriendRequests = client.me.outgoingFriendRequests.filter(profile => profile.id !== this.profile.id); } else if (response.status === 201) { this.profile.isFriend = false; - client.me.friendList = client.me.friendList.client.me.incomingFriendRequests = filter(friend => friend.id !== this.profile.id); + client.me.friendList = client.me.friendList.filter(friend => friend.id !== this.profile.id); } } From dbb8e07d7d60a427ff42d75e814e7ca11b6eab20 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 25 Apr 2024 15:44:09 +0200 Subject: [PATCH 11/16] tournament: game: finish event --- games/consumers.py | 1 - games/models.py | 31 ++++------------ games/objects/AGame.py | 8 +++- games/objects/GameManager.py | 4 +- games/serializers.py | 4 +- matchmaking/consumers.py | 4 +- tournament/consumers.py | 46 ++++++++++++++++++++--- tournament/models.py | 71 +++++++++++++++++++++++------------- tournament/serializers.py | 30 +++++++++++++-- 9 files changed, 135 insertions(+), 64 deletions(-) diff --git a/games/consumers.py b/games/consumers.py index 825ffe5..5bac2ef 100644 --- a/games/consumers.py +++ b/games/consumers.py @@ -21,7 +21,6 @@ if TYPE_CHECKING: from .objects.pong.PongGame import PongGame from .objects.tictactoe.TicTacToeGame import TicTacToeGame - from .objects.tictactoe.TicTacToeSpectator import TicTacToeSpectator game_manager: GameManager = GameManager() diff --git a/games/models.py b/games/models.py index 573a891..edd26f7 100644 --- a/games/models.py +++ b/games/models.py @@ -21,43 +21,28 @@ class GameModel(models.Model): stop_timestamp = models.BigIntegerField(null = True, blank = True) game_type = models.CharField(max_length = 60, default = "pong") - def create(self, players: list[User]): + def create(self, players: set[User]) -> GameModel: self.save() for player in players: GameMembersModel(game = self, player=player).save() - return self.pk + return self def start(self): self.start_timestamp = round(time.time() * 1000, 1) self.started = True self.save() - - def get_tournament(self) -> None | TournamentGameModel: - - from tournament.models import TournamentGameModel - - query = TournamentGameModel.objects.filter(game=self) - if (not query.exists()): - return None - return query[0] - def finish(self, winner_id): - self.winner_id = winner_id + def finish(self, winner: User): + self.winner = winner self.finished = True self.stop_timestamp = round(time.time() * 1000, 1) self.save() - tournament = self.get_tournament() - if tournament is not None: - from tournament.consumers import tournament_manager - room = tournament_manager.get(tournament) - room.set_game_as_finished(self) - - def get_players(self) -> list[User]: - return [game_player.player for game_player in GameMembersModel.objects.filter(game = self)] + def get_players(self) -> set[User]: + return {game_player.player for game_player in GameMembersModel.objects.filter(game = self)} - def get_players_profiles(self) -> list[User]: - return [game_player.player.profilemodel for game_player in GameMembersModel.objects.filter(game = self)] + def get_players_profiles(self) -> set[User]: + return {game_player.player.profilemodel for game_player in GameMembersModel.objects.filter(game = self)} def get_score_by_player_id(self, player_id: int) -> list[int]: query: QuerySet = GameGoalModel.objects.filter(game_id = self.pk, player_id = player_id) diff --git a/games/objects/AGame.py b/games/objects/AGame.py index 6839a61..6fe0583 100644 --- a/games/objects/AGame.py +++ b/games/objects/AGame.py @@ -6,6 +6,8 @@ from .ASpectator import ASpectator from ..models import GameModel +from tournament.models import TournamentGameModel + from django.contrib.auth.models import User class AGame(AbstractRoom): @@ -16,7 +18,11 @@ class AGame(AbstractRoom): self.game_manager = game_manager - self.model: GameModel = GameModel.objects.get(pk = game_id, game_type = game_type) + query = TournamentGameModel.objects.filter(pk = game_id, game_type = game_type) + if (query.exists()): + self.model: TournamentGameModel | GameModel = query[0] + else: + self.model: TournamentGameModel | GameModel = GameModel.objects.get(pk = game_id, game_type = game_type) players: list[User] = self.model.get_players() diff --git a/games/objects/GameManager.py b/games/objects/GameManager.py index ee1ced8..94de967 100644 --- a/games/objects/GameManager.py +++ b/games/objects/GameManager.py @@ -3,6 +3,8 @@ from ..models import GameModel from .pong.PongGame import PongGame from .tictactoe.TicTacToeGame import TicTacToeGame +from tournament.models import TournamentGameModel + class GameManager(): def __init__(self) -> None: @@ -14,7 +16,7 @@ class GameManager(): self._game_list.remove(game) def get(self, game_id: int, game_type: str) -> TicTacToeGame | PongGame: - + if (not GameModel.objects.filter(pk=game_id, finished=False, game_type=game_type).exists()): return None diff --git a/games/serializers.py b/games/serializers.py index 114c82d..b0338bb 100644 --- a/games/serializers.py +++ b/games/serializers.py @@ -15,11 +15,11 @@ class GameSerializer(serializers.ModelSerializer): finished = serializers.ReadOnlyField() start_timestamp = serializers.ReadOnlyField() stop_timestamp = serializers.ReadOnlyField() - gamemode = serializers.ReadOnlyField() + game_type = serializers.ReadOnlyField() class Meta: model = GameModel - fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "gamemode"] + fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type"] def get_state(self, instance: GameModel): if (instance.finished): diff --git a/matchmaking/consumers.py b/matchmaking/consumers.py index 331d391..9c65961 100644 --- a/matchmaking/consumers.py +++ b/matchmaking/consumers.py @@ -42,8 +42,8 @@ class MatchMaking(WebsocketConsumer): self.waiting_room.broadcast(f"{len(self.waiting_room)} / {self.waiting_room.mode}") if (len(self.waiting_room) == self.waiting_room.mode): - game_id: int = GameModel(game_type=self.game_type).create(self.waiting_room.get_members()) - self.waiting_room.broadcast("game_found", {"game_id": game_id, "game_type": self.game_type}) + game: GameModel = GameModel(game_type=self.game_type).create(self.waiting_room.get_members()) + self.waiting_room.broadcast("game_found", {"game_id": game.pk, "game_type": self.game_type}) def disconnect(self, close_code): super().disconnect(close_code) diff --git a/tournament/consumers.py b/tournament/consumers.py index 953b082..fe52b90 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -7,6 +7,7 @@ from django.db.models import QuerySet from django.utils.translation import gettext as _ from games.models import GameModel +from games.serializers import GameSerializer from profiles.models import ProfileModel from profiles.serializers.ProfileSerializer import ProfileSerializer from .models import TournamentModel @@ -83,6 +84,8 @@ class TournamentRoom: self._room_manager: TournamentRoomManager = room_manager self._member_list: set[TournamentMember] = set() self._model: TournamentModel = tournament + self._game_in_progress_list: set[GameModel] = set() + self._current_round = 0 def join(self, socket: TournamentWebConsumer) -> TournamentMember: @@ -92,21 +95,52 @@ class TournamentRoom: return member def set_game_as_finished(self, game: GameModel): - raise NotImplemented() + self._game_in_progress_list.remove(game) + + data: dict = GameSerializer(game).data + + data.update({"round": self._current_round}) + + self.broadcast("game_update", data) + + if len(self._game_in_progress_list) == 0: + self._round_finished() + + def _finish(self, winner: User): + self._model.finish(winner) + + def _round_finished(self): + + if self._current_round == self._model.round: + last_game: GameModel = self._model.get_games_by_round(self._current_round)[0] + self._finish(last_game.winner) + return + + self._start_round() + + def _start_round(self): + + self._current_round += 1 + + participant_list: set[User] = self._model.get_participants_by_round(self._current_round) + + self._game_in_progress_list = self._model.create_round(participant_list, self._current_round) + + for game in self._game_in_progress_list: + for player in game.get_players(): + participant: TournamentMember = self.get_participant_by_profile(player) + participant.send_goto(game) def get_participants_profiles(self) -> list[ProfileModel]: return [participant._socket.user.profilemodel for participant in self.get_participants()] def start(self) -> None: - games: list[GameModel] = self._model.start() + self._model.start() self.broadcast("start") - for game in games: - for player in game.get_players(): - participant: TournamentMember = self.get_participant_by_profile(player) - participant.send_goto(game) + self._start_round() def get_participant_by_profile(self, profile: ProfileModel): for participant in self.get_participants(): diff --git a/tournament/models.py b/tournament/models.py index 6a91526..cabc76d 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -19,24 +19,35 @@ class TournamentModel(models.Model): def _register_participant(self, participant: User) -> None: TournamentParticipantModel(participant=participant, tournament=self).save() - def start(self, participants: list[User]) -> None: + def create_round(self, participants: set[User], round: int) -> set[GameModel]: - games: list[GameModel] = [] + game_list: set[GameModel] = set() + + for i, (participant1, participant2) in enumerate(zip(participants[0::2], participants[1::2])): + game: GameModel = self.create_game([participant1, participant2], round, i) + game_list.add(game) + + return game_list + + def start(self, participant_list: set[User]) -> None: self.started = True + self.round = 1 - for player in participants: - self._register_participant(player) - - for (participant1, participant2) in zip(participants[0::2], participants[1::2]): - game: GameModel = self.create_game([participant1, participant2], round=1) - games.append(game) + for participant in participant_list: + self._register_participant(participant) self.save() - return games + def finish(self, winner: User): + + self.finished = True - def create_game(self, participants: list[User], round: int) -> GameModel: + self.winner = winner + + self.save() + + def create_game(self, participants: set[User], round: int, pos: int) -> GameModel: if (self.started == False): return None @@ -48,37 +59,47 @@ class TournamentModel(models.Model): game: GameModel = GameModel().create(participants) - TournamentGameModel(tournament=self, game=game, round=round).save() + TournamentGameModel(tournament=self, game=game, round=round, pos=pos).save() return game - def get_games(self) -> list[GameModel]: - return [games for games in self.get_games_by_round(i for i in range(1, self.round))] + def get_games(self) -> set[GameModel]: + return {games for games in self.get_games_by_round(i for i in range(1, self.round))} - def get_games_by_round(self, round: int) -> list[GameModel]: - return [tournament_game.game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)] + def get_games_by_round(self, round: int) -> set[GameModel]: + return {tournament_game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)} - def get_players_by_round(self, round: int) -> list[User]: - return [game.get_players() for game in self.get_games_by_round(round)] + def get_participants_by_round(self, round: int) -> set[User]: + if round == 1: + return self.get_participants() + return {game.winner for game in self.get_games_by_round(round - 1)} - def get_winners_by_round(self, round: int) -> list[User]: - return [game.winner for game in self.get_games_by_round(round)] + def get_winners_by_round(self, round: int) -> set[User]: + return {game.winner for game in self.get_games_by_round(round)} - def get_participants(self) -> list[TournamentParticipantModel]: - return TournamentParticipantModel.objects.filter(tournament=self.pk) + def get_participants(self) -> set[User]: + return {tournament_participant.participant for tournament_participant in TournamentParticipantModel.objects.filter(tournament=self.pk)} def get_state(self) -> str: return ("waiting to start", "in progress", "finish")[self.started + self.finished] - def is_participanting(self, profile: User) -> bool: + def is_participating(self, profile: User) -> bool: return TournamentParticipantModel.objects.filter(participant=profile, tournament=self).exists() class TournamentParticipantModel(models.Model): participant = models.ForeignKey(User, on_delete=CASCADE) tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE) - -class TournamentGameModel(models.Model): + +class TournamentGameModel(GameModel): tournament = models.ForeignKey(TournamentModel, on_delete=CASCADE, null=True, blank=True) round = models.IntegerField() - game = models.ForeignKey(GameModel, on_delete=CASCADE) \ No newline at end of file + pos = models.IntegerField() + + def finish(self, winner_id): + super().finish(winner_id) + + from .consumers import tournament_manager + + room = tournament_manager.get(self.tournament) + room.set_game_as_finished(self) \ No newline at end of file diff --git a/tournament/serializers.py b/tournament/serializers.py index cabb773..012aab6 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -1,8 +1,7 @@ from rest_framework import serializers -from .models import TournamentModel +from .models import TournamentModel, TournamentGameModel -from profiles.models import ProfileModel from profiles.serializers.ProfileSerializer import ProfileSerializer from games.serializers import GameSerializer @@ -34,4 +33,29 @@ class TournamentSerializer(serializers.ModelSerializer): def validate_nb_participants(self, value: int): if (value not in nb_participants): raise serializers.ValidationError(f"The numbers of participants must be {str(nb_participants)}.") - return value \ No newline at end of file + return value + +class TournamentGameSerializer(serializers.ModelSerializer): + + players = serializers.SerializerMethodField() + winner_id = serializers.ReadOnlyField() + state = serializers.SerializerMethodField() + started = serializers.ReadOnlyField() + finished = serializers.ReadOnlyField() + start_timestamp = serializers.ReadOnlyField() + stop_timestamp = serializers.ReadOnlyField() + gamemode = serializers.ReadOnlyField() + + class Meta: + model = TournamentGameModel + fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type"] + + def get_state(self, instance: TournamentGameModel): + if (instance.finished): + return "finished" + if (instance.started): + return "started" + return "waiting" + + def get_players(self, instance: TournamentGameModel): + return ProfileSerializer(instance.get_players_profiles(), many=True).data \ No newline at end of file From 5f58b65a34b5aea053daa3bdd4a51c6020272567 Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Thu, 25 Apr 2024 15:45:32 +0200 Subject: [PATCH 12/16] notice but without the t --- frontend/static/js/api/Client.js | 8 +- frontend/static/js/api/MyProfile.js | 8 +- frontend/static/js/api/Profile.js | 4 +- frontend/static/js/api/Profiles.js | 2 +- frontend/static/js/api/chat/Notice.js | 308 ------------------ frontend/static/js/api/notice/Notice.js | 83 +++++ frontend/static/js/index.js | 2 +- frontend/static/js/utils/noticeUtils.js | 29 +- frontend/static/js/views/ProfilePageView.js | 41 ++- .../static/js/views/accounts/LogoutView.js | 1 - frontend/templates/index.html | 3 +- games/serializers.py | 4 +- notice/__init__.py | 0 notice/admin.py | 3 + notice/apps.py | 6 + notice/consumers.py | 68 ++++ notice/models.py | 7 + notice/routing.py | 7 + notice/tests.py | 3 + notice/views.py | 3 + .../ProfileSerializer.py => serializers.py} | 4 +- profiles/views/blocks.py | 2 +- profiles/views/friends.py | 11 +- profiles/viewsets/MyProfileViewSet.py | 2 +- profiles/viewsets/ProfileViewSet.py | 2 +- tournament/consumers.py | 4 +- tournament/serializers.py | 4 +- transcendence/asgi.py | 9 +- transcendence/settings.py | 1 + 29 files changed, 258 insertions(+), 371 deletions(-) delete mode 100644 frontend/static/js/api/chat/Notice.js create mode 100644 frontend/static/js/api/notice/Notice.js create mode 100644 notice/__init__.py create mode 100644 notice/admin.py create mode 100644 notice/apps.py create mode 100644 notice/consumers.py create mode 100644 notice/models.py create mode 100644 notice/routing.py create mode 100644 notice/tests.py create mode 100644 notice/views.py rename profiles/{serializers/ProfileSerializer.py => serializers.py} (90%) diff --git a/frontend/static/js/api/Client.js b/frontend/static/js/api/Client.js index 5341b08..3732b9f 100644 --- a/frontend/static/js/api/Client.js +++ b/frontend/static/js/api/Client.js @@ -4,7 +4,7 @@ import { Profiles } from "./Profiles.js"; import { Channels } from './chat/Channels.js'; import { MyProfile } from "./MyProfile.js"; import { Tourmanents } from "./tournament/Tournaments.js"; -import { Notice } from "./chat/Notice.js"; +import Notice from "./notice/Notice.js"; import { Channel } from "./chat/Channel.js"; import LanguageManager from './LanguageManager.js'; @@ -83,7 +83,7 @@ class Client { if (this._logged == undefined) this._logged = await this._test_logged(); - return this._logged; + return this._logged; } /** @@ -217,7 +217,7 @@ class Client { this.me = new MyProfile(this); await this.me.init(); - this.notice.connect(); + this.notice.start(); document.getElementById('navbarLoggedOut').classList.add('d-none'); document.getElementById('navbarLoggedIn').classList.remove('d-none'); document.getElementById('navbarDropdownButton').innerHTML = this.me.username; @@ -226,7 +226,7 @@ class Client else { this.me = undefined; - this.notice.disconnect(); + this.notice.stop(); document.getElementById('navbarLoggedOut').classList.remove('d-none'); document.getElementById('navbarLoggedIn').classList.add('d-none'); document.getElementById('navbarDropdownButton').innerHTML = 'Me'; diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index ef2e694..17dd2df 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -40,26 +40,26 @@ class MyProfile extends Profile async getBlockedUsers() { const response = await this.client._get('/api/profiles/block'); const data = await response.json(); - data.forEach(profileData => this.blockedUsers.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); + data.forEach(profileData => this.blockedUsers.push(new Profile(this.client, profileData.username, profileData.id, profileData.avatar))); } async getFriends() { const response = await this.client._get('/api/profiles/friends'); const data = await response.json(); - data.forEach(profileData => this.friendList.push(new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar))); + data.forEach(profileData => this.friendList.push(new Profile(this.client, profileData.username, profileData.id, profileData.avatar))); } async getIncomingFriendRequests() { const response = await this.client._get('/api/profiles/incoming_friend_requests'); const data = await response.json(); data.forEach(profileData => this.incomingFriendRequests.push( - new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar) + new Profile(this.client, profileData.username, profileData.id, profileData.avatar) )); } async getOutgoingFriendRequests() { const response = await this.client._get('/api/profiles/outgoing_friend_requests'); const data = await response.json(); data.forEach(profileData => this.outgoingFriendRequests.push( - new Profile(this.client, profileData.username, profileData.user_id, profileData.avatar) + new Profile(this.client, profileData.username, profileData.id, profileData.avatar) )); } diff --git a/frontend/static/js/api/Profile.js b/frontend/static/js/api/Profile.js index feb9570..a1e553d 100644 --- a/frontend/static/js/api/Profile.js +++ b/frontend/static/js/api/Profile.js @@ -55,17 +55,17 @@ export class Profile extends AExchangeable return response.status; let response_data = await response.json(); - this.id = response_data.user_id; + this.id = response_data.id; this.username = response_data.username; this.avatar = response_data.avatar; if (!this.client.me || this.client.me.id === this.id) return; - this.isFriend = this.client.me._isFriend(this); this.isBlocked = this.client.me._isBlocked(this); this.hasIncomingRequest = this.client.me._hasIncomingRequestFrom(this); this.hasOutgoingRequest = this.client.me._hasOutgoingRequestTo(this); + this.isFriend = this.client.me._isFriend(this); } /** diff --git a/frontend/static/js/api/Profiles.js b/frontend/static/js/api/Profiles.js index 796abc7..04d1666 100644 --- a/frontend/static/js/api/Profiles.js +++ b/frontend/static/js/api/Profiles.js @@ -24,7 +24,7 @@ class Profiles let profiles = []; response_data.forEach((profile) => { - profiles.push(new Profile(this.client, profile.username, profile.user_id, profile.avatar)); + profiles.push(new Profile(this.client, profile.username, profile.id, profile.avatar)); }); return profiles; } diff --git a/frontend/static/js/api/chat/Notice.js b/frontend/static/js/api/chat/Notice.js deleted file mode 100644 index 9448e2a..0000000 --- a/frontend/static/js/api/chat/Notice.js +++ /dev/null @@ -1,308 +0,0 @@ -import { navigateTo } from "../../index.js"; -import {createNotification} from "../../utils/noticeUtils.js"; - -class Notice { - constructor(client) { - this.client = client; - this.data = {}; - - // users online, invited by ..., asked by ..., asker to ... - let data_variable = ["online", "invited", "asked", "asker"]; - for (let i in data_variable) - this.data[data_variable[i]] = []; - - //this.connect(); - - } - - async connect() { - return - let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/chat/notice`; - - this.chatSocket = new WebSocket(url); - this.chatSocket.onmessage = (event) =>{ - let send = JSON.parse(event.data); - //console.log("notice: ", send); - - try { - this["receive_" + send.type](send); - } - catch (error) { - console.log("receive_" + send.type + ": Function not found"); - } - - }; - this.chatSocket.onopen = (event) => { - this.getOnlineUser(); - this.ask_friend(); - }; - } - - async disconnect() { - if (this.chatSocket == undefined) - return ; - - this.chatSocket.close(); - } - - async reconnect() { - this.disconnect(); - this.connect(); - } - - async accept_invite(invitedBy) { - - this.sendRequest({ - type: "accept_invite", - targets: [invitedBy], - }); - - } - - async receive_accept_invite(send) { - - this.data.invited = send.invites; - let id_game = send.id_game; - navigateTo("/game/" + id_game); - - } - - async refuse_invite(invitedBy) { - - this.sendRequest({ - type: "refuse_invite", - targets: [invitedBy], - }); - - - } - async receive_refuse_invite(send) { - - this.data.invited = send.invites; - - if (send.author_id == this.client.me.id) { - if (this.rewrite_invite !== undefined) - this.rewrite_invite(); - } - else { - let sender = await this.client.profiles.getProfileId(send.author_id); - createNotification(sender.username + " refuse your invitation"); - } - - } - - - async send_invite(id_inviteds) { - - this.sendRequest({ - type: "invite", - targets: id_inviteds, - time: new Date().getTime(), - }); - - } - - async receive_invite(send) { - - if (this.client.me == undefined) - return ; - - let content = send.invites; - - if (send.author_id == this.client.me.id) { - if (send.status == 200) { - for (let target in send.targets) - return createNotification("Invitation send"); - } - else if (send.status == 444) - return createNotification("User not connected"); - else if (send.status == 409) - return createNotification("Already invited"); - } - else { - - // Regarder qu'il est bien invité par l'auteur - // Et qu'il n'est pas déjà invité - if (!content.includes(send.author_id) || - this.data.invited.includes(send.author_id)) - return; - - this.data.invited = content; - let sender = await this.client.profiles.getProfileId(send.author_id); - - createNotification("Invitation received by " + sender.username); - - if (this.rewrite_invite !== undefined) - this.rewrite_invite(); - } - } - - async getOnlineUser() { - - this.online_users = {}; - - this.sendRequest({ - type: "online_users", - targets: [], - time: new Date().getTime(), - }); - - } - - async receive_online_users(send) { - let content = send.online; - if (content !== undefined) { - - if (this.data.online.length > 0) { - // get all disconnect user - //let disconnects = this.data["online"].filter(id => !Object.keys(content).includes(id)); - - let disconnects = []; - - for (const [key, value] of Object.entries(this.data.online)) { - if (content[key] == "red" && value == "green") - disconnects.push(key); - } - - // delete invite - this.data.invited = this.data.invited.filter(id => !disconnects.includes(id)); - - //console.log(this.data["invited"]); - } - - this.data.online = content; - - if (this.rewrite_usernames !== undefined) - this.rewrite_usernames(); - } - } - - async ask_friend(user_id=undefined) { - this.sendRequest({ - type: "ask_friend", - targets: [user_id], - time: new Date().getTime(), - }); - } - - async receive_ask_friend(send) { - - let my_id = (this.client.me && this.client.me.id) || send.author_id; - if (send.author_id == my_id) { - if (send.status == 400) - createNotification("Friend ask error"); - else if (send.status == 409) - createNotification("Already asked friend"); - } - - //if (!send.asked.includes(send.author_id) || - //this.data["asked"].includes(send.author_id)) - //return; - - //if (!send.asker.includes(send.author_id) || - //this.data["asker"].includes(send.author_id)) - //return; - - this.data.asked = send.asked; - this.data.asker = send.asker; - - if (send.author_id != my_id) { - let sender = await this.client.profiles.getProfileId(send.author_id); - if (this.data.asker.includes(send.author_id)) - createNotification(sender.username + " ask you as friend"); - if (this.rewrite_profile !== undefined) - await this.rewrite_profile(); - } - - } - - async remove_friend(user_id) { - this.sendRequest({ - type: "remove_friend", - targets: [user_id], - time: new Date().getTime(), - }); - } - - async receive_remove_friend(send) { - - if (send.author_id == this.client.me.id) { - if (send.status == 400) - createNotification("Error remove Friend"); - else if (send.status == 409) - createNotification("Not friend, wtf"); - - } - - if (this.rewrite_profile !== undefined) - await this.rewrite_profile(); - - this.receive_online_users(send); - } - - async accept_friend(user_id) { - this.sendRequest({ - type: "accept_friend", - targets: [user_id], - time: new Date().getTime(), - }); - } - - async receive_accept_friend(send) { - this.data.asked = send.asked; - this.data.asker = send.asker; - let sender = await this.client.profiles.getProfileId(send.author_id); - - if (send.author_id == this.client.me.id) { - if (send.status == 400) - createNotification("Error accept Friend"); - else if (send.status == 404) - createNotification("Not found request Friend"); - else if (send.status == 409) - createNotification("Already Friend, wtf"); - } - else { - createNotification(sender.username + " accept your friend request"); - } - - if (this.rewrite_profile !== undefined) - await this.rewrite_profile(); - - this.receive_online_users(send); - } - - async refuse_friend(user_id) { - this.sendRequest({ - type: "refuse_friend", - targets: [user_id], - time: new Date().getTime(), - }); - } - - async receive_refuse_friend(send) { - this.data.asked = send.asked; - this.data.asker = send.asker; - let sender = await this.client.profiles.getProfileId(send.author_id); - - if (send.author_id == this.client.me.id) { - if (send.status == 400) - createNotification("Error refuse Friend"); - else if (send.status == 404) - createNotification("Not found request Friend"); - else if (send.status == 409) - createNotification("Already Friend, WTF"); - - } - if (this.rewrite_profile !== undefined) - await this.rewrite_profile(); - } - - async sendRequest(content) { - if (this.chatSocket == undefined) - return; - - this.chatSocket.send(JSON.stringify(content)); - } -} - -export {Notice}; diff --git a/frontend/static/js/api/notice/Notice.js b/frontend/static/js/api/notice/Notice.js new file mode 100644 index 0000000..e2407e4 --- /dev/null +++ b/frontend/static/js/api/notice/Notice.js @@ -0,0 +1,83 @@ +import {Client} from '../Client.js'; +import {createNotification} from '../../utils/noticeUtils.js' +import { client, lastView } from '../../index.js'; +import { Profile } from '../Profile.js'; +import ProfilePageView from '../../views/ProfilePageView.js'; + +export default class Notice { + + /** + * @param {Client} client + */ + constructor(client) { + /** + * @type {Client} + */ + this.client = client; + this.url = location.origin.replace('http', 'ws') + '/ws/notice'; + } + + start() { + this._socket = new WebSocket(this.url); + + this._socket.onclose = _ => this._socket = undefined; + this._socket.onmessage = message => { + const data = JSON.parse(message.data); + console.log(data) + + if (data.type === 'friend_request') { + this.friend_request(data.author); + } else if (data.type === 'new_friend') { + this.new_friend(data.friend); + } else if (data.type === 'friend_removed') { + this.friend_removed(data.friend); + } else if (data.type === 'friend_request_canceled') { + this.friend_request_canceled(data.author); + } + }; + } + + stop() { + if (this._socket) { + this._socket.close(); + this._socket = undefined; + } + } + + friend_request(author) { + console.log('hey') + client.me.incomingFriendRequests.push(new Profile(author.username, author.id, author.avatar)); + createNotification('Friend Request', `${author.username} sent you a friend request.`); + if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) { + lastView.profile.hasIncomingRequest = true; + lastView.loadFriendshipStatus(); + } + } + + new_friend(friend) { + client.me.friendList.push(new Profile(friend.username, friend.id, friend.avatar)); + createNotification('New Friend', `${friend.username} accepted your friend request.`); + if (lastView instanceof ProfilePageView && lastView.profile.id === friend.id) { + lastView.profile.isFriend = true; + lastView.profile.hasIncomingRequest = false; + lastView.profile.hasOutgoingRequest = false; + lastView.loadFriendshipStatus(); + } + } + + friend_removed(exFriend) { + client.me.friendList = client.me.friendList.filter(friend => friend.id !== exFriend.id); + if (lastView instanceof ProfilePageView && lastView.profile.id === exFriend.id) { + lastView.profile.isFriend = false; + lastView.loadFriendshipStatus(); + } + } + + friend_request_canceled(author) { + client.me.incomingFriendRequests = client.me.incomingFriendRequests.filter(user => user.id !== author.id); + if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) { + lastView.profile.hasIncomingRequest = false; + lastView.loadFriendshipStatus(); + } + } +} diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 81a72fe..8b5c5e0 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -167,4 +167,4 @@ document.addEventListener("DOMContentLoaded", async () => { document.querySelector('a[href=\'' + location.pathname + '\']')?.classList.add('active'); }); -export { client, lang, navigateTo, reloadView }; +export { client, lang, lastView, navigateTo, reloadView }; diff --git a/frontend/static/js/utils/noticeUtils.js b/frontend/static/js/utils/noticeUtils.js index b604de1..f5e1eb7 100644 --- a/frontend/static/js/utils/noticeUtils.js +++ b/frontend/static/js/utils/noticeUtils.js @@ -1,22 +1,15 @@ -export function createNotification(text, timer = 3000) { +export function createNotification(title = 'New notification', content, delay = 3000) { - if (!createNotification.templateToast) { - createNotification.templateToast = new DOMParser().parseFromString(` - - `, 'text/html') - .querySelector('body') - .firstChild; - } - - const toastElement = createNotification.templateToast.cloneNode(true); - toastElement.getElementsByClassName('toast-body')[0].innerHTML = text; + const toastElement = document.createElement('div'); + toastElement.classList.add('toast'); + toastElement.role = 'alert'; + toastElement.setAttribute('data-bs-delay', delay); + toastElement.innerHTML = + `
+ ${title} + +
+
${content}
` toastElement.addEventListener('hidden.bs.toast', e => e.target.remove()); new bootstrap.Toast(toastElement).show(); diff --git a/frontend/static/js/views/ProfilePageView.js b/frontend/static/js/views/ProfilePageView.js index 65e94fa..63a2168 100644 --- a/frontend/static/js/views/ProfilePageView.js +++ b/frontend/static/js/views/ProfilePageView.js @@ -16,23 +16,15 @@ export default class extends AbstractView { if (!this.profile) return 404; + if (this.profile.id === client.me.id) + return; + const addFriendButton = document.getElementById('addFriendButton'), removeFriendButton = document.getElementById('removeFriendButton'), blockButton = document.getElementById('blockButton'), unblockButton = document.getElementById('unblockButton'); - if (this.profile.hasIncomingRequest) { - addFriendButton.classList.remove('d-none'); - addFriendButton.innerHTML = 'Accept Request'; - } else if (this.profile.hasOutgoingRequest) { - removeFriendButton.classList.remove('d-none'); - removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); - removeFriendButton.innerHTML = 'Cancel Request' - } else if (this.profile.isFriend) - removeFriendButton.classList.remove('d-none'); - else - addFriendButton.classList.remove('d-none'); - + this.loadFriendshipStatus(); if (this.profile.isBlocked) unblockButton.classList.remove('d-none'); else @@ -44,6 +36,31 @@ export default class extends AbstractView { blockButton.onclick = _ => this.blockUser(); } + loadFriendshipStatus() { + const addFriendButton = document.getElementById('addFriendButton'), + removeFriendButton = document.getElementById('removeFriendButton'); + + if (this.profile.hasIncomingRequest) { + removeFriendButton.classList.add('d-none'); + addFriendButton.classList.remove('d-none'); + addFriendButton.innerHTML = 'Accept Request'; + } else if (this.profile.hasOutgoingRequest) { + addFriendButton.classList.add('d-none'); + removeFriendButton.classList.remove('d-none'); + removeFriendButton.classList.replace('btn-danger', 'btn-secondary'); + removeFriendButton.innerHTML = 'Cancel Request'; + } else if (this.profile.isFriend) { + addFriendButton.classList.add('d-none'); + removeFriendButton.classList.remove('d-none'); + removeFriendButton.classList.replace('btn-secondary', 'btn-danger'); + removeFriendButton.innerHTML = 'Remove Friend'; + } else { + addFriendButton.innerHTML = 'Add Friend'; + removeFriendButton.classList.add('d-none'); + addFriendButton.classList.remove('d-none'); + } + } + async getHtml() { this.profile = await client.profiles.getProfile(this.username); diff --git a/frontend/static/js/views/accounts/LogoutView.js b/frontend/static/js/views/accounts/LogoutView.js index 18360a3..933dcb5 100644 --- a/frontend/static/js/views/accounts/LogoutView.js +++ b/frontend/static/js/views/accounts/LogoutView.js @@ -10,7 +10,6 @@ export default class extends AbstractAuthenticatedView async postInit() { await client.logout(); - await client.notice.disconnect(); navigateTo(this.lastPageUrl); } } diff --git a/frontend/templates/index.html b/frontend/templates/index.html index 8c9b1f5..96aeaa1 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -45,9 +45,8 @@ +
-
-
diff --git a/games/serializers.py b/games/serializers.py index b0338bb..746b02d 100644 --- a/games/serializers.py +++ b/games/serializers.py @@ -4,7 +4,7 @@ from django.contrib.auth.models import User from django.db.models import QuerySet from .models import GameModel -from profiles.serializers.ProfileSerializer import ProfileSerializer +from profiles.serializers import ProfileSerializer class GameSerializer(serializers.ModelSerializer): @@ -29,4 +29,4 @@ class GameSerializer(serializers.ModelSerializer): return "waiting" def get_players(self, instance: GameModel): - return ProfileSerializer(instance.get_players_profiles(), many=True).data \ No newline at end of file + return ProfileSerializer(instance.get_players_profiles(), many=True).data diff --git a/notice/__init__.py b/notice/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notice/admin.py b/notice/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/notice/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/notice/apps.py b/notice/apps.py new file mode 100644 index 0000000..f945881 --- /dev/null +++ b/notice/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class NoticeConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'notice' diff --git a/notice/consumers.py b/notice/consumers.py new file mode 100644 index 0000000..02aca83 --- /dev/null +++ b/notice/consumers.py @@ -0,0 +1,68 @@ +from __future__ import annotations +import json + +from channels.generic.websocket import WebsocketConsumer +from django.contrib.auth.models import User + +from profiles.serializers import ProfileSerializer +from profiles.models import ProfileModel +from .models import NoticeModel + + +class NoticeManager: + def __init__(self): + self._list: list[NoticeConsumer] = [] + + def add(self, consumer: NoticeConsumer): + self._list.append(consumer) + + unsend_notices = NoticeModel.objects.filter(user=consumer.user) + for notice in unsend_notices: + self.notify_user(consumer.user, json_data=notice.data) + notice.delete() + + def remove(self, consumer: NoticeConsumer): + self._list.remove(consumer) + + def get_consumer_by_user(self, user: User): + for consumer in self._list: + if consumer.user == user: + return consumer + + def notify_user(self, user: User, data: dict = None, json_data: str = None): + consumer = self.get_consumer_by_user(user) + data_str: str = json.dumps(data) if json_data is None else json_data + if consumer: + consumer.send(data_str) + else: + NoticeModel(user=user, data=data_str).save() + + def notify_friend_request(self, user: User, friend: ProfileModel): + self.notify_user(user, {'type': 'friend_request', 'author': ProfileSerializer(friend).data}) + + def notify_friend_request_canceled(self, user: User, friend: ProfileModel): + self.notify_user(user, {'type': 'friend_request_canceled', 'author': ProfileSerializer(friend).data}) + + def notify_new_friend(self, user: User, friend: ProfileModel): + self.notify_user(user, {'type': 'new_friend', 'friend': ProfileSerializer(friend).data}) + + def notify_friend_removed(self, user: User, friend: ProfileModel): + self.notify_user(user, {'type': 'friend_removed', 'friend': ProfileSerializer(friend).data}) + + +notice_manager = NoticeManager() + + +class NoticeConsumer(WebsocketConsumer): + def connect(self): + self.user: User = self.scope['user'] + if not self.user.is_authenticated: + self.close() + return + + self.accept() + notice_manager.add(self) + + def disconnect(self, code): + notice_manager.remove(self) + super().disconnect(code) diff --git a/notice/models.py b/notice/models.py new file mode 100644 index 0000000..cf26f6a --- /dev/null +++ b/notice/models.py @@ -0,0 +1,7 @@ +from django.db.models import Model, ForeignKey, CharField, CASCADE +from django.contrib.auth.models import User + + +class NoticeModel(Model): + user = ForeignKey(User, on_delete=CASCADE) + data = CharField(max_length=100) diff --git a/notice/routing.py b/notice/routing.py new file mode 100644 index 0000000..2e3460a --- /dev/null +++ b/notice/routing.py @@ -0,0 +1,7 @@ +from django.urls import re_path + +from .consumers import NoticeConsumer + +websocket_urlpatterns = [ + re_path(r'ws/notice$', NoticeConsumer.as_asgi()), +] diff --git a/notice/tests.py b/notice/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/notice/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/notice/views.py b/notice/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/notice/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/profiles/serializers/ProfileSerializer.py b/profiles/serializers.py similarity index 90% rename from profiles/serializers/ProfileSerializer.py rename to profiles/serializers.py index fffa21e..972c3d8 100644 --- a/profiles/serializers/ProfileSerializer.py +++ b/profiles/serializers.py @@ -3,7 +3,7 @@ from django.utils.translation import gettext as _ from rest_framework import serializers -from ..models import ProfileModel +from .models import ProfileModel class ProfileSerializer(serializers.ModelSerializer): @@ -13,7 +13,7 @@ class ProfileSerializer(serializers.ModelSerializer): class Meta: model = ProfileModel - fields = ["username", "avatar", "user_id"] + fields = ["username", "avatar", "id"] def validate_avatar(self, value): ''' diff --git a/profiles/views/blocks.py b/profiles/views/blocks.py index 6907f1c..4d37471 100644 --- a/profiles/views/blocks.py +++ b/profiles/views/blocks.py @@ -8,7 +8,7 @@ from django.utils.translation import gettext as _ from django.shortcuts import get_object_or_404 from ..models import BlockModel, ProfileModel -from ..serializers.ProfileSerializer import ProfileSerializer +from ..serializers import ProfileSerializer class GetBlocksView(APIView): diff --git a/profiles/views/friends.py b/profiles/views/friends.py index 125b061..a2ea4f9 100644 --- a/profiles/views/friends.py +++ b/profiles/views/friends.py @@ -7,7 +7,8 @@ from django.utils.translation import gettext as _ from django.shortcuts import get_object_or_404 from ..models import ProfileModel, FriendRequestModel -from ..serializers.ProfileSerializer import ProfileSerializer +from ..serializers import ProfileSerializer +from notice.consumers import notice_manager class GetFriendsView(APIView): @@ -41,9 +42,11 @@ class EditFriendView(APIView): incoming_request = user_profile.get_received_friend_request_from(friend_profile) if incoming_request: incoming_request.accept() - return Response(_('Friendship succssfully created.'), status.HTTP_201_CREATED) + notice_manager.notify_new_friend(friend_profile.user, user_profile) + return Response(_('Friendship successfully created.'), status.HTTP_201_CREATED) FriendRequestModel(author=user_profile, target=friend_profile).save() + notice_manager.notify_friend_request(friend_profile.user, user_profile) return Response(_('Friend request sent.'), status.HTTP_200_OK) def delete(self, request, pk=None): @@ -53,13 +56,15 @@ class EditFriendView(APIView): outgoing_request = user_profile.get_outgoing_friend_request_to(friend_profile) if outgoing_request: outgoing_request.delete() + notice_manager.notify_friend_request_canceled(friend_profile.user, user_profile) return Response(_('Friend request cancelled.')) if not user_profile.is_friend(friend_profile): return Response(_('You are not friend with this user.'), status.HTTP_400_BAD_REQUEST) user_profile.delete_friend(friend_profile) - return Response(_('Friendship succssfully deleted.'), status.HTTP_201_CREATED) + notice_manager.notify_friend_removed(friend_profile.user, user_profile) + return Response(_('Friendship successfully deleted.'), status.HTTP_201_CREATED) class GetIncomingFriendRequestView(APIView): diff --git a/profiles/viewsets/MyProfileViewSet.py b/profiles/viewsets/MyProfileViewSet.py index fbc034a..97a7b6a 100644 --- a/profiles/viewsets/MyProfileViewSet.py +++ b/profiles/viewsets/MyProfileViewSet.py @@ -3,7 +3,7 @@ from rest_framework import viewsets from rest_framework.response import Response from rest_framework.authentication import SessionAuthentication -from ..serializers.ProfileSerializer import ProfileSerializer +from ..serializers import ProfileSerializer from ..models import ProfileModel diff --git a/profiles/viewsets/ProfileViewSet.py b/profiles/viewsets/ProfileViewSet.py index b476d71..2aa0aa0 100644 --- a/profiles/viewsets/ProfileViewSet.py +++ b/profiles/viewsets/ProfileViewSet.py @@ -5,7 +5,7 @@ from rest_framework import permissions from rest_framework import viewsets from rest_framework.response import Response -from ..serializers.ProfileSerializer import ProfileSerializer +from ..serializers import ProfileSerializer from ..models import ProfileModel diff --git a/tournament/consumers.py b/tournament/consumers.py index fe52b90..a26a89a 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -9,7 +9,7 @@ from django.utils.translation import gettext as _ from games.models import GameModel from games.serializers import GameSerializer from profiles.models import ProfileModel -from profiles.serializers.ProfileSerializer import ProfileSerializer +from profiles.serializers import ProfileSerializer from .models import TournamentModel import json @@ -228,4 +228,4 @@ class TournamentWebConsumer(WebsocketConsumer): self.room.leave(self.member) - super().disconnect(close_code) # proutman à encore frappé \ No newline at end of file + super().disconnect(close_code) # proutman à encore frappé diff --git a/tournament/serializers.py b/tournament/serializers.py index 012aab6..82e12b3 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -2,7 +2,7 @@ from rest_framework import serializers from .models import TournamentModel, TournamentGameModel -from profiles.serializers.ProfileSerializer import ProfileSerializer +from profiles.serializers import ProfileSerializer from games.serializers import GameSerializer nb_participants = [2 ** i for i in range(2, 6)] @@ -58,4 +58,4 @@ class TournamentGameSerializer(serializers.ModelSerializer): return "waiting" def get_players(self, instance: TournamentGameModel): - return ProfileSerializer(instance.get_players_profiles(), many=True).data \ No newline at end of file + return ProfileSerializer(instance.get_players_profiles(), many=True).data diff --git a/transcendence/asgi.py b/transcendence/asgi.py index 8d907a4..11ca834 100644 --- a/transcendence/asgi.py +++ b/transcendence/asgi.py @@ -15,20 +15,21 @@ import chat.routing import matchmaking.routing import tournament.routing import games.routing +import notice.routing from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings') application = ProtocolTypeRouter({ - 'http':get_asgi_application(), - 'websocket':AuthMiddlewareStack( + 'http': get_asgi_application(), + 'websocket': AuthMiddlewareStack( URLRouter( chat.routing.websocket_urlpatterns + matchmaking.routing.websocket_urlpatterns + tournament.routing.websocket_urlpatterns + - games.routing.websocket_urlpatterns + games.routing.websocket_urlpatterns + + notice.routing.websocket_urlpatterns ) ) }) - diff --git a/transcendence/settings.py b/transcendence/settings.py index 8d55720..6e7f774 100644 --- a/transcendence/settings.py +++ b/transcendence/settings.py @@ -51,6 +51,7 @@ INSTALLED_APPS = [ 'profiles.apps.ProfilesConfig', 'frontend.apps.FrontendConfig', 'chat.apps.ChatConfig', + 'notice.apps.NoticeConfig', 'corsheaders', 'rest_framework', From c4407adffbf9af3fd6b98ecec518596e0ba599bd Mon Sep 17 00:00:00 2001 From: AdrienLSH Date: Thu, 25 Apr 2024 15:48:26 +0200 Subject: [PATCH 13/16] notice: forgot some logs mb --- frontend/static/js/api/notice/Notice.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend/static/js/api/notice/Notice.js b/frontend/static/js/api/notice/Notice.js index e2407e4..f0db7e1 100644 --- a/frontend/static/js/api/notice/Notice.js +++ b/frontend/static/js/api/notice/Notice.js @@ -23,7 +23,6 @@ export default class Notice { this._socket.onclose = _ => this._socket = undefined; this._socket.onmessage = message => { const data = JSON.parse(message.data); - console.log(data) if (data.type === 'friend_request') { this.friend_request(data.author); @@ -45,7 +44,6 @@ export default class Notice { } friend_request(author) { - console.log('hey') client.me.incomingFriendRequests.push(new Profile(author.username, author.id, author.avatar)); createNotification('Friend Request', `${author.username} sent you a friend request.`); if (lastView instanceof ProfilePageView && lastView.profile.id === author.id) { From ff230bccf60c3116037ef4bda4a5ce70bd8cce0b Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 25 Apr 2024 16:11:27 +0200 Subject: [PATCH 14/16] tournament: use tournamentGameModel instead gameModel --- tournament/consumers.py | 30 +++++++++++++++++------------- tournament/models.py | 19 +++++++++++++++---- tournament/serializers.py | 4 +++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/tournament/consumers.py b/tournament/consumers.py index a26a89a..7d15241 100644 --- a/tournament/consumers.py +++ b/tournament/consumers.py @@ -6,12 +6,13 @@ from django.contrib.auth.models import User from django.db.models import QuerySet from django.utils.translation import gettext as _ -from games.models import GameModel -from games.serializers import GameSerializer from profiles.models import ProfileModel from profiles.serializers import ProfileSerializer from .models import TournamentModel +from .models import TournamentGameModel +from .serializers import TournamentGameSerializer + import json class TournamentMember: @@ -33,7 +34,7 @@ class TournamentMember: self.send("error", data_to_send) - def send_goto(self, game: GameModel): + def send_goto(self, game: TournamentGameModel): self.send("go_to", {"game_id": game.pk}) def _receive_participating(self, data: dict) -> None: @@ -84,7 +85,7 @@ class TournamentRoom: self._room_manager: TournamentRoomManager = room_manager self._member_list: set[TournamentMember] = set() self._model: TournamentModel = tournament - self._game_in_progress_list: set[GameModel] = set() + self._game_in_progress_list: set[TournamentGameModel] = set() self._current_round = 0 def join(self, socket: TournamentWebConsumer) -> TournamentMember: @@ -94,17 +95,20 @@ class TournamentRoom: return member - def set_game_as_finished(self, game: GameModel): + def set_game_as_finished(self, game: TournamentGameModel): + self._game_in_progress_list.remove(game) - data: dict = GameSerializer(game).data - - data.update({"round": self._current_round}) - - self.broadcast("game_update", data) + self.broadcast("game_update", TournamentGameSerializer(game).data) if len(self._game_in_progress_list) == 0: self._round_finished() + + def set_game_as_started(self, game: TournamentGameModel): + + self._game_in_progress_list.add(game) + + self.broadcast("game_update", TournamentGameSerializer(game).data) def _finish(self, winner: User): self._model.finish(winner) @@ -112,7 +116,7 @@ class TournamentRoom: def _round_finished(self): if self._current_round == self._model.round: - last_game: GameModel = self._model.get_games_by_round(self._current_round)[0] + last_game: TournamentGameSerializer = self._model.get_games_by_round(self._current_round)[0] self._finish(last_game.winner) return @@ -124,9 +128,9 @@ class TournamentRoom: participant_list: set[User] = self._model.get_participants_by_round(self._current_round) - self._game_in_progress_list = self._model.create_round(participant_list, self._current_round) + game_list = self._model.create_round(participant_list, self._current_round) - for game in self._game_in_progress_list: + for game in game_list: for player in game.get_players(): participant: TournamentMember = self.get_participant_by_profile(player) participant.send_goto(game) diff --git a/tournament/models.py b/tournament/models.py index cabc76d..42ce432 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -1,4 +1,5 @@ from __future__ import annotations +from typing import Any from games.models import GameModel @@ -96,10 +97,20 @@ class TournamentGameModel(GameModel): round = models.IntegerField() pos = models.IntegerField() + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + + from .consumers import tournament_manager + self.room = tournament_manager.get(self.tournament) + + + def start(self): + super().start() + + self.room.set_game_as_started(self) + + def finish(self, winner_id): super().finish(winner_id) - from .consumers import tournament_manager - - room = tournament_manager.get(self.tournament) - room.set_game_as_finished(self) \ No newline at end of file + self.room.set_game_as_finished(self) \ No newline at end of file diff --git a/tournament/serializers.py b/tournament/serializers.py index 82e12b3..d5c8de9 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -45,10 +45,12 @@ class TournamentGameSerializer(serializers.ModelSerializer): start_timestamp = serializers.ReadOnlyField() stop_timestamp = serializers.ReadOnlyField() gamemode = serializers.ReadOnlyField() + round = serializers.ReadOnlyField() + pos = serializers.ReadOnlyField() class Meta: model = TournamentGameModel - fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type"] + fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "game_type", "round", "pos"] def get_state(self, instance: TournamentGameModel): if (instance.finished): From 8c22dd28fb67ef0f82778c73632a445c22919569 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 25 Apr 2024 16:25:30 +0200 Subject: [PATCH 15/16] notice: update run.sh --- run.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/run.sh b/run.sh index 66e42ce..b8fbb27 100755 --- a/run.sh +++ b/run.sh @@ -7,6 +7,7 @@ python manage.py makemigrations games python manage.py makemigrations profiles python manage.py makemigrations chat python manage.py makemigrations tournament +python manage.py makemigrations notice python manage.py migrate python manage.py compilemessages python manage.py runserver 0.0.0.0:8000 From 55d29d5763f22f136d2b4bae6464463acb514a5c Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 25 Apr 2024 16:44:36 +0200 Subject: [PATCH 16/16] tournament: fix: serializer --- tournament/models.py | 2 +- tournament/serializers.py | 4 ++-- tournament/viewset.py | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tournament/models.py b/tournament/models.py index 42ce432..fd9199e 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -65,7 +65,7 @@ class TournamentModel(models.Model): return game def get_games(self) -> set[GameModel]: - return {games for games in self.get_games_by_round(i for i in range(1, self.round))} + return [{games for games in self.get_games_by_round(i)} for i in range(1, self.round)] def get_games_by_round(self, round: int) -> set[GameModel]: return {tournament_game for tournament_game in TournamentGameModel.objects.filter(tournament=self, round=round)} diff --git a/tournament/serializers.py b/tournament/serializers.py index d5c8de9..cb84e87 100644 --- a/tournament/serializers.py +++ b/tournament/serializers.py @@ -5,7 +5,7 @@ from .models import TournamentModel, TournamentGameModel from profiles.serializers import ProfileSerializer from games.serializers import GameSerializer -nb_participants = [2 ** i for i in range(2, 6)] +nb_participants = [2 ** i for i in range(1, 6)] class TournamentSerializer(serializers.ModelSerializer): @@ -25,7 +25,7 @@ class TournamentSerializer(serializers.ModelSerializer): return ProfileSerializer(instance.get_participants(), many=True).data def get_games(self, instance: TournamentModel): - return GameSerializer(instance.get_games(), many=True).data + return [GameSerializer(games, many=True).data for games in instance.get_games()] def get_state(self, instance: TournamentModel): return ["waiting", "started", "finished"][instance.started + instance.finished] diff --git a/tournament/viewset.py b/tournament/viewset.py index dcec5a4..a2957bb 100644 --- a/tournament/viewset.py +++ b/tournament/viewset.py @@ -10,6 +10,8 @@ from django.db.models import QuerySet from .models import TournamentModel from .serializers import TournamentSerializer +import math + # Create your views here. class TournamentViewSet(viewsets.ModelViewSet): @@ -19,7 +21,7 @@ class TournamentViewSet(viewsets.ModelViewSet): authentication_classes = (SessionAuthentication,) def perform_create(self, serializer: TournamentSerializer): - tournament = serializer.save(round=1) + tournament = serializer.save(round=math.log2(serializer.validated_data['nb_participants']) + 1) return Response(self.serializer_class(tournament).data, status=status.HTTP_201_CREATED)