LeaderboardBackend/backend.py

50 lines
1.6 KiB
Python

from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS
import sqlite3
app = Flask("LeaderAPI")
CORS(app)
api = Api(app)
class LeaderPostAPI(Resource):
def post(self):
data = request.get_json()
print(data)
# Pylance error seems to be fine, no problems I guess
username = data['username']
time = data['time']
gamemap = data['map']
version = str(data['version'])
dbcon = sqlite3.connect("leaderboard.db")
db = dbcon.cursor()
db.execute(f"INSERT INTO leaderboard (time_username, time_time, time_map, time_gamever) VALUES ('{username}', {time}, '{gamemap}', '{version}')")
dbcon.commit()
db.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):
def get(self):
print(request.remote_addr)
api.add_resource(LeaderGetAPI, "/get/")
api.add_resource(LeaderPostAPI, "/post/")
api.add_resource(TokenAPI, "/token/")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=25540)