Modified file: backend.py
Refactored leaderboard request to prepare for multimap environmentmain
parent
50a5a55641
commit
339ef339e4
31
backend.py
31
backend.py
|
@ -6,19 +6,8 @@ import sqlite3
|
||||||
app = Flask("LeaderAPI")
|
app = Flask("LeaderAPI")
|
||||||
CORS(app)
|
CORS(app)
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
#app.config['CORS_HEADERS'] = 'Content-Type'
|
|
||||||
|
|
||||||
class LeaderAPI(Resource):
|
|
||||||
|
|
||||||
def get(self):
|
|
||||||
dbcon = sqlite3.connect("leaderboard.db")
|
|
||||||
db = dbcon.cursor()
|
|
||||||
leaderboard = db.execute("SELECT time_username,time_time FROM leaderboard ORDER BY time_time LIMIT 10").fetchall()
|
|
||||||
db.close()
|
|
||||||
dbcon.close()
|
|
||||||
# dictionary of leaderboard is json, encoding using json.dumps results in double encoding with "" and return to browser as type string
|
|
||||||
return list(leaderboard)
|
|
||||||
|
|
||||||
|
class LeaderPostAPI(Resource):
|
||||||
def post(self):
|
def post(self):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
print(data)
|
print(data)
|
||||||
|
@ -34,11 +23,27 @@ class LeaderAPI(Resource):
|
||||||
db.close()
|
db.close()
|
||||||
dbcon.close()
|
dbcon.close()
|
||||||
|
|
||||||
|
class LeaderGetAPI(Resource):
|
||||||
|
|
||||||
|
def post(self):
|
||||||
|
data = request.get_json()
|
||||||
|
print(data)
|
||||||
|
# Pylance error seems to be fine, no problems I guess
|
||||||
|
gamemap = data['map']
|
||||||
|
dbcon = sqlite3.connect("leaderboard.db")
|
||||||
|
db = dbcon.cursor()
|
||||||
|
leaderboard = db.execute(f"SELECT time_username,time_time FROM leaderboard WHERE time_map = '{gamemap}' ORDER BY time_time LIMIT 10").fetchall()
|
||||||
|
db.close()
|
||||||
|
dbcon.close()
|
||||||
|
# dictionary of leaderboard is json, encoding using json.dumps results in double encoding with "" and return to browser as type string
|
||||||
|
return list(leaderboard)
|
||||||
|
|
||||||
class TokenAPI(Resource):
|
class TokenAPI(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
print(request.remote_addr)
|
print(request.remote_addr)
|
||||||
|
|
||||||
api.add_resource(LeaderAPI, "/")
|
api.add_resource(LeaderGetAPI, "/get/")
|
||||||
|
api.add_resource(LeaderPostAPI, "/post/")
|
||||||
api.add_resource(TokenAPI, "/token/")
|
api.add_resource(TokenAPI, "/token/")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue