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):
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})
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()
......
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:
......
......@@ -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:
......@@ -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}!")
......
......@@ -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
......
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
......
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)
......
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
python-socketio==4.5.1
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
......
......@@ -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
......
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