Updated Highscore server

This commit is contained in:
Michał Gdula 2023-05-09 15:35:31 +03:00
parent 3c3b6b8b23
commit e7bbb02aba
26 changed files with 1023 additions and 321 deletions

View file

@ -1,62 +1,23 @@
from flask import Blueprint, jsonify, request, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired
from models import Scores, Users
from extensions import db, cache
from config import BEARER_TOKEN
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('/', methods=['GET'])
@cache.cached(timeout=60)
def index():
top_scores = Scores.query.order_by(Scores.score.desc()).limit(10).all()
return render_template('scores.html', top_scores=top_scores)
@blueprint.route('/post', methods=['POST'])
def post():
form = ScoreForm()
if not form:
return "Invalid form", 400
if request.headers.get('Authentication') != 'Bearer ' + BEARER_TOKEN:
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 ['easy', 'medium', 'hard']:
return "Invalid difficulty", 400
user = Users.query.filter_by(steam_uuid=form.playerId.data).first()
if not user:
user = Users(
steam_uuid=form.playerId.data,
steam_name=form.playerName.data,
)
db.session.add(user)
db.session.commit()
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 jsonify({'message': 'Success!'})
from flask import Blueprint, request, render_template
from server.models import Scores
blueprint = Blueprint('views', __name__)
@blueprint.route('/')
# @cache.cached(timeout=60)
def index():
difficulty = request.args.get('diff', 0)
top_scores = (Scores.query
.order_by(Scores.score.desc())
.filter_by(difficulty=difficulty)
.limit(10)
.all())
return render_template('scores.html', top_scores=top_scores)
@blueprint.route('/about')
def about():
return render_template('about.html')