Socket rewrite
This commit is contained in:
parent
cf16878503
commit
89431be9ed
4 changed files with 187 additions and 39 deletions
66
game.py
66
game.py
|
@ -28,11 +28,25 @@ class GameState(Enum):
|
|||
FINISHED = 2
|
||||
|
||||
|
||||
def int_to_hex_str(x: int):
|
||||
hex_x = format(x, "x")
|
||||
return hex_x if len(hex_x) > 1 else f"0{hex_x}"
|
||||
|
||||
|
||||
class Color:
|
||||
def __init__(self, red: int, green: int, blue: int):
|
||||
self.red = red
|
||||
self.green = green
|
||||
self.blue = blue
|
||||
hex_red = int_to_hex_str(red)
|
||||
hex_green = int_to_hex_str(green)
|
||||
hex_blue = int_to_hex_str(blue)
|
||||
self.hex = f'#{hex_red}{hex_green}{hex_blue}'
|
||||
|
||||
def get_dict(self):
|
||||
return {
|
||||
"color": self.hex
|
||||
}
|
||||
|
||||
|
||||
class DefaultColors(Enum):
|
||||
|
@ -45,11 +59,23 @@ class DefaultColors(Enum):
|
|||
|
||||
|
||||
class User:
|
||||
def __init__(self, username: str):
|
||||
def __init__(self, username: str, password: str):
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.color = None
|
||||
self.initiative = None
|
||||
self.money = None
|
||||
self.token = str(uuid4())
|
||||
self.in_room = ""
|
||||
self.room_password = ""
|
||||
|
||||
def get_dict(self):
|
||||
return {
|
||||
"username": self.username,
|
||||
"color": get_color(self.color).get_dict(),
|
||||
"initiative": self.initiative,
|
||||
"money": self.money,
|
||||
}
|
||||
|
||||
|
||||
class StreetType(Enum):
|
||||
|
@ -65,6 +91,14 @@ class StreetType(Enum):
|
|||
TO_PRISON = 9
|
||||
|
||||
|
||||
def get_color(color: Color | DefaultColors) -> Color:
|
||||
if color is None:
|
||||
return Color(0, 0, 0)
|
||||
if color in list(DefaultColors):
|
||||
return color.value
|
||||
return color
|
||||
|
||||
|
||||
class Street:
|
||||
def __init__(self, street_type: StreetType, name: str, color: Color, price: int, home_price: int,
|
||||
hotel_price: int, mortgage: int, rent: tuple):
|
||||
|
@ -78,6 +112,19 @@ class Street:
|
|||
self.rent = rent
|
||||
self.owner = None
|
||||
|
||||
def get_dict(self):
|
||||
return {
|
||||
"street_type": self.street_type.name,
|
||||
"name": self.name,
|
||||
"color": get_color(self.color).get_dict(),
|
||||
"price": self.price,
|
||||
"home_price": self.home_price,
|
||||
"hotel_price": self.hotel_price,
|
||||
"mortgage": self.mortgage,
|
||||
"rent": self.rent,
|
||||
"owner": self.owner
|
||||
}
|
||||
|
||||
|
||||
def str_to_street_type(s: str):
|
||||
return StreetType[s.upper()]
|
||||
|
@ -120,15 +167,30 @@ def roll_dices(dice: int, n: int):
|
|||
roll_2d6 = roll_dices(6, 2)
|
||||
|
||||
|
||||
def get_dict_user_or_none(user: User | None):
|
||||
if user:
|
||||
return user.get_dict()
|
||||
return user
|
||||
|
||||
|
||||
class Room:
|
||||
def __init__(self, password: str = ""):
|
||||
self.room_id = uuid4()
|
||||
self.room_id = str(uuid4())
|
||||
self.users = []
|
||||
self.state = GameState.WAITING
|
||||
self.password = password
|
||||
self.current_player = None
|
||||
self.streets = copy_streets()
|
||||
|
||||
def get_dict(self):
|
||||
return {
|
||||
"room_id": self.room_id,
|
||||
"users": [user.get_dict()for user in self.users],
|
||||
"state": self.state.name,
|
||||
"current_player": self.current_player.get_dict() if self.current_player else self.current_player,
|
||||
"streets": [street.get_dict() for street in self.streets]
|
||||
}
|
||||
|
||||
def check_color(self, color: Color):
|
||||
return color not in [user.color for user in self.users]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue