forked from Noctem/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 17
/
gyms.py
executable file
·119 lines (106 loc) · 3.56 KB
/
gyms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
from datetime import datetime, timedelta
from pkg_resources import resource_filename
import time
import argparse
from flask import Flask, render_template
from monocle import db, sanitized as conf
from monocle.names import POKEMON
from monocle.web_utils import get_args
from monocle.bounds import area
app = Flask(__name__, template_folder=resource_filename('monocle', 'templates'))
CACHE = {
'data': None,
'generated_at': None,
}
def get_stats():
cache_valid = (
CACHE['data'] and
CACHE['generated_at'] > datetime.now() - timedelta(minutes=15)
)
if cache_valid:
return CACHE['data']
with db.session_scope() as session:
forts = db.get_forts(session)
count = {t.value: 0 for t in db.Team}
strongest = {t.value: None for t in db.Team}
guardians = {t.value: {} for t in db.Team}
top_guardians = {t.value: None for t in db.Team}
prestige = {t.value: 0 for t in db.Team}
percentages = {}
prestige_percent = {}
total_prestige = 0
last_date = 0
pokemon_names = POKEMON
for fort in forts:
if fort['last_modified'] > last_date:
last_date = fort['last_modified']
team = fort['team']
count[team] += 1
if team != 0:
# Strongest gym
existing = strongest[team]
should_replace = (
existing is not None and
fort['prestige'] > existing[0] or
existing is None
)
pokemon_id = fort['guard_pokemon_id']
if should_replace:
strongest[team] = (
fort['prestige'],
pokemon_id,
pokemon_names[pokemon_id],
)
# Guardians
guardian_value = guardians[team].get(pokemon_id, 0)
guardians[team][pokemon_id] = guardian_value + 1
# Prestige
prestige[team] += fort['prestige']
total_prestige = sum(prestige.values())
for team in db.Team:
percentages[team.value] = (
count.get(team.value) / len(forts) * 100
)
prestige_percent[team.value] = (
prestige.get(team.value) / total_prestige * 100
)
if guardians[team.value]:
pokemon_id = sorted(
guardians[team.value],
key=guardians[team.value].__getitem__,
reverse=True
)[0]
top_guardians[team.value] = pokemon_names[pokemon_id]
CACHE['generated_at'] = datetime.now()
CACHE['data'] = {
'order': sorted(count, key=count.__getitem__, reverse=True),
'count': count,
'total_count': len(forts),
'strongest': strongest,
'prestige': prestige,
'prestige_percent': prestige_percent,
'percentages': percentages,
'last_date': last_date,
'top_guardians': top_guardians,
'generated_at': CACHE['generated_at'],
}
return CACHE['data']
@app.route('/')
def index():
stats = get_stats()
team_names = {k.value: k.name.title() for k in db.Team}
styles = {1: 'primary', 2: 'danger', 3: 'warning'}
return render_template(
'gyms.html',
area_name=conf.AREA_NAME,
area_size=area,
minutes_ago=int((datetime.now() - stats['generated_at']).seconds / 60),
last_date_minutes_ago=int((time.time() - stats['last_date']) / 60),
team_names=team_names,
styles=styles,
**stats
)
if __name__ == '__main__':
args = get_args()
app.run(debug=args.debug, host=args.host, port=args.port)