Initial commit: initial backend, objects and game-logic implementation
This commit is contained in:
commit
cf16878503
4 changed files with 896 additions and 0 deletions
162
game.py
Normal file
162
game.py
Normal file
|
@ -0,0 +1,162 @@
|
|||
# Copyright (C) 2023 Evgenij Titarenko
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from uuid import uuid4
|
||||
from enum import Enum
|
||||
from random import choice, randint
|
||||
|
||||
MAX_PLAYERS = 8
|
||||
MIN_PLAYERS = 2
|
||||
STREETS = []
|
||||
|
||||
|
||||
class GameState(Enum):
|
||||
WAITING = 0
|
||||
IN_GAME = 1
|
||||
FINISHED = 2
|
||||
|
||||
|
||||
class Color:
|
||||
def __init__(self, red: int, green: int, blue: int):
|
||||
self.red = red
|
||||
self.green = green
|
||||
self.blue = blue
|
||||
|
||||
|
||||
class DefaultColors(Enum):
|
||||
RED = Color(255, 0, 0)
|
||||
GREEN = Color(0, 255, 0)
|
||||
BLUE = Color(0, 0, 255)
|
||||
YELLOW = Color(255, 255, 0)
|
||||
CYAN = Color(0, 255, 255)
|
||||
MAGENTA = Color(255, 0, 255)
|
||||
|
||||
|
||||
class User:
|
||||
def __init__(self, username: str):
|
||||
self.username = username
|
||||
self.color = None
|
||||
self.initiative = None
|
||||
self.money = None
|
||||
|
||||
|
||||
class StreetType(Enum):
|
||||
NORMAL = 0
|
||||
START = 1
|
||||
CHEST = 2
|
||||
TAX = 3
|
||||
TRAIN = 4
|
||||
CHANCE = 5
|
||||
PRISON = 6
|
||||
SERVICE = 7
|
||||
STOP = 8
|
||||
TO_PRISON = 9
|
||||
|
||||
|
||||
class Street:
|
||||
def __init__(self, street_type: StreetType, name: str, color: Color, price: int, home_price: int,
|
||||
hotel_price: int, mortgage: int, rent: tuple):
|
||||
self.street_type = street_type
|
||||
self.name = name
|
||||
self.color = color
|
||||
self.price = price
|
||||
self.home_price = home_price
|
||||
self.hotel_price = hotel_price
|
||||
self.mortgage = mortgage
|
||||
self.rent = rent
|
||||
self.owner = None
|
||||
|
||||
|
||||
def str_to_street_type(s: str):
|
||||
return StreetType[s.upper()]
|
||||
|
||||
|
||||
def load_streets():
|
||||
with open("streets.txt") as f:
|
||||
for line in f:
|
||||
street_info = line.split(":")
|
||||
street_type = str_to_street_type(street_info[0])
|
||||
name = street_info[1]
|
||||
color = Color(*map(int, street_info[2].split(",")[:3]))
|
||||
price = int(street_info[3])
|
||||
home_price = int(street_info[4])
|
||||
hotel_price = int(street_info[5])
|
||||
mortgage = int(street_info[6])
|
||||
rent = tuple(map(int, street_info[7].split(",")))
|
||||
street = Street(street_type, name, color, price, home_price, hotel_price, mortgage, rent)
|
||||
STREETS.append(street)
|
||||
|
||||
|
||||
def copy_streets():
|
||||
new_list = []
|
||||
for street in STREETS:
|
||||
new_list.append(Street(street.street_type, street.name, street.color, street.price, street.home_price,
|
||||
street.hotel_price, street.mortgage, street.rent))
|
||||
return new_list
|
||||
|
||||
|
||||
def choice_color_from_list(colors: list):
|
||||
return choice(colors)
|
||||
|
||||
|
||||
def roll_dices(dice: int, n: int):
|
||||
def roll():
|
||||
return sum(randint(1, dice) for _ in range(n))
|
||||
return roll
|
||||
|
||||
|
||||
roll_2d6 = roll_dices(6, 2)
|
||||
|
||||
|
||||
class Room:
|
||||
def __init__(self, password: str = ""):
|
||||
self.room_id = uuid4()
|
||||
self.users = []
|
||||
self.state = GameState.WAITING
|
||||
self.password = password
|
||||
self.current_player = None
|
||||
self.streets = copy_streets()
|
||||
|
||||
def check_color(self, color: Color):
|
||||
return color not in [user.color for user in self.users]
|
||||
|
||||
def add_user(self, user: User):
|
||||
if len(self.users) == MAX_PLAYERS:
|
||||
return # TODO
|
||||
available_colors = list(filter(self.check_color, list(DefaultColors)))
|
||||
user.color = choice_color_from_list(available_colors)
|
||||
self.users.append(user)
|
||||
|
||||
def sort_players_by_initiative(self):
|
||||
self.users.sort(key=lambda user: user.initiative, reverse=True)
|
||||
|
||||
def roll_initiative(self):
|
||||
places = list(range(MAX_PLAYERS))
|
||||
for user in self.users:
|
||||
place = choice(places)
|
||||
user.initiative = place
|
||||
places.remove(place)
|
||||
|
||||
def start_game(self):
|
||||
if self.state == GameState.IN_GAME or len(self.users) < MIN_PLAYERS:
|
||||
return # TODO
|
||||
self.roll_initiative()
|
||||
self.sort_players_by_initiative()
|
||||
self.current_player = self.users[0]
|
||||
self.state = GameState.IN_GAME
|
||||
|
||||
def remove_user(self, user: User):
|
||||
if user in self.users:
|
||||
self.users.remove(user)
|
Loading…
Add table
Add a link
Reference in a new issue