def execute(self): """ 初始发牌 :return: """ logger.debug(u"初始发牌: %s", str([])) card_results = [] for i in range(self.max_player_num): drew_card_vals = self.card_dealer.draw_cards(num=13) # 处理测试初始化发牌 if GlobalConfig().test_sure_next_cards.get(self.desk_id): test_cards = GlobalConfig().test_sure_next_cards[ self.desk_id][i] if test_cards: min_len = min(len(test_cards), len(drew_card_vals)) for index in range(min_len): drew_card_vals[index] = test_cards[index] # 测试初始化发牌结束 card_results.append(drew_card_vals) for x in self.players: result = [] for y in self.players: if x.seat_id == y.seat_id: data = { "seat_id": y.seat_id, "card_list": card_results[y.seat_id] } else: data = {"seat_id": y.seat_id, "card_list": [BLACK] * 13} result.append(data) notify_single_user(self.desk_id, x.seat_id, message_ids.PUSH_DEAL_CARD, data={"card_list": result}) for x in self.players: if self.game_config.draw_card_bu_hua: self.bu_hua(x.seat_id, card_results[x.seat_id]) # 发牌时候同时将牌添加入手牌 print("card_results[%s] = %s len=%s" % (x.seat_id, card_results[x.seat_id], len(card_results[x.seat_id]))) self.players[x.seat_id].hand_card.add_hand_card_by_vals( card_results[x.seat_id]) # 添加玩家动作, Waite_answer # for i in range(self.game_data.max_player_num): # self.game_data.add_player_to_act(i, Act.WAITE_ANSWER) # 添加超时回调 self.game_data.next_speaker_callback = { "type": CallbackFuncType.FUNC_NOTIFY_PLAYER_ACT, "call_params": { "seat_id": self.game_data.banker_seat_id, "interval": self.get_act_wait_time(self.game_data.banker_seat_id, Act.WAITE_ANSWER), "act_info": {} } } return 1
def get_act_wait_time(self, seat_id, act_type=Act.CHU): if self.game_data.is_player_auto(seat_id): return GlobalConfig().auto_op_wait_time else: if act_type in [Act.CHI, Act.PENG, Act.DIAN_GANG, Act.DIAN_HU]: return GlobalConfig().manual_op_against_act_time else: return GlobalConfig().manual_op_wait_time
def get_act_wait_time(self, seat_id, act_type=Act.CHU): if self.game_data.is_player_auto(seat_id): return GlobalConfig().auto_op_wait_time elif self.game_data.players[ seat_id].hand_card.is_ting and act_type not in [ Act.AN_GANG, Act.ZI_MO, Act.BU_GANG ]: return GlobalConfig().ting_auto_chu_time else: if act_type in [Act.CHI, Act.PENG, Act.DIAN_GANG, Act.DIAN_HU]: return GlobalConfig().manual_op_against_act_time elif act_type in [Act.WAITE_ANSWER]: return GlobalConfig().waite_answer_time else: return GlobalConfig().manual_op_wait_time
def bu_hua(self): """检查是否补花""" hua_card = [] for c in self.drew_cardvals: if CardType.HUA == Card.cal_card_type(c): hua_card.append(c) if not hua_card: return 0 logger.debug('补花:%d', len(hua_card)) # 将花牌从手牌中移除 self.players[self.seat_id].hand_card.del_hand_card_by_val(hua_card[0]) # 将补花数量添加进手牌中 self.players[self.seat_id].hand_card.hua_card_vals.extend(hua_card) # 通知所有人有玩家补花 notify_all_desk_player(self.desk_id, PUSH_GAME_BU_HUA, { "seat_id": self.seat_id, "hua_card_list": hua_card }) timer_manager_ins.add_timer(self.desk_id, self.seat_id, GlobalConfig().bu_hua_show_time, t_type=TimerType.KEEP, call_type=CallbackFuncType.FUNC_DRAW_CARD, call_params={ "seat_id": self.seat_id, "card_num": len(hua_card), "is_last": True }) return 1
def execute(self, *args, **kwargs): """ 解散桌子请求处理 :param args: :param kwargs: :return: {need_agree: 0/1} 成功退出时0, 1表示需要其他玩家同意 """ validator = DissolveDeskValidator(handler=self) if validator.desk.is_last_round( ) and DeskStatus.PLAYING != validator.desk.status: # 最后一局结束,此时直接退出桌子 if validator.user.seat_id == validator.desk.owner_seat: # 房主退出, 桌子直接解散 response_data = {"need_agree": 0} validator.desk.notify_player(validator.user.seat_id, DISSOLVE_FRIEND_DESK, response_data) data = { "user_id": validator.user.user_id, "nick": validator.user.nick_name, "success": 1 } validator.desk.notify_desk(PUSH_DESK_DISSOLVE_RESULT, data) room_mgr.del_desk(validator.desk.desk_id) else: data = { "user_id": validator.user.user_id, "nick": validator.user.nick_name } validator.desk.notify_desk_some_user(PUSH_USER_EXIT, data, [validator.user.user_id]) room_mgr.user_exit(validator.user.user_id) response_data = {"need_agree": 0} validator.desk.notify_player(validator.user.seat_id, DISSOLVE_FRIEND_DESK, response_data) else: # 不是最后一局,需要发送解散请求征询其他玩家同意 data = { "user_id": validator.user.user_id, "nick": validator.user.nick_name } validator.desk.notify_desk_some_user(PUSH_DESK_DISSOLVE, data, [validator.user.user_id]) TimerTask.call_later(GlobalConfig().dissolve_time, answer_dissolve_desk, validator.desk, validator.user, agree=1, is_auto=1) response_data = {"need_agree": 1} validator.desk.notify_player(validator.user.seat_id, DISSOLVE_FRIEND_DESK, response_data) return {"need_push": 0}
def notify_player_next(self, seat_id, code=200): data = { "test_type": TestActionType.SURE_NEXT_CARDS, "seat_id": seat_id, "next_cards": GlobalConfig().test_sure_next_cards[self.desk_id][seat_id] } notify_single_user(self.desk_id, seat_id, USER_TEST_ACT, data, code)
def sure_next_cards(self, seat_id, test_params): if not self.game_data.game_config.test_mode: self.notify_player_card_change(seat_id, code=CANT_USE_TEST) return card_list = test_params.get("target_card") if not isinstance(card_list, list): self.notify_player_card_change(seat_id, TEST_PARAMS_ERROR) return logger.debug(u"确定接下来的牌:%s", str([seat_id, card_list])) if GlobalConfig().test_sure_next_cards.get(self.desk_id): GlobalConfig().test_sure_next_cards[self.desk_id][seat_id].extend( card_list) else: GlobalConfig().test_sure_next_cards[self.desk_id] = [ [] for _ in xrange(self.game_data.max_player_num) ] GlobalConfig().test_sure_next_cards[self.desk_id][seat_id].extend( card_list) self.notify_player_next(seat_id)
def execute(self, *args, **kwargs): """ 测试接口 :param args: :param kwargs: :return: """ validator = TestActValidator(handler=self) if validator.test_type.data == 4: cards = validator.test_params.data.get("target_card") if not GlobalConfig().test_sure_next_cards.get(validator.desk.desk_id): GlobalConfig().test_sure_next_cards[validator.desk.desk_id] = [[],[],[],[]] GlobalConfig().test_sure_next_cards[validator.desk.desk_id][validator.user.seat_id] = cards return {"need_push":1, "init_cards": cards, "test_type": 4} else: user_test_act(validator.desk.desk_id, validator.user.seat_id , validator.test_type.data, validator.test_params.data) return {"need_push": 0}
def apply_match_desk(user_id, session_id, max_player_num=4): """ 申请一张匹配桌 :return: 返回桌子对象, 用户对象 """ cur_desk_player_count = [[] for _ in range(max_player_num) ] # 当前桌子人数统计[[desk_id1, ...], [desk_id2, ...]...} # num = 0 for _id, desk in desk_mgr.match_desks.items(): max_player_num = desk.max_player_num # 匹配场麻将最大人数默认不变 if desk.is_full(): continue num = desk.people_count if num in cur_desk_player_count: cur_desk_player_count[num].append(desk) else: cur_desk_player_count[num] = [desk] desk_weight = [100 for _ in range(max_player_num)] for i, lst in enumerate(cur_desk_player_count): if 0 == len(lst): desk_weight[i] = 0 else: desk_weight[i] = desk_weight[i] * (1 + 10 * i) desk_weight[0] = 100 index = weight_choice(desk_weight) print("index = ", index, desk_weight) if 0 == index: # 创建桌子 desk_id = DBDesk.get_a_id() custom_config = GlobalConfig().room_cfg_list[0] print("bbbbbbbbbbbb:") desk = RoomDesk(desk_id, max_player_num=max_player_num, desk_type=DeskType.MATCH_DESK, custom_config=custom_config) print("bbbbbbbbbbddddbb1111:") desk_mgr.add_room_desk(desk) print("bbbbbbbbbbbb333333:") user = UserManager().add_user(user_id, desk_id, session_id) print("after join_desk UserManager=", UserManager()._session_user_dict) desk.user_sit(user, seat_id=0) return desk, user else: desk = random.choice(cur_desk_player_count[index]) user = UserManager().add_user(user_id, desk.desk_id, session_id) desk.user_sit(user) return desk, user
def set_data(self, **kwargs): self.drew_cardvals = [] for i in range(self.card_num): if self.game_data.max_player_num == 2: self.drew_cardvals.append( self.card_dealer.draw_a_card_first_without_hua( is_last=self.is_last, seat_id=self.seat_id, card_list=GlobalConfig().test_sure_next_cards.get( self.desk_id, [[], [], [], []]))) else: self.drew_cardvals.append( self.card_dealer.draw_a_card(is_last=self.is_last)) self.players[self.seat_id].hand_card.add_hand_card_by_vals( self.drew_cardvals) # 将最近入手的牌装入game_data self.game_data.last_get_card_val[self.seat_id] = self.drew_cardvals[0] # 当前牌局上正在操作的那张牌 self.game_data.cur_deal_card_val = self.drew_cardvals[0] return 1
def execute(self): """ 游戏结束 :return: """ logger.debug(u"游戏结束: %s", str([])) self.game_data.game_status = GameStatus.OVER # 重置状态机 self.game_data.state_machine.reset_data() # # 通知玩家游戏结束 data = {"player_info": {}} for x in xrange(self.game_data.max_player_num): data["player_info"][x] = {} data["player_info"][x]["hand_card"] = self.game_data.players[x].hand_card.hand_card_for_settle_show notify_all_desk_player(self.desk_id, messageids.PUSH_GAME_OVER, data=data) notify_desk_game_over(self.desk_id) GlobalConfig().reset_test_data()