Added RaidTemplate sites

This commit is contained in:
t.ruspekhofer 2022-03-13 14:57:48 +01:00
parent e30645c505
commit dd79b0e333
7 changed files with 499 additions and 2 deletions

View file

@ -22,7 +22,71 @@ namespace Lieb.Data
.ToList();
}
public async Task CreateNewRaid(int raidTempalteId)
public RaidTemplate GetTemplate(int raidTemplateId)
{
using var context = _contextFactory.CreateDbContext();
return context.RaidTemplates
.Include(r => r.Roles)
.Include(r => r.Reminders)
.FirstOrDefault(t => t.RaidTemplateId == raidTemplateId);
}
public async Task AddOrEditTemplate(RaidTemplate template)
{
if (template != null)
{
using var context = _contextFactory.CreateDbContext();
if (template.RaidTemplateId == 0)
{
context.RaidTemplates.Add(template);
await context.SaveChangesAsync();
}
else
{
RaidTemplate raidToChange = await context.RaidTemplates
.Include(r => r.Roles)
.Include(r => r.Reminders)
.FirstOrDefaultAsync(r => r.RaidTemplateId == template.RaidTemplateId);
raidToChange.Title = template.Title;
raidToChange.Description = template.Description;
raidToChange.Organizer = template.Organizer;
raidToChange.Guild = template.Guild;
raidToChange.VoiceChat = template.VoiceChat;
raidToChange.RaidType = template.RaidType;
raidToChange.RequiredRole = template.RequiredRole;
raidToChange.DiscordChannelId = template.DiscordChannelId;
raidToChange.DiscordGuildId = template.DiscordGuildId;
raidToChange.StartTime = template.StartTime;
raidToChange.EndTime = template.EndTime;
raidToChange.FreeForAllTime = template.FreeForAllTime;
raidToChange.TimeZone = template.TimeZone;
raidToChange.Frequency = template.Frequency;
raidToChange.CreateDaysBefore = template.CreateDaysBefore;
context.PlannedRaidRoles.RemoveRange(raidToChange.Roles);
context.RaidReminders.RemoveRange(raidToChange.Reminders);
raidToChange.Roles.Clear();
raidToChange.Reminders.Clear();
raidToChange.Roles = template.Roles;
raidToChange.Reminders = template.Reminders;
await context.SaveChangesAsync();
}
}
}
public async Task DeleteTemplate(int raidTemplateId)
{
using var context = _contextFactory.CreateDbContext();
RaidTemplate template = GetTemplate(raidTemplateId);
context.PlannedRaidRoles.RemoveRange(template.Roles);
context.RaidReminders.RemoveRange(template.Reminders);
await context.SaveChangesAsync();
context.RaidTemplates.Remove(template);
await context.SaveChangesAsync();
}
public async Task CreateRaidFromTemplate(int raidTempalteId)
{
using var context = _contextFactory.CreateDbContext();
RaidTemplate? template = await context.RaidTemplates

View file

@ -34,7 +34,7 @@ namespace Lieb.Data
DateTime UTCStartTime = TimeZoneInfo.ConvertTimeToUtc(template.StartTime, timeZone);
if(UTCStartTime.AddDays(-template.CreateDaysBefore).AddHours(1) < DateTime.UtcNow)
{
raidTemplateService.CreateNewRaid(template.RaidTemplateId).Wait();
raidTemplateService.CreateRaidFromTemplate(template.RaidTemplateId).Wait();
}
}

View file

@ -1,9 +1,11 @@
@page "/raidedit"
@page "/raidedit/{raidId}"
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations
@inject RaidService RaidService
@inject UserService UserService
@inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@ -61,12 +63,41 @@
</label>
</p>
<p>
<label>
Required Role:
<InputSelect @bind-Value="_raid.RequiredRole">
<option value="">Not Locked</option>
@foreach(LiebRole role in UserService.GetLiebRoles())
{
if (!role.IsSystemRole)
{
<option value="@role.RoleName">@role.RoleName</option>
}
}
</InputSelect>
</label>
</p>
<p>
<label>
Free for all date:
<InputDate @bind-Value="_freeForAllDate" />
</label>
</p>
<p>
<label>
Free for all time:
<input type="time" @bind="_freeForAllTime" />
</label>
</p>
<p>
<label>
Organizer:
<InputText @bind-Value="_raid.Organizer" />
</label>
</p>
<p>
<label>
Guild:

View file

@ -0,0 +1,78 @@
@using System.Security.Claims
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2.Raid
@inject UserService UserService
@inject RaidService RaidService
@inject TimeZoneService TimeZoneService
@inject RaidRandomizerService RaidRandomizerService
<body>
<h5>@_template.Title</h5>
<div>@_template.Description</div>
<div >
<div class="times">
<h5>Date</h5>
<p>@_template.StartTime.ToLongDateString()</p>
</div>
<div class="times">
<h5>Time</h5>
<p>from: @_template.StartTime.ToShortTimeString() to: @_template.EndTime.ToShortTimeString()</p>
</div>
<div class="times">
<h5>TimeZone</h5>
<p>@_template.TimeZone</p>
</div>
</div>
<div>
<div class="details">
<h5>Organizer</h5>
<p>@_template.Organizer</p>
</div>
<div class="details">
<h5>Guild</h5>
<p>@_template.Guild</p>
</div>
<div class="details">
<h5>Voice chat</h5>
<p>@_template.VoiceChat</p>
</div>
</div>
<div>
<table class="table">
<tbody>
@foreach (var role in _template.Roles)
{
<tr>
<td><h5>@role.Name: @role.Description (@role.Spots)</h5></td>
</tr>
}
</tbody>
</table>
</div>
<AuthorizeView Policy="@Constants.Roles.RaidLead">
<div class="nav-item px-3">
@{string navLink = $"raidtemplateedit/{_template.RaidTemplateId}";}
<NavLink class="nav-link" href="@navLink">
<span class="oi oi-plus" aria-hidden="true"></span> Edit
</NavLink>
</div>
</AuthorizeView>
</body>
@code {
[Parameter]
public RaidTemplate _template { get; set; }
protected override async Task OnParametersSetAsync()
{
}
}

View file

@ -0,0 +1,32 @@
body {
background-color: rgb(38 38 38);
border-radius: 25px;
padding: 25px;
width: 700px;
/*width: fit-content;*/
color: lightgray;
}
h5 {
color: lightgrey;
}
.times {
float: left;
display: inline;
width: 33%;
padding-top: 15px;
}
.details {
float: left;
display: inline;
width: 150px;
padding-top: 15px;
}
table {
column-width: auto;
color: lightgray;
}

View file

@ -0,0 +1,254 @@
@page "/raidtemplateedit"
@page "/raidtemplateedit/{raidId}"
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2.Raid
@using System.ComponentModel.DataAnnotations
@inject RaidTemplateService RaidTemplateService
@inject UserService UserService
@inject TimeZoneService TimeZoneService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
<h3>CreateRaid</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead" Context="authorizationContext">
<EditForm Model="@_template" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
@{
bool _isEdit = _template.RaidTemplateId != 0;
}
<p>
<label>
Title:
<InputText @bind-Value="_template.Title" />
</label>
</p>
<p>
<label>
Description:
<InputTextArea @bind-Value="_template.Description" />
</label>
</p>
<p>
<label>
Raid Type:
<InputSelect @bind-Value="_template.RaidType" disabled="@_isEdit">
<option value="@RaidType.Planned">Planned</option>
<option value="@RaidType.RandomWithBoons">Random with boons covred</option>
<option value="@RaidType.RandomClasses">Random classes</option>
<option value="@RaidType.RandomEliteSpecialization">Random elite specializations</option>
</InputSelect>
</label>
</p>
<p>
<label>
Date:
<InputDate @bind-Value="_raidDate" />
</label>
</p>
<p>
<label>
Start Time:
<input type="time" @bind="_startTime" />
</label>
</p>
<p>
<label>
End Time:
<input type="time" @bind="_endTime" />
</label>
</p>
<p>
<label>
Time Zone:
<InputSelect @bind-Value="_template.RequiredRole">
<option value="@_template.TimeZone">@_template.TimeZone</option>
@if(_userTimeZone != @_template.TimeZone)
{
<option value="@_userTimeZone">@_userTimeZone</option>
}
</InputSelect>
</label>
</p>
<p>
<label>
Required Role:
<InputSelect @bind-Value="_template.RequiredRole">
<option value="">Not Locked</option>
@foreach(LiebRole role in UserService.GetLiebRoles())
{
if (!role.IsSystemRole)
{
<option value="@role.RoleName">@role.RoleName</option>
}
}
</InputSelect>
</label>
</p>
<p>
<label>
Free for all date:
<InputDate @bind-Value="_freeForAllDate" />
</label>
</p>
<p>
<label>
Free for all time:
<input type="time" @bind="_freeForAllTime" />
</label>
</p>
<p>
<label>
Organizer:
<InputText @bind-Value="_template.Organizer" />
</label>
</p>
<p>
<label>
Guild:
<InputText @bind-Value="_template.Guild" />
</label>
</p>
<p>
<label>
Voice chat:
<InputText @bind-Value="_template.VoiceChat" />
</label>
</p>
@if(_template.RaidType == RaidType.Planned)
{
<p>
<label>
Roles:
</label>
<button type=button @onclick="() => AddRoleClicked()">Add role</button>
<table>
<tr>
<th>Spots</th>
<th>Role name</th>
<th>Description</th>
</tr>
@foreach( PlannedRaidRole role in _template.Roles)
{
<tr>
<td><InputNumber @bind-Value="role.Spots" /></td>
<td><InputText @bind-Value="role.Name" /></td>
<td><InputText @bind-Value="role.Description" /></td>
<td><button type=button @onclick="() => DeleteRoleClicked(role)">Delete</button></td>
</tr>
}
</table>
</p>
}
<ValidationSummary />
<label class="validation-message" >@_errorMessage</label>
<button type="submit">Submit</button>
</EditForm>
<br/>
<button type=button @onclick="() => DeleteRaidClicked()">Delete Raid</button>
</AuthorizeView>
@code {
[Parameter]
public string raidId { get; set; }
public RaidTemplate _template;
private string _errorMessage = string.Empty;
private DateTimeOffset _raidDate = DateTime.Now.Date;
private DateTimeOffset _startTime;
private DateTimeOffset _endTime;
private DateTimeOffset _freeForAllDate = DateTime.Now.Date;
private DateTimeOffset _freeForAllTime;
private string _userTimeZone = string.Empty;
protected override async Task OnInitializedAsync()
{
if(!string.IsNullOrEmpty(raidId) && int.TryParse(raidId, out int parsedId))
{
_template = RaidTemplateService.GetTemplate(parsedId);
_startTime = _template.StartTime;
_endTime = _template.EndTime;
_raidDate = _startTime.Date;
_freeForAllTime = _template.FreeForAllTime;
_freeForAllDate = _freeForAllTime.Date;
}
else
{
_template = new RaidTemplate();
}
_userTimeZone = await TimeZoneService.GetUserTimeZone();
}
async Task AddRoleClicked()
{
_template.Roles.Add(new PlannedRaidRole());
}
async Task DeleteRoleClicked(PlannedRaidRole role)
{
_template.Roles.Remove(role);
}
async Task DeleteRaidClicked()
{
bool confirmed = await JsRuntime.InvokeAsync<bool>("confirm", "Are you sure you want to delete the raid?");
if (confirmed)
{
await RaidTemplateService.DeleteTemplate(_template.RaidTemplateId);
NavigationManager.NavigateTo("raidoverview");
}
}
private async Task HandleValidSubmit()
{
if(_template.RaidType != RaidType.Planned)
{
_template.Roles.Clear();
_template.Roles.Add(new PlannedRaidRole()
{
Spots = 10,
Name = "Random",
Description = _template.RaidType.ToString()
});
}
if(_template.Roles.Count == 0)
{
_errorMessage = "Roles are needed for a raid.";
return;
}
_template.TimeZone = await TimeZoneService.GetUserTimeZone();
_template.StartTime =_raidDate.Date + _startTime.TimeOfDay;
if(_startTime.TimeOfDay > _endTime.TimeOfDay)
{
_template.EndTime = _raidDate.Date + _endTime.TimeOfDay;
}
else
{
_template.EndTime = _raidDate.Date.AddDays(1) + _endTime.TimeOfDay;
}
_template.FreeForAllTime = _freeForAllDate.Date + _freeForAllTime.TimeOfDay;
await RaidTemplateService.AddOrEditTemplate(_template);
NavigationManager.NavigateTo("raidoverview");
}
}

View file

@ -0,0 +1,38 @@
@page "/raidtemplateoverview"
@using Lieb.Data
@using System.Security.Claims
@using Lieb.Models
@using Lieb.Models.GuildWars2.Raid
@inject RaidService RaidService
@inject UserService UserService
@inject AuthenticationStateProvider AuthenticationStateProvider
<h3>RaidTemplateOverview</h3>
<AuthorizeView Policy="@Constants.Roles.RaidLead">
<div class="nav-item px-3">
<NavLink class="nav-link" href="raidedit">
<span class="oi oi-plus" aria-hidden="true"></span> Add Raid
</NavLink>
</div>
</AuthorizeView>
@foreach (var raid in _raids.OrderBy(r => r.StartTimeUTC))
{
<br />
<RaidTemplateDetails _raid=@raid/>
}
@code
{
private List<Raid> _raids;
protected override async Task OnInitializedAsync()
{
_raids = RaidService.GetRaids();
}
}