def _calc_wear(self, data, req, timer): herostar = herostar_business.get_herostar_by_id(data, req.hero_star) if herostar is None: raise Exception("No Such HeroStar") if HeroStarInfo.get_herostar_level(herostar.star_id) <= 0: raise Exception("Can't wear herostar which level is 0") hero = hero_business.get_hero_by_id(data, req.hero.basic_id) if hero is None: raise Exception("No Such Hero") if req.hero_star in hero.get_herostars_set(): raise Exception("HeroStar Already weared.") index = [item_id for item_id in req.hero.hero_star].index(req.hero_star) if not herostar_business.is_hero_star_ok(data, hero, index): raise Exception("Hero Star too low") if hero.get_herostars()[index] != 0: raise Exception("Weared a herostar in this pos") herostar_business.wear_herostar(data, hero, req.hero_star, index, timer) if not herostar_business.validate_hero_herostar(data, hero, req.hero): raise Exception("Bad herostar") return DataBase().commit(data)
def _finish_visit_succeed(self, data, req, timer): res = event_pb2.FinishEventVisitRes() res.status = 0 if req.HasField("hero"): hero = hero_business.get_hero_by_id(data, req.hero.basic_id, True) pack.pack_hero_info(hero, res.hero, timer.now) pack.pack_resource_info(data.resource.get(True), res.resource) response = res.SerializeToString() log = log_formater.output(data, "Finish visit succeed", req, res, timer.count_ms()) logger.notice(log) return response
def _calc_unload(self, data, req, timer): herostar = herostar_business.get_herostar_by_id(data, req.hero_star) if herostar is None: raise Exception("No Such HeroStar") hero = hero_business.get_hero_by_id(data, req.hero.basic_id) if hero is None: raise Exception("No Such Hero") if req.hero_star not in hero.get_herostars_set(): raise Exception("No Such herostar in this hero") herostar_business.unload_herostar(data, hero, req.hero_star, timer) if not herostar_business.validate_hero_herostar(data, hero, req.hero): raise Exception("Bad herostar") return DataBase().commit(data)
def _calc_refine_upgrade(self, data, req, timer): res = hero_pb2.RefineHeroRes() res.status = 0 hero = hero_business.get_hero_by_id(data, req.hero.basic_id) if hero is None: raise Exception( "Hero not non-existent[user_id=%d][req_basic_id=%d]" % (data.id, req.hero.basic_id)) ret = hero_business.hero_refine_upgrade(data, hero, timer.now) if ret == res.Refine_CANNT_UPGRADE: return self._pack_refine_cannt_upgrade(data, req, hero, timer) return self._pack_refine_upgrade_succeed(data, req, hero, timer)
def _calc_refine(self, data, req, timer): res = hero_pb2.RefineHeroRes() res.status = 0 hero = hero_business.get_hero_by_id(data, req.hero.basic_id) if hero is None: raise Exception( "Hero not non-existent[user_id=%d][req_basic_id=%d]" % (data.id, req.hero.basic_id)) ret = hero_business.hero_refine(data, hero) if ret == res.Refine_NEED_ITEM: return self._pack_refine_need_item(data, req, hero, timer) return self._pack_refine_succeed(data, req, hero, timer)
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_finish(self, data, req, timer): union = data.union.get(True) if not union.is_belong_to_target_union(req.union_id): #玩家不属于联盟 res = union_battle_pb2.FinishUnionBattleRes() res.status = 0 res.ret = union_pb2.UNION_NOT_MATCHED defer = DataBase().commit(data) defer.addCallback(self._finish_succeed, req, res, timer) return defer skip = False if req.HasField("is_skip") and req.is_skip == True: #跳过战斗 skip = True union = data.union.get() node_id = NodeInfo.generate_id( data.id, union.get_battle_mapping_node_basic_id()) battle = data.battle_list.get(node_id) rival = data.rival_list.get(battle.rival_id, True) teams = [] for team_index in battle.get_battle_team(): team_id = TeamInfo.generate_id(data.id, team_index) teams.append(data.team_list.get(team_id)) heroes = [] for hero_id in battle.get_battle_hero(): hero = hero_business.get_hero_by_id(data, hero_id) heroes.append(hero) #存活兵力信息 if skip: (win, own_soldier_info, enemy_soldier_info) = \ appoint_business.calc_battle_result(data, battle, rival, teams, heroes) else: own_soldier_info = [] for result in req.battle.attack_heroes: hero_basic_id = result.hero.basic_id hero_id = HeroInfo.generate_id(data.id, hero_basic_id) hero = data.hero_list.get(hero_id, True) own_soldier_info.append( (hero.soldier_basic_id, hero.soldier_level, result.soldier_survival)) enemy_soldier_info = [] for result in req.battle.defence_heroes: enemy_soldier_info.append( (result.hero.soldier_basic_id, result.hero.soldier_level, result.soldier_survival)) if req.battle.result == req.battle.WIN: win = True else: win = False #检查是否可以结束战斗 if not union_battle_business.is_able_to_finish_battle(data, timer.now): raise Exception("Not able to finish battle") #结束战斗,向防守方联盟发送结束请求 user = data.user.get(True) union_req = internal_union_pb2.InternalFinishUnionBattleReq() union_req.is_attack_side = False union_req.node_index = req.node_index union_req.node_level = req.node_level union_req.is_attacker_win = win union_req.attacker_user_id = data.id union_req.attacker_user_level = user.level union_req.defender_user_id = req.rival_user_id union_req.attacker_kills = union_battle_business.calc_kills( enemy_soldier_info) union_req.defender_kills = union_battle_business.calc_kills( own_soldier_info) defer = GlobalObject().remote['gunion'].callRemote( "finish_battle", req.rival_union_id, union_req.SerializeToString()) defer.addCallback(self._calc_rival_finish_result, union_req, win, own_soldier_info, enemy_soldier_info, data, skip, heroes, req, timer) return defer
def _calc_finish_battle(self, data, req, timer): #兵力信息 res = union_boss_pb2.FinishUnionBossBattleRes() res.status = 0 own_soldier_info = [] for result in req.battle.attack_heroes: hero_basic_id = result.hero.basic_id hero = hero_business.get_hero_by_id(data, hero_basic_id) own_soldier_info.append(( hero.soldier_basic_id, hero.soldier_level, result.soldier_survival)) enemy_soldier_info = [] if req.battle.HasField("relive_times"): for i in range(req.battle.relive_times): for result in req.battle.defence_heroes: #攻打世界boss时有重生逻辑,实际杀敌会更多 enemy_soldier_info.append(( result.hero.soldier_basic_id, result.hero.soldier_level, 0)) for result in req.battle.defence_heroes: enemy_soldier_info.append(( result.hero.soldier_basic_id, result.hero.soldier_level, result.soldier_survival)) #计算杀敌数 kill_soldier_num = battle_business._calc_number_of_death(enemy_soldier_info) node_id = NodeInfo.generate_id(data.id, UserUnionInfo.get_union_boss_node_basic_id()) node = data.node_list.get(node_id) rival = data.rival_list.get(node_id) boss = union_boss_business.get_boss_by_id(data, rival.rival_id) change_nodes = [] new_items = [] new_mails = [] new_arena_records = [] if req.battle.result == req.battle.WIN: win = True if not battle_business.win_battle( data, node, enemy_soldier_info, own_soldier_info, change_nodes, timer.now, new_arena_records, is_unionboss=True): raise Exception("win union boss battle failed") else: win = False if not battle_business.lose_battle( data, node, timer.now, enemy_soldier_info, own_soldier_info, change_nodes, new_items, new_mails, new_arena_records): raise Exception("loss union boss battle failed") union_boss_business.finish_battle(data, boss, win, kill_soldier_num) if req.battle.result == req.battle.WIN: if not compare.check_user_r(data, req.monarch, with_level=True): res.battle_ret = union_pb2.BATTLE_MONARCH_ERROR return self._finish_battle_succeed(data, req, res, timer) for info in req.battle.attack_heroes: if not compare.check_hero_r(data, info.hero, with_level=True): res.battle_ret = union_pb2.BATTLE_HERO_ERROR return self._finish_battle_succeed(data, req, res, timer) for item_info in req.items: compare.check_item(data, item_info) #请求gunion模块 user = data.user.get(True) union_req = internal_union_pb2.InternalSyncBossReq() union_req.user_id = data.id union_req.user_name = user.name union_req.boss_id = boss.boss_id union_req.kill_addition = kill_soldier_num defer = GlobalObject().remote['gunion'].callRemote( "sync_unionboss", req.union_id, union_req.SerializeToString()) defer.addCallback(self._calc_finish_battle_result, data, req, own_soldier_info, enemy_soldier_info, kill_soldier_num, boss, timer) return defer