fix multiple clients bug (#16)
* switch to relative imports for DIY files * fix player instance always belonging to last joined client
This commit is contained in:
1 parent
2b1bb586c9
commit
fb74527e82
1 file changed
+17
-9
+17
-9
@@ -9,7 +9,7 @@ T_PlayerStateDict = dict[str, T_PlayerDict | bool]
|
||||
T_SpectatorStateDict = dict[str, T_PlayerDict]
|
||||
|
||||
MAX_PLAYERS = 2
|
||||
PLAYERS: set[Player] = set()
|
||||
PLAYERS: list[Player] = []
|
||||
|
||||
|
||||
def num_players() -> int:
|
||||
@@ -62,11 +62,6 @@ async def broadcast(message):
|
||||
|
||||
|
||||
async def handler(websocket: ServerConnection):
|
||||
# client_id = next(CLIENT_IDS)
|
||||
# CLIENTS.add(Client(websocket))
|
||||
|
||||
player: Player | None = None
|
||||
|
||||
try:
|
||||
async for msg in websocket:
|
||||
raw_action = json.loads(msg)
|
||||
@@ -75,7 +70,7 @@ async def handler(websocket: ServerConnection):
|
||||
if raw_action["action"] == "join":
|
||||
name = raw_action["name"]
|
||||
player = Player(websocket, name, is_spectator())
|
||||
PLAYERS.add(player)
|
||||
PLAYERS.append(player)
|
||||
|
||||
await player.send_message({"player": player.as_dict()})
|
||||
|
||||
@@ -97,8 +92,21 @@ async def handler(websocket: ServerConnection):
|
||||
await player.send_message({"player": player.as_dict()})
|
||||
|
||||
else:
|
||||
if player is None:
|
||||
raise Exception("Player has not joined.")
|
||||
query = list(
|
||||
filter(
|
||||
lambda player: player.websocket is not None
|
||||
and player.websocket.id == websocket.id,
|
||||
PLAYERS,
|
||||
)
|
||||
)
|
||||
|
||||
if len(query) == 0:
|
||||
raise Exception("Player did not join yet.")
|
||||
|
||||
player = query[0]
|
||||
|
||||
if player.is_disconnected:
|
||||
raise Exception("Player is not connected.")
|
||||
|
||||
if raw_action["action"] == "toggleRole":
|
||||
if player.is_spectator:
|
||||
|
||||
Reference in new issue
Block a user