mirror of
https://github.com/Project-Redacted/Highscores-Server.git
synced 2025-05-28 22:13:14 +00:00
Add Username and ID field
This commit is contained in:
parent
4d5212a32c
commit
330201be3a
76 changed files with 5062 additions and 41 deletions
|
@ -15,6 +15,7 @@ class Scores(db.Model):
|
|||
score = db.Column(db.Integer, nullable=False)
|
||||
difficulty = db.Column(db.String, nullable=False)
|
||||
achievements = db.Column(db.String, nullable=False)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
|
||||
|
||||
scored_at = db.Column(
|
||||
db.DateTime,
|
||||
|
@ -33,6 +34,8 @@ class Users(db.Model):
|
|||
steam_uuid = db.Column(db.String, unique=True, nullable=False)
|
||||
steam_name = db.Column(db.String, nullable=False)
|
||||
|
||||
scores = db.relationship('Scores', backref='user', lazy=True)
|
||||
|
||||
creation_data = db.Column(
|
||||
db.DateTime,
|
||||
nullable=False,
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
from flask import Blueprint, jsonify, render_template_string
|
||||
from flask import Blueprint, jsonify, render_template_string, request, abort
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, IntegerField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from server.models import Scores
|
||||
from server.models import Scores, Users
|
||||
from server.extensions import db, cache
|
||||
|
||||
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()])
|
||||
|
@ -19,6 +21,7 @@ class ScoreForm(FlaskForm):
|
|||
@cache.cached(timeout=60)
|
||||
def index():
|
||||
top_scores = Scores.query.order_by(Scores.score.desc()).limit(10).all()
|
||||
users = Users.query.all()
|
||||
return render_template_string('''
|
||||
<h1>Top Scores</h1>
|
||||
<table>
|
||||
|
@ -26,38 +29,67 @@ def index():
|
|||
<th>Score</th>
|
||||
<th>Difficulty</th>
|
||||
<th>Achievements</th>
|
||||
<th>Player</th>
|
||||
</tr>
|
||||
{% for score in top_scores %}
|
||||
<tr>
|
||||
<td>{{ score.score }}</td>
|
||||
<td>{{ score.difficulty }}</td>
|
||||
<td>{{ score.achievements }}</td>
|
||||
<td>{{ score.user.steam_name }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<a href="/post">Post a score</a>
|
||||
''', top_scores=top_scores)
|
||||
|
||||
<h1>Players</h1>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Steam ID</th>
|
||||
<th>Steam Name</th>
|
||||
</tr>
|
||||
{% for user in users %}
|
||||
<tr>
|
||||
<td>{{ user.steam_uuid }}</td>
|
||||
<td>{{ user.steam_name }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
''', top_scores=top_scores, users=users)
|
||||
|
||||
|
||||
@blueprint.route('/post', methods=['GET', 'POST'])
|
||||
@blueprint.route('/post', methods=['POST'])
|
||||
def post():
|
||||
form = ScoreForm()
|
||||
|
||||
if form:
|
||||
score = Scores(
|
||||
score=form.score.data,
|
||||
difficulty=form.difficulty.data,
|
||||
achievements=form.achievements.data,
|
||||
)
|
||||
db.session.add(score)
|
||||
db.session.commit()
|
||||
return jsonify({'message': 'Success!'})
|
||||
if not form:
|
||||
return "Invalid form", 400
|
||||
if request.headers.get('Authentication') != 'Bearer 1234':
|
||||
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!'})
|
||||
|
||||
|
||||
return render_template_string('''
|
||||
<form method="POST" action="/post">
|
||||
{{ form.score.label }} {{ form.score(size=20) }}
|
||||
{{ form.difficulty.label }} {{ form.difficulty(size=20) }}
|
||||
{{ form.achievements.label }} {{ form.achievements(size=20) }}
|
||||
<input type="submit" value="Go">
|
||||
</form>
|
||||
''', form=form)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue