diff --git a/api.py b/api.py index 8605864..f2509f6 100644 --- a/api.py +++ b/api.py @@ -2,10 +2,16 @@ from models import PinballMachine, Player, Score from data_manager import load_data, save_data import datetime +import os + + app = Flask(__name__) data = load_data() +print( os.environ['GIST_TOKEN'] ) +print("Debug") + @app.route('/') def index(): diff --git a/data_manager.py b/data_manager.py index 78809f6..484c5c3 100644 --- a/data_manager.py +++ b/data_manager.py @@ -1,14 +1,46 @@ import yaml import os +import requests +import json -DATA_PATH = "data.yaml" +GIST_ID = "8542e223cdd0ff01905aa79b439927bb" # Replace with your Gist ID +TOKEN = "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" # Replace with your GitHub token +GIST_FILENAME = "score.yaml" + +headers = {"Authorization": f"token {TOKEN}"} + +def fetch_gist_content(): + url = f"https://api.github.com/gists/{GIST_ID}" + response = requests.get(url, headers=headers) + response.raise_for_status() + gist_data = response.json() + return gist_data['files'][GIST_FILENAME]['content'] + +def update_gist(content): + url = f"https://api.github.com/gists/{GIST_ID}" + data = { + "files": { + GIST_FILENAME: { + "content": content + } + } + } + response = requests.patch(url, headers=headers, data=json.dumps(data)) + response.raise_for_status() + return response.json() def load_data(): - if not os.path.exists(DATA_PATH): + try: + content = fetch_gist_content() + # print("LOAD DEBUG"+content) + return yaml.safe_load(content) + except Exception as e: return {"pinball_machines": [], "players": [], "scores": []} - with open(DATA_PATH, "r") as file: - return yaml.safe_load(file) def save_data(data): - with open(DATA_PATH, "w") as file: - yaml.dump(data, file) + try: + content = yaml.dump(data) + update_gist(content) + #print("SAVECONTENT: " + data) + except Exception as e: + print(f"Error saving data: {e}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index b83f472..e7bcc41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask -PyYAML \ No newline at end of file +PyYAML +requests \ No newline at end of file diff --git a/static/mobile.js b/static/mobile.js index 1b24d3a..49ed5ae 100644 --- a/static/mobile.js +++ b/static/mobile.js @@ -54,6 +54,8 @@ function submitScore() { const pinball = document.getElementById('pinball-select').value; const score = document.getElementById('score-input').value; + console.log(parseInt(score.replaceAll(",",""))); + // Ermitteln des heutigen Datums im Format YYYY-MM-DD const today = new Date(); const formattedDate = today.toISOString().split('T')[0]; @@ -66,7 +68,7 @@ function submitScore() { body: JSON.stringify({ player_abbreviation: player, pinball_abbreviation: pinball, - points: parseInt(score, 10), + points: parseInt(score.replaceAll(",","")), date: formattedDate }) }) @@ -84,7 +86,7 @@ function formatScoreInput(inputElement) { let value = inputElement.value.replace(/[^\d]/g, '').replace(/^0+/, ''); // Teile die Zahl in Gruppen von drei Ziffern - value = value.replace(/\B(?=(\d{3})+(?!\d))/g, '.'); + value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ','); // Setze den formatierten Wert zurück ins Eingabefeld inputElement.value = value; diff --git a/static/script.js b/static/script.js index 3f728b2..e8dc48f 100644 --- a/static/script.js +++ b/static/script.js @@ -10,10 +10,8 @@ document.addEventListener('DOMContentLoaded', function() { }); } }); - let playerNamesMap = {}; - function loadPlayers() { fetch('/players') .then(response => response.json())