This commit is contained in:
Harvey Tindall 2020-06-28 14:15:46 +01:00
parent 1378b9cbc9
commit ff04f6b33d

View File

@ -11,56 +11,54 @@ api.debug = 0
app = Flask(__name__) app = Flask(__name__)
@app.route('/')
@app.route("/")
def index(): def index():
return render_template('index.html') return render_template("index.html")
@app.route('/search', methods=['POST']) @app.route("/search", methods=["POST"])
def search(): def search():
data = request.get_json() data = request.get_json()
games = api.search(srcomapi.datatypes.Game, data)[:10] games = api.search(srcomapi.datatypes.Game, data)[:10]
response = {'games': [g.name for g in games]} response = {"games": [g.name for g in games]}
return jsonify(response), 200 return jsonify(response), 200
@app.route('/categories', methods=['POST']) @app.route("/categories", methods=["POST"])
def categories(): def categories():
data = request.get_json() data = request.get_json()
game = api.search(srcomapi.datatypes.Game, data)[0] game = api.search(srcomapi.datatypes.Game, data)[0]
raw_cats = game.categories raw_cats = game.categories
cats = {'categories': []} cats = {"categories": []}
for cat in raw_cats: for cat in raw_cats:
if cat.type == 'per-game': if cat.type == "per-game":
cats['categories'].append(cat.name) cats["categories"].append(cat.name)
return jsonify(cats) return jsonify(cats)
@app.route('/graph', methods=['POST']) @app.route("/graph", methods=["POST"])
def graph(): def graph():
data = request.get_json() data = request.get_json()
game = api.search(srcomapi.datatypes.Game, {'name': data['name']})[0] game = api.search(srcomapi.datatypes.Game, {"name": data["name"]})[0]
for cat in game.categories: for cat in game.categories:
if (data['category'] == cat.name and if data["category"] == cat.name and cat.type == "per-game":
cat.type == 'per-game'):
category = cat category = cat
release_date = datetime.datetime.strptime(game.release_date, release_date = datetime.datetime.strptime(game.release_date, "%Y-%m-%d")
'%Y-%m-%d')
data = srcomapi.datatypes.Leaderboard( data = srcomapi.datatypes.Leaderboard(
api, api,
data=api.get(f"leaderboards/{game.id}/category/{category.id}?embed=variables") data=api.get(f"leaderboards/{game.id}/category/{category.id}?embed=variables"),
) )
raw_runs = data.runs raw_runs = data.runs
runs = {'Time': [], 'Days since release': [], 'Annotation': []} runs = {"Time": [], "Days since release": [], "Annotation": []}
for run in raw_runs: for run in raw_runs:
r = run['run'] r = run["run"]
time = r.times['primary_t'] time = r.times["primary_t"]
if r.date is not None: if r.date is not None:
date = datetime.datetime.strptime(r.date, date = datetime.datetime.strptime(r.date, "%Y-%m-%d")
'%Y-%m-%d')
time_since_release = date - release_date time_since_release = date - release_date
runs['Time'].append(time / 60) runs["Time"].append(time / 60)
runs['Days since release'].append(time_since_release.days) runs["Days since release"].append(time_since_release.days)
# if len(r.players) == 1: # if len(r.players) == 1:
# players = r.players[0].name # players = r.players[0].name
# elif len(r.players) > 1: # elif len(r.players) > 1:
@ -71,36 +69,30 @@ def graph():
# players += ', ' # players += ', '
annotation = ( annotation = (
# f'players: {players}\n' # f'players: {players}\n'
f'date: {r.date}' f"date: {r.date}"
) )
runs['Annotation'].append(annotation) runs["Annotation"].append(annotation)
graph = plotly.graph_objs.Figure() graph = plotly.graph_objs.Figure()
trace = plotly.graph_objs.Scatter(x=runs['Days since release'], trace = plotly.graph_objs.Scatter(
y=runs['Time'], x=runs["Days since release"],
mode='markers', y=runs["Time"],
hoverinfo='text', mode="markers",
hovertext=runs['Annotation']) hoverinfo="text",
hovertext=runs["Annotation"],
)
graph.add_trace(trace) graph.add_trace(trace)
graph.update_layout(title=f'{game.name} ({category.name}): Run time since release', graph.update_layout(
xaxis_title='Days since release', title=f"{game.name} ({category.name}): Run time since release",
yaxis_title='Time (Minutes)') xaxis_title="Days since release",
yaxis_title="Time (Minutes)",
)
graph_json = graph.to_json() graph_json = graph.to_json()
# graph_json = json.dumps([trace], cls=plotly.utils.PlotlyJSONEncoder) # graph_json = json.dumps([trace], cls=plotly.utils.PlotlyJSONEncoder)
return app.response_class( return app.response_class(
response=graph_json, response=graph_json, status=200, mimetype="application/json"
status=200,
mimetype='application/json'
) )
def main(): def main():
global app global app
serve(app, host='0.0.0.0', port=8059) serve(app, host="0.0.0.0", port=8059)