From cfada799f968fa525726f19bd0d0e3a243fe56c7 Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Sun, 26 Apr 2020 13:12:17 +0200 Subject: [PATCH] refactor: Extract Value/Color, encoders --- server/encoding.py | 29 +++++++++++++++++++++++++++++ server/game/lobby.py | 4 ++-- server/model/card.py | 32 +++++++++----------------------- server/model/deck.py | 4 +++- server/model/game.py | 12 +++++++----- server/model/hand.py | 11 ++++++++++- server/model/hands.py | 4 +++- server/model/known.py | 3 ++- server/model/value.py | 20 ++++++++++++++++++++ server/requirements.txt | 2 +- server/test/test_data.py | 4 +++- server/test/test_lobby.py | 2 +- 12 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 server/encoding.py create mode 100644 server/model/value.py diff --git a/server/encoding.py b/server/encoding.py new file mode 100644 index 0000000..0992328 --- /dev/null +++ b/server/encoding.py @@ -0,0 +1,29 @@ +from server.model.color import Color +from server.model.value import Value + + +def encode_value(v: Value): + return { + "Two": "Deux", + "Three": "Trois", + "Four": "Quatre", + "Five": "Cinq", + "Six": "Six", + "Seven": "Sept", + "Eight": "Huit", + "Nine": "Neuf", + "Ten": "Dix", + "Jack": "Valet", + "Queen": "Dame", + "King": "Roi", + "Ace": "As" + }[v.name] + + +def encode_color(c: Color): + return { + "Hearts": "Coeur", + "Spades": "Pique", + "Clubs": "Trèfle", + "Diamonds": "Carreau" + }[c.name] \ No newline at end of file diff --git a/server/game/lobby.py b/server/game/lobby.py index f214a06..ed09a8d 100644 --- a/server/game/lobby.py +++ b/server/game/lobby.py @@ -103,7 +103,7 @@ class LobbyManager(ClientManager): data["extra"] = extras print(f"MSGOUT|{sid} ({to.name}), {data}") - await self.sio.emit('messageChannel', json.dumps(data), room=sid) + await self.sio.emit('messageChannel', json.dumps(data, ensure_ascii=False).encode("utf8"), room=sid) async def handle_message(self, sid, data): message = None @@ -169,6 +169,6 @@ class LobbyManager(ClientManager): async def send_waiting_for(self, player: Player): game = self.game_with(player) - await self.send(player, MessageToPlayer.YourTurn, extras={"bet": game.current_bet}) + await self.send(player, MessageToPlayer.YourTurn, extras={"bet": game.current_bet.json()}) for p in [p for p in game.players if p != player]: await self.send(p, MessageToPlayer.Waiting, extras={"waitingFor": p.name}) diff --git a/server/model/card.py b/server/model/card.py index f95fa43..39cf76d 100644 --- a/server/model/card.py +++ b/server/model/card.py @@ -1,36 +1,22 @@ -from enum import Enum from typing import Optional from pydantic.main import BaseModel - -class Value(Enum): - Two = 1 - Three = 2 - Four = 3 - Five = 4 - Six = 5 - Seven = 6 - Eight = 7 - Nine = 8 - Ten = 9 - Jack = 10 - Queen = 11 - King = 12 - Ace = 13 - - -class Color(Enum): - Hearts = "♥" - Spades = "♠" - Clubs = "♣" - Diamonds = "♦" +from server.encoding import encode_value, encode_color +from server.model.color import Color +from server.model.value import Value class Card(BaseModel): value: Value color: Optional[Color] = None + class Config: + json_encoders = { + Value: encode_value, + Color: encode_color, + } + def __cmp__(self, other: "Card"): my = self.score() their = other.score() diff --git a/server/model/deck.py b/server/model/deck.py index fd4af26..9eec098 100644 --- a/server/model/deck.py +++ b/server/model/deck.py @@ -1,6 +1,8 @@ from random import randrange, shuffle -from server.model.card import Card, Value, Color +from server.model.card import Card +from server.model.color import Color +from server.model.value import Value class Deck: diff --git a/server/model/game.py b/server/model/game.py index 895e367..d7f67b3 100644 --- a/server/model/game.py +++ b/server/model/game.py @@ -4,10 +4,11 @@ from typing import List, Dict, Optional from server.game.manager import ClientManager from server.game.message import MessageToPlayer -from server.model.card import Card, Value +from server.model.card import Card from server.model.deck import Deck from server.model.hand import Hand from server.model.players import Player, Announce +from server.model.value import Value class Game: @@ -26,9 +27,9 @@ class Game: self.manager = manager async def message(self, message: MessageToPlayer, - *to: Player, - extra=None - ) -> None: + *to: Player, + extra=None + ) -> None: if self.manager: if not to: to = self.players @@ -152,7 +153,8 @@ class Game: self.current_bet = announce.bet print(f"{current_player} starts the round: {self.current_bet}") print("Game: Awaiting start announce") - await self.message(MessageToPlayer.Announce, extra={"player": current_player.name, "announce": announce.json()}) + await self.message(MessageToPlayer.Announce, + extra={"player": current_player.name, "announce": announce.json()}) else: print(f"You cannot say Menteur on first round, {current_player}!") diff --git a/server/model/hand.py b/server/model/hand.py index e4a4c7e..6e0a405 100644 --- a/server/model/hand.py +++ b/server/model/hand.py @@ -3,12 +3,21 @@ from typing import List from pydantic.main import BaseModel -from server.model.card import Card, Value +from server.encoding import encode_color, encode_value +from server.model.card import Card +from server.model.color import Color +from server.model.value import Value class Hand(BaseModel): cards: List[Card] = [] + class Config: + json_encoders = { + Value: encode_value, + Color: encode_color, + } + def __contains__(self, item: Card): return item in self.cards diff --git a/server/model/hands.py b/server/model/hands.py index 38a2b85..9aed868 100644 --- a/server/model/hands.py +++ b/server/model/hands.py @@ -1,7 +1,9 @@ from random import choice from typing import List -from server.model.card import Value, Color, Card +from server.model.card import Card +from server.model.color import Color +from server.model.value import Value from server.model.hand import Hand diff --git a/server/model/known.py b/server/model/known.py index e340263..d8f0627 100644 --- a/server/model/known.py +++ b/server/model/known.py @@ -1,4 +1,5 @@ -from server.model.card import Value, Color +from server.model.color import Color +from server.model.value import Value from server.model.hands import pair, single, double_pair, brelan, full, carre, card ACE_OF_HEARTS = card(Value.Ace, Color.Hearts) diff --git a/server/model/value.py b/server/model/value.py new file mode 100644 index 0000000..c382b49 --- /dev/null +++ b/server/model/value.py @@ -0,0 +1,20 @@ +from enum import Enum + + +class Value(Enum): + Two = 1 + Three = 2 + Four = 3 + Five = 4 + Six = 5 + Seven = 6 + Eight = 7 + Nine = 8 + Ten = 9 + Jack = 10 + Queen = 11 + King = 12 + Ace = 13 + + def json(self): + return self.value \ No newline at end of file diff --git a/server/requirements.txt b/server/requirements.txt index 4617764..2501c1a 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,3 +1,3 @@ -fastapi==0.53.2 +fastapi==0.54.1 uvicorn==0.11.3 python-socketio==4.5.1 diff --git a/server/test/test_data.py b/server/test/test_data.py index 6fba3f2..5ba0c62 100644 --- a/server/test/test_data.py +++ b/server/test/test_data.py @@ -1,6 +1,8 @@ from unittest import TestCase -from server.model.card import Value, Color, Card, lowest_value_and_rest +from server.model.card import Card, lowest_value_and_rest +from server.model.color import Color +from server.model.value import Value from server.model.deck import Deck from server.model.hand import Hand from server.model.hands import full, brelan, pair, single, double_pair, carre diff --git a/server/test/test_lobby.py b/server/test/test_lobby.py index ad09f45..f5a1d34 100644 --- a/server/test/test_lobby.py +++ b/server/test/test_lobby.py @@ -3,7 +3,7 @@ from unittest import TestCase from server.game.manager import ClientManager from server.game.message import MessageToPlayer -from server.model.card import Value +from server.model.value import Value from server.model.deck import Deck from server.model.game import Game from server.model.hand import Hand -- libgit2 0.27.0