Spielablauf umgesetzt

This commit is contained in:
BuildTools 2024-09-03 16:15:42 +02:00
parent 749bdb5179
commit 1097ab98fc
29 changed files with 828 additions and 148 deletions

View File

@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FWLAZ", "FWLAZ\FWLAZ.csproj
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FWLAZData", "FWLAZData\FWLAZData.csproj", "{EE7C2DA0-9B00-4A6E-9333-DB3FF265AB61}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FWLAZData", "FWLAZData\FWLAZData.csproj", "{EE7C2DA0-9B00-4A6E-9333-DB3FF265AB61}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FWLAZ_Web", "FWLAZ_Web\FWLAZ_Web.csproj", "{8109C64A-C8EC-48E2-9356-E6599ABB0B6F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FWLAZ_Web", "FWLAZ_Web\FWLAZ_Web.csproj", "{8109C64A-C8EC-48E2-9356-E6599ABB0B6F}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution

30
FWLAZ_Web/.dockerignore Normal file
View File

@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**

View File

@ -7,7 +7,7 @@
<main> <main>
<div class="top-row px-4"> <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> </div>
<article class="content px-4"> <article class="content px-4">

View File

@ -1,6 +1,6 @@
<div class="top-row ps-3 navbar navbar-dark"> <div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" href="">FWLAZ_Web</a> <a class="navbar-brand" href="">FWLAZ Webquiz</a>
</div> </div>
</div> </div>
@ -10,24 +10,12 @@
<nav class="flex-column"> <nav class="flex-column">
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All"> <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home <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="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
</NavLink> </NavLink>
</div> </div>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="settings/quizzes"> <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> </NavLink>
</div> </div>
</nav> </nav>

View File

@ -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++;
}
}

View File

@ -1,7 +1,160 @@
@page "/" @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> <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);
}
}

View File

@ -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();
}
}
}

View File

@ -12,6 +12,10 @@
<label class="form-label" for="name">Name</label> <label class="form-label" for="name">Name</label>
<input id="name" name="name" type="text" class="form-control" @bind="@SelectedItem.Name" /> <input id="name" name="name" type="text" class="form-control" @bind="@SelectedItem.Name" />
</div> </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> <h3>Question Groups</h3>
<div class="input-group m-3"> <div class="input-group m-3">
<input id="qgnew" name="qgnew" type="text" class="form-control" @bind="@NewQuestionGroup" /> <input id="qgnew" name="qgnew" type="text" class="form-control" @bind="@NewQuestionGroup" />

View File

@ -1,5 +1,6 @@
@page "/settings/quizzes/questions/{id}" @page "/settings/quizzes/questions/{id}"
@using FWLAZ_Web.Data @using FWLAZ_Web.Data
@using FWLAZ_Web.Objects
@using Microsoft.EntityFrameworkCore; @using Microsoft.EntityFrameworkCore;
@inject IDbContextFactory<LocalDbContext> DbFactory; @inject IDbContextFactory<LocalDbContext> DbFactory;
@inject NavigationManager nav; @inject NavigationManager nav;
@ -32,7 +33,14 @@
{ {
<div class="input-group mb-3"> <div class="input-group mb-3">
<div class="input-group-text"> <div class="input-group-text">
@* @if (SelectedItem.IsMultipleChoice)
{ *@
<input class="form-check-input mt-0" type="checkbox" @bind="aw.IsCorrect" @onkeyup="AddQuestion_Enter" /> <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> </div>
<input class="form-control col-sm" style="max-width:70px;" type="text" @bind="aw.Position" @onkeyup="AddQuestion_Enter" /> <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" /> <input class="form-control" type="text" @bind="aw.Text" @onkeyup="AddQuestion_Enter" />
@ -156,9 +164,11 @@
private void PrepareNewQuestion() private void PrepareNewQuestion()
{ {
NewQuestion = new(); NewQuestion = new();
if (SelectedItem.Questions.Count > 0) { if (SelectedItem.Questions.Count > 0)
{
NewQuestion.Number = SelectedItem.Questions.Max(q => q.Number) + 1; NewQuestion.Number = SelectedItem.Questions.Max(q => q.Number) + 1;
} else }
else
{ {
NewQuestion.Number = 1; 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) private async Task AddQuestion_Enter(KeyboardEventArgs e)
{ {
if (e.Code == "Enter" || e.Code == "NumpadEnter") await AddQuestion(new MouseEventArgs()); 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) private async Task SaveQuiz(MouseEventArgs e)
{ {
foreach (Question question in SelectedItem.Questions) foreach (Question question in SelectedItem.Questions)

View File

@ -8,7 +8,7 @@
<a class="btn btn-primary mb-3" href="/settings/quizzes/edit">Neues Quiz</a> <a class="btn btn-primary mb-3" href="/settings/quizzes/edit">Neues Quiz</a>
<div class="table-responsive"> <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> <tbody>
@foreach (Quiz q in QuizList) @foreach (Quiz q in QuizList)
{ {

View File

@ -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);
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FWLAZ_Web.Data namespace FWLAZ_Web.Data
@ -13,6 +14,7 @@ namespace FWLAZ_Web.Data
public string Text { get; set; } = null!; public string Text { get; set; } = null!;
public bool IsCorrect { get; set; } public bool IsCorrect { get; set; }
[JsonIgnore]
public virtual Question Question { get; set; } = null!; public virtual Question Question { get; set; } = null!;
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FWLAZ_Web.Data namespace FWLAZ_Web.Data
@ -12,7 +13,11 @@ namespace FWLAZ_Web.Data
public int Number { get; set; } public int Number { get; set; }
public string Text { get; set; } = null!; public string Text { get; set; } = null!;
public List<Answer> Answers { get; set; } = new List<Answer>(); public List<Answer> Answers { get; set; } = new List<Answer>();
[JsonIgnore]
public virtual List<QuestionGroup> QuestionGroups { get; set; } = new(); public virtual List<QuestionGroup> QuestionGroups { get; set; } = new();
[JsonIgnore]
public virtual Quiz Quiz { get; set; } = null!; public virtual Quiz Quiz { get; set; } = null!;
} }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FWLAZ_Web.Data namespace FWLAZ_Web.Data
@ -15,6 +16,8 @@ namespace FWLAZ_Web.Data
public string Name { get; set; } = null!; public string Name { get; set; } = null!;
public int Order { get; set; } public int Order { get; set; }
public List<Question> Questions { get; set; } = new List<Question>(); public List<Question> Questions { get; set; } = new List<Question>();
[JsonIgnore]
public virtual Quiz Quiz { get; set; } = null!; public virtual Quiz Quiz { get; set; } = null!;
public QuestionGroup() { } public QuestionGroup() { }

View File

@ -15,6 +15,7 @@ namespace FWLAZ_Web.Data
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } = null!; public string Name { get; set; } = null!;
public bool IsMultipleChoice { get; set; }
public List<Question> Questions { get; set; } = new(); public List<Question> Questions { get; set; } = new();
public List<QuestionGroup> Questiongroups { get; set; } = new(); public List<QuestionGroup> Questiongroups { get; set; } = new();

Binary file not shown.

30
FWLAZ_Web/Dockerfile Normal file
View File

@ -0,0 +1,30 @@
# Unter https://aka.ms/customizecontainer erfahren Sie, wie Sie Ihren Debugcontainer anpassen und wie Visual Studio dieses Dockerfile verwendet, um Ihre Images für ein schnelleres Debuggen zu erstellen.
# Diese Stufe wird verwendet, wenn sie von VS im Schnellmodus ausgeführt wird (Standardeinstellung für Debugkonfiguration).
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
# Diese Stufe wird zum Erstellen des Dienstprojekts verwendet.
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["FWLAZ_Web.csproj", "."]
RUN dotnet restore "./FWLAZ_Web.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./FWLAZ_Web.csproj" -c $BUILD_CONFIGURATION -o /app/build
# Diese Stufe wird verwendet, um das Dienstprojekt zu veröffentlichen, das in die letzte Phase kopiert werden soll.
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./FWLAZ_Web.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# Diese Stufe wird in der Produktion oder bei Ausführung von VS im regulären Modus verwendet (Standard, wenn die Debugkonfiguration nicht verwendet wird).
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FWLAZ_Web.dll"]

View File

@ -4,9 +4,23 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>974b7a0a-88b5-41b5-9ec8-75d75421e174</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Data\quiz.db" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\quiz.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@ -17,6 +31,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,190 @@
// <auto-generated />
using FWLAZ_Web.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FWLAZ_Web.Migrations
{
[DbContext(typeof(LocalDbContext))]
[Migration("20240901192908_AddQuizType")]
partial class AddQuizType
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.8");
modelBuilder.Entity("FWLAZ_Web.Data.Answer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("IsCorrect")
.HasColumnType("INTEGER");
b.Property<string>("Position")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("QuestionId")
.HasColumnType("INTEGER");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("QuestionId");
b.ToTable("Answers");
});
modelBuilder.Entity("FWLAZ_Web.Data.Question", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("Number")
.HasColumnType("INTEGER");
b.Property<int>("QuizId")
.HasColumnType("INTEGER");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("QuizId");
b.ToTable("Question");
});
modelBuilder.Entity("FWLAZ_Web.Data.QuestionGroup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Order")
.HasColumnType("INTEGER");
b.Property<int>("QuizId")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("QuizId");
b.ToTable("QuestionGroup");
});
modelBuilder.Entity("FWLAZ_Web.Data.Quiz", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("IsMultipleChoice")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Quiz");
});
modelBuilder.Entity("QuestionQuestionGroup", b =>
{
b.Property<int>("QuestionGroupsId")
.HasColumnType("INTEGER");
b.Property<int>("QuestionsId")
.HasColumnType("INTEGER");
b.HasKey("QuestionGroupsId", "QuestionsId");
b.HasIndex("QuestionsId");
b.ToTable("QuestionQuestionGroup");
});
modelBuilder.Entity("FWLAZ_Web.Data.Answer", b =>
{
b.HasOne("FWLAZ_Web.Data.Question", "Question")
.WithMany("Answers")
.HasForeignKey("QuestionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Question");
});
modelBuilder.Entity("FWLAZ_Web.Data.Question", b =>
{
b.HasOne("FWLAZ_Web.Data.Quiz", "Quiz")
.WithMany("Questions")
.HasForeignKey("QuizId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Quiz");
});
modelBuilder.Entity("FWLAZ_Web.Data.QuestionGroup", b =>
{
b.HasOne("FWLAZ_Web.Data.Quiz", "Quiz")
.WithMany("Questiongroups")
.HasForeignKey("QuizId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Quiz");
});
modelBuilder.Entity("QuestionQuestionGroup", b =>
{
b.HasOne("FWLAZ_Web.Data.QuestionGroup", null)
.WithMany()
.HasForeignKey("QuestionGroupsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FWLAZ_Web.Data.Question", null)
.WithMany()
.HasForeignKey("QuestionsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FWLAZ_Web.Data.Question", b =>
{
b.Navigation("Answers");
});
modelBuilder.Entity("FWLAZ_Web.Data.Quiz", b =>
{
b.Navigation("Questiongroups");
b.Navigation("Questions");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FWLAZ_Web.Migrations
{
/// <inheritdoc />
public partial class AddQuizType : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsMultipleChoice",
table: "Quiz",
type: "INTEGER",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsMultipleChoice",
table: "Quiz");
}
}
}

View File

@ -95,6 +95,9 @@ namespace FWLAZ_Web.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");
b.Property<bool>("IsMultipleChoice")
.HasColumnType("INTEGER");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasColumnType("TEXT"); .HasColumnType("TEXT");

View File

@ -0,0 +1,55 @@
using FWLAZ_Web.Data;
using Blazored.LocalStorage;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Components;
namespace FWLAZ_Web.Objects
{
public class GameState
{
public enum QuizMode
{
Practice,
Tournament
}
public const string STORAGEKEY = "QuizSetting";
public QuizMode Mode { get; set; } = QuizMode.Practice;
public int QuizId { get; }
public bool QuizIsMultipleChoice { get; set; }
public string? Title { get; set; } = null!;
public string? Username { get; set; }
public List<Question>? Questions { get; set; }
public List<QuestionAnswer>? QuestionAnswers { get; set; }
public Question? CurrentQuestion
{
get
{
return Questions?.FirstOrDefault();
}
}
public GameState() { }
public GameState(Quiz quiz)
{
QuizId = quiz.Id;
QuizIsMultipleChoice = quiz.IsMultipleChoice;
Title = quiz.Name;
}
public bool NextQuestion()
{
if (Questions == null || Questions.Count == 0)
{
return false;
}
Questions.Remove(Questions[0]);
return true;
}
}
}

View File

@ -0,0 +1,23 @@
using FWLAZ_Web.Data;
namespace FWLAZ_Web.Objects
{
public class QuestionAnswer
{
public Question question { get; set; }
public List<Answer> GivenAnswers { get; set; } = new();
public bool IsCorrect
{
get
{
return GivenAnswers.All(a => a.IsCorrect);
}
}
public QuestionAnswer(Question question)
{
this.question = question;
}
}
}

View File

@ -0,0 +1,17 @@
using FWLAZ_Web.Data;
namespace FWLAZ_Web.Objects
{
public class QuestionGroupSelect
{
public bool Checked { get; set; }
public QuestionGroup questionGroup { get; }
public QuestionGroupSelect(QuestionGroup questionGroup)
{
this.questionGroup = questionGroup;
}
}
}

View File

@ -0,0 +1,19 @@
using FWLAZ_Web.Data;
namespace FWLAZ_Web.Objects
{
public class QuestionGroupSummary
{
public int QuestionGroupID { get; set; }
public string QuestionGroupName { get; set; }
public int QuestionCount { get; set; }
public QuestionGroupSummary(QuestionGroup questionGroup)
{
QuestionGroupID = questionGroup.Id;
QuestionGroupName = questionGroup.Name;
QuestionCount = questionGroup.Questions.Count;
}
}
}

View File

@ -0,0 +1,34 @@
using Blazored.LocalStorage;
namespace FWLAZ_Web.Objects
{
public class SessionState
{
public GameState? LoadGame { get; set; }
private ILocalStorageService localStorage;
public SessionState(ILocalStorageService localStorage)
{
this.localStorage = localStorage;
}
public async Task LoadAsync()
{
try
{
LoadGame = await localStorage.GetItemAsync<GameState>(nameof(LoadGame));
}
catch (Exception ex)
{
Console.WriteLine($"Error loading state: {ex.Message}");
}
}
public async Task SaveAsync()
{
await localStorage.SetItemAsync(nameof(LoadGame), LoadGame);
}
}
}

View File

@ -1,6 +1,8 @@
using FWLAZ_Web.Components; using FWLAZ_Web.Components;
using FWLAZ_Web.Data; using FWLAZ_Web.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Blazored.LocalStorage;
using FWLAZ_Web.Objects;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var connectionstring = builder.Configuration.GetConnectionString("QuizDB"); var connectionstring = builder.Configuration.GetConnectionString("QuizDB");
@ -9,6 +11,8 @@ var connectionstring = builder.Configuration.GetConnectionString("QuizDB");
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
builder.Services.AddDbContextFactory<LocalDbContext>(options => options.UseSqlite(connectionstring)); builder.Services.AddDbContextFactory<LocalDbContext>(options => options.UseSqlite(connectionstring));
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped<SessionState>();
var app = builder.Build(); var app = builder.Build();

View File

@ -1,4 +1,42 @@
{ {
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5018"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7281;http://localhost:5018"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Container (Dockerfile)": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "8080"
},
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "http://json.schemastore.org/launchsettings.json", "$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": { "iisSettings": {
"windowsAuthentication": false, "windowsAuthentication": false,
@ -7,32 +45,5 @@
"applicationUrl": "http://localhost:44728", "applicationUrl": "http://localhost:44728",
"sslPort": 44312 "sslPort": 44312
} }
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5018",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7281;http://localhost:5018",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
} }
} }

View File

@ -6,7 +6,7 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"QuizDB": "Data Source=Data\\quiz.db" "QuizDB": "Data Source=Data/quiz.db"
}, },
"AllowedHosts": "*" "AllowedHosts": "*"
} }