Ejemplo n.º 1
0
def get_iv():
    if not app_state.accept_new_requests:
        return reject('Not accepting new requests.')
    if not have_active_scouts():
        return reject('No active scout available. All banned?')

    pokemon_id = request.args["pokemon_id"]
    pokemon_name = get_pokemon_name(pokemon_id)
    forced = request.args.get('forced')
    prio = PRIO_HIGH if forced is not None else get_pokemon_prio(pokemon_id)

    cache_enable = cfg_get('cache_timer') > 0
    max_queued_jobs = cfg_get('max_queued_jobs')
    num_jobs = jobs.qsize()
    if max_queued_jobs and num_jobs >= max_queued_jobs and prio == PRIO_LOW:
        return reject(
            "Job queue full ({} items). Rejecting encounter with priority '{}'."
            .format(num_jobs, PRIO_NAMES[prio]))

    lat = request.args["latitude"]
    lng = request.args["longitude"]
    weather = request.args.get("weather", "unknown")

    encounter_id = normalize_encounter_id(request.args.get("encounter_id"))
    # Spawn point ID is assumed to be a hex string
    spawn_point_id = request.args.get("spawn_point_id")
    despawn_time = request.args.get("despawn_time")

    if cache_enable:
        # Check cache
        cache_key = "{}-{}".format(
            encounter_id, weather) if encounter_id else "{}-{}-{}".format(
                pokemon_id, lat, lng)
        result = get_cached_encounter(cache_key)
        if result:
            log.info(
                u"Returning cached result: {:.1f}% level {} {} with {} CP".
                format(result['iv_percent'], result['level'], pokemon_name,
                       result['cp']))
            return jsonify(result)

    # Create a ScoutJob
    job = ScoutJob(pokemon_id,
                   encounter_id,
                   spawn_point_id,
                   lat,
                   lng,
                   despawn_time=despawn_time)

    # Enqueue and wait for job to be processed
    jobs.put((prio, time.time(), job))
    while not job.processed:
        time.sleep(1)

    # Cache successful jobs and return result
    if cache_enable and job.result['success']:
        cache_encounter(cache_key, job.result)
    return jsonify(job.result)
Ejemplo n.º 2
0
    def __init__(self,
                 pokemon_id,
                 encounter_id,
                 spawn_point_id,
                 lat,
                 lng,
                 despawn_time=None):
        self.pokemon_id = int(pokemon_id)
        self.pokemon_name = get_pokemon_name(pokemon_id)
        self.encounter_id = encounter_id
        self.spawn_point_id = spawn_point_id
        self.lat = float(lat)
        self.lng = float(lng)
        self.despawn_time = despawn_time

        self.processed = False
        self.result = {}

        # Set a time when this job expires if a TTL was given
        ttl = cfg_get('max_job_ttl')
        self.expire_at = time.time() + (ttl * 60) if ttl else None

        # Use fixed random altitude per job
        self.altitude = random.randint(12, 108)
Ejemplo n.º 3
0
def pokemon(page=1):
    def td(cell):
        return u"<td>{}</td>".format(cell)

    pstats = get_pokemon_stats()
    headers = ["#", "Pokemon Name", "Encounters"]
    hdict = {'#': 'pid', 'Pokemon Name': 'pname', 'Encounters': 'count'}
    sort = request.args.get('sort', 'count')
    reverse = request.args.get('reverse', True)
    max_pokemon_per_page = int(request.args.get('max_per_page', 25))
    if reverse == "False":
        reverse = False
    elif reverse == "True":
        reverse = True
    for i in range(0, len(pstats)):
        pstats[i]['pname'] = get_pokemon_name(pstats[i]['pid'])
    pstats.sort(key=lambda x: x[sort], reverse=reverse)
    max_page = int(math.ceil(len(pstats) / float(max_pokemon_per_page)))

    lines = u"<style> th,td { padding-left: 10px; padding-right: 10px; border: 1px solid #ddd; } table " \
            u"{ border-collapse: collapse } td { text-align:center }</style>"
    lines += "<h3>Pokemon Stats</h3>"
    lines += "<table><tr>"
    for h in headers:
        if hdict[h] == sort:
            r = not reverse
            arrow = u" ▲" if reverse else u" ▼"
        else:
            r = False
            arrow = ""
        lines += u"<th><a href=./{}?sort={}&reverse={}&max_per_page={}>{}{}</a></th>".format(
            page, hdict[h], r, max_pokemon_per_page, h, arrow)
    lines += "</tr>"

    if page * max_pokemon_per_page > len(
            pstats):  #Page number is too great, set to last page
        page = max_page
    if page < 1:
        page = 1

    for i in range((page - 1) * max_pokemon_per_page,
                   page * max_pokemon_per_page):
        if i >= len(pstats):
            break
        pid = pstats[i]['pid']
        lines += "<tr>"
        lines += td(pid)
        lines += td(get_pokemon_name(pid))
        lines += td(pstats[i]['count'])
        lines += "</tr>"
    lines += "</table>"

    if len(
            pstats
    ) > max_pokemon_per_page:  # Use pages if we have more than max_scouts_per_page
        lines += "<br>"
        lines += "Page: "
        if max_page > 1 and page > 1:
            lines += u"<a href=./{}?sort={}&reverse={}&max_per_page={}>&lt;</a> | ".format(
                page - 1, sort, reverse, max_pokemon_per_page)
        for p in range(1, max_page + 1):
            if p == page:
                lines += str(p)
            else:
                lines += u"<a href=./{}?sort={}&reverse={}&max_per_page={}>{}</a>".format(
                    p, sort, reverse, max_pokemon_per_page, p)
            if p < max_page:
                lines += " | "
        if max_page > 1 and page < max_page:
            lines += u" | <a href=./{}?sort={}&reverse={}&max_per_page={}>&gt;</a>".format(
                page + 1, sort, reverse, max_pokemon_per_page)
        lines += "<br>"

    lines += "<br>Max Per Page:&nbsp;&nbsp;"
    lines += "<select onchange='this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);'>"
    lines += u"<option value=./{}?sort={}&reverse={}&max_per_page=10 {}>10</option>".format(
        page, sort, reverse, "selected" if max_pokemon_per_page == 10 else "")
    lines += u"<option value=./{}?sort={}&reverse={}&max_per_page=25 {}>25</option>".format(
        page, sort, reverse, "selected" if max_pokemon_per_page == 25 else "")
    lines += u"<option value=./{}?sort={}&reverse={}&max_per_page=50 {}>50</option>".format(
        page, sort, reverse, "selected" if max_pokemon_per_page == 50 else "")
    lines += u"<option value=./{}?sort={}&reverse={}&max_per_page=100 {}>100</option>".format(
        page, sort, reverse, "selected" if max_pokemon_per_page == 100 else "")
    lines += "</select><br>"

    return lines
Ejemplo n.º 4
0
 def format_pstat_line(current_line, e):
     return line_tmpl.format(get_pokemon_name(e['pid']), e['count'])