fix(Game): Unfreshen announce, better extras, reset ready when done playing

parent 6e8d3d76
import asyncio import asyncio
import json import json
import sys
from sys import stderr from sys import stderr
from typing import List, Dict, Optional from typing import List, Dict, Optional
...@@ -91,13 +92,14 @@ class LobbyManager(ClientManager): ...@@ -91,13 +92,14 @@ class LobbyManager(ClientManager):
game = Game(players, manager=self) game = Game(players, manager=self)
self.games.append(game) self.games.append(game)
print(f"Game started : {' vs '.join([p.name for p in players])}")
winner = await game.new_game()
print(f"Game ended : winner is {winner.name}! {len(players)} players back waiting in lobby.")
for p in players: for p in players:
self.metadata[p.name].ready = False self.metadata[p.name].ready = False
print(f"Game started : {' vs '.join([p.name for p in players])}") async def reset(self) -> str:
await game.new_game()
async def reset(self):
players = len(self.players) players = len(self.players)
games = len(self.games) games = len(self.games)
msg = f"Resetting! sorry for the {players} players / {games} games..." msg = f"Resetting! sorry for the {players} players / {games} games..."
...@@ -110,12 +112,18 @@ class LobbyManager(ClientManager): ...@@ -110,12 +112,18 @@ class LobbyManager(ClientManager):
self.players.clear() self.players.clear()
self.metadata.clear() self.metadata.clear()
self.games.clear() self.games.clear()
print(f"Reset done, {players} players, {games} games.")
return msg return msg
async def send(self, to: Player, message: MessageToPlayer, extras=None): async def send(self, to: Player, message: MessageToPlayer, extras=None) -> None:
sid = self.metadata[to.name].sid sid: str = "?"
game = self.game_with(to)
try:
sid = self.metadata[to.name].sid
except KeyError as e:
print(f"Error: Cannot send message to player {to.name}", file=stderr)
return # TODO: Warn other players and kill game
game: Game = self.game_with(to)
data = to.hand.json() # Start with "cards": [] data = to.hand.json() # Start with "cards": []
data = json.loads(data) # JSONEncode and decode, starting with dict would miss `json()` encoders data = json.loads(data) # JSONEncode and decode, starting with dict would miss `json()` encoders
......
...@@ -9,6 +9,7 @@ class MessageToPlayer(Enum): ...@@ -9,6 +9,7 @@ class MessageToPlayer(Enum):
GiveHand = "GIVE_HAND" GiveHand = "GIVE_HAND"
WaitTurn = "WAITING_TURN" WaitTurn = "WAITING_TURN"
YourTurn = "YOUR_TURN" YourTurn = "YOUR_TURN"
WrongAnnounce = "WRONG_ANNOUNCE"
Announce = "ANNOUNCE" Announce = "ANNOUNCE"
LoseRound = "LOSE_ROUND" LoseRound = "LOSE_ROUND"
Win = "WINNER" Win = "WINNER"
......
import itertools import itertools
from collections import defaultdict from collections import defaultdict
from time import sleep
from typing import List, Dict, Optional from typing import List, Dict, Optional
from server.game.manager import ClientManager from server.game.manager import ClientManager
...@@ -57,8 +58,9 @@ class Game: ...@@ -57,8 +58,9 @@ class Game:
winner = self.players[0] winner = self.players[0]
await self.message(MessageToPlayer.Win, winner) await self.message(MessageToPlayer.Win, winner)
await self.message(MessageToPlayer.WinnerIs, extras=winner) await self.message(MessageToPlayer.WinnerIs, extras={"winner": winner.name})
print(f"Game over - {winner.name} wins with {len(winner.hand)} cards!") print(f"Game over - {winner.name} wins with {len(winner.hand)} cards!")
return winner
async def new_turn(self) -> Player: async def new_turn(self) -> Player:
""" """
...@@ -149,7 +151,8 @@ class Game: ...@@ -149,7 +151,8 @@ class Game:
if not self.current_bet: # First player, has to bet something if not self.current_bet: # First player, has to bet something
print("Game: First awaiting current bet") print("Game: First awaiting current bet")
await self.message(MessageToPlayer.YourTurn, current_player, extras=self.current_bet) message = MessageToPlayer.YourTurn
await self.message(message, current_player, extras=self.current_bet)
while not self.current_bet: # Ask a valid bet while not self.current_bet: # Ask a valid bet
print("Game: While no bet, awaiting") print("Game: While no bet, awaiting")
announce = await current_player.announce(self.current_bet) announce = await current_player.announce(self.current_bet)
...@@ -161,6 +164,8 @@ class Game: ...@@ -161,6 +164,8 @@ class Game:
extras={"player": current_player.name, "announce": announce.json()}) extras={"player": current_player.name, "announce": announce.json()})
else: else:
print(f"You cannot say Menteur on first round, {current_player}!") print(f"You cannot say Menteur on first round, {current_player}!")
message = MessageToPlayer.WrongAnnounce
sleep(5)
else: # Next player, announce or menteur else: # Next player, announce or menteur
...@@ -170,7 +175,7 @@ class Game: ...@@ -170,7 +175,7 @@ class Game:
announce = Announce() # MENTEUR obligatoire announce = Announce() # MENTEUR obligatoire
else: else:
print("Game: Awaiting bet") print("Game: Awaiting bet")
await self.message(MessageToPlayer.YourTurn, current_player, extras=self.current_bet) await self.message(message, current_player, extras=self.current_bet)
announce = await current_player.announce(self.current_bet) announce = await current_player.announce(self.current_bet)
if announce.bet: if announce.bet:
......
...@@ -32,7 +32,9 @@ class ClientPlayer(Player): ...@@ -32,7 +32,9 @@ class ClientPlayer(Player):
sleep(5) sleep(5)
print(f"Client announced: {metadata.last_announce.bet}!") print(f"Client announced: {metadata.last_announce.bet}!")
return metadata.last_announce got_announce = metadata.last_announce
metadata.fresh_announce = False
return got_announce
@sio.event @sio.event
......
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