def execute(self, *args, **kwargs): """ 加入匹配场桌子请求处理 :param args: :param kwargs: :return: """ validator = JoinMatchDeskValidator(handler=self) desk, user = apply_match_desk(validator.user_id.data, validator.session_id.data, max_player_num=2) print "after JoinMatchDeskHandler _id_user_dict =", UserManager( )._id_user_dict print "after JoinMatchDeskHandler _session_user_dict=", UserManager( )._session_user_dict session_gate_ins.update_rel(validator.session_id.data, self.gate_name) data = user.to_dict() r = notify_web_server_join_room(validator.user_id.data, validator.session_id.data) print "JoinMatchDeskHandler r=", r desk.notify_desk_some_user(PUSH_USER_JOIN_DESK, data, [user.user_id]) return { "desk_id": desk.desk_id, "seat_info": desk.get_users_info(), "room_type": 0 }
def execute(self, *args, **kwargs): """ 解散二人麻将匹配桌子请求处理 :param args: :param kwargs: :return: {need_agree: 0/1} 成功退出时0, 1表示需要其他玩家同意 """ print "DissolveDeskHandler!!" validator = DissolveDeskValidator(handler=self) url = "http://127.0.0.1:8889/mj/left_room" print "DissolveDeskHandler desk_user_ids=", validator.desk_user_ids params = { "user_id": ujson.dumps(validator.desk_user_ids), "session_id": validator.session_id.data, "room_type": 0 } print "DissolveDeskHandler params=", params r = requests.get(url, params) print "DissolveDeskHandler r=", r.text # 房主退出, 桌子直接解散 response_data = {"need_agree": 0} validator.desk.notify_player(validator.user.seat_id, DISSOLVE_MATCH_DESK, response_data) data = { "user_id": validator.user_id.data, "nick": validator.user.nick_name, "success": 1 } validator.desk.notify_desk(PUSH_MATCH_DESK_DISSOLVE_RESULT, data) room_mgr.del_desk(validator.desk.desk_id) UserManager().exit_user(validator.user_id.data) return {"need_push": 0}
def execute(self, *args, **kwargs): """ 玩家断线重连 :param args: :param kwargs: :return: """ validator = UserReconnectValidator(handler=self) UserManager().add_user(validator.user.user_id, validator.desk.desk_id, self.session_id) session_gate_ins.update_rel(self.session_id, self.gate_name) validator.user.is_online = 1 data = {"user_id": validator.user.user_id, "nick": validator.user.nick_name} validator.desk.notify_desk(PUSH_USER_RECONNECT, data) game_data = validator.desk.get_reconnect_info(validator.user.seat_id) user_info = validator.desk.get_users_info() end_time = game_data["wait_task"].get("end_time", 0) if end_time: game_data["wait_task"]["end_time"] = end_time - time.time() else: game_data["wait_task"]["end_time"] = end_time validator.desk.notify_player(validator.user.seat_id, USER_RECONNECT, {"user_info": user_info, "game_data": game_data}) if game_data.get('wait_task', None): pass # 每个玩家同时只可能有一个定时任务待执行 # validator.desk.notify_player(validator.user.seat_id, game_data["wait_task"]["command_id"], act_info) return {"need_push": 0}
def apply_match_desk(user_id, session_id, max_player_num=4): """ 申请一张匹配桌 :return: 返回桌子对象, 用户对象 """ cur_desk_player_count = [[] for _ in xrange(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 xrange(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 if 0 == index: # 创建桌子 desk_id = DBDesk.get_a_id() custom_config = GlobalConfig().room_cfg_list[0] desk = RoomDesk(desk_id, max_player_num=max_player_num, desk_type=DeskType.MATCH_DESK, custom_config=custom_config) desk_mgr.add_room_desk(desk) 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 execute(self, *args, **kwargs): """ 加入桌子请求处理 :param args: :param kwargs: :return: """ validator = JoinDeskValidator(handler=self) user = UserManager().add_user(validator.user_id.data, validator.desk_id.data, validator.session_id.data) session_gate_ins.update_rel(validator.session_id.data, self.gate_name) desk = desk_mgr.get_room_desk(validator.desk_id.data) desk.user_sit(user) data = user.to_dict() desk.notify_desk_some_user(PUSH_USER_JOIN_DESK, data, [user.user_id]) return {"desk_id": validator.desk_id.data, "seat_info": desk.get_users_info()}
def execute(self, *args, **kwargs): """ 玩家请求退出桌子 :param args: :param kwargs: :return: """ validator = UserExitValidator(handler=self) data = validator.user.to_dict() validator.desk.notify_desk_some_user(PUSH_USER_EXIT, data, [validator.user.user_id]) session_gate_ins.del_rel(validator.session_id.data) validator.desk.user_exit(validator.user.user_id) UserManager().exit_user(validator.user.user_id) return {"code": 200}
def validate_session_id(self, field): if not UserManager().get_user_by_sessionid(self.session_id.data): raise validators.ValidationError(errorcode.USER_NOT_FOUND_ON_DESK)
def validate_user_id(self, field): if UserManager().get_user_by_id(self.user_id.data): raise validators.ValidationError(errorcode.USER_IN_OTHER_DESK)
def cur_user_from_manager_by_id(self, user_id): return UserManager().get_user_by_id(user_id=user_id)
def cur_user_from_manager(self): return UserManager().get_user_by_sessionid(session_id=self.session_id)