Beispiel #1
0
    def _pack_draw_response(self, data, hero_list, item_list,
            resource, search_num, next_time, now):
        """封装响应
        返回客户端的是抽奖获得的英雄和物品(并未将已经拥有的英雄分解成将魂石)
        客户端会自行进行分解
        """
        win_hero = []
        for (basic_id, num) in hero_list:
            soldier_basic_id = HeroInfo.get_default_soldier_basic_id(basic_id)
            soldier_id = SoldierInfo.generate_id(data.id, soldier_basic_id)
            soldier = data.soldier_list.get(soldier_id, True)

            hero = HeroInfo.create(data.id, basic_id, soldier, technology_basic_ids = [])
            for i in range(0, num):
                win_hero.append(hero)

        win_item = []
        for (basic_id, num) in item_list:
            item = ItemInfo.create(data.id, basic_id, num)
            win_item.append(item)

        res = wineShop_pb2.WineShopDrawRes()
        res.status = 0

        for item in win_item:
            pack.pack_item_info(item, res.items.add())
        for hero in win_hero:
            pack.pack_hero_info(hero, res.heroes.add(), now)
        pack.pack_resource_info(resource, res.resource)
        res.draw.search_num = search_num
        res.draw.next_left_time = max(0, next_time - now)
        return res
Beispiel #2
0
    def _pack_signin_response(self, data, hero_list, item_list, now):
        """封装签到响应
        """
        #为了封装 response 构造 HeroInfo 和 ItemInfo
        win_hero = []
        for (basic_id, num) in hero_list:
            soldier_basic_id = HeroInfo.get_default_soldier_basic_id(basic_id)
            soldier_id = SoldierInfo.generate_id(data.id, soldier_basic_id)
            soldier = data.soldier_list.get(soldier_id, True)
            hero = HeroInfo.create(data.id, basic_id, soldier, [])
            for i in range(0, num):
                win_hero.append(hero)

        win_item = []
        for (basic_id, num) in item_list:
            item = ItemInfo.create(data.id, basic_id, num)
            win_item.append(item)

        res = user_pb2.SignInRes()
        res.status = 0
        if len(win_hero) > 0:
            assert len(win_hero) == 1
            pack.pack_hero_info(win_hero[0], res.hero, now)
        if len(win_item) > 0:
            assert len(win_item) == 1
            pack.pack_item_info(win_item[0], res.item)

        return res
Beispiel #3
0
    def _mock_hero(self, basic_id, star, level):
        USER_ID = 0
        USER_LEVEL = int(
            float(data_loader.OtherBasicInfo_dict["MaxMonarchLevel"].value))
        hero_id = HeroInfo.generate_id(USER_ID, basic_id)
        soldier_basic_id = HeroInfo.get_default_soldier_basic_id(basic_id)
        soldier = SoldierInfo.create(USER_ID, soldier_basic_id)

        hero = HeroInfo.create_special(USER_ID,
                                       USER_LEVEL,
                                       basic_id,
                                       level,
                                       star,
                                       soldier, [],
                                       technology_basic_ids=[])
        return hero
Beispiel #4
0
def generate_array_detail(worldboss, user):
    """生成阵容详情返回
    """
    teams = []
    for array_id in worldboss.get_arrays_id():
        array = data_loader.WorldBossArrayBasicInfo_dict[array_id]
        #buff和tech是针对整个阵容生效的
        buffs_id = array.buffIds
        techs_id = array.techIds

        heroes = []
        for i in range(len(array.heroIds)):
            basic_id = array.heroIds[i]
            star = array.heroStars[i]
            equipments_id = []
            equipments_id.append(array.heroEquipmentIds[i * 3])
            equipments_id.append(array.heroEquipmentIds[i * 3 + 1])
            equipments_id.append(array.heroEquipmentIds[i * 3 + 2])
            evolution_level = array.heroEvolutionLevels[i]
            soldier_basic_id = array.soldierBasicIds[i]
            soldier_level = array.soldierLevels[i]
            soldier = SoldierInfo.create(user.id, soldier_basic_id, soldier_level)

            hero = HeroInfo.create_special(user.id, user.level, basic_id, 
                    user.level, star, soldier, [], techs_id, equipments_id, 
                    buffs_id, False, evolution_level, False)
            heroes.append(hero)

        teams.append(heroes)

    return teams
Beispiel #5
0
def is_able_to_start_aid(data, item_basic_id, item_num, now):
    """发起联盟援助
    """
    union = data.union.get(True)

    #需要拥有对应英雄或者拥有将魂石
    item_id = ItemInfo.generate_id(data.id, item_basic_id)
    item = data.item_list.get(item_id, True)
    if item is not None and not item.is_starsoul_item():
        logger.warning("Item is not a starsoul stone[item_basic_id=%d]" %
                       item_basic_id)
        return False

    hero_basic_id = ItemInfo.get_corresponding_value(item_basic_id)
    hero_id = HeroInfo.generate_id(data.id, hero_basic_id)
    hero = data.hero_list.get(hero_id, True)

    if item is None and hero is None:
        logger.warning("Can not start aid[item_basic_id=%d]" % item_basic_id)
        return False

    #数量正确
    expect_num = union.calc_aid_item_num(item_basic_id)
    if item_num != expect_num:
        logger.warning("Aid item num error[wrong_num=%d][item_num=%d]" %
                       (item_num, expect_num))
        return False

    return True
Beispiel #6
0
    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)
Beispiel #7
0
    def _calc_start_exploit(self, data, req, timer):
        """开始进行开采的逻辑
        """
        user = data.user.get(True)
        resource = data.resource.get()
        resource.update_current_resource(timer.now)

        node_id = NodeInfo.generate_id(data.id, req.node.basic_id)
        node = data.node_list.get(node_id)

        work_heroes = []  #参与采集的英雄
        for basic_id in req.node.exploitation.hero_basic_ids:
            if basic_id == 0:
                work_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, basic_id)
                hero = data.hero_list.get(hero_id)
                work_heroes.append(hero)

        #将参与研究的英雄从其他建筑物中移除,更新原有建筑的影响
        #不结算英雄经验 : 英雄经验由客户端周期性请求结算
        #WARNING : 在上一次请求到此时英雄驻守的经验不结算
        for hero in work_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)
                defense = None
                if building.is_defense():
                    defense = data.defense_list.get(building.id)
                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        #所有生效的内政科技
        interior_technologys = [
            tech for tech in data.technology_list.get_all(True)
            if tech.is_interior_technology() and not tech.is_upgrade
        ]

        #离线开采事件的开采时间单独设置
        if node.is_exploit_offline():
            if (req.node.exploitation.total_time != OFFLINE_EXPLOIT_TIME_1 and
                    req.node.exploitation.total_time != OFFLINE_EXPLOIT_TIME_2
                    and req.node.exploitation.total_time !=
                    OFFLINE_EXPLOIT_TIME_3):
                raise Exception("Exploitation total time error")
            else:
                node.set_exploit_total_time(req.node.exploitation.total_time)
                node.set_exploit_level(req.node.exploitation.level)

        if not exploitation_business.start_exploit_event(
                data, node, work_heroes, timer.now, interior_technologys):
            raise Exception("Start exploit failed")

        #compare.check_node(data, req.node, with_exploit = True)

        return DataBase().commit(data)
Beispiel #8
0
    def _check_exist_hero(self, data, req, timer):
        hero_basic_id = req.hero.basic_id
        hero_id = HeroInfo.generate_id(data.id, hero_basic_id)

        #内部接口,使用时需要保证英雄没有和其他建筑等关联
        hero = data.hero_list.get(hero_id)
        if hero is not None:
            data.hero_list.delete(hero_id)
            return DataBase().commit(data)
        else:
            return data
Beispiel #9
0
    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
Beispiel #10
0
def pack_team_info(team, message):
    message.index = team.index
    #message.status = team.status

    heroes_id = team.get_heroes()
    assert len(heroes_id) == 3
    for hero_id in heroes_id:
        basic_id = HeroInfo.get_basic_id(hero_id)
        message.heroes.add().basic_id = basic_id

    message.battle_node_id = team.battle_node_basic_id
    message.union_battle_node_index = team.union_battle_node_index
    message.in_anneal_sweep = (team.anneal_sweep_floor != 0)
Beispiel #11
0
    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)
Beispiel #12
0
def _pack_node_exploitation_info(data, node, message, now):
    message.node_id = node.basic_id #TODO delete
    message.type = node.exploit_type

    message.gather_speed = node.gather_speed
    message.gather_duration = now - node.gather_start_time

    message.total_time = node.exploit_total_time
    message.duration = now - node.exploit_start_time
    if node.is_exploit_money():
        message.total_money = node.exploit_reserve
        message.level = node.exploit_level

    elif node.is_exploit_food():
        message.total_food = node.exploit_reserve
        message.level = node.exploit_level

    elif node.is_exploit_gold():
        message.total_gold = node.exploit_reserve
        message.level = node.exploit_level

    elif node.is_exploit_material():
        message.level = node.exploit_level

    elif node.is_exploit_random_item():
        exploitation = data.exploitation.get()
        if node.is_exploit_exist():
            message.level = node.exploit_level
        else:
            message.level = exploitation.search_level
        message.progress = exploitation.get_search_progress()

    elif node.is_exploit_enchant_material():
        exploitation = data.exploitation.get()
        if node.is_exploit_exist():
            message.level = node.exploit_level
        else:
            message.level = exploitation.deep_mining_level
        message.progress = exploitation.get_deep_mining_progress()

    elif node.is_exploit_hero_star_soul():
        exploitation = data.exploitation.get()
        if node.is_exploit_exist():
            message.level = node.exploit_level
        else:
            message.level = exploitation.hermit_level
        message.progress = exploitation.get_hermit_progress()

    for hero_id in node.get_exploit_hero():
        message.hero_basic_ids.append(HeroInfo.get_basic_id(hero_id))
Beispiel #13
0
    def _calc_update_team(self, data, req, timer):
        user = data.user.get()

        defense_building_id = BuildingInfo.generate_id(
            data.id, req.defence.city_basic_id, req.defence.location_index,
            req.defence.building_basic_id)
        defense_building = data.building_list.get(defense_building_id, True)
        defense = data.defense_list.get(defense_building_id)

        if not user.allow_pvp:
            #TODO 临时的规则:如果城防开启的话,那么可以使用防守阵容
            city_id = CityInfo.generate_id(data.id, req.defence.city_basic_id)
            city = data.city_list.get(city_id, True)
            mansion = data.building_list.get(city.mansion_id, True)

            key = "%d_%d" % (2, 1)
            need_user_level = data_loader.BuildingLevelBasicInfo_dict[
                key].limitMonarchLevel
            need_mansion_level = data_loader.BuildingLevelBasicInfo_dict[
                key].limitMansionLevel

            if user.level >= need_user_level and mansion.level >= need_mansion_level:
                user.allow_pvp = True
            else:
                raise Exception("PVP not allowed")

        guard = data.guard_list.get(defense.guard_id)
        if guard is None:
            index = len(data.guard_list) + 1
            guard = GuardInfo.create(data.id, index)
            defense.update_guard(guard.id)
            data.guard_list.add(guard)

        guard_heroes = []
        for basic_id in req.defence.hero_basic_ids:
            hero_id = HeroInfo.generate_id(data.id, basic_id)
            hero = data.hero_list.get(hero_id, True)
            guard_heroes.append(hero)

        grade = data.grade.get(True)

        if not guard.update(guard_heroes, grade.score):
            raise Exception("Update guard failed")

        all_guards = data.guard_list.get_all(True)
        if not guard_business.validation(all_guards):
            raise Exception("Validation guard failed")

        return DataBase().commit(data)
Beispiel #14
0
def pack_building_info(info, message, now):
    message.basic_id = info.basic_id
    message.city_basic_id = BuildingInfo.get_city_basic_id(info.id)
    message.location_index = info.slot_index

    message.level = info.level
    message.garrision_num = info.garrison_num
    hero_ids = utils.split_to_int(info.hero_ids)
    for id in hero_ids:
        message.hero_basic_ids.append(HeroInfo.get_basic_id(id))

    message.is_upgrading = info.is_upgrade
    message.upgrade_start_time = info.upgrade_start_time
    message.upgrade_consume_time = info.upgrade_consume_time
    message.upgrade_passed_time = now - message.upgrade_start_time
Beispiel #15
0
    def _calc_start_conscript(self, data, req, timer):
        """开始执行征兵
        """
        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 = [] #来征兵的英雄
        for basic_id in req.building.hero_basic_ids:
            if basic_id == 0:
                work_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, basic_id)
                hero = data.hero_list.get(hero_id)
                work_heroes.append(hero)

        resource.update_current_resource(timer.now)

        #将参与研究的英雄从其他建筑物中移除,更新原有建筑的影响
        #不结算英雄经验 : 英雄经验由客户端周期性请求结算
        #WARNING : 在上一次请求到此时英雄驻守的经验不结算
        for hero in work_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)
                defense = None
                if building.is_defense():
                    defense = data.defense_list.get(building.id)
                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        #所有生效的内政科技
        interior_technologys = [tech for tech in data.technology_list.get_all(True)
                if tech.is_interior_technology() and not tech.is_upgrade]

        if not conscript_business.start_conscript(
                conscript, conscript_building, work_heroes,
                req.conscript.conscript_num, resource, interior_technologys, timer.now):
            raise Exception("Start conscript building failed")

        #compare.check_conscript(data, req.conscript, with_time = True)

        return DataBase().commit(data)
Beispiel #16
0
    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)
Beispiel #17
0
    def _calc_upgrade_skill(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 not hero_business.skill_upgrade(data, hero, req.index - 1,
                                           timer.now):
            #raise Exception("Skill upgrade failed")
            pass

        #验证
        #compare.check_hero(data, req.hero, with_skill_index = req.index - 1)
        #for item in req.items:
        #    compare.check_item(data, item)

        return DataBase().commit(data)
Beispiel #18
0
def update_team(data, team_index, heroes_basic_id):
    """更新队伍阵容
    """
    team_id = TeamInfo.generate_id(data.id, team_index)
    team = data.team_list.get(team_id)
    if team is None:
        team = TeamInfo.create(data.id, team_index)
        data.team_list.add(team)

    if not team.is_able_to_update():
        logger.warning("Team is not able to update[index=%d]" % team_index)
        return False

    #计算羁绊
    relationships_id = _calc_relationship(heroes_basic_id)
    array_heroes = []
    for basic_id in heroes_basic_id:
        if basic_id == 0:
            array_heroes.append(None)
        else:
            hero_id = HeroInfo.generate_id(data.id, basic_id)
            hero = data.hero_list.get(hero_id)
            if hero is None:
                logger.warning("Hero not exist[basic id=%d]" % basic_id)
                return False

            _update_hero_buffs(data, hero, relationships_id)
            array_heroes.append(hero)

    #更换英雄
    if not team.update(array_heroes, relationships_id):
        logger.warning("Update team failed")
        return False

    logger.debug("new team[heroes_basic_id=%s][relationships_id=%s]" %
                 (utils.join_to_string(heroes_basic_id),
                  utils.join_to_string(relationships_id)))

    return _post_update_team(data)
Beispiel #19
0
    def _calc_update_garrison_hero_exp(self, data, req, timer):
        """
        """
        heroes = []
        for hero in req.heroes:
            hero_id = HeroInfo.generate_id(data.id, hero.basic_id)
            hero = data.hero_list.get(hero_id)
            heroes.append(hero)

        for hero in heroes:
            building = data.building_list.get(hero.place_id, True)

            if not building_business.calc_garrison_exp(data, building, [hero],
                                                       timer.now):
                raise Exception('Calc garrison exp failed')

        res = self._pack_update_garrison_hero_exp_response(heroes, timer.now)

        defer = DataBase().commit(data)
        defer.addCallback(self._update_garrison_hero_exp_succeed, req, res,
                          timer)
        return defer
Beispiel #20
0
def _default_generate_array_detail(dungeon, battle_level, user, detail_type):
    """默认生成阵容方法"""
    if battle_level == 0:
        level = dungeon.level(user.level)
    else:
        battle_id = "%d_%d" % (dungeon.basic_id, battle_level)
        level = data_loader.ExpandDungeonBattleBasicInfo_dict[battle_id].enemyLevel

    array_id = "%d_%d" % (dungeon.basic_id, level)
    if detail_type == 'enemy':
        array = data_loader.ExpandDungeonEnemyBasicInfo_dict[array_id]
    else:
        array = data_loader.ExpandDungeonOwnBasicInfo_dict[array_id]

    buffs_id = array.buffIds
    techs_id = array.techIds
    heroes = []
    for i in range(len(array.heroIds)):
        basic_id = array.heroIds[i]
        hero_level = array.heroLevels[i]
        star = array.heroStarLevels[i]
        equipments_id = array.heroEquipmentIds[i*3 : i*3 + 3]
        evolution_level = array.heroEvolutionLevels[i]
        herostars_id = array.heroHeroStars[i*6 : i*6 + 6]
        soldier_basic_id = array.soldierBasicIds[i]
        soldier_level = array.soldierLevels[i]
        soldier = SoldierInfo.create(user.id, soldier_basic_id, soldier_level)

        if basic_id == 0:
            heroes.append(None)
        else:
            hero = HeroInfo.create_special(user.id, user.level, basic_id,
                    hero_level, star, soldier, [], techs_id, equipments_id,
                    buffs_id, False, evolution_level, False, herostars_id)
            heroes.append(hero)

    return heroes
Beispiel #21
0
    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)
Beispiel #22
0
def use_starsoul_item(data, item, num, soldier):
    """消耗将魂石,生成对应英雄
    Args:
        data[UserData]: 用户信息
        item[ItemInfo out] 出售的物品
        num[int] 消耗的数量
        soldier[SoldierInfo] 生成的英雄所配置的兵种
    Returns:
        成功返回 HeroInfo
        失败返回 None
    """
    info = item.use_starsoul_item(num)
    if info is None:
        return None

    (hero_basic_id, starsoul_num) = info

    #影响英雄所带兵种战力的科技
    battle_technology_basic_id = technology_module.get_battle_technology_for_soldier(
        data.technology_list.get_all(True), soldier.basic_id)

    return HeroInfo.create_by_starsoul(item.user_id, hero_basic_id,
                                       starsoul_num, soldier,
                                       battle_technology_basic_id)
Beispiel #23
0
    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
Beispiel #24
0
    def _calc_start_research(self, data, req, timer):
        """开始研究科技
        1 指派参与研究的英雄
        1 开始科技研究,计算时间消耗和资源消耗
        """
        user = data.user.get()
        resource = data.resource.get()

        research_building_id = BuildingInfo.generate_id(
            data.id, req.building.city_basic_id, req.building.location_index,
            req.building.basic_id)
        research_building = data.building_list.get(research_building_id)

        if research_building is None:
            raise Exception("Research building not exist")

        research_heroes = []  #参与升级的英雄
        for basic_id in req.building.hero_basic_ids:
            if basic_id == 0:
                research_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, basic_id)
                hero = data.hero_list.get(hero_id)
                research_heroes.append(hero)

        #更新资源
        resource.update_current_resource(timer.now)

        #将参与研究的英雄从其他建筑物中移除,更新原有建筑的影响
        #不结算英雄经验 : 英雄经验由客户端周期性请求结算
        #WARNING : 在上一次请求到此时英雄驻守的经验不结算
        for hero in research_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)
                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        #前置依赖科技
        pre_technology_id = technology_business.get_pre_technology_id(
            data.id, req.tech.basic_id, req.tech.type)
        pre_technology = data.technology_list.get(pre_technology_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 = technology_business.start_research(
            req.tech.basic_id, req.tech.type, user, research_building,
            research_heroes, resource, pre_technology, interior_technologys,
            timer.now)
        if not new_technology:
            raise Exception("Start reasearch technology failed[basic_id=%d]" %
                            req.tech.basic_id)
        data.technology_list.add(new_technology)

        #验证
        compare.check_technology(data, req.tech)

        return DataBase().commit(data)
Beispiel #25
0
def check_hero(data,
               message,
               with_level=False,
               with_star=False,
               with_soldier=False,
               with_equipment_type=0,
               with_skill_index=-1,
               with_evolution=False,
               with_equipment_stone=False,
               with_herostar=False,
               with_awaken=False):
    """
    验证英雄信息
    Args:
        data[UserData]
        message[protobuf]
        with_xxx[bool] 是否需要验证某些字段,包括:等级、星级、兵种、装备、技能
    """
    hero_id = HeroInfo.generate_id(data.id, message.basic_id)
    hero = data.hero_list.get(hero_id, True)
    assert hero is not None

    if with_level:
        logger.debug("check hero[level=%d][exp=%d]" % (hero.level, hero.exp))
        assert hero.level == message.level
        #客户端bug导致此处经常失败,先放过
        #assert hero.exp == message.exp
        if hero.exp != message.exp:
            logger.warning(
                "check hero error[basic_id=%d][exp=%d][req_exp=%d]" %
                (message.basic_id, hero.exp, message.exp))
    if with_star:
        assert hero.star == message.star_level
    if with_soldier:
        assert hero.soldier_basic_id == message.soldier_basic_id
        assert hero.soldier_level == message.soldier_level
    if with_equipment_type != 0 and with_equipment_stone != True:
        if with_equipment_type == HeroInfo.EQUIPMENT_TYPE_WEAPON:
            assert hero.get_equipment(
                with_equipment_type) == message.equipment_weapon_id
        elif with_equipment_type == HeroInfo.EQUIPMENT_TYPE_ARMOR:
            assert hero.get_equipment(
                with_equipment_type) == message.equipment_armor_id
        else:
            assert hero.get_equipment(
                with_equipment_type) == message.equipment_treasure_id
    if with_skill_index != -1:
        if with_skill_index == 0:
            assert hero.get_skill(with_skill_index) == message.first_skill_id
        elif with_skill_index == 1:
            assert hero.get_skill(with_skill_index) == message.second_skill_id
        elif with_skill_index == 2:
            assert hero.get_skill(with_skill_index) == message.third_skill_id
        else:
            assert hero.get_skill(with_skill_index) == message.fourth_skill_id
    if with_evolution:
        assert hero.evolution_level == message.evolution_level

    if with_equipment_stone == True and with_equipment_type != 0:
        if with_equipment_type == HeroInfo.EQUIPMENT_TYPE_WEAPON:
            assert cmp(hero.get_equipment_stones(with_equipment_type),
                       message.stone_weapon) == 0
        elif with_equipment_type == HeroInfo.EQUIPMENT_TYPE_ARMOR:
            assert cmp(hero.get_equipment_stones(with_equipment_type),
                       message.stone_armor) == 0
        else:
            assert cmp(hero.get_equipment_stones(with_equipment_type),
                       message.stone_treasure) == 0

    if with_herostar:
        herostars = hero.get_herostars()
        assert cmp(hero.get_herostars(), message.hero_star) == 0

    if with_awaken:
        assert hero.is_awaken == message.hero_awakening
Beispiel #26
0
    def _calc_add_hero(self, data, req, timer):
        hero_basic_id = req.hero.basic_id

        soldier_basic_id = HeroInfo.get_default_soldier_basic_id(hero_basic_id)
        soldier_id = SoldierInfo.generate_id(data.id, soldier_basic_id)
        soldier = data.soldier_list.get(soldier_id, True)

        skill_ids = []
        skill_ids.append(req.hero.first_skill_id)
        skill_ids.append(req.hero.second_skill_id)
        skill_ids.append(req.hero.third_skill_id)
        skill_ids.append(req.hero.fourth_skill_id)

        user = data.user.get(True)
        #影响英雄所带兵种战力的科技
        battle_technology_basic_id = technology_module.get_battle_technology_for_soldier(
            data.technology_list.get_all(True), soldier_basic_id)

        equipments_id = []
        if (req.hero.HasField("equipment_weapon_id")
                and req.hero.HasField("equipment_armor_id")
                and req.hero.HasField("equipment_treasure_id")):
            equipments_id.append(req.hero.equipment_weapon_id)
            equipments_id.append(req.hero.equipment_armor_id)
            equipments_id.append(req.hero.equipment_treasure_id)

        stone_weapon = []
        if len(req.hero.stone_weapon) == 4:
            stone_weapon.append(req.hero.stone_weapon[0])
            stone_weapon.append(req.hero.stone_weapon[1])
            stone_weapon.append(req.hero.stone_weapon[2])
            stone_weapon.append(req.hero.stone_weapon[3])
        stone_armor = []
        if len(req.hero.stone_armor) == 4:
            stone_armor.append(req.hero.stone_armor[0])
            stone_armor.append(req.hero.stone_armor[1])
            stone_armor.append(req.hero.stone_armor[2])
            stone_armor.append(req.hero.stone_armor[3])
        stone_treasure = []
        if len(req.hero.stone_treasure) == 4:
            stone_treasure.append(req.hero.stone_treasure[0])
            stone_treasure.append(req.hero.stone_treasure[1])
            stone_treasure.append(req.hero.stone_treasure[2])
            stone_treasure.append(req.hero.stone_treasure[3])

        evolution_level = 1
        if req.hero.HasField("evolution_level"):
            evolution_level = req.hero.evolution_level

        hero = HeroInfo.create_special(user.id,
                                       user.level,
                                       hero_basic_id,
                                       req.hero.level,
                                       req.hero.star_level,
                                       soldier,
                                       skill_ids,
                                       battle_technology_basic_id,
                                       equipments_id, [],
                                       True,
                                       evolution_level,
                                       stones_weapon_id=stone_weapon,
                                       stones_armor_id=stone_armor,
                                       stones_treasure_id=stone_treasure)
        if hero is None:
            raise Exception("Create hero failed")

        data.hero_list.add(hero)
        hero_business.post_gain_hero(data, hero)

        return DataBase().commit(data)
Beispiel #27
0
    def _select_teams(self, proxy, data, melee, users_id, users_arena,
                      users_arena_ranking):
        """查team信息
        """
        cache_proxy = DataProxy()
        users = {}
        users_id_usable = []
        guards = {}
        tech_basic_ids = {}
        heroes_id = {}
        for user_id in users_id:
            user_result = proxy.get_result("user", user_id)
            if not user_result.allow_pvp_arena:
                #若恰好匹配到演武场还未开启的玩家,则跳过
                continue

            guard_result = proxy.get_result("guard", user_id)
            if guard_result is None:
                continue

            users[user_id] = user_result
            users_id_usable.append(user_id)
            guards[user_id] = guard_result  #目前只有一个防守阵容

            results = proxy.get_all_result("technology")
            battle_technologys = []
            for result in results:
                if (result.user_id == user_id and not result.is_upgrade
                        and result.is_battle_technology()):
                    battle_technologys.append(result.basic_id)
            tech_basic_ids[user_id] = battle_technologys

            #heroes_id[user_id] = utils.split_to_int(guards[user_id].teams_hero)
            #for hero_id in heroes_id[user_id]:
            #    if hero_id != 0:
            #        cache_proxy.search("hero", hero_id)

        #设置rival的演武场信息
        rivals = {}
        rivals_id = melee.generate_arena_rivals_id()
        for i in range(len(users_id_usable)):
            user_id = users_id_usable[i]
            rival_id = rivals_id[i]
            rival = data.rival_list.get(rival_id)
            if rival is None:
                rival = RivalInfo.create(rival_id, data.id)
                data.rival_list.add(rival)
            rivals[rival_id] = rival

            user_arena = users_arena[user_id]
            heroes_basic_id = utils.split_to_int(user_arena.heroes_basic_id)
            heroes_id = []
            for basic_id in heroes_basic_id:
                hero_id = HeroInfo.generate_id(user_id, basic_id)
                heroes_id.append(hero_id)
                if hero_id != 0:
                    cache_proxy.search("hero", hero_id)

            battle_score = guards[user_id].get_team_score()
            heroes = utils.join_to_string(heroes_id)
            #计算积分变化
            (self_win_score,
             self_lose_score) = melee_business.calc_battle_score(
                 melee, MeleeInfo.get_real_score(user_arena.score))
            (rival_win_score,
             rival_lose_score) = melee_business.calc_battle_score(
                 user_arena, MeleeInfo.get_real_score(melee.score))

            arena_buff_id = user_arena.calc_arena_buff_id(
                users_arena_ranking[user_id])
            rival.set_melee(user_id, battle_score, heroes,
                            MeleeInfo.get_real_score(user_arena.score),
                            users_arena_ranking[user_id], arena_buff_id,
                            self_win_score, self_lose_score,
                            user_arena.heroes_position)
            logger.debug(
                "melee rival:[user_id=%d][battle_score=%d][heroes=%s][arena_score=%d]"
                "[arena_ranking=%d][arena_buff_id=%d][self_win_score=%d][self_lose_score=%d]"
                "[rival_win_score=%d][rival_lose_score=%d]" %
                (user_id, battle_score, heroes,
                 MeleeInfo.get_real_score(user_arena.score),
                 users_arena_ranking[user_id], arena_buff_id, self_win_score,
                 self_lose_score, rival_win_score, rival_lose_score))

        defer = cache_proxy.execute()
        defer.addCallback(self._set_rivals_info, data, melee, rivals, users,
                          heroes_id, tech_basic_ids)
        return defer
Beispiel #28
0
    def _calc_start_upgrade(self, data, req, timer):
        """开始升级
        """
        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)

        garrison_heroes = []  #原有的驻守的英雄
        garrison_hero_ids = upgrade_building.get_working_hero()
        for hero_id in garrison_hero_ids:
            if hero_id == 0:
                garrison_heroes.append(None)
            else:
                hero = data.hero_list.get(hero_id)
                garrison_heroes.append(hero)

        build_heroes = []  #参与升级的英雄
        for basic_id in req.building.hero_basic_ids:
            if basic_id == 0:
                build_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, basic_id)
                hero = data.hero_list.get(hero_id)
                build_heroes.append(hero)

        #更新资源
        resource.update_current_resource(timer.now)

        #升级前处理
        if not building_business.pre_upgrade(upgrade_building, timer.now,
                                             resource, garrison_heroes):
            raise Exception("Pre pocess for upgrade error")

        #如果参与建造的英雄驻守在其他的建筑物中,将其从其他建筑物中移除
        #不结算经验,但是会对建筑物造成影响
        for hero in build_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)
                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        #所有生效的内政科技
        interior_technologys = [
            tech for tech in data.technology_list.get_all(True)
            if tech.is_interior_technology() and not tech.is_upgrade
        ]

        city = data.city_list.get(upgrade_building.city_id)
        mansion = data.building_list.get(city.mansion_id, True)

        #开始升级 指派 build_heroes 参与升级
        if not building_business.start_upgrade(
                upgrade_building, resource, build_heroes, interior_technologys,
                user, mansion, timer.now):
            raise Exception(
                "Start upgrade building failed[basic_id=%d][level=%d]" %
                (upgrade_building.basic_id, upgrade_building.level))

        #check
        return DataBase().commit(data)
        return defer
Beispiel #29
0
    def _calc_start_create(self, data, req, timer):
        """
        新建建筑
        1 指派英雄参与建造
        2 开始建造一个建筑,计算消耗时间和消耗资源
        Args:
            data[UserData]: 原来的数据,包括
            req[protobuf]: 请求
        """
        user = data.user.get(True)
        resource = data.resource.get()

        build_heroes = []  #参与升级的英雄
        for basic_id in req.building.hero_basic_ids:
            if basic_id == 0:
                build_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, basic_id)
                hero = data.hero_list.get(hero_id)
                build_heroes.append(hero)

        #更新资源
        resource.update_current_resource(timer.now)

        #如果参与建造的英雄驻守在其他的建筑物中,将其从其他建筑物中移除
        #不结算经验,但是会对建筑物造成影响
        for hero in build_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)
                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        city_id = CityInfo.generate_id(data.id, req.building.city_basic_id)
        city = data.city_list.get(city_id)
        mansion = data.building_list.get(city.mansion_id, True)

        #新建建筑,此时建筑物为0级
        new_building = city_business.create_building(
            city, user, mansion.level, req.building.location_index,
            req.building.basic_id)
        if new_building is None:
            raise Exception("Create buliding failed[basic_id=%d]" %
                            req.building.basic_id)
        data.building_list.add(new_building)

        #所有生效的内政科技
        interior_technologys = [
            tech for tech in data.technology_list.get_all(True)
            if tech.is_interior_technology() and not tech.is_upgrade
        ]

        #升级建筑,从0级升到1级
        if not building_business.start_upgrade(
                new_building, resource, build_heroes, interior_technologys,
                user, mansion, timer.now):
            raise Exception(
                "Start upgrade building failed[basic_id=%d][level=%d]" %
                (new_building.basic_id, new_building.level))

        #check
        if req.building.level != 0:
            raise Exception("Building level must equal 0")

        return DataBase().commit(data)
Beispiel #30
0
    def _calc_update_garrison_hero(self, data, req, timer):
        """
        """
        resource = data.resource.get()

        update_building_id = BuildingInfo.generate_id(
            data.id, req.building.city_basic_id, req.building.location_index,
            req.building.basic_id)
        update_building = data.building_list.get(update_building_id)

        origin_heroes = []  #原有的驻守武将
        heroes_id = update_building.get_working_hero()
        for hero_id in heroes_id:
            if hero_id == 0:
                origin_heroes.append(None)
            else:
                hero = data.hero_list.get(hero_id)
                origin_heroes.append(hero)

        garrison_heroes = []  #新的驻守武将
        for hero_basic_id in req.building.hero_basic_ids:
            if hero_basic_id == 0:
                garrison_heroes.append(None)
            else:
                hero_id = HeroInfo.generate_id(data.id, hero_basic_id)
                hero = data.hero_list.get(hero_id)
                garrison_heroes.append(hero)

        #更新资源
        resource.update_current_resource(timer.now)

        #如果新的驻守英雄在其他的建筑物中,将其从其他建筑物中移除
        for hero in garrison_heroes:
            if hero is not None and hero.is_working_in_building():
                building = data.building_list.get(hero.place_id)

                if not building_business.remove_garrison(
                        building, hero, resource, timer.now):
                    raise Exception("Reassign build hero error")

        #将原来驻守在建筑物中的英雄移除
        for hero in origin_heroes:
            if hero is None or not hero.is_working_in_building():
                continue
            if not building_business.remove_garrison(update_building, hero,
                                                     resource, timer.now):
                raise Exception("Reassign build hero error")

        #所有生效的内政科技
        interior_technologys = [
            tech for tech in data.technology_list.get_all(True)
            if tech.is_interior_technology() and not tech.is_upgrade
        ]

        #将新的英雄派驻进建筑物
        if not building_business.set_garrison(update_building, garrison_heroes,
                                              resource, interior_technologys,
                                              timer.now):
            raise Exception("Set garrison hero error")

        defer = DataBase().commit(data)
        return defer