@page "/" @using FWLAZ_Web.Data @using FWLAZ_Web.Objects @using Microsoft.EntityFrameworkCore @inject Blazored.LocalStorage.ILocalStorageService localStorage; @inject IDbContextFactory DbFactory; @inject SessionState CurrentSession; @inject NavigationManager nav; Home @if (CurrentSession.LoadGame != null) {

Vorherigen Lauf fortsetzen

@CurrentSession.LoadGame.Title
}

Neues Quiz beginnen

@foreach (Quiz quiz in QuizList) { }
@SelectedQuiz?.Name; @if (SelectedQuiz != null && NewGame != null) { @* *@
}
@if (NewGame != null) { }
@code { public List QuizList { get; set; } = new(); private LocalDbContext? DbContext; private ModalComponent modal = null!; private GameState? NewGame; private GameState? LoadGame; private Quiz? SelectedQuiz; private static Random rng = new(); private int _QuestionGroupId; public int QuestionGroupId { get { return _QuestionGroupId; } set { _QuestionGroupId = value; QuestionCountMax = SelectedQuiz?.Questiongroups.Single(qg => qg.Id == value).Questions.Count ?? 0; QuestionCountValue = QuestionCountMax; // QuestionCountValue = Math.Min(QuestionCountMax, QuestionCountValue); } } public int QuestionCountMax { get; set; } = 1; public int QuestionCountValue { get; set; } = 20; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) await CurrentSession.LoadAsync(); } protected override async Task OnInitializedAsync() { await FillListAsync(); } private async Task FillListAsync() { DbContext ??= await DbFactory.CreateDbContextAsync(); if (DbContext == null) return; QuizList = await DbContext.Quiz.OrderBy(q => q.Name).ToListAsync(); } private async Task OpenDialog(Quiz itm) { DbContext ??= await DbFactory.CreateDbContextAsync(); if (DbContext == null) return; SelectedQuiz = DbContext.Quiz.Include(q => q.Questiongroups).ThenInclude(qg => qg.Questions).Single(q => q.Id == itm.Id); QuestionGroupId = SelectedQuiz.Questiongroups.First().Id; NewGame = new GameState(SelectedQuiz); await InvokeAsync(StateHasChanged); await modal.ShowAsync(); } private async Task CloseDialog() { NewGame = null; await modal.CloseAsync(); } private async Task StartQuiz(GameState state) { if (state.Questions == null) { // Bei neuem Spiel - Frageliste füllen DbContext ??= await DbFactory.CreateDbContextAsync(); if (DbContext == null || state == null) return; state.Questions = DbContext.QuestionGroup .Include(qg => qg.Questions) .ThenInclude(q => q.Answers) .Single(qg => qg.Id == QuestionGroupId).Questions.OrderBy(_ => rng.Next()).ToList(); state.Questions = state.Questions.Take(QuestionCountValue).ToList(); CurrentSession.LoadGame = state; await CurrentSession.SaveAsync(); } if (state != null) nav.NavigateTo($"quizstart"); await CloseDialog(); } private async Task DeleteGameState(MouseEventArgs e) { CurrentSession.LoadGame = null; await localStorage.RemoveItemAsync(GameState.STORAGEKEY); } }