This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
web.py
executable file
·276 lines (232 loc) · 9.62 KB
/
web.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# DISCLAIMER: This is jank
from __future__ import print_function
import argparse
import csv
import json
import os
import zerorpc
from collections import defaultdict
from flask import Flask, flash, jsonify, redirect, render_template, url_for
from werkzeug.exceptions import NotFound
from poketrainer.poke_lvl_data import TCPM_VALS
from poketrainer.pokemon import Pokemon
class ReverseProxied(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
if script_name:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
environ['PATH_INFO'] = path_info[len(script_name):]
scheme = environ.get('HTTP_X_SCHEME', '')
if scheme:
environ['wsgi.url_scheme'] = scheme
return self.app(environ, start_response)
app = Flask(__name__, template_folder="web/templates", static_folder='web/static')
app.wsgi_app = ReverseProxied(app.wsgi_app)
app.secret_key = ".t\x86\xcb3Lm\x0e\x8c:\x86\xe8FD\x13Z\x08\xe1\x04(\x01s\x9a\xae"
app.debug = True
options = {}
attacks = {}
with open("resources" + os.sep + "GAME_ATTACKS_v0_1.tsv") as tsv:
reader = csv.DictReader(tsv, delimiter='\t')
for row in reader:
attacks[int(row["Num"])] = row["Move"]
def init_config(username):
config_file = "config.json"
config_data = {}
if os.path.isfile(config_file):
with open(config_file) as data:
config_data.update(json.load(data))
filtered_data = filter(lambda x: x.get('username') == username, config_data.get('accounts', []))
return filtered_data[0]
def set_columns_to_ignore(columns_to_ignore):
options['ignore_recent'] = ''
options['ignore_#'] = ''
options['ignore_name'] = ''
options['ignore_lvl'] = ''
options['ignore_score'] = ''
options['ignore_IV'] = ''
options['ignore_CP'] = ''
options['ignore_max_CP'] = ''
options['ignore_candies'] = ''
options['ignore_candy_needed'] = ''
options['ignore_dust_needed'] = ''
options['ignore_power_up'] = ''
options['ignore_stamina'] = ''
options['ignore_attkIV'] = ''
options['ignore_staIV'] = ''
options['ignore_defIV'] = ''
options['ignore_moves'] = ''
options['ignore_transfer'] = ''
for column in columns_to_ignore:
if column.lower() == 'recent':
options['ignore_recent'] = 'display: none;'
elif column.lower() == '#':
options['ignore_id'] = 'display: none;'
elif column.lower() == 'name':
options['ignore_name'] = 'display: none;'
elif column.lower() == 'lvl':
options['ignore_lvl'] = 'display: none;'
elif column.lower() == 'score':
options['ignore_score'] = 'display: none;'
elif column.lower() == 'iv':
options['ignore_IV'] = 'display: none;'
elif column.lower() == 'cp':
options['ignore_CP'] = 'display: none;'
elif column.lower() == 'max cp':
options['ignore_max_CP'] = 'display: none;'
elif column.lower() == 'candies':
options['ignore_candies'] = 'display: none;'
elif column.lower() == 'candy needed':
options['ignore_candy_needed'] = 'display: none;'
elif column.lower() == 'dust needed':
options['ignore_dust_needed'] = 'display: none;'
elif column.lower() == 'power up':
options['ignore_power_up'] = 'display: none;'
elif column.lower() == 'stamina':
options['ignore_stamina'] = 'display: none;'
elif column.lower() == 'att iv':
options['ignore_attkIV'] = 'display: none;'
elif column.lower() == 'sta iv':
options['ignore_staIV'] = 'display: none;'
elif column.lower() == 'def iv':
options['ignore_defIV'] = 'display: none;'
elif column.lower() == 'move 1':
options['ignore_move1'] = 'display: none;'
elif column.lower() == 'move 2':
options['ignore_move2'] = 'display: none;'
elif column.lower() == 'transfer':
options['ignore_transfer'] = 'display: none;'
def get_api_rpc(username):
desc_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), ".listeners")
sock_port = 0
with open(desc_file) as f:
data = f.read()
data = json.loads(data if len(data) > 0 else '{}')
if username not in data:
# FIXME Use logger instead of print statements!
print("There is no bot running with username '%s'!" % username)
return None
sock_port = int(data[username])
c = zerorpc.Client()
c.connect("tcp://127.0.0.1:%i" % sock_port)
return c
@app.route("/favicon.ico")
def favicon():
# Explicitly handle favicon.ico so it doesn't route to the status function.
return NotFound()
@app.route("/<username>")
@app.route("/<username>/status")
def status(username):
c = get_api_rpc(username)
if c is None:
return("There is no bot running with username '%s'!" % username)
config = init_config(username)
options['SCORE_METHOD'] = config.get('POKEMON_CLEANUP', {}).get("SCORE_METHOD", "CP")
options['IGNORE_COLUMNS'] = config.get("IGNORE_COLUMNS", [])
set_columns_to_ignore(options['IGNORE_COLUMNS'])
player_json = json.loads(c.get_player_info())
currency = player_json['player_data']['currencies'][1]['amount']
latlng = c.current_location()
latlng = "%f,%f" % (latlng[0], latlng[1])
items = json.loads(c.get_inventory())['inventory_items']
pokemons_data = []
candy = defaultdict(int)
for item in items:
item = item['inventory_item_data']
pokemon = item.get("pokemon_data", {})
if "pokemon_id" in pokemon:
pokemons_data.append(pokemon)
if 'player_stats' in item:
player = item['player_stats']
if "candy" in item:
filled_family = str(item['candy']['family_id']).zfill(4)
candy[filled_family] += item['candy'].get("candy", 0)
# add candy back into pokemon json
pokemons = []
for pokemon in pokemons_data:
pkmn = Pokemon(pokemon, player['level'], options['SCORE_METHOD'])
pkmn.candy = candy[pkmn.family_id]
pkmn.set_max_cp(TCPM_VALS[int(player['level'] * 2 + 1)])
pkmn.score = format(pkmn.score, '.2f').rstrip('0').rstrip('.') # makes the value more presentable to the user
pokemons.append(pkmn)
player['username'] = player_json['player_data']['username']
player['level_xp'] = player.get('experience', 0) - player.get('prev_level_xp', 0)
with open('./data_dumps/' + str(username) + '.json') as json_data:
d = json.load(json_data)['GET_PLAYER']['player_data']['hourly_exp']
player['hourly_exp'] = d # Not showing up in inv or player data
player['goal_xp'] = player.get('next_level_xp', 0) - player.get('prev_level_xp', 0)
return render_template('status.html', pokemons=pokemons, player=player, currency="{:,d}".format(currency), candy=candy, latlng=latlng, attacks=attacks, username=username, options=options)
@app.route("/<username>/pokemon")
def pokemon(username):
s = get_api_rpc(username)
try:
pokemons = json.loads(s.get_caught_pokemons())
except ValueError:
# FIXME Use logger instead of print statements!
print("Not valid Json")
return render_template('pokemon.html', pokemons=pokemons, username=username)
@app.route("/<username>/inventory")
def inventory(username):
s = get_api_rpc(username)
try:
inventory = json.loads(s.get_inventory())
except ValueError:
# FIXME Use logger instead of print statements!
print("Not valid Json")
return render_template('inventory.html', inventory=json.dumps(inventory, indent=2), username=username)
@app.route("/<username>/transfer/<p_id>")
def transfer(username, p_id):
c = get_api_rpc(username)
if c and c.release_pokemon_by_id(p_id) == 1:
flash("Released")
else:
flash("Failed!")
return redirect(url_for('status', username=username))
@app.route("/<username>/snipe/<latlng>")
def snipe(username, latlng):
c = get_api_rpc(username)
try:
if len(latlng.split(',')) == 2:
l = latlng.split(',')
lat = float(l[0])
lng = float(l[1])
else:
l = latlng.split(' ')
lat = float(l[0])
lng = float(l[1])
except:
return jsonify(status=1, result='Error parsing coordinates.')
if c.snipe_pokemon(lat, lng):
msg = "Sniped!"
status = 0
else:
msg = "Failed sniping!"
status = 1
return jsonify(status=status, result=msg)
def init_web_config():
default_configs = {
"hostname": "0.0.0.0",
"port": 5000,
"debug": True
}
config_file = "web_config.json"
# If config file exists, load variables from json
if os.path.isfile(config_file):
with open(config_file) as data:
default_configs.update(json.load(data))
# Read passed in Arguments
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--hostname", help="Server hostname/IP", default=default_configs['hostname'])
parser.add_argument("-p", "--port", help="Server TCP port number", type=int, default=default_configs['port'])
parser.add_argument("-d", "--debug", help="Debug Mode", action='store_true', default=default_configs['debug'])
web_config = parser.parse_args()
return web_config
def main():
web_config = init_web_config()
app.run(host=web_config.hostname, port=web_config.port, debug=web_config.debug)
if __name__ == "__main__":
main()