2022-10-30 18:47:39 +00:00
|
|
|
from flask import Flask, request
|
2022-10-30 15:24:08 +00:00
|
|
|
from flask_restful import Resource, Api
|
2022-10-30 22:04:59 +00:00
|
|
|
from flask_cors import CORS
|
2022-10-30 16:15:39 +00:00
|
|
|
import sqlite3
|
2022-10-30 15:24:08 +00:00
|
|
|
|
|
|
|
app = Flask("LeaderAPI")
|
2022-10-30 22:04:59 +00:00
|
|
|
CORS(app)
|
2022-10-30 15:24:08 +00:00
|
|
|
api = Api(app)
|
2022-10-30 18:47:39 +00:00
|
|
|
|
2022-11-01 08:15:13 +00:00
|
|
|
class LeaderPostAPI(Resource):
|
2022-10-30 22:04:59 +00:00
|
|
|
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()
|
|
|
|
|
2022-11-01 08:15:13 +00:00
|
|
|
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)
|
|
|
|
|
2022-11-08 15:21:13 +00:00
|
|
|
class LeaderGetCacheAPI(Resource):
|
|
|
|
|
|
|
|
def post(self):
|
|
|
|
data = request.get_json()
|
|
|
|
# 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
|
|
|
|
#leaderwmap = gamemap + list(leaderboard)
|
|
|
|
#print(leaderwmap)
|
|
|
|
leaderwmap = {
|
|
|
|
"map": gamemap,
|
|
|
|
"leaderboard": list(leaderboard)
|
|
|
|
}
|
|
|
|
return leaderwmap
|
2022-10-30 18:47:39 +00:00
|
|
|
class TokenAPI(Resource):
|
|
|
|
def get(self):
|
|
|
|
print(request.remote_addr)
|
2022-10-30 15:24:08 +00:00
|
|
|
|
2022-11-01 08:15:13 +00:00
|
|
|
api.add_resource(LeaderGetAPI, "/get/")
|
|
|
|
api.add_resource(LeaderPostAPI, "/post/")
|
2022-10-30 18:47:39 +00:00
|
|
|
api.add_resource(TokenAPI, "/token/")
|
2022-11-08 15:21:13 +00:00
|
|
|
api.add_resource(LeaderGetCacheAPI, "/cache/")
|
2022-10-30 15:24:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-10-30 18:47:39 +00:00
|
|
|
app.run(host="0.0.0.0", port=25540)
|