def _calc_reset_hard_attack_num(self, data, req, timer): res = anneal_pb2.ResetHardAttackNumRes() res.status = 0 ret = Ret() if not anneal_business.reset_hard_attack_num(data, req.floor, timer.now, ret): if ret.get() == 'GOLD_NOT_ENOUGH': res.ret = anneal_pb2.ResetHardAttackNumRes.GOLD_NOT_ENOUGH elif ret.get() == 'VIP_NOT_ENOUGH': res.ret = anneal_pb2.ResetHardAttackNumRes.VIP_NOT_ENOUGH return self._reset_hard_attack_num_succeed(data, req, res, timer) resource = data.resource.get() resource.update_current_resource(timer.now) res.ret = anneal_pb2.ResetHardAttackNumRes.OK pack.pack_resource_info(resource, res.resource) pack.pack_anneal_info(data, data.anneal.get(True), res.anneal, timer.now) defer = DataBase().commit(data) defer.addCallback(self._reset_hard_attack_num_succeed, req, res, timer) return defer
def _calc_buy(self, data, req, timer): res = transfer_arena_pb2.BuyChallengeTimesRes() res.status = 0 #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") ret = Ret() if not transfer_business.buy_attack_times(data, timer.now, ret): if ret.get() == "NO_ENOUGH_GOLD": res.ret = transfer_arena_pb2.BuyChallengeTimesRes.NO_ENOUGH_GOLD return self._buy_succeed(data, req, res, timer) elif ret.get() == "UPPER_LIMIT": res.ret = transfer_arena_pb2.BuyChallengeTimesRes.UPPER_LIMIT return self._buy_succeed(data, req, res, timer) transfer = data.transfer.get() transfer_records = data.transfer_record_list.get_all(True) res.ret = transfer_arena_pb2.BuyChallengeTimesRes.OK 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()) resource = data.resource.get(True) pack.pack_resource_info(resource, res.resource) defer = DataBase().commit(data) defer.addCallback(self._buy_succeed, req, res, timer) return defer
def _calc_end_conscript(self, data, req, timer, force): #涉及到跨天的数据统计,所以此处要更新所有跨天数据 res = conscript_pb2.EndConscriptRes() res.status = 0 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") user = data.user.get(True) resource = data.resource.get() conscript_building_id = BuildingInfo.generate_id( data.id, req.building.city_basic_id, req.building.location_index, req.building.basic_id) conscript_building = data.building_list.get(conscript_building_id) conscript = data.conscript_list.get(conscript_building_id) work_heroes = [] #来征兵的英雄 work_heroes_id = conscript_building.get_working_hero() for hero_id in work_heroes_id: if hero_id == 0: work_heroes.append(None) else: hero = data.hero_list.get(hero_id) work_heroes.append(hero) ret = Ret() if not conscript_business.end_conscript(data, conscript, conscript_building, resource, user, work_heroes, timer.now, force): if ret.get() == "NOT_CONSCRIPTING": res.ret = conscript_pb2.EndConscriptRes.NOT_CONSCRIPTING pack.pack_conscript_info(conscript, res.conscript, timer.now) pack.pack_building_info(conscript_building, res.building, timer.now) return self._end_conscript_succeed(data, req, res, timer) elif ret.get() == "CANNT_END": res.ret = conscript_pb2.EndConscriptRes.CANNT_END pack.pack_conscript_info(conscript, res.conscript, timer.now) pack.pack_building_info(conscript_building, res.building, timer.now) return self._end_conscript_succeed(data, req, res, timer) else: raise Exception('End conscript failed') #由于网络原因导致end_conscript一直重发,此处不check,直接返回 #compare.check_conscript(data, req.conscript) res.ret = conscript_pb2.EndConscriptRes.OK pack.pack_resource_info(data.resource.get(True), res.resource) defer = DataBase().commit(data) defer.addCallback(self._end_conscript_succeed, req, res, timer) return defer
def _calc_start_battle(self, data, req, timer): res = transfer_arena_pb2.StartTransferArenaBattleRes() res.status = 0 #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") ret = Ret() if not transfer_business.start_battle(data, req.target_id, timer.now, ret): if ret.get() == "NO_CHALLENGE_TIMES": res.ret = transfer_arena_pb2.StartTransferArenaBattleRes.NO_CHALLENGE_TIMES elif ret.get() == "COOLING": res.ret = transfer_arena_pb2.StartTransferArenaBattleRes.COOLING elif ret.get() == "TARGET_ERROR": res.ret = transfer_arena_pb2.StartTransferArenaBattleRes.TARGET_ERROR return self._start_battle_succeed(data, req, res, timer) res.ret = transfer_arena_pb2.StartTransferArenaBattleRes.OK defer = DataBase().commit(data) defer.addCallback(self._start_battle_succeed, req, res, timer) return defer
def _calc_finish_exploit(self, data, req, timer, force): """结束开采 """ res = exploitation_pb2.ExploitRes() res.status = 0 node_basic_id = req.node.basic_id node_id = NodeInfo.generate_id(data.id, node_basic_id) node = data.node_list.get(node_id) change_nodes = [] new_items = [] new_mails = [] ret = Ret() if not exploitation_business.finish_exploit_event( data, node, timer.now, timer.now, change_nodes, new_items, new_mails, force, ret): if ret.get() == 'NOT_EXPLOITING': res.ret = exploitation_pb2.ExploitRes.NOT_EXPLOITING pack.pack_node_info(data, node, res.node, timer.now) return self._finish_exploit_succeed(data, req, res, timer) elif ret.get() == 'CANNT_FINISH': res.ret = exploitation_pb2.ExploitRes.CANNT_FINISH pack.pack_node_info(data, node, res.node, timer.now) return self._finish_exploit_succeed(data, req, res, timer) else: raise Exception("Finish exploit failed") resource = data.resource.get(True) assert len(new_mails) == 1 mail = new_mails[0] res = self._pack_exploit_response(new_items, resource, mail, timer.now) defer = DataBase().commit(data) defer.addCallback(self._finish_exploit_succeed, req, res, timer) return defer
def _calc_finish_appoint(self, data, req, timer): node_id = NodeInfo.generate_id(data.id, req.node.basic_id) node = data.node_list.get(node_id) battle = data.battle_list.get(node_id) #存活兵力信息 own_soldier_info = battle.get_own_soldier_info() enemy_soldier_info = battle.get_enemy_soldier_info() change_nodes = [] items = [] heroes = [] mails = [] ret = Ret() if not appoint_business.finish_appoint( data, node, battle, change_nodes, items, heroes, mails, timer.now, ret): if ret.get() == "FINISHED": res = appoint_pb2.FinishAppointRes() res.status = 0 res.ret = appoint_pb2.FinishAppointRes.FINISHED pack.pack_node_info(data, node, res.node, timer.now) return self._finish_appoint_succeed(data, req, res, timer) else: raise Exception("Finish appoint failed") #为新刷出的敌方节点匹配敌人 invalid_rival = [data.id] for node in data.node_list.get_all(True): if node.is_rival_pvp() and node.is_enemy_complete(): rival_id = node.id rival = data.rival_list.get(rival_id, True) invalid_rival.append(rival.rival_id) user = data.user.get(True) matcher = RivalMatcher(user.level, invalid_rival) for node in change_nodes: if node.is_lack_enemy_detail(): matcher.add_condition(data, node) defer = matcher.match(user.country) defer.addCallback(self._pack_finish_appoint_response, data, change_nodes, items, heroes, mails, req, timer) return defer
def _calc_clear_lucky_event(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) change_nodes = [] new_items = [] new_mails = [] ret = Ret() if not event_business.clear_lucky_event( data, node, timer.now, change_nodes, new_items, new_mails, ret): if ret.get() == "NO_EVENT": res = map_pb2.ClearLuckyEventRes() res.status = 0 res.ret = map_pb2.ClearLuckyEventRes.NO_EVENT pack.pack_node_info(data, node, res.node, timer.now) return self._clear_lucky_event_succeed(data, req, res, timer) else: raise Exception("Clear lucky event failed") #为新刷出的敌方节点匹配敌人 #不合法的敌人:自己,以及已经在地图上出现过的 PVP 敌人 invalid_rival = [data.id] for node in data.node_list.get_all(True): if node.is_rival_pvp() and node.is_enemy_complete(): rival_id = node.id rival = data.rival_list.get(rival_id, True) invalid_rival.append(rival.rival_id) user = data.user.get(True) matcher = RivalMatcher(user.level, invalid_rival) for node in change_nodes: if node.is_lack_enemy_detail(): matcher.add_condition(data, node) defer = matcher.match(user.country) defer.addCallback(self._pack_clear_lucky_event_response, data, change_nodes, new_items, new_mails, req, timer) return defer
def _calc_finish_research(self, data, req, timer, force): """ """ res = technology_pb2.UpgradeTechRes() res.status = 0 #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") user = data.user.get(True) resource = data.resource.get() technology_id = TechnologyInfo.generate_id(data.id, req.tech.basic_id, req.tech.type) technology = data.technology_list.get(technology_id) research_building = data.building_list.get(technology.building_id) if research_building is None: logger.fatal("research building is None[building_id=%d]" % technology.building_id) research_heroes = [] # 参与研究的英雄 research_heroes_id = research_building.get_working_hero() for hero_id in research_heroes_id: if hero_id == 0: research_heroes.append(None) else: hero = data.hero_list.get(hero_id) research_heroes.append(hero) resource.update_current_resource(timer.now) #结束研究 ret = Ret() if not technology_business.finish_research( data, technology, user, research_building, research_heroes, resource, timer.now, force, ret): if ret.get() == "NOT_UPGRADING": res.ret = technology_pb2.UpgradeTechRes.NOT_UPGRADING pack.pack_technology_info(technology, res.tech, timer.now) return self._finish_research_succeed(data, req, res, timer) elif ret.get() == "CANNT_FINISH": res.ret = technology_pb2.UpgradeTechRes.CANNT_FINISH pack.pack_technology_info(technology, res.tech, timer.now) return self._finish_research_succeed(data, req, res, timer) else: raise Exception("Finish research technology failed") #前置依赖科技 pre_technology_id = technology_business.get_pre_technology_id( data.id, technology.basic_id, technology.type) pre_technology = None if pre_technology_id != 0: pre_technology = data.technology_list.get(pre_technology_id) #升级完成的后续处理 #兵种科技 if technology.is_soldier_technology(): #删除前置依赖科技 if pre_technology_id != 0: data.technology_list.delete(pre_technology_id) #兵种科技研究完成后,会将原有兵种升级 soldier_id = technology_business.get_related_soldier_id( data.id, req.tech.basic_id) soldier = data.soldier_list.get(soldier_id) #所有配置相关兵种的英雄 related_heroes = [ hero.get() for hero in data.hero_list if hero.get(True).soldier_basic_id == soldier.basic_id ] soldier = technology_business.post_research_for_soldier_technology( data, data.id, technology, timer.now, soldier, related_heroes) if not soldier: raise Exception( "Post process for research soldier technology failed") #战斗科技 elif technology.is_battle_technology(): #删除前置依赖科技 if pre_technology_id != 0: data.technology_list.delete(pre_technology_id) soldier_basic_ids = data_loader.BattleTechnologyBasicInfo_dict[ technology.basic_id].soldierBasicInfoId related_heroes = [] #所带兵种的战力受该科技影响的英雄 for soldier_basic_id in soldier_basic_ids: for hero in data.hero_list: if hero.get(True).soldier_basic_id == soldier_basic_id: related_heroes.append(hero.get()) if not technology_business.post_research_for_battle_technology( data, technology, related_heroes): raise Exception( "Post process for research battle technology failed") #内政科技 elif technology.is_interior_technology(): #遍历所有建筑,更新建筑产量或容量 for building in data.building_list.get_all(): building_heroes = [] #建筑中驻守的英雄 build_hero_ids = building.get_working_hero() for hero_id in build_hero_ids: if hero_id == 0: building_heroes.append(None) else: hero = data.hero_list.get(hero_id) building_heroes.append(hero) if not building_business.update_building_with_interior_technology( building, building_heroes, resource, data, technology, pre_technology): raise Exception( "Post process for research interior technology failed") #删除前置依赖科技 if pre_technology_id != 0: data.technology_list.delete(pre_technology_id) #验证 compare.check_technology(data, req.tech) #记录次数 trainer = data.trainer.get() trainer.add_daily_tech_upgrade_num(1) #科技升级的广播 try: self._add_technology_broadcast(user, technology) except: logger.warning("Send technology broadcast failed") res.ret = technology_pb2.UpgradeTechRes.OK pack.pack_resource_info(data.resource.get(True), res.resource) defer = DataBase().commit(data) defer.addCallback(self._finish_research_succeed, req, res, timer) return defer
def _calc_finish_upgrade(self, data, req, timer, force): """建筑升级完成 1 完成建筑升级,更新建筑信息,包括结算英雄参与建造升级获得的经验 2 指派新的驻守英雄 3 更新建筑升级完成的影响(农田、市场、兵营、城防) """ res = building_pb2.UpgradeBuildingRes() res.status = 0 #涉及到跨天的数据统计,所以此处要更新所有跨天数据 if not account_business.update_across_day_info(data, timer.now): raise Exception("Update across day info failed") user = data.user.get(True) resource = data.resource.get() upgrade_building_id = BuildingInfo.generate_id( data.id, req.building.city_basic_id, req.building.location_index, req.building.basic_id) upgrade_building = data.building_list.get(upgrade_building_id) build_heroes = [] #参与升级的英雄 build_hero_ids = upgrade_building.get_working_hero() for hero_id in build_hero_ids: if hero_id == 0: build_heroes.append(None) else: hero = data.hero_list.get(hero_id) build_heroes.append(hero) #更新资源 resource.update_current_resource(timer.now) #如果不处于升级状态 """if upgrade_building.is_upgrade == False: return DataBase().commit(data)""" #建筑结束升级状态 包括结算参与建造的英雄获得的经验 结算主公获得的经验 ret = Ret() if not building_business.finish_upgrade(data, upgrade_building, resource, user, build_heroes, timer.now, force, ret): if ret.get() == "NOT_UPGRADING": res.ret = building_pb2.UpgradeBuildingRes.NOT_UPGRADING pack.pack_building_info(upgrade_building, res.building, timer.now) return self._finish_upgrade_succeed(data, req, res, timer) elif ret.get() == "CANNT_FINISH": res.ret = building_pb2.UpgradeBuildingRes.CANNT_FINISH pack.pack_building_info(upgrade_building, res.building, timer.now) return self._finish_upgrade_succeed(data, req, res, timer) else: raise Exception("Finish upgrade building failed") defense = None conscript = None if upgrade_building.is_defense(): defense = data.defense_list.get(upgrade_building.id) elif upgrade_building.is_barrack(): conscript = data.conscript_list.get(upgrade_building.id) #所有生效的内政科技 interior_technologys = [ tech for tech in data.technology_list.get_all(True) if tech.is_interior_technology() and not tech.is_upgrade ] #升级完成之后的处理 new_technology = [] new_defense = [] new_conscript = [] if not building_business.post_upgrade( data, upgrade_building, timer.now, build_heroes, interior_technologys, resource, defense, conscript, new_technology, new_defense, new_conscript): raise Exception("Post process for upgrade failed") #可能解锁出已经完成研究的兵种科技,因此可能会解锁新的兵种 for technology in new_technology: data.technology_list.add(technology) if technology.is_soldier_technology(): soldier = technology_business.post_research_for_soldier_technology( data, data.id, technology, timer.now, new=True) if soldier is None: raise Exception( "Post process for soldier technology failed") data.soldier_list.add(soldier) for defense in new_defense: data.defense_list.add(defense) for conscript in new_conscript: data.conscript_list.add(conscript) #检查请求 compare.check_user(data, req.monarch, with_level=True) res.ret = building_pb2.UpgradeBuildingRes.OK pack.pack_resource_info(data.resource.get(True), res.resource) defer = DataBase().commit(data) defer.addCallback(self._finish_upgrade_succeed, req, res, timer) return defer