def _calc_query(self, data, req, timer): """查询 """ if not data.is_valid(): #联盟不存在 res = self._pack_query_response_invalid(union_pb2.UNION_NOT_MATCHED) return self._query_succeed(data, req, res, timer) member = member_business.find_member(data, req.user_id) if member is None: res = self._pack_query_response_invalid(union_pb2.UNION_MEMBER_INVALID) else: res = self._pack_query_response(data, req.user_id, timer) defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer
def _treasure(self, user_id, request): """夺宝抽奖 """ type = 0 timer = Timer(user_id) req = activity_pb2.TurntableDrawReq() req.ParseFromString(request) if req.times == 1: type = 5 elif req.times == 10: type = 6 else: raise Exception("treasure times error") defer = DataBase().get_basic_data(basic_view.BASIC_ID) #获取basic data defer.addCallback(self._get_treasure_basic_data, user_id, req, type, timer) return defer
def _do_patch_query_info(self, matcher, data, req, res, timer): for aid_msg in res.aids: (name, level, icon, last_login_time, battle_score, honor) = matcher.result[aid_msg.sender.user_id] aid_msg.sender.name = name aid_msg.sender.level = level aid_msg.sender.headicon_id = icon if res.HasField("aid_own"): user = data.user.get(True) res.aid_own.sender.name = user.get_readable_name() res.aid_own.sender.level = user.level res.aid_own.sender.headicon_id = user.icon_id defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer
def _calc_search_visit(self, data, req, timer): """查询探访逻辑 """ node_basic_id = req.node.basic_id node_id = NodeInfo.generate_id(data.id, node_basic_id) node = data.node_list.get(node_id) candidate = [] if not visit_business.search_visit_event(data, node, candidate, timer.now): raise Exception("Search visit failed") res = self._pack_search_visit_response(candidate) defer = DataBase().commit(data) defer.addCallback(self._search_visit_succeed, req, res, timer) return defer
def _calc_query_basic_activity_hero_reward(self, basic_data, req, timer): """ """ rewards = [] for id in req.ids: reward = basic_data.activity_hero_reward_list.get(id) if reward is not None: rewards.append(reward) if req.HasField("is_all"): if req.is_all: rewards = basic_data.activity_hero_reward_list.get_all() defer = DataBase().commit_basic(basic_data) defer.addCallback(self._query_basic_activity_hero_reward_succeed, rewards, req, timer) return defer
def _calc_query_basic_activity_step(self, basic_data, req, timer): """ """ steps = [] for id in req.ids: step = basic_data.activity_step_list.get(id) if step is not None: steps.append(step) if req.HasField("is_all"): if req.is_all: steps = basic_data.activity_step_list.get_all() defer = DataBase().commit_basic(basic_data) defer.addCallback(self._query_basic_activity_step_succeed, steps, req, timer) return defer
def _do_create(self, is_name_exist, data, req, timer): if is_name_exist: #名字重复 logger.debug("Union name is conflict[name=%s]" % req.name) res = self._pack_invalid_query_response( union_pb2.UNION_NAME_CONFLICT) return self._create_succeed(None, req, res, timer) else: if not union_business.init_union(data, req.user_id, req.name, req.icon_id, timer.now): raise Exception("Init union failed") res = self._pack_query_response(data, req.user_id, timer, True) defer = DataBase().commit(data) defer.addCallback(self._create_succeed, req, res, timer) return defer
def _calc_join_result(self, union_response, data, req, timer): union_res = internal_union_pb2.InternalQueryUnionRes() union_res.ParseFromString(union_response) if union_res.status != 0: raise Exception("Join union res error") if union_res.ret != union_pb2.UNION_OK: res = union_pb2.JoinUnionRes() res.status = 0 res.ret = union_res.ret defer = DataBase().commit(data) defer.addCallback(self._join_succeed, req, res, timer) return defer else: return self._patch_query_info(union_res, data, req, timer)
def _calc_receive_deploy_force(self, data, req, timer): union = data.union.get(True) if not union.is_belong_to_target_union(req.union_id): #玩家不属于联盟 res = union_battle_pb2.DeployUnionBattleForceRes() res.status = 0 res.ret = union_pb2.UNION_NOT_MATCHED defer = DataBase().commit(data) defer.addCallback(self._receive_deploy_succeed, req, res, timer) return defer battle_score = data.battle_score.get() teams_index = battle_score.get_powerful_teams_id()[0:3] #取前三队 teams = [] for team_index in teams_index: team_id = TeamInfo.generate_id(data.id, team_index) team = data.team_list.get(team_id, True) teams.append(team) if not union_battle_business.is_able_to_deploy(data, req.node_index, teams, True): raise Exception("Team deploy invalid") union_req = internal_union_pb2.InternalDeployUnionBattleReq() user = data.user.get(True) union_req.user_id = data.id union_req.defender_user_id = data.id union_req.defender_user_name = user.get_readable_name() union_req.defender_user_icon = user.icon_id union_req.defender_user_level = user.level union_req.node_index = req.node_index for team in teams: team_msg = union_req.teams.add() team_msg.index = team.index for hero_id in team.get_heroes(): if hero_id != 0: hero = data.hero_list.get(hero_id, True) pack.pack_hero_info(hero, team_msg.heroes.add(), timer.now) for tech in data.technology_list.get_all(True): if tech.is_battle_technology() and not tech.is_upgrade: union_req.battle_tech_ids.append(tech.basic_id) defer = GlobalObject().remote['gunion'].callRemote( "deploy_battle", req.union_id, union_req.SerializeToString()) defer.addCallback(self._calc_receive_deploy_result, data, req, timer) return defer
def _calc_query(self, data, req, timer): union = data.union.get(True) if not union.is_belong_to_target_union(req.union_id): res = union_battle_pb2.QueryUnionBattleRes() res.status = 0 res.ret = union_pb2.UNION_NOT_MATCHED defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer union_req = internal_union_pb2.InternalQueryUnionBattleReq() union_req.user_id = data.id defer = GlobalObject().remote['gunion'].callRemote( "query_battle", req.union_id, union_req.SerializeToString()) defer.addCallback(self._calc_query_result, data, req, timer) return defer
def _calc_update_anneal_record(self, data, req, timer): """更新逻辑 """ if not data.is_valid(): if not console_business.init_console(data, timer.now): raise Exception("Init console failed") for record in req.records: if not anneal_record_business.update_anneal_record( data, req.floor, record.type, record.name, record.level, record.icon_id, record.finish_passed_time, record.cost_time, timer.now): raise Exception("Update anneal record failed") defer = DataBase().commit(data) defer.addCallback(self._update_anneal_record_succeed, req, timer) return defer
def _check_forward_be_invited_result(self, response, data, inviter_id, req, timer): res = user_pb2.InviteRes() res.ParseFromString(response) if res.status != 0: logger.warning("Forward be invited result failed") #raise Exception("Notice battle result failed") else: if res.ret == user_pb2.INVITE_OK: user = data.user.get() user.set_inviter(inviter_id) res.inviter_user_id = inviter_id defer = DataBase().commit(data) defer.addCallback(self._be_invited_succeed, req, res, timer) return defer
def _calc_view_apply_info(self, matcher_friend, condition, data, req, timer): res = friend_pb2.QueryFriendsRes() res.status = 0 res.ret = friend_pb2.FRIEND_OK for id in condition: (id, name, icon_id, level, score) = matcher_friend.result[id] message = res.friends.add() message.user_id = id message.name = name message.icon_id = icon_id message.level = level message.score = score logger.notice("message = %s" % message) defer = DataBase().commit(data) defer.addCallback(self._view_apply_succeed, req, res, timer) return defer
def _calc_query_cat(self, data, req, common_id, timer): cat_list = data.cat_list.get_all() if len(cat_list) == 0: res = activity_pb2.QueryCatRes() res.status = 0 return self._query_cat_succeed(data, req, res, timer) logger.notice("cat_len=%d" % len(cat_list)) candidates = [] candidates = cat_list[::-1] res = activity_pb2.QueryCatRes() res.status = 0 for candidate in candidates: cat = res.cats.add() cat.name = candidate.name cat.gold = candidate.gold defer = DataBase().commit(data) defer.addCallback(self._query_cat_succeed, req, res, timer) return defer
def _calc_query(self, data, req, timer): if not data.is_valid(): if not console_business.init_console(data, timer.now): raise Exception("Init console failed") exchange_business.refresh_exchange(data, timer.now) exchange = data.exchange.get() res = exchange_pb2.QueryExchangeProportionRes() res.status = 0 res.money2food_exchange_ratio = exchange.money_proportion res.food2money_exchange_ratio = exchange.food_proportion res.next_fresh_ratio_time = \ exchange.last_refresh_time + CommonExchangeInfo.refresh_time() - timer.now defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer
def _calc_add_honor(self, data, req, timer): if not data.is_valid(): #联盟不存在 res = self._pack_add_honor_response(union_pb2.UNION_NOT_MATCHED) return self._add_honor_succeed(data, req, res, timer) member = member_business.find_member(data, req.user_id) if member is None: #已经不在此联盟中 logger.debug("User is not belong to union") res = self._pack_add_honor_response(union_pb2.UNION_NOT_MATCHED) else: member.gain_honor(req.honor) res = self._pack_add_honor_response(union_pb2.UNION_OK) defer = DataBase().commit(data) defer.addCallback(self._add_honor_succeed, req, res, timer) return defer
def _calc_query_result(self, searcher, data, req, timer): res = union_battle_pb2.QueryUnionBattleIndividualsRes() res.status = 0 res.ret = union_pb2.UNION_OK for user_id in searcher.members: member = searcher.members[user_id] user = searcher.users[user_id] if member.union_id == req.union_id: pack.pack_union_battle_individual_info(member, user, res.own_side.add()) else: pack.pack_union_battle_individual_info(member, user, res.rival_side.add()) defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer
def _calc_add_honor_result(self, union_response, data, req, timer): union_res = internal_union_pb2.InternalAddUnionHonorRes() union_res.ParseFromString(union_response) if union_res.status != 0: raise Exception("Add union honor res error") if union_res.ret == union_pb2.UNION_OK: union = data.union.get() union.gain_honor(req.honor) res = union_pb2.JoinUnionRes() res.status = 0 res.ret = union_res.ret defer = DataBase().commit(data) defer.addCallback(self._add_honor_succeed, req, res, timer) return defer
def _pack_specify_user_response(self, proxy, data, plunder, plunder_matcher, req, timer): """构造返回 Returns: res[protobuf]: 向客户端的返回的响应 """ res = map_pb2.QueryPlayersRes() res.status = 0 for enemy in plunder_matcher._pvp_players: pack.pack_plunder_enemy(enemey, res.players.add()) res.attack_num = plunder.attack_num res.left_reset_num = plunder.get_left_reset_num() defer = DataBase().commit(data) defer.addCallback(self._query_pvp_players_succeed, req, res, timer) return defer
def _calc_reborn(self, data, req, timer): res = hero_pb2.RebornHeroRes() res.status = 0 if req.type == hero_pb2.RebornHeroReq.PERFECT: ret = hero_business.is_able_to_perfect_reborn_hero(data, timer.now) if ret != True: res.return_ret = ret return self._reborn_succeed(data, req, res, timer) is_perfect = True else: is_perfect = False open_flags = account_business.get_flags() if "HeroUpStarUseSoul" in open_flags: #分解 if not hero_business.resolve_hero(data, req.hero.basic_id, is_perfect, timer.now): res.return_ret = hero_pb2.RebornHeroRes.REBORN_ERROR return self._reborn_succeed(data, req, res, timer) else: if not hero_business.reborn_hero(data, req.hero.basic_id, is_perfect, timer.now): res.return_ret = hero_pb2.RebornHeroRes.REBORN_ERROR return self._reborn_succeed(data, req, res, timer) ''' if not compare.check_hero_r(data, req.hero, True, True, True, 0, -1, True, True, True): res.return_ret = hero_pb2.RebornHeroRes.REBORN_ERROR return self._reborn_succeed(data, req, res, timer) ''' #for item in req.items: # if not compare.check_item_r(data, item): # res.return_ret = hero_pb2.RebornHeroRes.REBORN_ERROR # return self._reborn_succeed(data, req, res, timer) resource = data.resource.get() resource.update_current_resource(timer.now) res.return_ret = hero_pb2.RebornHeroRes.REBORN_SUCCESS pack.pack_resource_info(resource, res.resource) defer = DataBase().commit(data) defer.addCallback(self._reborn_succeed, req, res, timer) return defer
def _calc_approve(self, data, req, timer): """审批 """ if not data.is_valid(): #联盟不存在 res = self._pack_approve_response(union_pb2.UNION_NOT_MATCHED) return self._approve_succeed(data, req, res, timer) member = member_business.find_member(data, req.user_id) target_member = member_business.find_member(data, req.target_user_id) application = member_business.find_application(data, req.target_user_id, timer.now) if member is None: res = self._pack_approve_response(union_pb2.UNION_NOT_MATCHED) elif member.is_normal_member(): #普通成员没有权限 res = self._pack_approve_response(union_pb2.UNION_NO_AUTH) elif target_member is not None: #待审批玩家已经加入联盟 res = self._pack_approve_response(union_pb2.UNION_OK) elif application is None: #申请已经失效 res = self._pack_approve_response( union_pb2.UNION_APPLICATION_INVALID) elif not req.agree: #拒绝申请 member_business.disagree_application(data, application) res = self._pack_approve_response(union_pb2.UNION_OK) elif req.agree: #同意申请 union = data.union.get() if union.is_member_full(): #已满员,拒绝申请 member_business.disagree_application(data, application) res = self._pack_approve_response( union_pb2.UNION_APPLICATION_INVALID) else: return self._accept(data, member, application, req, timer) defer = DataBase().commit(data) defer.addCallback(self._approve_succeed, req, res, timer) return defer
def _calc_query_summary(self, data, req, timer): """ """ if not data.is_valid(): #联盟不存在 res = self._pack_invalid_query_response( union_pb2.UNION_NOT_MATCHED) return self._query_succeed(data, req, res, timer) res = internal_union_pb2.InternalQueryUnionRes() res.status = 0 res.ret = union_pb2.UNION_OK union = data.union.get(True) pack.pack_union_info(union, res.union) defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer
def _calc_refresh_goods(self, data, req, types, timer): #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") shops = [] index = 1 if req.HasField("index"): index = req.index for type in types: shop_id = ShopInfo.generate_id(data.id, type, index) shops.append(data.shop_list.get(shop_id)) is_refresh_with_item = False refresh_item = None if req.HasField("item"): #用刷新代币刷新商铺 is_refresh_with_item = True item_id = ItemInfo.generate_id(data.id, req.item.basic_id) refresh_item = data.item_list.get(item_id) if refresh_item is None: raise Exception("Item not exist") free = False if shops[0].is_able_to_refresh_free(timer.now): free = True goods = [] if not shop_business.refresh_goods(data, shops, goods, timer.now, refresh_item, free): raise Exception("Refresh goods failed") if is_refresh_with_item: compare.check_item(data, req.item) resource = data.resource.get(True) #任意选择一个商店的刷新次数 res = self._pack_refresh_goods_response(goods, shops[0], resource, req, timer.now) defer = DataBase().commit(data) defer.addCallback(self._refresh_goods_succeed, types, req, res, timer) return defer
def _pack_event_response(self, matcher, data, items, mails, nodes, type, req, timer): """打包返回 response """ resource = data.resource.get(True) map = data.map.get(True) #更新邮件中的敌人信息 for mail in mails: if mail.related_node_id in matcher.players: rival_id = mail.related_node_id rival = data.rival_list.get(rival_id) mail.attach_enemy_info(rival) else: node = data.node_list.get(mail.related_node_id) if node.is_key_node(): mail.attach_enemy_detail( NameGenerator().gen(), 0, NodeInfo.ENEMY_TYPE_PVE_RESOURCE) #TODO 随机 #打包响应 res = map_pb2.TriggerEventRes() res.status = 0 #res.map.next_war_gap = map.next_war_time - timer.now res.map.next_luck_gap = map.next_luck_time - timer.now for node in nodes: pack.pack_node_info(data, node, res.map.nodes.add(), timer.now) pack.pack_resource_info(resource, res.resource) for item in items: item_message = res.items.add() item_message.basic_id = item[1] item_message.num = item[0] for mail in mails: pack.pack_mail_info(mail, res.mails.add(), timer.now) energy = data.energy.get(True) pack.pack_energy_info(energy, res.energy_info, timer.now) map_business.check_map_data(data) defer = DataBase().commit(data) defer.addCallback(self._trigger_event_succeed, req, res, timer) return defer
def _calc_query_transfer_arena_match(self, results, data, req, timer, ranks): res = ranking_pb2.QueryRankingRes() res.status = 0 for rank in ranks: rank_info = res.rankings.add() rank_info.id = rank.user_id rank_info.ranking = rank.rank rank_info.name = results[rank.user_id]['name'] rank_info.level = results[rank.user_id]['level'] rank_info.value = results[ rank.user_id]['battle_score'] if not rank.is_robot else 0 rank_info.icon_id = results[rank.user_id]['icon_id'] rank_info.value1 = int(rank.transfer_type) defer = DataBase().commit(data) defer.addCallback(self._query_transfer_arena_succeed, req, res, timer) return defer
def _calc_create_result_unionboss(self, union_response, data, req, res, timer): union_res = internal_union_pb2.InternalQueryUnionBossRes() union_res.ParseFromString(union_response) if union_res.status != 0: raise Exception("Query union boss failed") if union_res.ret != union_pb2.UNION_OK: res.ret = union_res.ret return self._create_succeed(data, req, res, timer) union_boss_business.try_update_union_boss(data, union_res.bosses_id, union_res.last_update_time) if len(union_res.bosses_id) != 0: self._pack_unionboss(data, union_res, res.union.union_boss, timer.now) defer = DataBase().commit(data) defer.addCallback(self._create_succeed, req, res, timer) return defer
def _calc_receive_notice(self, data, req, timer): """接收到战斗结果通知 1 计算资源变化 2 生成新邮件 """ if req.win: mail = self._calc_receive_win_notice(data, req.rival_type, timer.now) else: mail = self._calc_receive_lose_notice(data, req, req.rival_type, timer.now) if req.rival_type == NodeInfo.ENEMY_TYPE_PVP_CITY: #主城防守失败 node_id = NodeInfo.generate_id(data.id, 0) #0表示主城 node = data.node_list.get(node_id) if node.is_in_protect(timer.now): #主城若开保护罩 try: self._add_protect_broadcast(data.user.get(), req.rival_name) except: logger.warning("Send protect broadcast failed") else: #广播通知被掠夺消息 if req.HasField("lost_money") and req.HasField( "lost_food"): if req.lost_money != 0 or req.lost_food != 0: try: self._add_battle_defeated_broadcast( data.user.get(), req.rival_name, req.lost_money, req.lost_food) except: logger.warning( "Send battle defeated broadcast failed") if mail is not None: mail.attach_enemy_detail(req.rival_name, req.rival_user_id, req.rival_type) defer = DataBase().commit(data) defer.addCallback(self._receive_notice_succeed, req, mail, timer) return defer
def _calc_get_friends_info(self, matcher_friend, matcher, data, req, timer): res = friend_pb2.GetFriendsRes() res.status = 0 res.ret = friend_pb2.FRIEND_OK #proxy = DataProxy() for user in matcher.result: (id, name, icon_id, level, score) = matcher_friend.result[user.id] message = res.friends.add() message.user_id = id message.name = name message.icon_id = icon_id message.level = level message.score = score defer = DataBase().commit(data) defer.addCallback(self._get_friends_succeed, req, res, timer) return defer
def _draw(self, user_id, request, type): """ 抽奖 Args: request[protobuf] 请求 type[int]: 抽奖类型 _TYPE_GOLD_DRAW 元宝抽奖 _TYPE_MONEY_DRAW 金钱抽奖 _TYPE_GOLD_MULTI_DRAW 元宝十连抽 _TYPE_MONEY_MULTI_DRAW 金钱十连抽 """ timer = Timer(user_id) req = wineShop_pb2.WineShopDrawReq() req.ParseFromString(request) defer = DataBase().get_basic_data(basic_view.BASIC_ID) #获取basic data defer.addCallback(self._get_basic_data, user_id, req, type, timer) return defer
def _calc_query(self, data, req, timer): #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") transfer = data.transfer.get(True) transfer_records = data.transfer_record_list.get_all(True) res = transfer_arena_pb2.QueryTransferArenaRes() res.status = 0 res.arena_info.remain_times = transfer.get_remain_times() res.arena_info.cd_end_time = transfer.get_cd_end_time(timer.now) for transfer_record in transfer_records: pack.pack_transfer_record(transfer_record, res.arena_info.records.add()) defer = DataBase().commit(data) defer.addCallback(self._query_succeed, req, res, timer) return defer