示例#1
0
 def _finish_upgrade_failed(self, err, req, timer):
     logger.fatal("Finish upgrade failed[reason=%s]" % err)
     res = building_pb2.UpgradeBuildingRes()
     res.status = -1
     response = res.SerializeToString()
     logger.notice(
         "Finish upgrade failed[user_id=%d][req=%s][res=%s][consume=%d]" %
         (timer.id, req, res, timer.count_ms()))
     return response
示例#2
0
 def _cancel_upgrade_succeed(self, data, req, timer):
     res = building_pb2.UpgradeBuildingRes()
     res.status = 0
     pack.pack_resource_info(data.resource.get(True), res.resource)
     response = res.SerializeToString()
     log = log_formater.output(data, "Cancel upgrade building succeed", req,
                               res, timer.count_ms())
     logger.notice(log)
     return response
示例#3
0
    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