Esempio n. 1
0
    def battle(self):
        need_sycee = 0

        counter = Counter(self.char_id, 'arena')
        if counter.remained_value <= 0:
            counter = Counter(self.char_id, 'arena_buy')
            if counter.remained_value <= 0:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(
                            char.vip, VIP_MAX_LEVEL))
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL, self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(
                        VIP_MAX_LEVEL))
            else:
                need_sycee = ARENA_COST_SYCEE

        rival_id = self.choose_rival()
        if not rival_id:
            raise SanguoException(errormsg.ARENA_NO_RIVAL, self.char_id,
                                  "Arena Battle", "no rival.")

        if need_sycee:
            resource = Resource(self.char_id, "Arena Battle",
                                "battle for no free times")
            resource.check_and_remove(sycee=-need_sycee)

        counter.incr()

        # set battle cd
        redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD)

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        if msg.self_win:
            achievement = Achievement(self.char_id)
            achievement.trig(11, 1)

        self_score = self.score
        rival_arena = Arena(rival_id)
        rival_score = rival_arena.score

        new_score = calculate_score(self_score, rival_score, msg.self_win)
        self.set_score(new_score)

        t = Task(self.char_id)
        t.trig(2)

        self.send_notify(score=new_score)

        rival_arena.be_beaten(rival_score, self_score, not msg.self_win,
                              self.char_id)

        return msg
Esempio n. 2
0
    def get_reward(self):
        counter = Counter(self.char_id, 'official_reward')
        if counter.remained_value <= 0:
            raise SanguoException(
                errormsg.OFFICAL_ALREADY_GET_REWARD,
                self.char_id,
                "OfficialDailyReward Get Reward",
                "already got"
            )


        char = Char(self.char_id)
        official_level = char.mc.official
        if official_level == 0:
            raise SanguoException(
                errormsg.OFFICAL_ZERO_GET_REWARD,
                self.char_id,
                "OfficialDailyReward Get Reward",
                "char official level = 0"
            )

        counter = Counter(self.char_id, 'official_reward')
        counter.incr()

        gold = OFFICIAL[official_level].gold

        resource = Resource(self.char_id, "Daily Official", 'official reward')
        standard_drop = resource.add(gold=gold)
        return standard_drop_to_attachment_protomsg(standard_drop)
Esempio n. 3
0
    def battle(self):
        counter = Counter(self.char_id, 'arena')
        try:
            # 免费次数
            counter.incr()
        except CounterOverFlow:
            counter = Counter(self.char_id, 'arena_buy')

            try:
                # 花费元宝次数
                counter.incr()
            except CounterOverFlow:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES, self.char_id, "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(
                            char.vip, VIP_MAX_LEVEL))
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL, self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(
                        VIP_MAX_LEVEL))

            else:
                resource = Resource(self.char_id, "Arena Battle",
                                    "battle for no free times")
                resource.check_and_remove(sycee=-ARENA_COST_SYCEE)

        rival_id = self.choose_rival()

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        achievement = Achievement(self.char_id)

        if msg.self_win:
            score = ARENA_GET_SCORE_WHEN_WIN
            achievement.trig(11, 1)
            self.mongo_arena.continues_win += 1
        else:
            score = ARENA_GET_SCORE_WHEN_LOST
            self.mongo_arena.continues_win = 0

        self.mongo_arena.save()

        if score:
            self.mongo_day.score += score
            self.mongo_day.save()

        t = Task(self.char_id)
        t.trig(2)

        self.send_notify()
        return msg
Esempio n. 4
0
    def reset_total(self):
        counter = Counter(self.char_id, 'stage_elite_buy_total')
        if counter.remained_value <= 0:
            raise SanguoException(errormsg.STAGE_ELITE_RESET_TOTAL_FULL,
                                  self.char_id, "Elite Reset Total",
                                  "reset total")

        cost = self.get_total_reset_cost()

        resource = Resource(self.char_id, "Elite Reset Total", "")
        with resource.check(sycee=-cost):
            counter = Counter(self.char_id, 'stage_elite')
            counter.reset()

        self.send_remained_times_notify()
Esempio n. 5
0
    def battle(self, _id):
        try:
            self.this_stage = STAGE_ACTIVITY[_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ACTIVITY_NOT_EXIST,
                                  self.char_id, "StageActivity Battle",
                                  "StageActivity {0} not exist".format(_id))

        if _id not in self.stage.activities:
            raise SanguoException(errormsg.STAGE_ACTIVITY_NOT_OPEN,
                                  self.char_id, "StageActivity Battle",
                                  "StageActivity {0} not open".format(_id))

        if self.this_stage.tp == 1:
            func_name = 'stage_active_type_one'
        else:
            func_name = 'stage_active_type_two'

        counter = Counter(self.char_id, func_name)
        if counter.remained_value <= 0:
            raise SanguoException(
                errormsg.STAGE_ACTIVITY_TOTAL_NO_TIMES, self.char_id,
                "StageActivity Battle",
                "StageActivity no total times. battle {0}".format(_id))

        battle_msg = protomsg.Battle()
        b = ActivityPVE(self.char_id, _id, battle_msg)
        b.start()

        if battle_msg.self_win:
            counter.incr()
            self.send_remained_times_notify()

        return battle_msg
Esempio n. 6
0
    def plunder(self, _id):
        if str(_id) not in self.mongo_plunder.chars:
            raise SanguoException(
                errormsg.PLUNDER_NOT_IN_LIST, self.char_id, "Plunder Plunder",
                "Plunder, {0} not in plunder list".format(_id))

        counter = Counter(self.char_id, 'plunder')
        if counter.remained_value <= 0:
            char = Char(self.char_id).mc
            if char.vip < VIP_MAX_LEVEL:
                raise SanguoException(
                    errormsg.PLUNDER_NO_TIMES, self.char_id, "Plunder Battle",
                    "Plunder no times. vip current: {0}, max: {1}".format(
                        char.vip, VIP_MAX_LEVEL))
            raise SanguoException(
                errormsg.PLUNDER_NO_TIMES_FINAL, self.char_id,
                "Plunder Battle",
                "Plunder no times. vip reach max level {0}".format(
                    VIP_MAX_LEVEL))

        msg = MsgBattle()
        pvp = PVP(self.char_id, _id, msg)
        pvp.start()

        if not self.mongo_plunder.chars[str(_id)].is_robot:
            char = Char(self.char_id)
            h = Hang(_id)
            h.plundered(char.cacheobj.name, not msg.self_win)

        t = Task(self.char_id)
        t.trig(3)

        ground_win_times = 0
        if msg.first_ground.self_win:
            ground_win_times += 1
        if msg.second_ground.self_win:
            ground_win_times += 1
        if msg.third_ground.self_win:
            ground_win_times += 1

        got_point = PLUNDER_POINT.get(ground_win_times, 0)
        if got_point:
            self.mongo_plunder.points += got_point

        if msg.self_win:
            counter.incr()
            self.mongo_plunder.target_char = _id

            drop_official_exp = PLUNDER_GET_OFFICIAL_EXP_WHEN_WIN
            drop_gold = PLUNDER_DEFENSE_FAILURE_GOLD

            resource = Resource(self.char_id, "Plunder")
            resource.add(gold=drop_gold, official_exp=drop_official_exp)
        else:
            self.mongo_plunder.target_char = 0

        self.mongo_plunder.got_reward = []
        self.mongo_plunder.save()
        self.send_notify()
        return msg
Esempio n. 7
0
    def send_remained_times_notify(self):
        msg = protomsg.EliteStageRemainedTimesNotify()
        free_counter = Counter(self.char_id, 'stage_elite')
        msg.max_free_times = free_counter.max_value
        msg.cur_free_times = free_counter.cur_value

        msg.total_reset_cost = self.get_total_reset_cost()
        publish_to_char(self.char_id, pack_msg(msg))
Esempio n. 8
0
    def get_total_reset_cost(self):
        counter = Counter(self.char_id, 'stage_elite_buy_total')
        buy_times = counter.cur_value
        buy_times += 1
        for t, cost in STAGE_ELITE_TOTAL_RESET_COST:
            if buy_times >= t:
                return cost

        return 0
Esempio n. 9
0
    def battle(self, _id):
        str_id = str(_id)

        try:
            self.this_stage = STAGE_ELITE[_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ELITE_NOT_EXIST, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} not exist".format(_id))

        try:
            times = self.stage.elites[str_id]
        except KeyError:
            raise SanguoException(errormsg.STAGE_ELITE_NOT_OPEN, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} not open".format(_id))

        if str(self.this_stage.open_condition) not in self.stage.stages:
            if self.this_stage.previous and str(
                    self.this_stage.previous) not in self.stage.elites:
                raise SanguoException(
                    errormsg.STAGE_ELITE_NOT_OPEN, self.char_id,
                    "StageElite Battle",
                    "StageElite {0} not open. XXX check source core/stage/EliteStage Open"
                    .format(_id))

        if times >= self.this_stage.times:
            raise SanguoException(errormsg.STAGE_ELITE_NO_TIMES, self.char_id,
                                  "StageElite Battle",
                                  "StageElite {0} no times".format(_id))

        counter = Counter(self.char_id, 'stage_elite')
        if counter.remained_value <= 0:
            raise SanguoException(errormsg.STAGE_ELITE_TOTAL_NO_TIMES,
                                  self.char_id, "StageElite Battle",
                                  "stageElite no total times.")

        battle_msg = protomsg.Battle()
        b = ElitePVE(self.char_id, _id, battle_msg)
        b.start()

        if battle_msg.self_win:
            self.stage.elites[str_id] += 1
            if self.this_stage.next:

                if str(self.this_stage.next) not in self.stage.elites:
                    self.stage.elites[str(self.this_stage.next)] = 0
                    self.send_new_notify(self.this_stage.next)
            self.stage.save()

            self.send_update_notify(_id)

            counter.incr()
            self.send_remained_times_notify()
            self.enable_next_elite_stage(_id)

        return battle_msg
Esempio n. 10
0
    def send_remained_times_notify(self):
        msg = protomsg.ActivityStageRemainedTimesNotify()

        counter = Counter(self.char_id, 'stage_active_type_one')
        msg.type_one_times = counter.remained_value

        counter.func_name = 'stage_active_type_two'
        msg.type_two_times = counter.remained_value

        publish_to_char(self.char_id, pack_msg(msg))
Esempio n. 11
0
    def check(self):
        char = Char(self.char_id)
        official_level = char.cacheobj.official
        if official_level == 0:
            return

        counter = Counter(self.char_id, 'official_reward')
        remained_value = counter.remained_value
        if remained_value > 0:
            attachment = Attachment(self.char_id)
            attachment.save_to_prize(6)
Esempio n. 12
0
    def send_times_notify(self):
        msg = protomsg.EliteStageTimesNotify()
        free_counter = Counter(self.char_id, 'stage_elite')
        msg.max_free_times = free_counter.max_value
        msg.cur_free_times = free_counter.cur_value

        msg.cicle_max_times = VALUE_SETTING['plunder_circle_times'].value
        msg.cicle_current_times = self.stage.elites_times

        msg.total_reset_cost = self.get_total_reset_cost()
        publish_to_char(self.char_id, pack_msg(msg))
Esempio n. 13
0
    def get_hero_cost(self, incr=False):
        counter = Counter(self.char_id, 'gethero')

        if incr:
            try:
                counter.incr()
                return 0
            except CounterOverFlow:
                return GET_HERO_COST
        else:
            if counter.remained_value > 0:
                return 0
            return GET_HERO_COST
Esempio n. 14
0
    def battle(self):
        need_sycee = 0

        counter = Counter(self.char_id, 'arena')
        if counter.remained_value <= 0:
            counter = Counter(self.char_id, 'arena_buy')
            if counter.remained_value <= 0:
                char = Char(self.char_id).mc
                if char.vip < VIP_MAX_LEVEL:
                    raise SanguoException(
                        errormsg.ARENA_NO_TIMES,
                        self.char_id,
                        "Arena Battle",
                        "arena no times. vip current: {0}, max {1}".format(char.vip, VIP_MAX_LEVEL)
                    )
                raise SanguoException(
                    errormsg.ARENA_NO_TIMES_FINAL,
                    self.char_id,
                    "Arena Battle",
                    "arena no times. vip reach max level {0}".format(VIP_MAX_LEVEL)
                )
            else:
                need_sycee = ARENA_COST_SYCEE

        rival_id = self.choose_rival()
        if not rival_id:
            raise SanguoException(
                errormsg.ARENA_NO_RIVAL,
                self.char_id,
                "Arena Battle",
                "no rival."
            )

        if need_sycee:
            resource = Resource(self.char_id, "Arena Battle", "battle for no free times")
            resource.check_and_remove(sycee=-need_sycee)

        counter.incr()

        # set battle cd
        redis_client.setex(REDIS_ARENA_BATTLE_CD_KEY(rival_id), 1, ARENA_CD)

        msg = protomsg.Battle()
        b = PVP(self.char_id, rival_id, msg)
        b.start()

        t = Task(self.char_id)
        t.trig(2)

        drop = make_standard_drop_from_template()
        adding_score = 0
        if msg.self_win:
            achievement = Achievement(self.char_id)
            achievement.trig(11, 1)

            # 只有打赢才设置积分
            self_score = self.score
            rival_arena = Arena(rival_id)
            rival_score = rival_arena.score

            new_score = calculate_score(self_score, rival_score, msg.self_win)
            self.set_score(new_score)
            adding_score = new_score - self_score

            rival_arena.be_beaten(rival_score, self_score, not msg.self_win, self.char_id)

            TimesLogArenaWin(self.char_id).inc()

            ae = ActivityEntry(self.char_id, 50004)
            if ae and ae.is_valid():
                drop = ae.get_additional_drop()
                Resource(self.char_id, "Arena Win").add(**drop)

        TimesLogArena(self.char_id).inc()
        ae = ActivityEntry(self.char_id, 40006)
        if ae:
            ae.trig()

        self.send_notify()
        drop['stuffs'].append((1001, adding_score))
        return msg, drop
Esempio n. 15
0
 def remained_buy_times(self):
     c = Counter(self.char_id, 'arena_buy')
     return c.remained_value
Esempio n. 16
0
 def remained_free_times(self):
     c = Counter(self.char_id, 'arena')
     return c.remained_value
Esempio n. 17
0
 def send_notify(self):
     counter = Counter(self.char_id, 'plunder')
     msg = PlunderNotify()
     msg.remained_free_times = counter.remained_value
     msg.points = self.mongo_plunder.points
     publish_to_char(self.char_id, pack_msg(msg))
Esempio n. 18
0
 def __init__(self, char_id):
     from core.counter import Counter
     self.char_id = char_id
     self.counter = Counter(char_id, 'horse_strength_free')
Esempio n. 19
0
    def get_links(self, max_workers=8):
        visited = set()
        links = set()
        depth = Counter()

        link = Link(self.url.lower(), depth)
        logger.info(f"Root url is {link.url}")

        links.add(link)
        visited.add(link)

        with concurrent.futures.ThreadPoolExecutor(
                max_workers=max_workers) as executor:
            while links:
                depth += 1
                logger.info(f"Collecting depth {depth} links")

                future_links_html = {
                    executor.submit(link.get_html): link
                    for link in links
                }
                links.clear()

                for future_link_html in concurrent.futures.as_completed(
                        future_links_html):
                    try:
                        link_html = future_link_html.result()

                        logger.info(
                            f"Trying to collect links from {future_links_html[future_link_html].url}"
                        )
                        a_elems = link_html.find_all('a')
                        for a_elem in a_elems:
                            link_url = self._make_link_url(a_elem, 'href')
                            if link_url:
                                new_link = Link(link_url, depth)

                                if new_link not in visited:
                                    links.add(new_link)
                                    visited.add(new_link)
                                else:
                                    link.occurrences += 1
                        logger.info(f"Successfully collected links")

                    except exceptions.BrokenLink:
                        logger.warn(
                            f"{future_links_html[future_link_html].url} is broken"
                        )
                        future_links_html[future_link_html].is_broken = True
                    except requests.RequestException:
                        logger.exception(
                            f"Something went wrong while requesting {future_links_html[future_link_html].url}"
                        )
                    except Exception:
                        logger.exception("Unknow error occurred")

                logger.info(
                    f"Visited {len(visited)} and has {len(links)} more links to go"
                )

        return visited
Esempio n. 20
0
 def __init__(self, url, depth, is_broken=False, is_img=False):
     self.url = url
     self.depth = depth
     self.occurrences = Counter()
     self.is_broken = is_broken
     self.is_img = is_img
Esempio n. 21
0
    def open(self, _id):
        if str(_id) not in self.panel.panel:
            raise SanguoException(
                errormsg.HEROPANEL_SOCKET_NOT_EXIST,
                self.char_id,
                "HeroPanel Open",
                "HeroPanel Socket {0} not exist".format(_id)
            )

        if self.panel.panel[str(_id)].opened:
            raise SanguoException(
                errormsg.HEROPANEL_SOCKET_ALREADY_OPENED,
                self.char_id,
                "HeroPanel Open",
                "HeroPanel Socket {0} already opended".format(_id)
            )

        none_opended_heros = self.none_opened_heros()
        if not none_opended_heros:
            raise SanguoException(
                errormsg.HEROPANEL_ALL_OPENED,
                self.char_id,
                "HeroPanel Open",
                "all opened."
            )

        none_opened_good_hero = None
        none_opened_other_heros = []
        for k, v in none_opended_heros:
            if v.good:
                none_opened_good_hero = (k, v)
                continue

            none_opened_other_heros.append((k, v))

        counter = Counter(self.char_id, 'gethero')
        try:
            counter.incr()
            using_sycee = 0
        except CounterOverFlow:
            # 没有免费次数了,需要用元宝
            using_sycee = GET_HERO_COST


        resource = Resource(self.char_id, "HeroPanel Open")
        with resource.check(sycee=-using_sycee):
            if none_opened_good_hero:
                # 还没有取到甲卡
                prob = GET_HERO_QUALITY_ONE_PROB[self.open_times + 1]
                if random.randint(1, 100) <= prob:
                    # 取得甲卡
                    socket_id, hero = none_opened_good_hero
                else:
                    socket_id, hero = random.choice(none_opened_other_heros)
            else:
                socket_id, hero = random.choice(none_opened_other_heros)

            self.panel.panel[str(_id)], self.panel.panel[socket_id] = self.panel.panel[socket_id], self.panel.panel[str(_id)]

            self.panel.panel[str(_id)].opened = True
            self.panel.save()
            save_hero(self.char_id, hero.oid)

        self.send_notify()
        return hero.oid
Esempio n. 22
0
 def free_times(self):
     # 翻卡牌的免费次数
     c = Counter(self.char_id, 'gethero')
     return c.remained_value
Esempio n. 23
0
 def __init__(self, char_id):
     self.char_id = char_id
     self.counter = Counter(char_id, 'levy')