Reworked random raids to be randomizable multiple times

This commit is contained in:
t.ruspekhofer 2022-03-13 17:21:17 +01:00
parent dd79b0e333
commit 6e4dce5b1f
9 changed files with 249 additions and 83 deletions

View file

@ -89,6 +89,13 @@ namespace Lieb.Data
Name = "Scourge", Name = "Scourge",
Spots = 8 Spots = 8
}; };
PlannedRaidRole randomRole = new PlannedRaidRole()
{
Spots = 10,
Name = "Random",
Description = RaidType.RandomWithBoons.ToString(),
IsRandomSignUpRole = true
};
Raid raid = new Raid() Raid raid = new Raid()
{ {
@ -101,7 +108,7 @@ namespace Lieb.Data
EndTimeUTC = DateTime.UtcNow.AddHours(2), EndTimeUTC = DateTime.UtcNow.AddHours(2),
FreeForAllTimeUTC = DateTime.UtcNow.AddHours(-2), FreeForAllTimeUTC = DateTime.UtcNow.AddHours(-2),
VoiceChat = "ts.lieb.games", VoiceChat = "ts.lieb.games",
Roles = new [] { ele, scourge} Roles = new [] { randomRole, ele, scourge}
}; };
context.Raids.Add(raid); context.Raids.Add(raid);
context.SaveChanges(); context.SaveChanges();

View file

@ -34,26 +34,21 @@ namespace Lieb.Data
if (raid == null || raid.RaidType == RaidType.Planned) if (raid == null || raid.RaidType == RaidType.Planned)
return; return;
switch (raid.RaidType)
if (!raid.IsRandomized)
{ {
switch (raid.RaidType) case RaidType.RandomClasses:
{ RandomizeClasses(raid);
case RaidType.RandomClasses: break;
RandomizeClasses(raid); case RaidType.RandomEliteSpecialization:
break; RandomizeEliteSpecs(raid);
case RaidType.RandomEliteSpecialization: break;
RandomizeEliteSpecs(raid); case RaidType.RandomWithBoons:
break; RandomizeWithBoons(raid);
case RaidType.RandomWithBoons: break;
RandomizeWithBoons(raid);
break;
}
raid.IsRandomized = true;
await context.SaveChangesAsync();
CleanUpRoles(raid, context);
await context.SaveChangesAsync();
} }
await context.SaveChangesAsync();
CleanUpRoles(raid, context);
await context.SaveChangesAsync();
} }
private void RandomizeClasses(Raid raid) private void RandomizeClasses(Raid raid)

View file

@ -57,76 +57,54 @@ namespace Lieb.Data
} }
else else
{ {
Raid raidToChange = await context.Raids Raid? raidToChange = await context.Raids
.Include(r => r.Roles) .Include(r => r.Roles)
.Include(r => r.SignUpHistory) .Include(r => r.SignUpHistory)
.Include(r => r.Reminders) .Include(r => r.Reminders)
.Include(r => r.SignUps) .Include(r => r.SignUps)
.FirstOrDefaultAsync(r => r.RaidId == raid.RaidId); .FirstOrDefaultAsync(r => r.RaidId == raid.RaidId);
raidToChange.Title = raid.Title; if (raidToChange != null)
raidToChange.Description = raid.Description;
raidToChange.StartTimeUTC = raid.StartTimeUTC;
raidToChange.EndTimeUTC = raid.EndTimeUTC;
raidToChange.Organizer = raid.Organizer;
raidToChange.Guild = raid.Guild;
raidToChange.VoiceChat = raid.VoiceChat;
raidToChange.RaidType = raid.RaidType;
raidToChange.RequiredRole = raid.RequiredRole;
raidToChange.FreeForAllTimeUTC = raid.FreeForAllTimeUTC;
raidToChange.DiscordMessageId = raid.DiscordMessageId;
raidToChange.DiscordChannelId = raid.DiscordChannelId;
raidToChange.DiscordGuildId = raid.DiscordGuildId;
List<PlannedRaidRole> rolesToRemove = new List<PlannedRaidRole>();
foreach (PlannedRaidRole role in raidToChange.Roles)
{ {
PlannedRaidRole? newRole = raid.Roles.FirstOrDefault(r => r.PlannedRaidRoleId == role.PlannedRaidRoleId); raidToChange.Title = raid.Title;
if(newRole != null) raidToChange.Description = raid.Description;
raidToChange.StartTimeUTC = raid.StartTimeUTC;
raidToChange.EndTimeUTC = raid.EndTimeUTC;
raidToChange.Organizer = raid.Organizer;
raidToChange.Guild = raid.Guild;
raidToChange.VoiceChat = raid.VoiceChat;
raidToChange.RaidType = raid.RaidType;
raidToChange.RequiredRole = raid.RequiredRole;
raidToChange.FreeForAllTimeUTC = raid.FreeForAllTimeUTC;
raidToChange.DiscordMessageId = raid.DiscordMessageId;
raidToChange.DiscordChannelId = raid.DiscordChannelId;
raidToChange.DiscordGuildId = raid.DiscordGuildId;
if (raidToChange.RaidType == RaidType.Planned)
{ {
role.Spots = newRole.Spots; EditRoles(raidToChange, raid, context);
role.Name = newRole.Name;
role.Description = newRole.Description;
} }
else else
{ {
rolesToRemove.Add(role); if(!raidToChange.Roles.Where(r => r.IsRandomSignUpRole).Any())
{
raidToChange.Roles.Add(raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole));
}
int randomRoleId = raidToChange.Roles.FirstOrDefault(r => r.IsRandomSignUpRole).PlannedRaidRoleId;
foreach (RaidSignUp signUp in raidToChange.SignUps)
{
if (randomRoleId == 0)
{
signUp.PlannedRaidRole = raidToChange.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
}
else
{
signUp.PlannedRaidRoleId = randomRoleId;
}
}
context.PlannedRaidRoles.RemoveRange(raidToChange.Roles.Where(r => !r.IsRandomSignUpRole));
} }
}
foreach(PlannedRaidRole role in rolesToRemove)
{
raidToChange.Roles.Remove(role);
context.PlannedRaidRoles.Remove(role);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToChange.Roles.Add(role);
}
List<RaidReminder> reminderToRemove = new List<RaidReminder>(); EditReminders(raidToChange, raid, context);
foreach (RaidReminder reminder in raidToChange.Reminders)
{
RaidReminder? newReminder = raid.Reminders.FirstOrDefault(r => r.RaidReminderId == reminder.RaidReminderId);
if (newReminder != null)
{
reminder.Type = newReminder.Type;
reminder.Message = newReminder.Message;
reminder.HoursBeforeRaid = newReminder.HoursBeforeRaid;
reminder.ChannelId = newReminder.ChannelId;
reminder.Sent = newReminder.Sent;
}
else
{
reminderToRemove.Add(reminder);
}
}
foreach(RaidReminder reminder in reminderToRemove)
{
raidToChange.Reminders.Remove(reminder);
context.RaidReminders.Remove(reminder);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToChange.Roles.Add(role);
} }
await context.SaveChangesAsync(); await context.SaveChangesAsync();
@ -134,7 +112,65 @@ namespace Lieb.Data
} }
} }
public async Task DeleteRaid(int raidId) private void EditRoles(Raid raidToEdit, Raid raid, LiebContext context)
{
List<PlannedRaidRole> rolesToRemove = new List<PlannedRaidRole>();
foreach (PlannedRaidRole role in raidToEdit.Roles)
{
PlannedRaidRole? newRole = raid.Roles.FirstOrDefault(r => r.PlannedRaidRoleId == role.PlannedRaidRoleId);
if (newRole != null)
{
role.Spots = newRole.Spots;
role.Name = newRole.Name;
role.Description = newRole.Description;
}
else
{
rolesToRemove.Add(role);
}
}
foreach (PlannedRaidRole role in rolesToRemove)
{
raidToEdit.Roles.Remove(role);
context.PlannedRaidRoles.Remove(role);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToEdit.Roles.Add(role);
}
}
private void EditReminders(Raid raidToEdit, Raid raid, LiebContext context)
{
List<RaidReminder> reminderToRemove = new List<RaidReminder>();
foreach (RaidReminder reminder in raidToEdit.Reminders)
{
RaidReminder? newReminder = raid.Reminders.FirstOrDefault(r => r.RaidReminderId == reminder.RaidReminderId);
if (newReminder != null)
{
reminder.Type = newReminder.Type;
reminder.Message = newReminder.Message;
reminder.HoursBeforeRaid = newReminder.HoursBeforeRaid;
reminder.ChannelId = newReminder.ChannelId;
reminder.Sent = newReminder.Sent;
}
else
{
reminderToRemove.Add(reminder);
}
}
foreach (RaidReminder reminder in reminderToRemove)
{
raidToEdit.Reminders.Remove(reminder);
context.RaidReminders.Remove(reminder);
}
foreach (PlannedRaidRole role in raid.Roles.Where(r => r.PlannedRaidRoleId == 0))
{
raidToEdit.Roles.Add(role);
}
}
public async Task DeleteRaid(int raidId)
{ {
using var context = _contextFactory.CreateDbContext(); using var context = _contextFactory.CreateDbContext();
Raid raid = GetRaid(raidId); Raid raid = GetRaid(raidId);

View file

@ -14,5 +14,7 @@ namespace Lieb.Models.GuildWars2.Raid
[Required] [Required]
[StringLength(200, ErrorMessage = "Description too long (200 character limit).")] [StringLength(200, ErrorMessage = "Description too long (200 character limit).")]
public string Description { get; set; } = String.Empty; public string Description { get; set; } = String.Empty;
public bool IsRandomSignUpRole { get; set; } = false;
} }
} }

View file

@ -22,8 +22,6 @@ namespace Lieb.Models.GuildWars2.Raid
public DateTimeOffset FreeForAllTimeUTC { get; set; } public DateTimeOffset FreeForAllTimeUTC { get; set; }
public bool IsRandomized { get; set; } = false;
public ICollection<RaidSignUp> SignUps { get; set; } = new HashSet<RaidSignUp>(); public ICollection<RaidSignUp> SignUps { get; set; } = new HashSet<RaidSignUp>();
public ICollection<SignUpHistory> SignUpHistory { get; set; } = new HashSet<SignUpHistory>(); public ICollection<SignUpHistory> SignUpHistory { get; set; } = new HashSet<SignUpHistory>();

View file

@ -49,7 +49,7 @@
} }
else else
{ {
<RaidRolesPlanned _raid=@_raid _user=@_user/> <RaidRolesRandom _raid=@_raid _user=@_user/>
} }
} }
else else
@ -65,7 +65,7 @@
<span class="oi oi-plus" aria-hidden="true"></span> Edit <span class="oi oi-plus" aria-hidden="true"></span> Edit
</NavLink> </NavLink>
</div> </div>
@if(_raid.RaidType != RaidType.Planned && !_raid.IsRandomized) @if(_raid.RaidType != RaidType.Planned)
{ {
<button type=button @onclick="() => RandomizeClicked()">Randomize</button> <button type=button @onclick="() => RandomizeClicked()">Randomize</button>
} }

View file

@ -209,12 +209,16 @@
{ {
if(_raid.RaidType != RaidType.Planned) if(_raid.RaidType != RaidType.Planned)
{ {
PlannedRaidRole role = _raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole);
int randomRoleId = role != null ? role.PlannedRaidRoleId : 0;
_raid.Roles.Clear(); _raid.Roles.Clear();
_raid.Roles.Add(new PlannedRaidRole() _raid.Roles.Add(new PlannedRaidRole()
{ {
Spots = 10, Spots = 10,
Name = "Random", Name = "Random",
Description = _raid.RaidType.ToString() Description = _raid.RaidType.ToString(),
IsRandomSignUpRole = true,
PlannedRaidRoleId = randomRoleId
}); });
} }

View file

@ -0,0 +1,115 @@
@using Lieb.Data
@using Lieb.Models
@using Lieb.Models.GuildWars2
@using Lieb.Models.GuildWars2.Raid
@inject RaidService RaidService
<table class="table">
<tbody>
@{
RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == _user.LiebUserId).FirstOrDefault();
bool isSignedUp = userRole != null;
}
@foreach (PlannedRaidRole role in _raid.Roles)
{
RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
<tr>
@if (role.IsRandomSignUpRole && _raid.SignUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count() < 10)
{
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.SignedUp)">Sign Up</button></td>
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.Maybe)">Maybe</button></td>
<td><button @onclick="() => SignUpClicked(role, _user, isSignedUp, SignUpType.Backup)">Backup</button></td>
}
else
{
<td></td>
<td></td>
<td></td>
}
<td><h5>@role.Name: @role.Description (@usedSpots /@role.Spots)</h5></td>
</tr>
@foreach (var signUp in signUps)
{
@if(signUp.SignUpType != SignUpType.SignedOff)
{
<tr>
@{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == _user.LiebUserId;}
<td></td>
<td></td>
@if(isUser)
{
<td><button @onclick="() => SignOffClicked(role, _user)">Sign Off</button></td>
}
else
{
<td></td>
}
@{string signUpStatus = string.Empty;}
@if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}";
@if (isUser && _usableAccounts.Count > 1)
{
<td>@signUp.LiebUser.Name
<select value=@signUp.GuildWars2AccountId @onchange="args => ChangeAccount(_user, args)">
@foreach (var account in _usableAccounts)
{
<option value=@account.GuildWars2AccountId>@account.AccountName</option>
}
</select> @signUpStatus </td>
}
else
{
<td>@signUp.LiebUser.Name (@signUp.GuildWars2Account.AccountName) @signUpStatus</td>
}
</tr>
}
}
}
</tbody>
</table>
@code {
[Parameter]
public Raid _raid { get; set; }
[Parameter]
public LiebUser _user { get; set; }
private List<GuildWars2Account> _usableAccounts;
protected override async Task OnParametersSetAsync()
{
_usableAccounts = _user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).ToList();
}
async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp, SignUpType signUpType)
{
if(isSignedUp)
{
await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, signUpType);
}
else
{
await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, _usableAccounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, signUpType);
}
_raid = RaidService.GetRaid(_raid.RaidId);
}
async Task SignOffClicked(PlannedRaidRole role, LiebUser liebUser)
{
await RaidService.SignOff(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId);
_raid = RaidService.GetRaid(_raid.RaidId);
}
async Task ChangeAccount(LiebUser liebUser, ChangeEventArgs e)
{
int accountId = int.Parse(e.Value.ToString());
await RaidService.ChangeAccount(_raid.RaidId, liebUser.LiebUserId, accountId);
_raid = RaidService.GetRaid(_raid.RaidId);
}
}

View file

@ -0,0 +1,9 @@
h5 {
color: lightgrey;
}
table {
column-width: auto;
color: lightgray;
width: max-content;
}