feat: Right to disconnect

parent 98e612aa
...@@ -87,7 +87,7 @@ export default { ...@@ -87,7 +87,7 @@ export default {
}, },
currentCards() { currentCards() {
let currentState = this.socketMessage; let currentState = this.socketMessage;
console.log("curCards! state now:", currentState); console.log("curCards! Last message:", JSON.stringify(currentState));
if (currentState["data"]) { if (currentState["data"]) {
if (currentState["data"]["cards"]) { if (currentState["data"]["cards"]) {
console.log("Setting cards..."); console.log("Setting cards...");
......
...@@ -66,7 +66,7 @@ export const store = new Vuex.Store({ ...@@ -66,7 +66,7 @@ export const store = new Vuex.Store({
state.text = text; state.text = text;
} }
if (extras) { if (extras) {
console.log("Extras: ", extras) console.log("Extras: ", extras);
let name = extras.name; let name = extras.name;
if (name) { if (name) {
console.log("Setting name:", name); console.log("Setting name:", name);
......
import asyncio import asyncio
import json import json
from sys import stderr
from typing import List, Dict, Optional from typing import List, Dict, Optional
from pydantic.main import BaseModel from pydantic.main import BaseModel
...@@ -39,12 +40,27 @@ class LobbyManager(ClientManager): ...@@ -39,12 +40,27 @@ class LobbyManager(ClientManager):
return [self.lobby[k] for k, m in self.metadata.items() if m.ready] return [self.lobby[k] for k, m in self.metadata.items() if m.ready]
async def new_player(self, player: Player, sid: str) -> None: async def new_player(self, player: Player, sid: str) -> None:
""" Registers a new player. """
self.lobby[player.name] = player self.lobby[player.name] = player
self.players.append(player) self.players.append(player)
self.metadata[player.name] = Metadata(sid=sid) self.metadata[player.name] = Metadata(sid=sid)
print(f"Added {player} to a lobby with {len(self.lobby)} players.") print(f"Added {player} to a lobby with {len(self.lobby)} players.")
await self.send(to=player, message=MessageToPlayer.Welcome, extras={"name": player.name}) 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): def wants_to_play(self, player: Player):
""" """
Notes that a player wants to play, starting a game if others too. Notes that a player wants to play, starting a game if others too.
......
...@@ -15,6 +15,7 @@ class MessageToPlayer(Enum): ...@@ -15,6 +15,7 @@ class MessageToPlayer(Enum):
WinnerIs = "WINNER_IS" WinnerIs = "WINNER_IS"
Lose = "LOSER" Lose = "LOSER"
Reset = "RESET" Reset = "RESET"
Disconnected = "DISCONNECTED"
class MessageFromPlayer(Enum): class MessageFromPlayer(Enum):
......
...@@ -55,5 +55,6 @@ async def ping_server(sid, data): ...@@ -55,5 +55,6 @@ async def ping_server(sid, data):
@sio.event @sio.event
def disconnect(sid): async def disconnect(sid):
print('[WS] Disconnect ', sid) print('[WS] Disconnect ', sid)
await lobby.player_left(sid)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment