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 start(self): # 游戏开始 global_users.broadcast_cmd(Cmd('GAME_START')) global_users.broadcast_cmd(Cmd('GAME_MSG', msg=u'游戏开始,分配角色')) # 分配角色 roles = self.game_board.get_roles() for u in global_users.users.itervalues(): r = roles.pop(randint(0, len(roles) - 1)) self.game_board.set_role(u, r) zhugong = self.game_board.zhugong global_users.broadcast_cmd( Cmd('SHOW_ROLE', seat_id=zhugong.seat_id, label=ROLE_LABELS[zhugong.role])) # 分配武将 #class ChooseFigurePhase(Phase): # next_phases = [ # ZhugongChooseFigurePhase # OtherChooseFigurePhase] # 主公选将 #class ZhugongChooseFigurePhase(Phase): global_users.broadcast_cmd(Cmd('GAME_MSG', msg=u'等待主公选择武将')) candidates = global_figures.take_zhugong_figures() self.game_board.zhugong.add_cmd( Cmd('SET_FIGURE_CANDIDATE', figures=[c.to_cmd_dict() for c in candidates])) global_figures.remove_used(candidates) self.wait(self._on_zhugong_choose_figure, candidates=candidates)
def restore(self): self.add_cmd(Cmd('JOIN', sender=self.pk, seat_id=self.seat_id)) if self.is_ready: self.add_cmd(Cmd('READY', sender=self.pk, seat_id=self.seat_id)) if self.role: self.add_cmd( Cmd('SET_ROLE', role_id=self.role, label=ROLE_LABELS[self.role])) if self.figure: self.add_cmd(Cmd('SET_FIGURE', **self.figure.to_cmd_dict()))
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 _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 set_cards(self, cards): self.cards.append(cards) for card in cards: self.add_cmd(Cmd('SET_CARD', **card.to_cmd_dict()))
def set_figure(self, figure): self.figure = figure self.add_cmd(Cmd('SET_FIGURE', **figure.to_cmd_dict()))
def set_role(self, role): self.role = role self.add_cmd(Cmd('SET_ROLE', role_id=role, label=ROLE_LABELS[role]))