Spielablauf umgesetzt
This commit is contained in:
1 parent
749bdb5179
commit
1097ab98fc
29 files changed
+828
-148
No files matched your search
@@ -7,7 +7,7 @@
|
||||
|
||||
<main>
|
||||
<div class="top-row px-4">
|
||||
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
|
||||
@* <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a> *@
|
||||
</div>
|
||||
|
||||
<article class="content px-4">
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<div class="top-row ps-3 navbar navbar-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="">FWLAZ_Web</a>
|
||||
<a class="navbar-brand" href="">FWLAZ Webquiz</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -10,24 +10,12 @@
|
||||
<nav class="flex-column">
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
|
||||
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
|
||||
</NavLink>
|
||||
</div>
|
||||
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="counter">
|
||||
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
|
||||
</NavLink>
|
||||
</div>
|
||||
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="weather">
|
||||
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
|
||||
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Start
|
||||
</NavLink>
|
||||
</div>
|
||||
<div class="nav-item px-3">
|
||||
<NavLink class="nav-link" href="settings/quizzes">
|
||||
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Quizzes
|
||||
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Einstellungen
|
||||
</NavLink>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
@page "/counter"
|
||||
|
||||
<PageTitle>Counter</PageTitle>
|
||||
|
||||
<h1>Counter</h1>
|
||||
|
||||
<p role="status">Current count: @currentCount</p>
|
||||
|
||||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
|
||||
|
||||
@code {
|
||||
private int currentCount = 0;
|
||||
|
||||
private void IncrementCount()
|
||||
{
|
||||
currentCount++;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,160 @@
|
||||
@page "/"
|
||||
@using FWLAZ_Web.Data
|
||||
@using FWLAZ_Web.Objects
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@inject Blazored.LocalStorage.ILocalStorageService localStorage;
|
||||
@inject IDbContextFactory<LocalDbContext> DbFactory;
|
||||
@inject SessionState CurrentSession;
|
||||
@inject NavigationManager nav;
|
||||
|
||||
<PageTitle>Home</PageTitle>
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
@if (CurrentSession.LoadGame != null)
|
||||
{
|
||||
<h1>Vorherigen Lauf fortsetzen</h1>
|
||||
<div>@CurrentSession.LoadGame.Title</div>
|
||||
<button class="btn btn-primary" type="button" @onclick="() => StartQuiz(CurrentSession.LoadGame)">Fortsetzen</button>
|
||||
<button class="btn btn-outline-danger" type="button" @onclick="DeleteGameState">Löschen</button>
|
||||
}
|
||||
|
||||
Welcome to your new app.
|
||||
|
||||
<h1>Neues Quiz beginnen</h1>
|
||||
|
||||
<div class="d-grid gap-2">
|
||||
@foreach (Quiz quiz in QuizList)
|
||||
{
|
||||
<button type="submit" class="btn btn-primary" @onclick="() => OpenDialog(quiz)">@quiz.Name</button>
|
||||
}
|
||||
</div>
|
||||
|
||||
<ModalComponent @ref="modal">
|
||||
<Title>
|
||||
@SelectedQuiz?.Name;
|
||||
</Title>
|
||||
<Body>
|
||||
@if (SelectedQuiz != null && NewGame != null)
|
||||
{
|
||||
@* <div class="mb-3">
|
||||
<label for="btngrpMode" class="form-label">Modus</label>
|
||||
<div class="btn-group form-control" id="btngrpMode">
|
||||
<a href="#" @onclick="() => NewGame.Mode = GameState.QuizMode.Practice" class="btn @(NewGame.Mode == GameState.QuizMode.Practice ? "btn-primary active" : "btn-outline-primary")">Üben</a>
|
||||
<a href="#" @onclick="() => NewGame.Mode = GameState.QuizMode.Tournament" class="btn @(NewGame.Mode == GameState.QuizMode.Tournament ? "btn-primary active" : "btn-outline-primary")">Rangliste</a>
|
||||
</div>
|
||||
</div> *@
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="cboQuestionGroup" class="form-label">Fragenkatalog</label>
|
||||
<select @bind="QuestionGroupId" class="form-select" id="cboQuestionGroup" aria-label="Select Question group">
|
||||
@foreach (QuestionGroup questionGroup in SelectedQuiz.Questiongroups)
|
||||
{
|
||||
<option value="@questionGroup.Id">@questionGroup.Name</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="rngQuestionCount" class="form-label">Anzahl der Fragen: @QuestionCountValue</label>
|
||||
<input type="range" min="1" max="@QuestionCountMax" @bind="@QuestionCountValue" class="form-range" id="rngQuestionCount">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="txtName" class="form-label">Name</label>
|
||||
<input @bind="NewGame.Username" class="form-control" type="text" placeholder="(optional)" id="txtName" />
|
||||
</div>
|
||||
}
|
||||
</Body>
|
||||
<Footer>
|
||||
@if (NewGame != null)
|
||||
{
|
||||
<button class="btn btn-danger" @onclick="() => StartQuiz(NewGame)">Start</button>
|
||||
}
|
||||
<button class="btn btn-primary" @onclick="CloseDialog">Abbruch</button>
|
||||
</Footer>
|
||||
</ModalComponent>
|
||||
|
||||
@code {
|
||||
public List<Quiz> 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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
@page "/quizstart"
|
||||
@using Blazored.LocalStorage;
|
||||
@using FWLAZ_Web.Data
|
||||
@using FWLAZ_Web.Objects
|
||||
@inject ILocalStorageService localStorage;
|
||||
@inject NavigationManager nav;
|
||||
@inject SessionState CurrentSession;
|
||||
|
||||
|
||||
<h2>@CurrentSession.LoadGame?.Title</h2>
|
||||
|
||||
@if (CurrentSession.LoadGame?.CurrentQuestion != null)
|
||||
{
|
||||
<h3>Frage @(CurrentSession.LoadGame.QuestionAnswers?.Count + 1) / @(CurrentSession.LoadGame.QuestionAnswers?.Count + @CurrentSession.LoadGame.Questions?.Count) </h3>
|
||||
<h4>@CurrentSession.LoadGame.CurrentQuestion?.Text [Nr. @CurrentSession.LoadGame.CurrentQuestion?.Number]</h4>
|
||||
<div class="mt-3">
|
||||
@if (CurrentSession.LoadGame?.CurrentQuestion != null)
|
||||
{
|
||||
@foreach (Answer aw in CurrentSession.LoadGame.CurrentQuestion.Answers)
|
||||
{
|
||||
<div class="mt-3">
|
||||
@if (!ShowSolution)
|
||||
{
|
||||
<button class="btn form-control @(SelectedAnswers.Contains(aw) ? "btn-primary active" : "btn-outline-primary" )" type="button" @onclick="() => ToggleAnswer(aw)">@aw.Position. @aw.Text</button>
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SelectedAnswers.Contains(aw) && aw.IsCorrect)
|
||||
{
|
||||
<button class="btn form-control btn-success" type="button">@aw.Position. @aw.Text</button>
|
||||
}
|
||||
else if (SelectedAnswers.Contains(aw) && !aw.IsCorrect)
|
||||
{
|
||||
<button class="btn form-control btn-danger" type="button">@aw.Position. @aw.Text</button>
|
||||
}
|
||||
else if (aw.IsCorrect)
|
||||
{
|
||||
<button class="btn form-control btn-outline-success" type="button">@aw.Position. @aw.Text</button>
|
||||
}
|
||||
else if (!aw.IsCorrect)
|
||||
{
|
||||
<button class="btn form-control btn-outline-danger" type="button">@aw.Position. @aw.Text</button>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-primary" type="button" @onclick="NextQuestion">Weiter</button>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<h3>Ergebnis</h3>
|
||||
<div class="w-25">
|
||||
|
||||
<table class="table">
|
||||
<tbody>
|
||||
|
||||
<tr class="table-primary">
|
||||
<td>Fragen Gesamt</td>
|
||||
<td>@(CurrentSession.LoadGame.QuestionAnswers.Count)</td>
|
||||
</tr>
|
||||
<tr class="table-success">
|
||||
<td>Korrekt beantwortet</td>
|
||||
<td>@(CurrentSession.LoadGame.QuestionAnswers.Where(qa => qa.IsCorrect).Count())</td>
|
||||
</tr>
|
||||
<tr class="table-danger">
|
||||
<td>Falsch beantwortet</td>
|
||||
<td>@(CurrentSession.LoadGame.QuestionAnswers.Where(qa => !qa.IsCorrect).Count())</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
private string PrevURL = "/";
|
||||
|
||||
private bool ShowSolution { get; set; }
|
||||
private List<Answer> SelectedAnswers { get; set; } = new();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (CurrentSession.LoadGame == null)
|
||||
{
|
||||
nav.NavigateTo(PrevURL);
|
||||
return;
|
||||
}
|
||||
if (CurrentSession.LoadGame.QuestionAnswers == null) CurrentSession.LoadGame.QuestionAnswers = new();
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private void ToggleAnswer(Answer aw)
|
||||
{
|
||||
if (CurrentSession.LoadGame == null) return;
|
||||
if (CurrentSession.LoadGame.QuizIsMultipleChoice)
|
||||
{
|
||||
if (SelectedAnswers.Contains(aw))
|
||||
{
|
||||
SelectedAnswers.Remove(aw);
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedAnswers.Add(aw);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedAnswers.Clear();
|
||||
SelectedAnswers.Add(aw);
|
||||
}
|
||||
|
||||
}
|
||||
private async Task NextQuestion(MouseEventArgs e)
|
||||
{
|
||||
if (ShowSolution == false)
|
||||
{
|
||||
ShowSolution = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowSolution = false;
|
||||
if (CurrentSession.LoadGame == null) return;
|
||||
if (CurrentSession.LoadGame.QuestionAnswers == null) CurrentSession.LoadGame.QuestionAnswers = new();
|
||||
if (CurrentSession.LoadGame.CurrentQuestion != null)
|
||||
{
|
||||
CurrentSession.LoadGame.QuestionAnswers.Add(new(CurrentSession.LoadGame.CurrentQuestion)
|
||||
{
|
||||
GivenAnswers = SelectedAnswers,
|
||||
});
|
||||
}
|
||||
SelectedAnswers.Clear();
|
||||
CurrentSession.LoadGame.NextQuestion();
|
||||
await CurrentSession.SaveAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,10 @@
|
||||
<label class="form-label" for="name">Name</label>
|
||||
<input id="name" name="name" type="text" class="form-control" @bind="@SelectedItem.Name" />
|
||||
</div>
|
||||
<div class="mb-3 form-check">
|
||||
<input class="form-check-input" type="checkbox" @bind="@SelectedItem.IsMultipleChoice" id="chkMultipleChoice"/>
|
||||
<label class="form-check-label" for="chkMultipleChoice">Is Multiple-Choice</label>
|
||||
</div>
|
||||
<h3>Question Groups</h3>
|
||||
<div class="input-group m-3">
|
||||
<input id="qgnew" name="qgnew" type="text" class="form-control" @bind="@NewQuestionGroup" />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
@page "/settings/quizzes/questions/{id}"
|
||||
@using FWLAZ_Web.Data
|
||||
@using FWLAZ_Web.Objects
|
||||
@using Microsoft.EntityFrameworkCore;
|
||||
@inject IDbContextFactory<LocalDbContext> DbFactory;
|
||||
@inject NavigationManager nav;
|
||||
@@ -32,7 +33,14 @@
|
||||
{
|
||||
<div class="input-group mb-3">
|
||||
<div class="input-group-text">
|
||||
<input class="form-check-input mt-0" type="checkbox" @bind="aw.IsCorrect" @onkeyup="AddQuestion_Enter" />
|
||||
@* @if (SelectedItem.IsMultipleChoice)
|
||||
{ *@
|
||||
<input class="form-check-input mt-0" type="checkbox" @bind="aw.IsCorrect" @onkeyup="AddQuestion_Enter" />
|
||||
@* }
|
||||
else
|
||||
{
|
||||
<input class="form-check-input mt-0" type="radio" @onchange="() => SetSingleChoiceAnswer(NewQuestion, aw)" name="NewQuestion_Answers" @onkeyup="AddQuestion_Enter" />
|
||||
} *@
|
||||
</div>
|
||||
<input class="form-control col-sm" style="max-width:70px;" type="text" @bind="aw.Position" @onkeyup="AddQuestion_Enter" />
|
||||
<input class="form-control" type="text" @bind="aw.Text" @onkeyup="AddQuestion_Enter" />
|
||||
@@ -156,9 +164,11 @@
|
||||
private void PrepareNewQuestion()
|
||||
{
|
||||
NewQuestion = new();
|
||||
if (SelectedItem.Questions.Count > 0) {
|
||||
if (SelectedItem.Questions.Count > 0)
|
||||
{
|
||||
NewQuestion.Number = SelectedItem.Questions.Max(q => q.Number) + 1;
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
NewQuestion.Number = 1;
|
||||
}
|
||||
@@ -169,6 +179,12 @@
|
||||
}
|
||||
}
|
||||
|
||||
private void SetSingleChoiceAnswer(Question question, Answer answer)
|
||||
{
|
||||
question.Answers.ForEach(aw => aw.IsCorrect = false);
|
||||
answer.IsCorrect = true;
|
||||
}
|
||||
|
||||
private async Task AddQuestion_Enter(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Code == "Enter" || e.Code == "NumpadEnter") await AddQuestion(new MouseEventArgs());
|
||||
@@ -243,17 +259,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
public class QuestionGroupSelect
|
||||
{
|
||||
public bool Checked { get; set; }
|
||||
|
||||
public QuestionGroup questionGroup { get; }
|
||||
|
||||
public QuestionGroupSelect(QuestionGroup questionGroup)
|
||||
{
|
||||
this.questionGroup = questionGroup;
|
||||
}
|
||||
}
|
||||
private async Task SaveQuiz(MouseEventArgs e)
|
||||
{
|
||||
foreach (Question question in SelectedItem.Questions)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<a class="btn btn-primary mb-3" href="/settings/quizzes/edit">Neues Quiz</a>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped table-hover table-bordered table-dark border-dark">
|
||||
<table class="table table-striped table-hover table-bordered">
|
||||
<tbody>
|
||||
@foreach (Quiz q in QuizList)
|
||||
{
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
@page "/weather"
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Temp. (C)</th>
|
||||
<th>Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate a loading indicator
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user