Beispiel #1
0
    def _calc_finish_battle_result(self, union_response, data, req, 
            own_soldier_info, enemy_soldier_info, kill_soldier_num, boss, timer):
        res = union_boss_pb2.FinishUnionBossBattleRes()
        res.status = 0

        union_res = internal_union_pb2.InternalSyncBossRes()
        union_res.ParseFromString(union_response)
        
        if union_res.status != 0:
            raise Exception("finish union boss battle failed")

        if union_res.ret != union_pb2.UNION_OK:
            res.ret = union_res.ret
            return self._finish_battle_succeed(data, req, res, timer)

        user = data.user.get(True)
        union = data.union.get(True)

        res.ret = union_pb2.UNION_OK
        res.battle_ret = battle_pb2.BATTLE_OK
        pack.pack_unionboss_info(boss, user, union_res.boss_status,
                union_res.total_soldier_num, union_res.current_soldier_num,
                res.boss, timer.now)
        
        conscript_list = data.conscript_list.get_all(True)
        for conscript in conscript_list:
            pack.pack_conscript_info(conscript, res.conscripts.add(), timer.now)
        
        resource = data.resource.get(True)
        pack.pack_resource_info(resource, res.resource)

        defer = DataBase().commit(data)
        defer.addCallback(self._finish_battle_succeed, req, res, timer)
        return defer
Beispiel #2
0
    def _calc_start_battle_result(self, union_response, data, req, timer):
        res = union_boss_pb2.StartUnionBossBattleRes()
        res.status = 0

        union_res = internal_union_pb2.InternalSyncBossRes()
        union_res.ParseFromString(union_response)
        
        if union_res.status != 0:
            raise Exception("Start union boss battle failed")

        if union_res.ret != union_pb2.UNION_OK:
            res.ret = union_res.ret
            return self._start_battle_succeed(data, req, res, timer)

        if union_res.boss_status != UserUnionBossInfo.BATTLE:
            if union_res.boss_status == UserUnionBossInfo.INACTIVE:
                res.ret = union_pb2.UNION_BOSS_INACTIVE
            else:
                res.ret = union_pb2.UNION_BOSS_KILLED
            return self._start_battle_succeed(data, req, res, timer)

        boss_id = union_res.boss_id
        array_index = req.world_boss_choose_index    #选择boss队伍的index

        #参战队伍 & 英雄
        teams = []
        heroes = []
        for team_info in req.battle.attack_teams:
            team_id = TeamInfo.generate_id(data.id, team_info.index)
            team = data.team_list.get(team_id)
            if team is None:
                continue
            teams.append(team)

            for hero_id in team.get_heroes():
                if hero_id != 0:
                    hero = data.hero_list.get(hero_id)
                    heroes.append(hero)

        (ret, battle_ret) = union_boss_business.start_battle(data, boss_id, 
                array_index, teams, heroes, timer.now, req.gold)
        if ret != union_pb2.UNION_OK or battle_ret != battle_pb2.BATTLE_OK:
            res.ret = ret
            res.battle_ret = battle_ret
            return self._start_battle_succeed(data, req, res, timer)

        node_id = NodeInfo.generate_id(data.id, UserUnionInfo.get_union_boss_node_basic_id())
        battle = data.battle_list.get(node_id, True)
        resource = data.resource.get(True)
        pack.pack_battle_reward_info(battle, res.reward)
        pack.pack_resource_info(resource, res.resource)
        conscript_list = data.conscript_list.get_all(True)
        for conscript in conscript_list:
            pack.pack_conscript_info(conscript, res.conscripts.add(), timer.now)

        res.ret = union_pb2.UNION_OK
        res.battle_ret = battle_pb2.BATTLE_OK
        defer = DataBase().commit(data)
        defer.addCallback(self._start_battle_succeed, req, res, timer)
        return defer
Beispiel #3
0
    def _calc_finish_post(self, patcher, data, req, res, timer):
        pack.pack_resource_info(data.resource.get(True), res.resource)
        for conscript in data.conscript_list.get_all(True):
            pack.pack_conscript_info(conscript, res.conscripts.add(),
                                     timer.now)

        defer = DataBase().commit(data)
        defer.addCallback(self._finish_succeed, req, res, timer)
        return defer
Beispiel #4
0
    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
Beispiel #5
0
    def _pack_finish_appoint_response(self, matcher, data, node_list, items,
                                      heroes, mails, req, timer):
        """构造返回
        args:
            items : list((num, basic_id)元组)
            heroes: list((basic_id, level, exp, battle_node_basic_id )元组)
        Returns:
            res[protobuf]: 向客户端的返回的响应
        """
        resource = data.resource.get(True)
        user = data.user.get(True)

        conscript_list = data.conscript_list.get_all(True)

        res = appoint_pb2.FinishAppointRes()
        res.status = 0
        res.ret = appoint_pb2.FinishAppointRes.OK

        pack.pack_resource_info(resource, res.resource)
        pack.pack_monarch_info(user, res.monarch)
        for conscript in conscript_list:
            pack.pack_conscript_info(conscript, res.conscripts.add(),
                                     timer.now)
        for node in node_list:
            pack.pack_node_info(data, node, res.nodes.add(), timer.now)

        for item in items:
            pack.pack_item_info(item, res.items.add())

        for hero in heroes:
            hero_message = res.heros.add()
            hero_message.basic_id = hero[0]
            hero_message.level = hero[1]
            hero_message.exp = hero[2]
            hero_message.battle_node_id = hero[3]

        for mail in mails:
            pack.pack_mail_info(mail, res.mails.add(), timer.now)

        if 'is_battle_cost_energy' in account_business.get_flags():
            energy = data.energy.get()
            energy.update_current_energy(timer.now)
            pack.pack_energy_info(energy, res.energy, timer.now)

        map_business.check_map_data(data)

        defer = DataBase().commit(data)
        defer.addCallback(self._finish_appoint_succeed, req, res, timer)
        return defer
Beispiel #6
0
    def _pack_start_appoint_response(self, data, now):
        """构造返回
        Returns:
            res[protobuf]: 向客户端的返回的响应
        """
        resource = data.resource.get(True)
        conscript_list = data.conscript_list.get_all(True)

        res = appoint_pb2.StartAppointRes()
        res.status = 0

        pack.pack_resource_info(resource, res.resource)
        for conscript in conscript_list:
            pack.pack_conscript_info(conscript, res.conscripts.add(), now)

        return res
Beispiel #7
0
    def _calc_rival_start_result(self, union_response, union_res, data, teams,
                                 heroes, req, timer):
        rival_union_res = internal_union_pb2.InternalStartUnionBattleRes()
        rival_union_res.ParseFromString(union_response)
        if rival_union_res.status != 0:
            raise Exception("Query rival union battle res error")

        res = union_battle_pb2.StartUnionBattleRes()
        res.status = 0
        res.ret = rival_union_res.ret

        if res.ret == union_pb2.UNION_OK:
            #开战逻辑
            if not union_battle_business.start_battle(
                    data, teams, heroes, req.rival_user_id,
                    rival_union_res.rival, timer.now, req.cost_gold > 0):
                raise Exception("Start battle failed")

            #补充响应
            res.unlock_time = union_res.unlock_time
            union = data.union.get(True)
            node_id = NodeInfo.generate_id(
                data.id, union.get_battle_mapping_node_basic_id())
            battle = data.battle_list.get(node_id, True)
            pack.pack_battle_reward_info(battle, res.reward)
            pack.pack_resource_info(data.resource.get(True), res.resource)
            conscript_list = data.conscript_list.get_all(True)
            for conscript in conscript_list:
                pack.pack_conscript_info(conscript, res.conscripts.add(),
                                         timer.now)

        if rival_union_res.HasField("battle") and 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_start_post, data, req, res, timer)
            return defer

        return self._calc_start_post(None, data, req, res, timer)
Beispiel #8
0
    def _pack_user_info(self,
                        data,
                        basic_data,
                        req,
                        timer,
                        first_init,
                        arena_matcher=None,
                        melee_matcher=None):
        """打包所有用户数据
        Args:
            data[UserData]: 用户数据
            req[protobuf]: 请求
        Returns:
            res[protobuf]
        """
        res = init_pb2.InitRes()
        res.status = 0
        res.first_init = first_init
        info = res.info

        user = data.user.get(True)
        pay = data.pay.get(True)
        pack.pack_monarch_info(user, info.monarch)
        union = data.union.get(True)
        resource = data.resource.get(True)
        if union.is_belong_to_union():
            info.monarch.union_id = union.union_id
            info.union_battle_stage = UnionBattleStagePatcher().patch(
                union.union_id)

        pack.pack_resource_info(data.resource.get(True), info.resource)
        for item in data.item_list.get_all(True):
            pack.pack_item_info(item, info.items.add())
        for hero in data.hero_list.get_all(True):
            pack.pack_hero_info(hero, info.heroes.add(), timer.now)
        for team in data.team_list.get_all(True):
            pack.pack_team_info(team, info.teams.add())

        pack.pack_map_info(data, data.map.get(True), info.map, timer.now)

        for city in data.city_list.get_all(True):
            pack.pack_city_info(city, info.cities.add())
        for building in data.building_list.get_all(True):
            pack.pack_building_info(building, info.buildings.add(), timer.now)
        for technology in data.technology_list.get_all(True):
            pack.pack_technology_info(technology, info.techs.add(), timer.now)
        for conscript in data.conscript_list.get_all(True):
            pack.pack_conscript_info(conscript, info.conscripts.add(),
                                     timer.now)
        assert len(data.defense_list) == 1

        pack.pack_money_draw_info(data.draw.get(True), info.money_draw,
                                  timer.now)
        pack.pack_gold_draw_info(data.draw.get(True), info.gold_draw,
                                 timer.now)

        for mission in data.mission_list:
            pack.pack_mission_info(mission.get(True), info.missions.add())

        #邮件看产生时间顺序给出
        # data.mail.sort(cmp = None, key=lambda x:x.time, reverse = False)
        for mail in data.mail_list.get_all(True):
            pack.pack_mail_info(mail, info.mails.add(), timer.now)

        pack.pack_sign_info(data.sign.get(True), info.sign_in)

        #演武场
        arena = data.arena.get(True)
        arena_ranking = 0
        if arena_matcher is not None:
            arena_ranking = arena_matcher.rank
        pack.pack_arena_info(user, arena, info.arena, timer.now, arena_ranking)
        #if user.allow_pvp_arena and arena.is_arena_active(timer.now):
        #    #胜场奖励
        #    if arena.is_able_to_get_win_num_reward():
        #        pack.pack_arena_reward_info(arena, info.arena.win_num_reward)
        #    #对手信息
        #    if arena.rivals_user_id != '':
        #        rivals_id = arena.generate_arena_rivals_id()
        #        for rival_id in rivals_id:
        #            rival = data.rival_list.get(rival_id, True)
        #            pack.pack_arena_player(rival, info.arena.rivals.add())
        #        #系统选定的对战对手
        #        choose_rival_id = arena.get_choose_rival_id()
        #        choose_rival = data.rival_list.get(choose_rival_id, True)
        #        info.arena.choosed_user_id = choose_rival.rival_id

        #    #对战记录
        #    record_list = data.arena_record_list.get_all(True)
        #    for record in record_list:
        #        pack.pack_arena_record(record, info.arena.records.add())

        flags = account_business.get_flags()
        if 'is_open_melee' in flags:
            #乱斗场
            melee = data.melee.get(True)
            melee_ranking = 0
            if melee_matcher is not None:
                melee_ranking = melee_matcher.rank
            pack.pack_melee_info(user, melee, info.melee_arena, timer.now,
                                 melee_ranking)
            #if melee.is_able_to_unlock(user) and melee.is_arena_active(timer.now):
            #    #胜场奖励
            #    if melee.is_able_to_get_win_num_reward():
            #        pack.pack_arena_reward_info(melee, info.melee_arena.win_num_reward)
            #    #对手信息
            #    if melee.rivals_user_id != '':
            #        rivals_id = melee.generate_arena_rivals_id()
            #        for rival_id in rivals_id:
            #            rival = data.rival_list.get(rival_id, True)
            #            pack.pack_melee_player(rival, info.melee_arena.rivals.add())

            #    #对战记录
            #    record_list = data.melee_record_list.get_all(True)
            #    for record in record_list:
            #        pack.pack_arena_record(record, info.melee_arena.records.add())
            ##乱斗场阵容
            #(heroes_list, positions_list) = melee.get_heroes_position()
            #if len(heroes_list) > 0:
            #    info.melee_team.index = 0
            #for hero_basic_id in heroes_list:
            #    (info.melee_team.heroes.add()).basic_id = hero_basic_id
            #for position in positions_list:
            #    info.melee_team.hero_positions.append(position)

        #政令
        energy = data.energy.get(True)
        pack.pack_energy_info(energy, info.energy_info, timer.now)

        #祈福
        pray = data.pray.get(True)
        pack.pack_pray_info(pray, info.pray, timer.now)

        #红包
        chest = data.chest.get(True)
        pack.pack_chest_info(chest, info.chest, timer.now)

        #试炼场
        anneal = data.anneal.get(True)
        pack.pack_anneal_info(data, anneal, info.anneal, timer.now)

        #打包新手引导进度信息
        pack.pack_guide_info(data.user.get(True), info.guide)

        #将星盘
        herostar_list = data.herostar_list.get_all(True)
        for herostar in herostar_list:
            info.hero_star.append(herostar.star_id)

        #世界boss
        #pack.pack_worldboss_info(data.worldboss.get(True), user, info.world_boss, timer.now)

        #功能开关
        #flags = account_business.get_flags()
        pack.pack_flag_info(flags, info.flags)

        #扩展副本
        dungeons = data.expand_dungeon_list.get_all()
        for dungeon in dungeons:
            dungeon.daily_update(timer.now)
            pack.pack_expand_dungeon_info(dungeon,
                                          user,
                                          info.expand_dungeons.add(),
                                          0,
                                          timer.now,
                                          brief=True)

        #主界面的按钮提示
        pack.pack_button_tips(basic_data, data, info, timer.now)

        #lua
        #pack.pack_luas(info.luas)

        logger.notice(
            "Submit Role[user_id=%d][level=%d][name=%s][vip=%d][status=LOGIN][create_time=%d][last_login_time=%d][money=%d][food=%d][gold=%d][pay_count=%d][pay_amount=%.2f]"
            % (user.id, user.level, user.name, user.vip_level,
               user.create_time, user.last_login_time, resource.money,
               resource.food, resource.gold, pay.pay_count, pay.pay_amount))

        #log = log_formater.output_gold(data, 0, log_formater.INIT_RESOURCE_GOLD,
        #       "Gain gold from init resource", before_gold = data.resource.get(True).gold)
        #logger.notice(log)

        return self._init_succeed(data, req, res, timer)