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

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