feat: Right to disconnect

parent 98e612aa
......@@ -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...");
......
......@@ -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);
......
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.
......
......@@ -15,6 +15,7 @@ class MessageToPlayer(Enum):
WinnerIs = "WINNER_IS"
Lose = "LOSER"
Reset = "RESET"
Disconnected = "DISCONNECTED"
class MessageFromPlayer(Enum):
......
......@@ -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)
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