refactor: Extract Value/Color, encoders

parent ac8ad5b5
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
...@@ -103,7 +103,7 @@ class LobbyManager(ClientManager): ...@@ -103,7 +103,7 @@ class LobbyManager(ClientManager):
data["extra"] = extras data["extra"] = extras
print(f"MSGOUT|{sid} ({to.name}), {data}") 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): async def handle_message(self, sid, data):
message = None message = None
...@@ -169,6 +169,6 @@ class LobbyManager(ClientManager): ...@@ -169,6 +169,6 @@ class LobbyManager(ClientManager):
async def send_waiting_for(self, player: Player): async def send_waiting_for(self, player: Player):
game = self.game_with(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]: for p in [p for p in game.players if p != player]:
await self.send(p, MessageToPlayer.Waiting, extras={"waitingFor": p.name}) await self.send(p, MessageToPlayer.Waiting, extras={"waitingFor": p.name})
from enum import Enum
from typing import Optional from typing import Optional
from pydantic.main import BaseModel from pydantic.main import BaseModel
from server.encoding import encode_value, encode_color
class Value(Enum): from server.model.color import Color
Two = 1 from server.model.value import Value
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 = "♦"
class Card(BaseModel): class Card(BaseModel):
value: Value value: Value
color: Optional[Color] = None color: Optional[Color] = None
class Config:
json_encoders = {
Value: encode_value,
Color: encode_color,
}
def __cmp__(self, other: "Card"): def __cmp__(self, other: "Card"):
my = self.score() my = self.score()
their = other.score() their = other.score()
......
from random import randrange, shuffle 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: class Deck:
......
...@@ -4,10 +4,11 @@ from typing import List, Dict, Optional ...@@ -4,10 +4,11 @@ from typing import List, Dict, Optional
from server.game.manager import ClientManager from server.game.manager import ClientManager
from server.game.message import MessageToPlayer 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.deck import Deck
from server.model.hand import Hand from server.model.hand import Hand
from server.model.players import Player, Announce from server.model.players import Player, Announce
from server.model.value import Value
class Game: class Game:
...@@ -26,9 +27,9 @@ class Game: ...@@ -26,9 +27,9 @@ class Game:
self.manager = manager self.manager = manager
async def message(self, message: MessageToPlayer, async def message(self, message: MessageToPlayer,
*to: Player, *to: Player,
extra=None extra=None
) -> None: ) -> None:
if self.manager: if self.manager:
if not to: if not to:
to = self.players to = self.players
...@@ -152,7 +153,8 @@ class Game: ...@@ -152,7 +153,8 @@ class Game:
self.current_bet = announce.bet self.current_bet = announce.bet
print(f"{current_player} starts the round: {self.current_bet}") print(f"{current_player} starts the round: {self.current_bet}")
print("Game: Awaiting start announce") 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: else:
print(f"You cannot say Menteur on first round, {current_player}!") print(f"You cannot say Menteur on first round, {current_player}!")
......
...@@ -3,12 +3,21 @@ from typing import List ...@@ -3,12 +3,21 @@ from typing import List
from pydantic.main import BaseModel 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): class Hand(BaseModel):
cards: List[Card] = [] cards: List[Card] = []
class Config:
json_encoders = {
Value: encode_value,
Color: encode_color,
}
def __contains__(self, item: Card): def __contains__(self, item: Card):
return item in self.cards return item in self.cards
......
from random import choice from random import choice
from typing import List 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 from server.model.hand import Hand
......
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 from server.model.hands import pair, single, double_pair, brelan, full, carre, card
ACE_OF_HEARTS = card(Value.Ace, Color.Hearts) ACE_OF_HEARTS = card(Value.Ace, Color.Hearts)
......
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
fastapi==0.53.2 fastapi==0.54.1
uvicorn==0.11.3 uvicorn==0.11.3
python-socketio==4.5.1 python-socketio==4.5.1
from unittest import TestCase 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.deck import Deck
from server.model.hand import Hand from server.model.hand import Hand
from server.model.hands import full, brelan, pair, single, double_pair, carre from server.model.hands import full, brelan, pair, single, double_pair, carre
......
...@@ -3,7 +3,7 @@ from unittest import TestCase ...@@ -3,7 +3,7 @@ from unittest import TestCase
from server.game.manager import ClientManager from server.game.manager import ClientManager
from server.game.message import MessageToPlayer 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.deck import Deck
from server.model.game import Game from server.model.game import Game
from server.model.hand import Hand from server.model.hand import Hand
......
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