diff --git a/chat/consumersNotice.py b/chat/consumersNotice.py index 1070c8e..c1bbe29 100644 --- a/chat/consumersNotice.py +++ b/chat/consumersNotice.py @@ -210,7 +210,7 @@ class ChatNoticeConsumer(WebsocketConsumer): if (targets[0] in self.channel_layer.invite[user.pk]): self.channel_layer.invite[user.pk].remove(targets[0]) - id_game = GameModel().create([user.pk, targets[0]]); + id_game = GameModel().create("pong", [user.pk, targets[0]]); return 200, id_game diff --git a/frontend/static/js/api/game/Game.js b/frontend/static/js/api/game/Game.js index 89fbd70..d260904 100644 --- a/frontend/static/js/api/game/Game.js +++ b/frontend/static/js/api/game/Game.js @@ -21,8 +21,9 @@ class Game * @param {Boolean} started * @param {Number} winner_id * @param {String} state + * @param {String} gamemode */ - constructor(client, id, disconnect_handler, goal_handler, finish_handler, winner_id, state, started, finished, players_data, start_timestamp, stop_timestamp) + constructor(client, id, disconnect_handler, goal_handler, finish_handler, winner_id, state, started, finished, players_data, start_timestamp, stop_timestamp, gamemode) { /** * @type {Client} @@ -94,6 +95,11 @@ class Game ) ); }); + + /** + * @type {String} + */ + this.gamemode = gamemode; } /** diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index 1297845..349c8a9 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -92,8 +92,7 @@ const router = async(uri) => { { path: "/matchmaking", view: MatchMakingView }, { path: "/games/offline", view: GameOfflineView }, { path: "/tictactoe", view: TicTacToeView }, - { path: "/games/:id/0", view: GameView2D }, - { path: "/games/:id/1", view: GameView3D }, + { path: "/games/pong/:id", view: GameView2D }, { path: "/games/tictactoe/:id", view: TicTacToeOnlineView }, ]; diff --git a/games/models.py b/games/models.py index 519dadb..2e93d61 100644 --- a/games/models.py +++ b/games/models.py @@ -15,8 +15,10 @@ class GameModel(models.Model): winner_id = models.IntegerField(default = -1) start_timestamp = models.BigIntegerField(null = True, blank = True) stop_timestamp = models.BigIntegerField(null = True, blank = True) + gamemode = models.CharField(max_length = 60, default = "pong") - def create(self, players_id: [int]): + def create(self, gamemode: str, players_id: [int]): + self.gamemode = gamemode self.save() for player_id in players_id: GameMembersModel(game_id = self.pk, player_id = player_id).save() diff --git a/games/serializers.py b/games/serializers.py index a4ae11b..6321d65 100644 --- a/games/serializers.py +++ b/games/serializers.py @@ -14,10 +14,11 @@ class GameSerializer(serializers.ModelSerializer): finished = serializers.ReadOnlyField() start_timestamp = serializers.ReadOnlyField() stop_timestamp = serializers.ReadOnlyField() + gamemode = serializers.ReadOnlyField() class Meta: model = GameModel - fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp"] + fields = ["id", "winner_id", "state", "started", "finished", "players", "start_timestamp", "stop_timestamp", "gamemode"] def get_state(self, instance: GameModel): if (instance.finished): diff --git a/matchmaking/consumers.py b/matchmaking/consumers.py index f66a9db..80bceeb 100644 --- a/matchmaking/consumers.py +++ b/matchmaking/consumers.py @@ -39,9 +39,9 @@ class MatchMaking(WebsocketConsumer): self.disconnect(1000) return - if (self.mode < 2 or self.mode > 4): + if (self.gamemode not in ["tictactoe", "pong"]): data: dict = { - "detail": "The mode must be > 1 and < 4.", + "detail": "The gamemode must 'pong' or 'tictactoe'.", } self.send(json.dumps(data)) self.disconnect(1000) @@ -49,8 +49,8 @@ class MatchMaking(WebsocketConsumer): waiting_room.broadcast(f"{len(waiting_room)} / {waiting_room.mode}") if (len(waiting_room) == waiting_room.mode): - game_id: int = GameModel().create(waiting_room.get_users_id()) - waiting_room.broadcast("game_found", {"game_id": game_id}) + 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() def disconnect(self, close_code): diff --git a/tournament/models.py b/tournament/models.py index 87eb9da..d018193 100644 --- a/tournament/models.py +++ b/tournament/models.py @@ -19,9 +19,10 @@ class TournamentModel(models.Model): level = models.IntegerField() started = models.BooleanField(default = False) finished = models.BooleanField(default = False) + gamemode = models.CharField(max_length = 50, default = "pong") def create_game(self, level, players_id): - game_id = GameModel().create(players_id = players_id) + game_id = GameModel().create(self.gamemode, players_id = players_id) TournamentGamesModel(game_id = game_id, tournament_id = self.pk, tournament_level = level).save() return game_id