def _on_zhugong_choose_figure(self, cmd): """ 等待主公选择武将 """ zhugong = global_users.get_user(cmd.sender) if zhugong.role != ROLES.ZHUGONG or cmd.cmd != "CHOOSE_FIGURE": return zhugong.set_figure(global_figures.figures[cmd.figure_id]) global_figures.add_used([cmd.figure_id]) global_users.broadcast_cmd( Cmd('SHOW_FIGURE', seat_id=zhugong.seat_id, figures=[zhugong.figure.to_cmd_dict()])) self.reset_wait_callback() # 其他人选将 #class OtherChooseFigurePhase(Phase): all_candidates = [] for user in self.game_board.one_round(except_user=[zhugong.pk]): candidates = global_figures.take_random_figure(count=2) all_candidates.extend(candidates) user.add_cmd( Cmd('SET_FIGURE_CANDIDATE', figures=[c.to_cmd_dict() for c in candidates])) global_figures.remove_used(all_candidates) self.wait(self._on_other_choose_figure, all_candidates=all_candidates)
def _ready(self, cmd): user = global_users.get_user(cmd.sender) user.is_ready = True global_users.broadcast_cmd(cmd) if self.game_board.is_all_ready(): self.start()
def on_new_cmd(self, cmds): # Closed client connection if self.request.connection.stream.closed(): user = global_users.get_user(self.get_current_user()['pk']) user.resend_cmd(cmds) return self.finish(dict(cmds=[cmds.to_simple_dict()]))
def get(self): user_dict = self.get_current_user() user = global_users.get_user(user_dict['pk']) if user: user.get_cmds(self.on_new_cmd) else: self.write(dict(cmds=[]))
def post(self): cmd_args = self.request.arguments cmd_args.pop('_xsrf', None) cmd = cmd_args.pop('cmd')[0] cmd_args = dict([(k, v[0]) if len(v) == 1 else v for k, v in cmd_args.iteritems()]) user = global_users.get_user(self.current_user['pk']) if user.seat_id is not None and 'seat_id' not in cmd_args: cmd_args['seat_id'] = user.seat_id cmd = Cmd(cmd, sender=user.pk, **cmd_args) logging.info('<-- [%s] %s' % (cmd, cmd_args)) self.write(dict(cmds=[cmd.get_ack_cmd().to_simple_dict()])) global_game.handle_cmd(cmd)
def _join(self, cmd): user = global_users.get_user(cmd.sender) if user.seat_id is None: seat_id = self.game_board.get_random_seat() if seat_id != -1: user.seat_id = seat_id self.game_board.join(seat_id, user) cmd.update_args({'seat_id': seat_id, 'max_seat': self.game_board.MAX_SEAT}) global_users.broadcast_cmd(cmd) else: user.restore() # 恢复用户游戏状态 for seat, u in self.game_board.seats.iteritems(): if u.pk == user.pk: continue user.add_cmd(Cmd('JOIN', sender=u.pk, seat_id=seat)) if u.is_ready: user.add_cmd(Cmd('READY', sender=u.pk, seat_id=seat))
def _join(self, cmd): user = global_users.get_user(cmd.sender) if user.seat_id is None: seat_id = self.game_board.get_random_seat() if seat_id != -1: user.seat_id = seat_id self.game_board.join(seat_id, user) cmd.update_args({ 'seat_id': seat_id, 'max_seat': self.game_board.MAX_SEAT }) global_users.broadcast_cmd(cmd) else: user.restore() # 恢复用户游戏状态 for seat, u in self.game_board.seats.iteritems(): if u.pk == user.pk: continue user.add_cmd(Cmd('JOIN', sender=u.pk, seat_id=seat)) if u.is_ready: user.add_cmd(Cmd('READY', sender=u.pk, seat_id=seat))
def _on_other_choose_figure(self, cmd): """ 等待其他玩家选择武将 """ user = global_users.get_user(cmd.sender) if user.figure is None: user.set_figure(global_figures.figures[cmd.figure_id]) global_figures.add_used([cmd.figure_id]) if not self.game_board.is_all_chosen_figure(): return self.reset_wait_callback() # 所有人亮武将 for user in self.game_board.one_round(): global_users.broadcast_cmd( Cmd('SHOW_FIGURE', seat_id=user.seat_id, figures=[user.figure.to_cmd_dict()])) # 每人首发4张牌 for user in self.game_board.one_round(): cards = global_cards.get_cards(count=4) user.set_cards(cards) global_users.broadcast_cmd( Cmd('GAIN_CARD', seat_id=user.seat_id, count=4))
def _unready(self, cmd): user = global_users.get_user(cmd.sender) user.is_ready = False global_users.broadcast_cmd(cmd)