async def add_player(self, player: Player): if len(self.players) == self.max_player: raise Exception("room is full!") await self.broadcast(player_join_event({'name': player.get_name()})) self.players[player.id] = player player.room = self
async def on_toggle_prepare_state(self, player: Player, state: bool): player.isPrepared = state await self.broadcast( player_prepare_event({ 'name': player.get_name(), 'state': player.isPrepared })) if self.is_everyone_ready() and len(self.players) == self.max_player: await asyncio.gather(self.broadcast(start_countdown_event), self.start_count_down())
async def remove_player(self, player: Player): del self.players[player.id] if len(self.players) == 0: self.close() if self.game is not None: await self.broadcast(game_interrupted_event()) await self.broadcast(player_leave_event({'name': player.get_name()}))
async def on_cut_card(self, player: Player, card: Card): role = self.player_role[player] role.Cut(role, card) self.cancel_timer() await self.broadcast(player_cut_card_event({'name': player.get_name()})) await self.draw_card()
async def websocket_handler(websocket, path): nickname = path print(path) print("get connection") player = Player(nickname, websocket) receiving = asyncio.create_task(receive_message(player)) await receiving await player.leave_room()
async def on_skip_turn(self, player: Player): role = self.player_role[player] role.Go() self.cancel_timer() if self.timer is not None: self.timer.cancel() await asyncio.gather( self.broadcast(player_skip_turn_event({'name': player.get_name()})), self.draw_card())
async def on_doubt_uno(self, player: Player, data): role = self.player_role[player] target = data['target_player'] for i in self.players: if target == i.get_name(): role.DoubtUno(self.player_role[i]) await self.broadcast( player_doubt_uno_event({ 'from': i.get_name(), 'to': player.get_name() })) return
async def on_put_card(self, player: Player, card: Card): role = self.player_role[player] result = role.Put(card) self.cancel_timer() if self.timer is not None: self.timer.cancel() await self.broadcast( player_put_card_event({ 'player': player.nickname, 'card': serialize_card(card) })) if result: await self.broadcast(player_win_event({'name': player.get_name()})) if len(self.game.player_list) > 1: await self.broadcast(game_finished_event()) self.game = None self.player_role = {} else: await self.draw_card()
async def on_uno(self, player: Player): role = self.player_role[player] role.Uno() await self.broadcast(player_uno_event({'name': player.get_name()}))