Merge branch 'faille'
This commit is contained in:
@@ -1,33 +1,32 @@
|
||||
from flask import Flask, jsonify, request, render_template
|
||||
import yaml
|
||||
import json
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
LEADERBOARD_FILE = './data/leaderboard.json'
|
||||
LEADERBOARD_FILE = './data/leaderboard.yaml'
|
||||
|
||||
if not os.path.exists(LEADERBOARD_FILE):
|
||||
with open(LEADERBOARD_FILE, 'w') as f:
|
||||
json.dump([], f)
|
||||
yaml.safe_dump([], f)
|
||||
|
||||
def read_leaderboard():
|
||||
with open(LEADERBOARD_FILE, 'r') as f:
|
||||
return json.load(f)
|
||||
return yaml.load(f, Loader=yaml.UnsafeLoader) or []
|
||||
|
||||
def write_leaderboard(data):
|
||||
with open(LEADERBOARD_FILE, 'w') as f:
|
||||
json.dump(data, f, indent=4)
|
||||
yaml.safe_dump(data, f)
|
||||
|
||||
@app.route('/api/leaderboard', methods=['POST'])
|
||||
def add_to_leaderboard():
|
||||
new_entry = request.json
|
||||
if not new_entry or 'name' not in new_entry or 'score' not in new_entry:
|
||||
return jsonify({'error': 'Name and score are required'}), 400
|
||||
|
||||
leaderboard = read_leaderboard()
|
||||
leaderboard.append(new_entry)
|
||||
write_leaderboard(leaderboard)
|
||||
|
||||
return jsonify({'message': 'Added successfully', 'leaderboard': leaderboard}), 201
|
||||
|
||||
@app.route('/api/leaderboard', methods=['GET'])
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
[]
|
||||
0
flask_base/data/leaderboard.yaml
Normal file
0
flask_base/data/leaderboard.yaml
Normal file
Reference in New Issue
Block a user