@page "/"
@using MemoryGame.Models
@using MemoryGame.Services
@inject GameService GameService
@inject NavigationManager Navigation
Memory-Spiel
@if (!_initialized)
{
Lädt...
}
else if (_currentSession == null)
{
Memory-Spiel
Übe das Zuordnen von Bildern zu Wörtern
}
else if (_currentSession.IsComplete && !_showStatistics)
{
Spiel beendet!
Richtig:
@_currentSession.CorrectAnswers
Falsch:
@_currentSession.IncorrectAnswers
}
else if (_currentSession.IsComplete && _showStatistics)
{
var words = GameService.GetWords()
.OrderBy(w => w.CorrectCount + w.IncorrectCount == 0 ? 1 : 0)
.ThenBy(w => w.CorrectCount + w.IncorrectCount == 0
? 0
: (double)w.CorrectCount / (w.CorrectCount + w.IncorrectCount))
.ToList();
var playedWords = words.Where(w => w.CorrectCount + w.IncorrectCount > 0).ToList();
var overallAccuracy = playedWords.Count == 0
? 0
: (int)Math.Round(100.0 * playedWords.Sum(w => w.CorrectCount) / playedWords.Sum(w => w.CorrectCount + w.IncorrectCount));
Statistik
@playedWords.Count von @words.Count Wörtern geübt
@if (playedWords.Count > 0)
{
· @overallAccuracy% richtig
}
@if (_confirmReset)
{
Alle Statistiken wirklich zurücksetzen?
}
else
{
}
@foreach (var word in words)
{
var total = word.CorrectCount + word.IncorrectCount;
var accuracy = total == 0 ? 0 : (int)Math.Round(100.0 * word.CorrectCount / total);
var barClass = total == 0 ? "bar-unplayed" : accuracy >= 70 ? "bar-green" : accuracy >= 40 ? "bar-orange" : "bar-red";
@(total == 0 ? "—" : $"{accuracy}% richtig")
}
}
else
{
var round = _currentSession.CurrentRound;
if (round != null)
{
@if (round.Type == RoundType.ImageWithWords)
{
@foreach (var option in round.Options)
{
}
}
else
{
@round.CorrectAnswer.Word
@foreach (var option in round.Options)
{
}
}
@if (_showFeedback && round.SelectedAnswer != null)
{
@_feedbackMessage
}
}
}
@code {
private bool _initialized;
private GameSession? _currentSession;
private bool _showFeedback;
private string _feedbackMessage = "";
private string _feedbackClass = "";
private bool _showStatistics;
private bool _confirmReset;
protected override async Task OnInitializedAsync()
{
await GameService.InitializeAsync();
_initialized = true;
}
private void StartNewGame()
{
_currentSession = GameService.CreateNewGame();
_showFeedback = false;
}
private async Task SelectAnswer(GameWord selectedWord)
{
if (_currentSession?.CurrentRound == null || _showFeedback)
return;
var round = _currentSession.CurrentRound;
round.SelectedAnswer = selectedWord;
_showFeedback = true;
if (round.IsCorrect)
{
_feedbackMessage = "Richtig! ✓";
_feedbackClass = "correct";
}
else
{
_feedbackMessage = $"Falsch. Die richtige Antwort ist: {round.CorrectAnswer.Word}";
_feedbackClass = "incorrect";
}
await GameService.UpdateStatisticsAsync(round);
StateHasChanged();
await Task.Delay(2000);
_showFeedback = false;
_currentSession.CurrentRoundIndex++;
if (_currentSession.IsComplete)
{
_currentSession.EndTime = DateTimeOffset.UtcNow;
}
StateHasChanged();
}
private void ShowStatistics()
{
_showStatistics = true;
}
private void GoToStart()
{
_currentSession = null;
_showStatistics = false;
_confirmReset = false;
}
private async Task ConfirmReset()
{
await GameService.ResetStatisticsAsync();
_confirmReset = false;
StateHasChanged();
}
}