-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
186 lines (162 loc) · 4.46 KB
/
app.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
"""Scoreboard server"""
import sqlite3
import os
from typing import Tuple
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS, cross_origin
from dotenv import load_dotenv
from datetime import datetime, timedelta
load_dotenv()
SQLITE_FILENAME = os.environ["SQLITE_FILENAME"]
app = Flask(__name__)
cors = CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
def db_connect() -> Tuple[sqlite3.Connection, sqlite3.Cursor]:
"""Connect to database and get cursor"""
con = sqlite3.connect(SQLITE_FILENAME)
return con, con.cursor()
@app.route("/")
@cross_origin()
def hello_world():
"""Test endpoint"""
return render_template("index.html")
@app.route("/scoreboard/top", methods=["GET"])
@cross_origin()
def scoreboard_top():
"""get the top scores for a timeframe
can specify:
- timeframe
- total number
e.g.,
curl -s "http://127.0.0.1:5000/scoreboard/top?total=10&timeframe=daily"
"""
total = request.args.get("total")
timeframe = request.args.get("timeframe")
unique = request.args.get("unique")
if timeframe == "daily":
now = datetime.now()
today = datetime(now.year, now.month, now.day)
earliest_date = today
if timeframe == "weekly":
now = datetime.now()
today = datetime(now.year, now.month, now.day)
start = today - timedelta(days=today.weekday())
earliest_date = start
if timeframe == "alltime":
earliest_date = datetime(1970, 1, 1)
earliest_date_str = earliest_date.strftime("%Y-%m-%d %H:%M:%S.%f")
if unique in ["0", 0, False, "False"]:
unique = False
sqlformat = {
"height": "max_height",
"groupby": "",
}
else:
unique = True
sqlformat = {
"height": "MAX(max_height)",
"groupby": "GROUP BY name",
}
_, cur = db_connect()
sql = """
SELECT name, {height}, timestamp FROM scores
WHERE
timestamp > ?
and
spurious = 0
{groupby}
ORDER BY
{height} DESC
LIMIT ?
""".format(
**sqlformat
)
res_obj = cur.execute(
sql,
[
earliest_date_str,
total,
],
)
res = res_obj.fetchall()
results = [{"name": r[0], "max_height": r[1], "timestamp": r[2]} for r in res]
data = {"scores": results}
all_accepts = request.headers.get("Accept", "")
accepts = all_accepts.split(",")
if "text/html" in accepts:
return render_template(
"scoreboard.html",
scores=data["scores"],
total=total,
timeframe=timeframe,
unique=unique,
)
return data
@app.route("/scoreboard/new", methods=["GET"])
@cross_origin()
def scoreboard_new():
"""get the most recent scores
can specify:
- total
e.g.,
curl -s "http://127.0.0.1:5000/scoreboard/new?total=10"
"""
total = request.args.get("total")
_, cur = db_connect()
res_obj = cur.execute(
"""
SELECT name, max_height, timestamp FROM scores
ORDER BY
timestamp DESC
LIMIT ?
""",
[total],
)
res = res_obj.fetchall()
results = [
{
"name": r[0],
"max_height": r[1],
"timestamp": r[2],
}
for r in res
]
data = {"scores": results}
all_accepts = request.headers.get("Accept", "")
accepts = all_accepts.split(",")
if "text/html" in accepts:
return render_template(
"scores.html",
scores=data["scores"],
total=total,
)
return data
@app.route("/score/new", methods=["POST"])
@cross_origin()
def score_new():
"""add a new score to the board!
e.g.,
curl -s -H "Content-Type: application/json" --request POST "http://127.0.0.1:5000/score/new" --data "@score.json"
"""
json = request.get_json()
con, cur = db_connect()
cur.execute(
"""
INSERT INTO scores VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
""",
[
json["timestamp"],
json["name"],
json["max_height"],
json["time_total_s"],
json["time_building_s"],
json["time_scaling_s"],
json["blocks_placed"],
json["jumps"],
json["distance_fallen"],
],
)
con.commit()
id_ = cur.lastrowid
return jsonify({"success": True, "id": id_})