diff --git a/app.py b/app.py index 04a7915..7f9a077 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,7 @@ import os import json import re from flask import Flask, render_template, request, jsonify -from models import db, Progress, Statistic +from models import db, Progress, Statistic, LessonStatistic app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///typewriter.db' @@ -464,6 +464,36 @@ def set_lesson(): print(f"Fehler in set_lesson: {e}") return jsonify({'error': 'Interner Serverfehler'}), 500 +@app.route('/save_lesson_statistics', methods=['POST']) +def save_lesson_statistics(): + """Speichert die Statistiken für eine abgeschlossene Lektion""" + try: + data = request.get_json() + lesson_index = data.get('lesson_index') + chars_per_second = data.get('chars_per_second') + error_rate = data.get('error_rate') + wpm = data.get('wpm') + + if lesson_index is None: + return jsonify({'error': 'Lektionsindex fehlt'}), 400 + + # Neue Statistik für die Lektion erstellen + lesson_stat = LessonStatistic( + lesson_index=lesson_index, + chars_per_second=chars_per_second, + error_rate=error_rate, + wpm=wpm + ) + + db.session.add(lesson_stat) + db.session.commit() + + return jsonify({'status': 'success', 'message': 'Lektionsstatistiken gespeichert'}) + + except Exception as e: + print(f"Fehler in save_lesson_statistics: {e}") + return jsonify({'error': 'Interner Serverfehler'}), 500 + @app.route('/end_session', methods=['POST']) def end_session(): """Beendet eine Tipp-Session und speichert Statistiken""" diff --git a/models.py b/models.py index 537570d..aaf6624 100644 --- a/models.py +++ b/models.py @@ -18,3 +18,11 @@ class Statistic(db.Model): duration_per_char = db.Column(db.Float, default=0.0) accuracy = db.Column(db.Float, default=0.0) wpm = db.Column(db.Float, default=0.0) + +class LessonStatistic(db.Model): + id = db.Column(db.Integer, primary_key=True) + lesson_index = db.Column(db.Integer, nullable=False) + chars_per_second = db.Column(db.Float, default=0.0) + error_rate = db.Column(db.Float, default=0.0) + wpm = db.Column(db.Float, default=0.0) + created_at = db.Column(db.DateTime, default=db.func.now()) diff --git a/static/js/script.js b/static/js/script.js index 13a21a1..0c84571 100644 --- a/static/js/script.js +++ b/static/js/script.js @@ -88,11 +88,41 @@ document.addEventListener('DOMContentLoaded', function() { currentLineIndex++; updateLineDisplay(); } else { - // Letzte Zeile abgeschlossen + // Letzte Zeile abgeschlossen - Lektion beendet completedLines.push(currentLineInput); currentLineInput = ''; this.value = ''; - alert('Lektion beendet!'); + + // Speichere die Statistiken für diese Lektion + const lessonIndex = window.currentLessonIndex; + const charsPerSecond = parseFloat(speedElement.textContent); + const errorRate = parseFloat(errorRateElement.textContent); + const wpm = parseFloat(wpmElement.textContent); + + fetch('/save_lesson_statistics', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + lesson_index: lessonIndex, + chars_per_second: charsPerSecond, + error_rate: errorRate, + wpm: wpm + }) + }) + .then(response => response.json()) + .then(data => { + if (data.status === 'success') { + alert('Lektion beendet! Statistiken wurden gespeichert.'); + } else { + alert('Lektion beendet, aber Statistiken konnten nicht gespeichert werden.'); + } + }) + .catch(error => { + console.error('Fehler beim Speichern der Statistiken:', error); + alert('Lektion beendet! (Fehler beim Speichern der Statistiken)'); + }); } } diff --git a/templates/index.html b/templates/index.html index b97afd2..073e0a5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -63,30 +63,8 @@ > - +