class QuickGame(Game): def __init__(self, id_: int, name: str): super().__init__(id_, 9, 9, 1000, Schemes.Static.value) for _ in range(8): self.add_bot_player() self.network = Network({'type': 'gh', 'id': self.id, 'game type': 'quick', 'name': name}) self.network.send({'type': 'start registration'}) Thread(target=lambda: self.network_process(), name=f'Quick game {self.id}: network process').start() def network_process(self): Debug.game_manager(f'Game {self.id}: ready to listen game') while True: try: request = self.network.receive() Debug.game_manager(f'Game {self.id}: message {request}') if request['type'] == 'add player' and not self.game_started: Debug.game_manager(f'Game {self.id}: add player') if self.add_real_player(request['name']): self.network.send({'type': 'start game'}) Thread(target=lambda: self.wait_for_end(), name=f'Game {self.id}: wait for end').start() elif request['type'] == 'delete player' and not self.game_started: Debug.game_manager(f'Game {self.id}: delete quick game') self.break_game() elif request['type'] == 'break': Debug.game_manager(f'Game {self.id}: break game') self.break_game() if self.thread: self.thread.join() self.network.send({'type': 'broken'}) except ValueError: continue except IndexError: continue def wait_for_end(self) -> None: self.thread.join() if not self.game_broken: self.network.send({'type': 'end game'})
def __init__(self, id_: int, name: str): super().__init__(id_, 9, 9, 1000, Schemes.Static.value) for _ in range(8): self.add_bot_player() self.network = Network({'type': 'gh', 'id': self.id, 'game type': 'quick', 'name': name}) self.network.send({'type': 'start registration'}) Thread(target=lambda: self.network_process(), name=f'Quick game {self.id}: network process').start()
def start_game(self) -> None: if any(player.controlled for player in self.players) or self.online: self.online = True Play.ExtendedName = False for table in self.tables: table.network = Network({'type': 'tb', 'name': str(table.id), 'id': self.id}) table.online = True table.players.network = table.network table.players.online = True else: Play.ExtendedName = True shuffle(self.players) for player in self.players: min_count_players_on_table = min(table.players.count for table in self.tables) tables_with_min_count = [table for table in self.tables if table.players.count == min_count_players_on_table] found_table: Table = choice(tables_with_min_count) found_table.players.add_player(player, False) self.game_started = True self.infinite()
def __init__(self, game_id: int, _id: int, name: str, money: int): play: Play = Play() network: BaseNetwork = Network({ 'type': 'py', 'name': f'{self.name}', 'id': self.id, 'game id': game_id }) super().__init__(_id, money, True, name, play, network)
def __init__(self, id_: int, name: str, total: int, bots: int, stack: int, seats: int, blinds: Scheme, start_blinds: int, password: str): super().__init__(id_, total, seats, stack, blinds) self.network = Network({ 'type': 'gh', 'id': self.id, 'game type': 'tournament', 'name': name, 'total players': total, 'initial stack': stack, 'table seats': seats, 'password': password, 'players left': bots }) self.network.send({'type': 'start registration'}) if total == bots: self.network.send({'type': 'start game'}) self.online = True for _ in range(bots): self.add_bot_player() if start_blinds == 1: self.blinds.curr_round = -1 elif start_blinds == 2: self.blinds.curr_round = 3 elif start_blinds == 3: self.blinds.curr_round = 11 Thread(target=lambda: self.network_process()).start() if total == bots: Thread(target=lambda: self.wait_for_end(), name=f'Game {self.id}: wait for end').start() Thread(target=lambda: self.send_players_left(), name=f'Game {self.id}: send players left').start()
class TournamentGame(Game): def __init__(self, id_: int, name: str, total: int, bots: int, stack: int, seats: int, blinds: Scheme, start_blinds: int, password: str): super().__init__(id_, total, seats, stack, blinds) self.network = Network({ 'type': 'gh', 'id': self.id, 'game type': 'tournament', 'name': name, 'total players': total, 'initial stack': stack, 'table seats': seats, 'password': password, 'players left': bots }) self.network.send({'type': 'start registration'}) if total == bots: self.network.send({'type': 'start game'}) self.online = True for _ in range(bots): self.add_bot_player() if start_blinds == 1: self.blinds.curr_round = -1 elif start_blinds == 2: self.blinds.curr_round = 3 elif start_blinds == 3: self.blinds.curr_round = 11 Thread(target=lambda: self.network_process()).start() if total == bots: Thread(target=lambda: self.wait_for_end(), name=f'Game {self.id}: wait for end').start() Thread(target=lambda: self.send_players_left(), name=f'Game {self.id}: send players left').start() def network_process(self): Debug.game_manager(f'Game {self.id}: ready to listen game') while True: try: request = self.network.receive() Debug.game_manager(f'Game {self.id}: message {request}') if request['type'] == 'add player' and not self.game_started: Debug.game_manager(f'Game {self.id}: add player') if self.add_real_player(request['name']): self.network.send({'type': 'start game'}) Thread(target=lambda: self.wait_for_end(), name=f'Game {self.id}: wait for end').start() Thread( target=lambda: self.send_players_left(), name=f'Game {self.id}: send players left').start() else: self.network.send({ 'type': 'update players', 'left': self.players_count }) elif request[ 'type'] == 'delete player' and not self.game_started: Debug.game_manager(f'Game {self.id}: delete player') self.delete_player(request['name']) elif request['type'] == 'break': Debug.game_manager(f'Game {self.id}: break game') self.break_game() if self.thread: self.thread.join() self.network.send({'type': 'broken'}) except ValueError: continue except IndexError: continue def wait_for_end(self) -> None: self.thread.join() if not self.game_broken: self.network.send({'type': 'end game'}) def send_players_left(self) -> None: while self.thread.is_alive(): if self.players_left is not None: self.network.send({ 'type': 'update players', 'left': self.players_left }) sleep(5)
def __init__(self): self.network: Network = Network({'type': 'ge', 'name': 'main'}) self.tournaments: Dict[int, Game] = dict() self.quick_games: Dict[int, Game] = dict() self.next_id = 0
def resit_players(self) -> None: total_players = sum(table.players.count for table in self.tables) if self.total_seats * len(self.tables) - total_players >= self.total_seats: Debug.resitting(f'Start to delete tables total tables = {len(self.tables)} ' f'seats = {self.total_seats * len(self.tables)} ' f'players = {total_players} ' f'difference = {self.total_seats * len(self.tables) - total_players}') while self.total_seats * len(self.tables) - total_players >= self.total_seats: if len(self.tables) == 2: self.tables[0].wait = True self.tables[1].wait = True while self.tables[0].in_game or self.tables[1].in_game: sleep(0.01) last_players = [player for table in self.tables for player in table.players.all_players()] shuffle(last_players) final_table = self.final_table if self.online: final_table.network = Network({'type': 'tb', 'name': '0', 'id': self.id}) final_table.online = True final_table.players.network = final_table.network final_table.players.online = True for player in last_players: final_table.players.add_player(player, False) Debug.resitting('Resit all players to final table') if self.tables[0].online: self.tables[0].network.end() if self.tables[1].online: self.tables[1].network.end() self.tables = [final_table] return if self.online: table_to_remove: Table = self.get_first_free_table(self.tables) else: table_to_remove: Table = choice(self.tables) while table_to_remove.in_game: sleep(0.01) while table_to_remove.players.count: other_min_count = min(table.players.count for table in self.tables if table != table_to_remove) other_tables_with_min_count = [table for table in self.tables if table != table_to_remove and table.players.count == other_min_count] table_to_resit: Table = choice(other_tables_with_min_count) Debug.resitting(f'Resit player from removing table {table_to_remove.id} ' f'count = {table_to_remove.players.count}' f' to table {table_to_resit.id} count = {table_to_resit.players.count}') table_to_remove.players.remove_player(table_to_resit.players) table_to_remove.players.resit_all_needed_players() if table_to_remove.players.count != 0: raise ValueError(f'Try to delete table {table_to_remove.id} with players in it') Debug.resitting(f'Delete table {table_to_remove.id}') del self.tables[self.tables.index(table_to_remove)] if table_to_remove.online: table_to_remove.network.end() del table_to_remove if self.online: return counts = [table.players.count for table in self.tables] max_counts = max(counts) min_counts = min(counts) if max_counts - min_counts > 1: Debug.resitting(f'Start to resit without deleting tables max = {max_counts} min = {min_counts} ' f'players = {sum(counts)} ' f'difference = {self.total_seats * len(self.tables) - sum(counts)}') while max_counts - min_counts > 1: tables_with_max_count = [table for table in self.tables if table.players.count == max_counts] tables_with_min_count = [table for table in self.tables if table.players.count == min_counts] table_to_resit: Table = choice(tables_with_min_count) if self.online: table_from_resit = self.get_first_free_table(tables_with_max_count) else: table_from_resit: Table = choice(tables_with_max_count) Debug.resitting(f'Resit player from table {table_from_resit.id} count = {table_from_resit.players.count}' f' to table {table_to_resit.id} count = {table_to_resit.players.count}') table_from_resit.players.remove_player(table_to_resit.players) while table_from_resit.in_game: sleep(0.01) table_from_resit.players.resit_all_needed_players() table_from_resit.wait = False if self.online: return counts = [table.players.count for table in self.tables] max_counts = max(counts) min_counts = min(counts)