modified: app.py
modified: templates/quiz.html
This commit is contained in:
37
app.py
37
app.py
@@ -78,30 +78,25 @@ def quiz(playlist_id):
|
||||
if not tracks:
|
||||
return "Keine Tracks verfügbar!"
|
||||
|
||||
# Gespielte Tracks aus der Session holen
|
||||
played_tracks = session.get(f'played_tracks_{playlist_id}', [])
|
||||
score = session.get(f'score_{playlist_id}', 0)
|
||||
|
||||
# Filtere bereits gespielte Tracks heraus
|
||||
available_tracks = [t for t in tracks if t["id"] not in played_tracks]
|
||||
|
||||
# Wenn alle gespielt wurden, zurücksetzen
|
||||
if not available_tracks:
|
||||
played_tracks = []
|
||||
available_tracks = tracks
|
||||
score = 0 # Score zurücksetzen, wenn alle gespielt
|
||||
|
||||
track = random.choice(available_tracks)
|
||||
played_tracks.append(track["id"])
|
||||
session[f'played_tracks_{playlist_id}'] = played_tracks
|
||||
session[f'score_{playlist_id}'] = score
|
||||
|
||||
# Token aus der Sitzung holen für das Web Playback SDK
|
||||
token_info = session.get('token_info', None)
|
||||
if not token_info:
|
||||
return redirect('/login')
|
||||
|
||||
# Zugriff auf access_token
|
||||
access_token = token_info['access_token']
|
||||
|
||||
# Alle Tracks für die Suchfunktion (für title und artist mode)
|
||||
|
||||
all_tracks = []
|
||||
for item in tracks:
|
||||
track_info = {
|
||||
@@ -109,17 +104,20 @@ def quiz(playlist_id):
|
||||
"name": item["name"],
|
||||
"artist": item["artists"][0]["name"],
|
||||
"uri": item["uri"],
|
||||
"release_date": item.get("album", {}).get("release_date", "Unbekannt")[:4] # Nur das Jahr
|
||||
"release_date": item.get("album", {}).get("release_date", "Unbekannt")[:4]
|
||||
}
|
||||
all_tracks.append(track_info)
|
||||
|
||||
|
||||
return render_template(
|
||||
"quiz.html",
|
||||
track=track,
|
||||
access_token=access_token,
|
||||
"quiz.html",
|
||||
track=track,
|
||||
access_token=access_token,
|
||||
playlist_id=playlist_id,
|
||||
game_mode=game_mode,
|
||||
all_tracks=all_tracks
|
||||
all_tracks=all_tracks,
|
||||
question_number=len(played_tracks),
|
||||
total_questions=len(tracks),
|
||||
score=score
|
||||
)
|
||||
|
||||
@app.route("/search_track", methods=["POST"])
|
||||
@@ -160,19 +158,22 @@ def check_answer():
|
||||
guess = data.get('guess', '').lower()
|
||||
correct_answer = data.get('correct_answer', '').lower()
|
||||
game_mode = data.get('game_mode', 'artist')
|
||||
playlist_id = data.get('playlist_id')
|
||||
|
||||
# Beim Titelmodus Klammerzusätze entfernen
|
||||
if game_mode == 'title':
|
||||
guess = clean_title(guess)
|
||||
correct_answer = clean_title(correct_answer)
|
||||
|
||||
# Bei Jahr-Modus: Exakte Übereinstimmung prüfen
|
||||
if game_mode == 'year':
|
||||
is_correct = guess == correct_answer
|
||||
else:
|
||||
# Bei anderen Modi: Ähnlichkeitsprüfung (90% Übereinstimmung gilt als korrekt)
|
||||
is_correct = similarity(guess, correct_answer) >= 0.9
|
||||
|
||||
# Score erhöhen, wenn richtig
|
||||
if is_correct and playlist_id:
|
||||
key = f'score_{playlist_id}'
|
||||
session[key] = session.get(key, 0) + 1
|
||||
|
||||
return {
|
||||
"correct": is_correct,
|
||||
"correct_answer": correct_answer
|
||||
|
||||
@@ -245,7 +245,8 @@
|
||||
body: JSON.stringify({
|
||||
guess: guess,
|
||||
correct_answer: correctAnswer,
|
||||
game_mode: currentGameMode
|
||||
game_mode: currentGameMode,
|
||||
playlist_id: "{{ playlist_id }}"
|
||||
})
|
||||
})
|
||||
.then(response => response.json())
|
||||
@@ -283,10 +284,15 @@
|
||||
window.location.href = `/quiz/{{ playlist_id }}?mode=${mode}`;
|
||||
}
|
||||
</script>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<div style="text-align:center; margin-bottom: 10px;">
|
||||
<span id="progressInfo">Frage {{ question_number }} von {{ total_questions }}</span>
|
||||
<span id="scoreInfo" style="margin-left:20px;">Punkte: {{ score }}</span>
|
||||
</div>
|
||||
<div style="text-align:center; margin-bottom: 20px;">
|
||||
<a href="/playlists" class="btn btn-secondary" style="text-decoration:none;">⬅️ Zurück zur Playlist-Auswahl</a>
|
||||
<a href="/playlists" class="btn btn-danger" style="margin-top:10px;">Quiz beenden</a>
|
||||
</div>
|
||||
<h2 id="question-text">Wer ist der Künstler dieses Songs?</h2>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user