mirror of
https://github.com/Project-Redacted/Highscores-Server.git
synced 2025-05-14 07:32:15 +00:00
Move score upload to the API section
This commit is contained in:
parent
10456f60a0
commit
aed2bbf4ec
2 changed files with 59 additions and 72 deletions
|
@ -3,8 +3,9 @@ import uuid
|
||||||
from flask import Blueprint, request, jsonify
|
from flask import Blueprint, request, jsonify
|
||||||
from flask_login import login_required, current_user
|
from flask_login import login_required, current_user
|
||||||
|
|
||||||
from server.models import Tokens
|
from server.models import Tokens, Scores
|
||||||
from server.extensions import db
|
from server.extensions import db
|
||||||
|
from server.config import BEARER_TOKEN
|
||||||
|
|
||||||
|
|
||||||
blueprint = Blueprint('api', __name__, url_prefix='/api')
|
blueprint = Blueprint('api', __name__, url_prefix='/api')
|
||||||
|
@ -37,3 +38,59 @@ def tokens():
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return jsonify({"success": "Token added!"}), 200
|
return jsonify({"success": "Token added!"}), 200
|
||||||
|
|
||||||
|
|
||||||
|
@blueprint.route('/post', methods=['POST'])
|
||||||
|
def post():
|
||||||
|
form = request.form
|
||||||
|
|
||||||
|
if not form:
|
||||||
|
return "Invalid form", 400
|
||||||
|
if not request.headers.get('Authentication'):
|
||||||
|
return "Invalid authentication", 401
|
||||||
|
|
||||||
|
if not isinstance(form['score'], int):
|
||||||
|
return "Score must be an integer", 400
|
||||||
|
if int(form['score']) < 0:
|
||||||
|
return "Score must be greater than 0", 400
|
||||||
|
if form['difficulty'] not in [0, 1, 2, 3, 4]:
|
||||||
|
# 0 = Easy, Level 1
|
||||||
|
# 1 = Easy, Level 2
|
||||||
|
# 2 = Easy, Level 3
|
||||||
|
# 3 = Normal
|
||||||
|
# 4 = Hard
|
||||||
|
return "Invalid difficulty", 400
|
||||||
|
|
||||||
|
if token_data := Tokens.query.filter_by(token=request.headers.get('Authentication')).first():
|
||||||
|
# User is authenticated
|
||||||
|
# This is a registered user
|
||||||
|
|
||||||
|
score = Scores(
|
||||||
|
score=form['score'],
|
||||||
|
difficulty=form['difficulty'],
|
||||||
|
achievements=form['achievements'],
|
||||||
|
user_id=token_data.holder,
|
||||||
|
)
|
||||||
|
db.session.add(score)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return "Success!", 200
|
||||||
|
elif request.headers.get('Authentication') == BEARER_TOKEN:
|
||||||
|
# User is not authenticated, but has the correct token
|
||||||
|
# This is an anonymous user
|
||||||
|
|
||||||
|
if not form['playerName'] or len(form['playerId']) != 4:
|
||||||
|
return "Invalid player name", 400
|
||||||
|
|
||||||
|
score = Scores(
|
||||||
|
anonymous=True,
|
||||||
|
username=form['playerName'],
|
||||||
|
score=form['score'],
|
||||||
|
difficulty=form['difficulty'],
|
||||||
|
)
|
||||||
|
db.session.add(score)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return "Success!", 200
|
||||||
|
|
||||||
|
return "Authentication failed", 401
|
||||||
|
|
|
@ -1,24 +1,10 @@
|
||||||
from flask import Blueprint, request, render_template
|
from flask import Blueprint, request, render_template
|
||||||
from flask_wtf import FlaskForm
|
from server.models import Scores
|
||||||
from wtforms import StringField, IntegerField
|
|
||||||
from wtforms.validators import DataRequired
|
|
||||||
|
|
||||||
from server.models import Scores, Tokens
|
|
||||||
from server.extensions import db
|
|
||||||
from server.config import BEARER_TOKEN
|
|
||||||
|
|
||||||
|
|
||||||
blueprint = Blueprint('views', __name__)
|
blueprint = Blueprint('views', __name__)
|
||||||
|
|
||||||
|
|
||||||
class ScoreForm(FlaskForm):
|
|
||||||
playerName = StringField('Player Name', validators=[DataRequired()])
|
|
||||||
playerId = StringField('Player ID', validators=[DataRequired()])
|
|
||||||
score = IntegerField('Score', validators=[DataRequired()])
|
|
||||||
difficulty = StringField('Difficulty', validators=[DataRequired()])
|
|
||||||
achievements = StringField('Achievements', validators=[DataRequired()])
|
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/')
|
@blueprint.route('/')
|
||||||
# @cache.cached(timeout=60)
|
# @cache.cached(timeout=60)
|
||||||
def index():
|
def index():
|
||||||
|
@ -35,59 +21,3 @@ def index():
|
||||||
@blueprint.route('/about')
|
@blueprint.route('/about')
|
||||||
def about():
|
def about():
|
||||||
return render_template('about.html')
|
return render_template('about.html')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/post', methods=['POST'])
|
|
||||||
def post():
|
|
||||||
form = ScoreForm()
|
|
||||||
|
|
||||||
if not form:
|
|
||||||
return "Invalid form", 400
|
|
||||||
if not request.headers.get('Authentication'):
|
|
||||||
return "Invalid authentication", 401
|
|
||||||
|
|
||||||
if not isinstance(form.score.data, int):
|
|
||||||
return "Score must be an integer", 400
|
|
||||||
if form.score.data < 0:
|
|
||||||
return "Score must be greater than 0", 400
|
|
||||||
if form.difficulty.data not in [0, 1, 2, 3, 4]:
|
|
||||||
# 0 = Easy, Level 1
|
|
||||||
# 1 = Easy, Level 2
|
|
||||||
# 2 = Easy, Level 3
|
|
||||||
# 3 = Normal
|
|
||||||
# 4 = Hard
|
|
||||||
return "Invalid difficulty", 400
|
|
||||||
|
|
||||||
if request.headers.get('Authentication') == BEARER_TOKEN:
|
|
||||||
# User is not authenticated, but has the correct token
|
|
||||||
# This is an anonymous user
|
|
||||||
|
|
||||||
if not form.playerName.data or len(form.playerId.data) != 4:
|
|
||||||
return "Invalid player name", 400
|
|
||||||
|
|
||||||
score = Scores(
|
|
||||||
anonymous=True,
|
|
||||||
username=form.playerName.data,
|
|
||||||
score=form.score.data,
|
|
||||||
difficulty=form.difficulty.data,
|
|
||||||
)
|
|
||||||
db.session.add(score)
|
|
||||||
db.session.commit()
|
|
||||||
return "Success!", 200
|
|
||||||
elif Tokens.query.filter_by(token=request.headers.get('Authentication')).first():
|
|
||||||
# User is authenticated
|
|
||||||
# This is a registered user
|
|
||||||
|
|
||||||
user = Tokens.query.filter_by(token=request.headers.get('Authentication')).first().holder
|
|
||||||
score = Scores(
|
|
||||||
score=form.score.data,
|
|
||||||
difficulty=form.difficulty.data,
|
|
||||||
achievements=form.achievements.data,
|
|
||||||
user_id=user.id,
|
|
||||||
)
|
|
||||||
db.session.add(score)
|
|
||||||
db.session.commit()
|
|
||||||
return "Success!", 200
|
|
||||||
|
|
||||||
return "Authentication failed", 401
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue