def _calc_demount_equipment_stone(self, data, req, timer): """装备卸下宝石逻辑 """ heroes_basic_id = [] equipments_type = [] equipments_stones_id =[] for hero_info in req.heros: heroes_basic_id.append(hero_info.basic_id) if len(hero_info.stone_weapon) != 0: equipments_type.append(HeroInfo.EQUIPMENT_TYPE_WEAPON) equipments_stones_id.append(hero_info.stone_weapon) elif len(hero_info.stone_armor) != 0: equipments_type.append(HeroInfo.EQUIPMENT_TYPE_ARMOR) equipments_stones_id.append(hero_info.stone_armor) elif len(hero_info.stone_treasure) != 0: equipments_type.append(HeroInfo.EQUIPMENT_TYPE_TREASURE) equipments_stones_id.append(hero_info.stone_treasure) else: raise Exception("Equipment type error") item_list = [] if not hero_business.demount_equipment_stone(data, heroes_basic_id, equipments_type, equipments_stones_id, req.item.basic_id, timer.now): raise Exception("Demount equipment stone failed") #验证 for i in range(len(req.heros)): compare.check_hero(data, req.heros[i], with_equipment_type = equipments_type[i], with_equipment_stone = True) compare.check_item(data, req.item) return DataBase().commit(data)
def _calc_upgrade_equipment_stone(self, data, req, timer): """装备上的宝石升级逻辑 """ hero_id = HeroInfo.generate_id(data.id, req.hero.basic_id) hero = data.hero_list.get(hero_id) if hero is None: raise Exception("Hero not exist") if len(req.hero.stone_weapon) != 0: equipment_type = HeroInfo.EQUIPMENT_TYPE_WEAPON equipment_stones_id = req.hero.stone_weapon elif len(req.hero.stone_armor) != 0: equipment_type = HeroInfo.EQUIPMENT_TYPE_ARMOR equipment_stones_id = req.hero.stone_armor elif len(req.hero.stone_treasure) != 0: equipment_type = HeroInfo.EQUIPMENT_TYPE_TREASURE equipment_stones_id = req.hero.stone_treasure else: raise Exception("Equipment type error") if not hero_business.upgrade_equipment_stone(data, req.hero.basic_id, req.item.basic_id, equipment_type, equipment_stones_id, timer.now): raise Exception("Upgrade equipment stone failed") #验证 compare.check_hero(data, req.hero, with_equipment_type = equipment_type, with_equipment_stone = True) compare.check_item(data, req.item) return DataBase().commit(data)
def _calc_own_finish_result(self, union_response, rival_union_res, win, own_soldier_info, enemy_soldier_info, data, skip, heroes, req, timer): union_res = internal_union_pb2.InternalFinishUnionBattleRes() union_res.ParseFromString(union_response) if union_res.status != 0: raise Exception("Query union battle res error") score = 0 if union_res.HasField("individual_battle_score_add"): score = union_res.individual_battle_score_add #结束战斗 if win: if not union_battle_business.win_battle(data, enemy_soldier_info, own_soldier_info, score, timer.now): raise Exception("Win battle failed") else: if not union_battle_business.lose_battle(data, enemy_soldier_info, own_soldier_info, score, timer.now): raise Exception("Lose battle failed") #检查请求 if win: compare.check_user(data, req.monarch, with_level=True) for info in req.battle.attack_heroes: compare.check_hero(data, info.hero, with_level=True) for item_info in req.items: compare.check_item(data, item_info) res = union_battle_pb2.FinishUnionBattleRes() res.status = 0 if union_res.ret != union_pb2.UNION_OK: res.ret = union_res.ret else: res.ret = rival_union_res.ret if skip: pack.pack_monarch_info(data.user.get(True), res.monarch) if win: res.battle_output.result = battle_pb2.BattleOutputInfo.WIN else: res.battle_output.result = battle_pb2.BattleOutputInfo.LOSE for i, hero in enumerate(heroes): attack_hero = res.battle_output.attack_heroes.add() pack.pack_hero_info(hero, attack_hero.hero, timer.now) attack_hero.soldier_survival = own_soldier_info[i][2] if union_res.HasField("battle") and rival_union_res.HasField("battle"): defer = UnionBattlePatcher().patch_by_both_message( res.battle, union_res.battle, rival_union_res.battle, data, timer.now) defer.addCallback(self._calc_finish_post, data, req, res, timer) return defer return self._calc_finish_post(None, data, req, res, timer)
def _calc_upgrade_star(self, data, req, timer): """重现客户端升星计算逻辑,和请求进行比较 """ res = hero_pb2.UpdateHeroRes() res.status = 0 hero_id = HeroInfo.generate_id(data.id, req.hero.basic_id) hero = data.hero_list.get(hero_id) item_id = ItemInfo.generate_id(data.id, req.item[0].basic_id) item = data.item_list.get(item_id) if hero is None or item is None: raise Exception("Hero or item not exist") #消耗精魄 soul_num = 0 open_flags = account_business.get_flags() if "HeroUpStarUseSoul" in open_flags: soul_num = data_loader.HeroStarLevelBasicInfo_dict[ req.hero.star_level].soul #使用灵魂石 consume_num = data_loader.HeroStarLevelBasicInfo_dict[ req.hero.star_level].perceptivity ret = hero_business.star_upgrade(data, hero, item, consume_num, soul_num, timer.now) if ret != hero_pb2.HERO_OK: res.ret = ret return self._upgrade_star_succeed(data, req, res, timer) #和请求进行比较 if req.hero.star_level != hero.star: raise Exception("Unexpect hero star[expect star=%d]" % hero.star) #验证 compare.check_hero(data, req.hero, with_star=True) compare.check_item(data, req.item[0]) resource = data.resource.get() resource.update_current_resource(timer.now) res.ret = hero_pb2.HERO_OK pack.pack_resource_info(resource, res.resource) try: if hero.is_need_broadcast_star_level(): self._add_upgrade_star_level_broadcast(data.user.get(), hero) except: logger.warning("Send upgrade star level broadcast failed") defer = DataBase().commit(data) defer.addCallback(self._upgrade_star_succeed, req, res, timer) return defer
def _calc_update_soldier(self, data, req, timer): hero_id = HeroInfo.generate_id(data.id, req.hero.basic_id) hero = data.hero_list.get(hero_id) soldier_id = SoldierInfo.generate_id(data.id, req.hero.soldier_basic_id) soldier = data.soldier_list.get(soldier_id, True) if not hero_business.soldier_update(data, hero, soldier, timer.now): raise Exception("Soldier update failed") #验证 compare.check_hero(data, req.hero, with_soldier=True) return DataBase().commit(data)
def _calc_upgrade_level(self, data, req, timer): """重现客户端英雄升级计算逻辑,和请求进行比较 Args: data[UserData]: 升级前的数据,从数据库中获得 req[UpdateHeroReq]: 升级后的数据,客户端发送的请求 Returns: data[UserData]: 升级后的数据 """ items = [] for item in req.item: item_id = ItemInfo.generate_id(data.id, item.basic_id) item = data.item_list.get(item_id) items.append(item) #新逻辑,传过来经验丹的消耗数量,以防客户端和服务器物品数量不一致 use_items = [] for item in req.use_item: item_id = ItemInfo.generate_id(data.id, item.basic_id) item = data.item_list.get(item_id) use_items.append(item) hero_id = HeroInfo.generate_id(data.id, req.hero.basic_id) hero = data.hero_list.get(hero_id) if (len(items) == 0 and len(use_items) == 0) or hero is None: raise Exception("Items or hero not exist") if len(use_items) != 0: #走新逻辑 for i in range(len(use_items)): consume_num = req.use_item[i].num if not hero_business.level_upgrade_by_item( data, hero, use_items[i], consume_num, timer.now): raise Exception("Hero level upgrade failed") else: for i in range(len(items)): consume_num = items[i].num - req.item[i].num if not hero_business.level_upgrade_by_item( data, hero, use_items[i], consume_num, timer.now): raise Exception("Hero level upgrade failed") #验证 compare.check_hero(data, req.hero, with_level=True, with_soldier=True) #for item in req.item: # compare.check_item(data, item) return DataBase().commit(data)
def _calc_upgrade_equipment(self, data, req, timer): #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") cost_gold = 0 if req.HasField("gold"): cost_gold = req.gold if not hero_business.equipment_upgrade( data, req.hero.basic_id, req.type, cost_gold, timer.now): raise Exception("Upgrade equipment failed") #验证 compare.check_hero(data, req.hero, with_equipment_type = req.type) for item_info in req.items: compare.check_item(data, item_info) return DataBase().commit(data)
def _calc_enchant_equipment(self, data, req, timer): #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") #精炼后的装备 id if req.type == HeroInfo.EQUIPMENT_TYPE_WEAPON: dest_equipment_id = req.hero.equipment_weapon_id elif req.type == HeroInfo.EQUIPMENT_TYPE_ARMOR: dest_equipment_id = req.hero.equipment_armor_id elif req.type == HeroInfo.EQUIPMENT_TYPE_TREASURE: dest_equipment_id = req.hero.equipment_treasure_id else: raise Exception("Invalid input equipment type[type=%d]" % req.type) #精炼消耗的材料 cost_item = [] for item_info in req.items: item_id = ItemInfo.generate_id(data.id, item_info.basic_id) item = data.item_list.get(item_id) cost_num = item.num - item_info.num cost_item.append((item.id, cost_num)) #精炼消耗的元宝 cost_gold = 0 if req.HasField("gold"): cost_gold = req.gold if not hero_business.equipment_enchant( data, req.hero.basic_id, req.type, dest_equipment_id, cost_item, cost_gold, timer.now): raise Exception("Enchant equipment failed") #验证 compare.check_hero(data, req.hero, with_equipment_type = req.type) for item_info in req.items: compare.check_item(data, item_info) return DataBase().commit(data)
def _calc_finish_question(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) if not question_business.finish_question_event( data, node, req.question_id, req.answer, req.correct, timer.now): raise Exception("Finish question failed") #验证 if req.HasField("hero"): compare.check_hero(data, req.hero, with_level=True, with_soldier=True) for item_info in req.items: compare.check_item(data, item_info) return DataBase().commit(data)
def _calc_finish_visit(self, data, req, timer): """结束探访逻辑 """ #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") node_basic_id = req.node.basic_id node_id = NodeInfo.generate_id(data.id, node_basic_id) node = data.node_list.get(node_id) gold = 0 if req.HasField("gold"): gold = req.gold candidate = [] if not visit_business.finish_visit_event(data, node, req.visit_id, timer.now, gold): raise Exception("Finish visit failed") #验证 if req.HasField("hero"): compare.check_hero(data, req.hero) if req.HasField("item"): compare.check_item(data, req.item) #获得S级武将要播广播 if req.visit_id != 0: hero_basic_id = data_loader.EventVisitBasicInfo_dict[ req.visit_id].heroBasicId if hero_basic_id != 0 and hero_business.is_need_broadcast( hero_basic_id): try: self._add_get_hero_broadcast(data.user.get(), hero_basic_id) except: logger.warning("Send get hero broadcast failed") return DataBase().commit(data)
def _calc_awaken(self, data, req, timer): res = hero_pb2.AwakeningHeroRes() res.status = 0 temple = None for building in data.building_list.get_all(True): if building.is_temple(): temple = building break if temple is None or temple.level < int( float( data_loader. OtherBasicInfo_dict['unlock_hero_awakening_level'].value)): res.return_ret = hero_pb2.AwakeningHeroRes.ERROR_BUILDING return self._awaken_succeed(data, req, res, timer) hero = hero_business.get_hero_by_id(data, req.hero.basic_id) if hero is None: res.return_ret = hero_pb2.AwakeningHeroRes.ERROR_HERO return self._awaken_succeed(data, req, res, timer) ret = hero_business.awaken_hero(data, hero, timer.now) if ret != hero_pb2.AwakeningHeroRes.SUCCESS: res.return_ret = ret return self._awaken_succeed(data, req, res, timer) try: compare.check_hero(data, req.hero, with_awaken=True) for item_info in req.items: compare.check_item(data, item_info) except: res.return_ret = hero_pb2.AwakeningHeroRes.INVALID return self._awaken_succeed(data, req, res, timer) res.return_ret = hero_pb2.AwakeningHeroRes.SUCCESS defer = DataBase().commit(data) defer.addCallback(self._awaken_succeed, req, res, timer) return defer
def _calc_upgrade_evolution_level(self, data, req, timer): """重现客户端升星计算逻辑,和请求进行比较 """ hero_id = HeroInfo.generate_id(data.id, req.hero.basic_id) hero = data.hero_list.get(hero_id) item_id = ItemInfo.generate_id(data.id, req.item[0].basic_id) item = data.item_list.get(item_id) if hero is None or item is None: raise Exception("Hero or item not exist") #使用突破石 consume_num = item.num - req.item[0].num if not hero_business.evolution_level_upgrade(data, hero, item, consume_num, timer.now): raise Exception("Hero evolution level upgrade failed]") #和请求进行比较 if req.hero.evolution_level != hero.evolution_level: raise Exception( "Unexpect hero evolution level[expect evolution level=%d]" % hero.evolution_level) #验证 compare.check_hero(data, req.hero, with_evolution=True) compare.check_item(data, req.item[0]) if hero.is_need_broadcast(): #玩家英雄达到一定等级时,广播 try: self._add_upgrade_evolution_level_broadcast( data.user.get(), hero) except: logger.warning("Send upgrade evolution level broadcast failed") return DataBase().commit(data)