Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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))
Ejemplo n.º 8
0
 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))
Ejemplo n.º 9
0
 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))
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
 def _unready(self, cmd):
     user = global_users.get_user(cmd.sender)
     user.is_ready = False
     global_users.broadcast_cmd(cmd)
Ejemplo n.º 12
0
 def _unready(self, cmd):
     user = global_users.get_user(cmd.sender)
     user.is_ready = False
     global_users.broadcast_cmd(cmd)