From cf9cbaf9b06ea440d2dd6f6d4528caec291a5e16 Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sun, 3 May 2020 13:23:06 +0200 Subject: [PATCH] feat: Right to disconnect --- client/src/App.vue | 2 +- client/src/vuex-store.js | 2 +- server/game/lobby.py | 16 ++++++++++++++++ server/game/message.py | 1 + server/ws.py | 3 ++- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/client/src/App.vue b/client/src/App.vue index adaf4ce..3664389 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -87,7 +87,7 @@ export default { }, currentCards() { let currentState = this.socketMessage; - console.log("curCards! state now:", currentState); + console.log("curCards! Last message:", JSON.stringify(currentState)); if (currentState["data"]) { if (currentState["data"]["cards"]) { console.log("Setting cards..."); diff --git a/client/src/vuex-store.js b/client/src/vuex-store.js index 789410b..360ac45 100644 --- a/client/src/vuex-store.js +++ b/client/src/vuex-store.js @@ -66,7 +66,7 @@ export const store = new Vuex.Store({ state.text = text; } if (extras) { - console.log("Extras: ", extras) + console.log("Extras: ", extras); let name = extras.name; if (name) { console.log("Setting name:", name); diff --git a/server/game/lobby.py b/server/game/lobby.py index 15f93cc..2f49b3f 100644 --- a/server/game/lobby.py +++ b/server/game/lobby.py @@ -1,5 +1,6 @@ import asyncio import json +from sys import stderr from typing import List, Dict, Optional from pydantic.main import BaseModel @@ -39,12 +40,27 @@ class LobbyManager(ClientManager): return [self.lobby[k] for k, m in self.metadata.items() if m.ready] async def new_player(self, player: Player, sid: str) -> None: + """ Registers a new player. """ self.lobby[player.name] = player self.players.append(player) self.metadata[player.name] = Metadata(sid=sid) print(f"Added {player} to a lobby with {len(self.lobby)} players.") await self.send(to=player, message=MessageToPlayer.Welcome, extras={"name": player.name}) + async def player_left(self, sid: str) -> None: + """ Reports the disconnection of a player, both internally and to other players.""" + + for name, meta in self.metadata.items(): + if meta.sid == sid: + print(f"Player {name} disconnected.") + player = self.lobby.pop(name) + self.players.remove(player) + + for p in self.players: + await self.send(p, MessageToPlayer.Disconnected, extras={"justLeft": name}) + return + print(f"No player for sid {sid}...", file=stderr) + def wants_to_play(self, player: Player): """ Notes that a player wants to play, starting a game if others too. diff --git a/server/game/message.py b/server/game/message.py index f5b42ad..57678c1 100644 --- a/server/game/message.py +++ b/server/game/message.py @@ -15,6 +15,7 @@ class MessageToPlayer(Enum): WinnerIs = "WINNER_IS" Lose = "LOSER" Reset = "RESET" + Disconnected = "DISCONNECTED" class MessageFromPlayer(Enum): diff --git a/server/ws.py b/server/ws.py index ecd57d3..df68865 100644 --- a/server/ws.py +++ b/server/ws.py @@ -55,5 +55,6 @@ async def ping_server(sid, data): @sio.event -def disconnect(sid): +async def disconnect(sid): print('[WS] Disconnect ', sid) + await lobby.player_left(sid) -- libgit2 0.27.0