added randomizer for classes and elite specs
This commit is contained in:
parent
8137b1ca6a
commit
788badff16
5 changed files with 199 additions and 52 deletions
126
Lieb/Data/RaidRandomizerService.cs
Normal file
126
Lieb/Data/RaidRandomizerService.cs
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
using Lieb.Models.GuildWars2;
|
||||||
|
using Lieb.Models.GuildWars2.Raid;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Lieb.Data
|
||||||
|
{
|
||||||
|
public class RaidRandomizerService
|
||||||
|
{
|
||||||
|
private readonly IDbContextFactory<LiebContext> _contextFactory;
|
||||||
|
|
||||||
|
public RaidRandomizerService(IDbContextFactory<LiebContext> contextFactory)
|
||||||
|
{
|
||||||
|
_contextFactory = contextFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RandomizeRaid(int raidId)
|
||||||
|
{
|
||||||
|
using var context = _contextFactory.CreateDbContext();
|
||||||
|
Raid? raid = context.Raids
|
||||||
|
.Include(r => r.Roles)
|
||||||
|
.Include(r => r.SignUpHistory)
|
||||||
|
.Include(r => r.Reminders)
|
||||||
|
.Include(r => r.SignUps)
|
||||||
|
.ThenInclude(s => s.LiebUser)
|
||||||
|
.Include(r => r.SignUps)
|
||||||
|
.ThenInclude(s => s.GuildWars2Account)
|
||||||
|
.ThenInclude(a => a.EquippedBuilds)
|
||||||
|
.ThenInclude(e => e.GuildWars2Build)
|
||||||
|
.Include(r => r.SignUps)
|
||||||
|
.ThenInclude(s => s.PlannedRaidRole)
|
||||||
|
.FirstOrDefault(r => r.RaidId == raidId);
|
||||||
|
|
||||||
|
if (raid == null || raid.RaidType == RaidType.Planned)
|
||||||
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
if (!raid.IsRandomized)
|
||||||
|
{
|
||||||
|
switch (raid.RaidType)
|
||||||
|
{
|
||||||
|
case RaidType.RandomClasses:
|
||||||
|
RandomizeClasses(raid);
|
||||||
|
break;
|
||||||
|
case RaidType.RandomEliteSpecialization:
|
||||||
|
RandomizeEliteSpecs(raid);
|
||||||
|
break;
|
||||||
|
case RaidType.RandomWithBoons:
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
raid.IsRandomized = true;
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
CleanUpRoles(raid, context);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RandomizeClasses(Raid raid)
|
||||||
|
{
|
||||||
|
Random rand = new Random();
|
||||||
|
foreach (RaidSignUp signUp in raid.SignUps)
|
||||||
|
{
|
||||||
|
HashSet<GuildWars2Class> possibleClasses = new HashSet<GuildWars2Class>();
|
||||||
|
foreach (Equipped build in signUp.GuildWars2Account.EquippedBuilds)
|
||||||
|
{
|
||||||
|
possibleClasses.Add(build.GuildWars2Build.Class);
|
||||||
|
}
|
||||||
|
PlannedRaidRole role = new PlannedRaidRole();
|
||||||
|
role.Spots = 1;
|
||||||
|
if (possibleClasses.Count > 0)
|
||||||
|
{
|
||||||
|
role.Name = possibleClasses.ToList()[rand.Next(possibleClasses.Count - 1)].ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
role.Name = "No class found.";
|
||||||
|
}
|
||||||
|
raid.Roles.Add(role);
|
||||||
|
signUp.PlannedRaidRole = role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RandomizeEliteSpecs(Raid raid)
|
||||||
|
{
|
||||||
|
Random rand = new Random();
|
||||||
|
foreach (RaidSignUp signUp in raid.SignUps)
|
||||||
|
{
|
||||||
|
HashSet<EliteSpecialization> possibleEliteSpecs = new HashSet<EliteSpecialization>();
|
||||||
|
foreach (Equipped build in signUp.GuildWars2Account.EquippedBuilds)
|
||||||
|
{
|
||||||
|
possibleEliteSpecs.Add(build.GuildWars2Build.EliteSpecialization);
|
||||||
|
}
|
||||||
|
PlannedRaidRole role = new PlannedRaidRole();
|
||||||
|
role.Spots = 1;
|
||||||
|
if (possibleEliteSpecs.Count > 0)
|
||||||
|
{
|
||||||
|
role.Name = possibleEliteSpecs.ToList()[rand.Next(possibleEliteSpecs.Count - 1)].ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
role.Name = "No class found.";
|
||||||
|
}
|
||||||
|
raid.Roles.Add(role);
|
||||||
|
signUp.PlannedRaidRole = role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CleanUpRoles(Raid raid, LiebContext context)
|
||||||
|
{
|
||||||
|
List<PlannedRaidRole> rolesToDelete = new List<PlannedRaidRole>();
|
||||||
|
foreach (PlannedRaidRole role in raid.Roles)
|
||||||
|
{
|
||||||
|
if (raid.SignUps.FirstOrDefault(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId) == null)
|
||||||
|
{
|
||||||
|
rolesToDelete.Add(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (PlannedRaidRole role in rolesToDelete)
|
||||||
|
{
|
||||||
|
raid.Roles.Remove(role);
|
||||||
|
}
|
||||||
|
context.PlannedRaidRoles.RemoveRange(rolesToDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,6 +46,8 @@ namespace Lieb.Models.GuildWars2.Raid
|
||||||
[Required]
|
[Required]
|
||||||
public RaidType RaidType { get; set; }
|
public RaidType RaidType { get; set; }
|
||||||
|
|
||||||
|
public bool IsRandomized { get; set; } = false;
|
||||||
|
|
||||||
public int Frequency { get; set; }
|
public int Frequency { get; set; }
|
||||||
|
|
||||||
public string RequiredRole { get; set; } = String.Empty;
|
public string RequiredRole { get; set; } = String.Empty;
|
||||||
|
|
|
@ -4,67 +4,72 @@
|
||||||
@using Lieb.Models.GuildWars2.Raid
|
@using Lieb.Models.GuildWars2.Raid
|
||||||
@inject UserService UserService
|
@inject UserService UserService
|
||||||
@inject RaidService RaidService
|
@inject RaidService RaidService
|
||||||
|
@inject RaidRandomizerService RaidRandomizerService
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h5>@Raid.Title</h5>
|
<h5>@_raid.Title</h5>
|
||||||
|
|
||||||
<div>@Raid.Description</div>
|
<div>@_raid.Description</div>
|
||||||
|
|
||||||
<div >
|
<div >
|
||||||
<div class="times">
|
<div class="times">
|
||||||
<h5>Date</h5>
|
<h5>Date</h5>
|
||||||
<p>@Raid.Date.ToLongDateString()</p>
|
<p>@_raid.Date.ToLongDateString()</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="times">
|
<div class="times">
|
||||||
<h5>Time</h5>
|
<h5>Time</h5>
|
||||||
<p>from: @Raid.StartTime.LocalDateTime.ToShortTimeString() to: @Raid.EndTime.LocalDateTime.ToShortTimeString()</p>
|
<p>from: @_raid.StartTime.LocalDateTime.ToShortTimeString() to: @_raid.EndTime.LocalDateTime.ToShortTimeString()</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<div class="details">
|
<div class="details">
|
||||||
<h5>Organizer</h5>
|
<h5>Organizer</h5>
|
||||||
<p>@Raid.Organizer</p>
|
<p>@_raid.Organizer</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="details">
|
<div class="details">
|
||||||
<h5>Guild</h5>
|
<h5>Guild</h5>
|
||||||
<p>@Raid.Guild</p>
|
<p>@_raid.Guild</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="details">
|
<div class="details">
|
||||||
<h5>Voice chat</h5>
|
<h5>Voice chat</h5>
|
||||||
<p>@Raid.VoiceChat</p>
|
<p>@_raid.VoiceChat</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<AuthorizeView>
|
<AuthorizeView>
|
||||||
<Authorized>
|
<Authorized>
|
||||||
@{
|
@{
|
||||||
ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
|
ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value);
|
||||||
LiebUser user = UserService.GetLiebUserSmall(discordId);
|
LiebUser user = UserService.GetLiebUser(discordId);
|
||||||
RaidSignUp userRole = Raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault();
|
RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault();
|
||||||
bool isSignedUp = userRole != null;
|
bool isSignedUp = userRole != null;
|
||||||
|
DateTime flexTime = _raid.FreeForAllDate.Date + _raid.FreeForAllTime.TimeOfDay;
|
||||||
|
bool isSignUoAllowed = !_raid.IsRandomized && (user.RoleAssignments.FirstOrDefault(a => a.LiebRole.RoleName == _raid.RequiredRole) != null || flexTime < DateTime.Now);
|
||||||
|
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach (var role in Raid.Roles)
|
@foreach (var role in _raid.Roles)
|
||||||
{
|
{
|
||||||
Models.GuildWars2.Raid.RaidSignUp[] signUps = Raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
|
Models.GuildWars2.Raid.RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
|
||||||
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
|
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
@if (@usedSpots < @role.Spots)
|
@if (isSignUoAllowed)
|
||||||
{
|
{
|
||||||
<td><button @onclick="() => SignUpClicked(role, user, isSignedUp)">Sign Up</button></td>
|
@if (@usedSpots < @role.Spots)
|
||||||
<td><button @onclick="() => MaybeClicked(role, user, isSignedUp)">Maybe</button></td>
|
{
|
||||||
}
|
<td><button @onclick="() => SignUpClicked(role, user, isSignedUp)">Sign Up</button></td>
|
||||||
else
|
<td><button @onclick="() => MaybeClicked(role, user, isSignedUp)">Maybe</button></td>
|
||||||
{
|
}
|
||||||
<td></td>
|
else
|
||||||
<td></td>
|
{
|
||||||
}
|
<td></td>
|
||||||
<td><button @onclick="() => BackupClicked(role, user, isSignedUp)">Backup</button></td>
|
<td></td>
|
||||||
<td><h5>@role.Name: @role.Description (@usedSpots /@role.Spots)</h5></td>
|
}
|
||||||
|
<td><button @onclick="() => BackupClicked(role, user, isSignedUp)">Backup</button></td>
|
||||||
|
}
|
||||||
|
<td><h5>@role.Name: @role.Description (@usedSpots /@role.Spots)</h5></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@foreach (var signUp in signUps)
|
@foreach (var signUp in signUps)
|
||||||
|
@ -72,16 +77,19 @@
|
||||||
@if(signUp.SignUpType != SignUpType.SignedOff)
|
@if(signUp.SignUpType != SignUpType.SignedOff)
|
||||||
{
|
{
|
||||||
<tr>
|
<tr>
|
||||||
<td></td>
|
|
||||||
<td></td>
|
|
||||||
@{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == user.LiebUserId;}
|
@{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == user.LiebUserId;}
|
||||||
@if(isUser)
|
@if (isSignUoAllowed)
|
||||||
{
|
|
||||||
<td><button @onclick="() => SignOffClicked(role, user)">Sign Off</button></td>
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
<td></td>
|
<td></td>
|
||||||
|
<td></td>
|
||||||
|
@if(isUser)
|
||||||
|
{
|
||||||
|
<td><button @onclick="() => SignOffClicked(role, user)">Sign Off</button></td>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<td></td>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@{string signUpStatus = string.Empty;}
|
@{string signUpStatus = string.Empty;}
|
||||||
@if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}";
|
@if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}";
|
||||||
|
@ -112,9 +120,9 @@
|
||||||
<div>
|
<div>
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach (var role in Raid.Roles)
|
@foreach (var role in _raid.Roles)
|
||||||
{
|
{
|
||||||
Models.GuildWars2.Raid.RaidSignUp[] signUps = Raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
|
Models.GuildWars2.Raid.RaidSignUp[] signUps = _raid.SignUps.Where(s => s.PlannedRaidRoleId == role.PlannedRaidRoleId).ToArray();
|
||||||
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
|
int usedSpots = signUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count();
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -139,68 +147,78 @@
|
||||||
</AuthorizeView>
|
</AuthorizeView>
|
||||||
<AuthorizeView Policy="@Constants.Roles.RaidLead">
|
<AuthorizeView Policy="@Constants.Roles.RaidLead">
|
||||||
<div class="nav-item px-3">
|
<div class="nav-item px-3">
|
||||||
@{string navLink = $"raidedit/{@Raid.RaidId}";}
|
@{string navLink = $"raidedit/{_raid.RaidId}";}
|
||||||
<NavLink class="nav-link" href="@navLink">
|
<NavLink class="nav-link" href="@navLink">
|
||||||
<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)
|
||||||
|
{
|
||||||
|
<button type=button @onclick="() => RandomizeClicked()">Randomize</button>
|
||||||
|
}
|
||||||
</AuthorizeView>
|
</AuthorizeView>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
|
||||||
[Parameter]
|
[Parameter]
|
||||||
public Raid Raid { get; set; }
|
public Raid _raid { get; set; }
|
||||||
|
|
||||||
async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
||||||
{
|
{
|
||||||
if(isSignedUp)
|
if(isSignedUp)
|
||||||
{
|
{
|
||||||
await RaidService.ChangeSignUpType(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.SignedUp);
|
await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.SignedUp);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await RaidService.SignUp(Raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.SignedUp);
|
await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.SignedUp);
|
||||||
}
|
}
|
||||||
Raid = RaidService.GetRaid(Raid.RaidId);
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task BackupClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
async Task BackupClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
||||||
{
|
{
|
||||||
if(isSignedUp)
|
if(isSignedUp)
|
||||||
{
|
{
|
||||||
await RaidService.ChangeSignUpType(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Backup);
|
await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Backup);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await RaidService.SignUp(Raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Backup);
|
await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Backup);
|
||||||
}
|
}
|
||||||
Raid = RaidService.GetRaid(Raid.RaidId);
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task MaybeClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
async Task MaybeClicked(PlannedRaidRole role, LiebUser liebUser, bool isSignedUp)
|
||||||
{
|
{
|
||||||
if(isSignedUp)
|
if(isSignedUp)
|
||||||
{
|
{
|
||||||
await RaidService.ChangeSignUpType(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Maybe);
|
await RaidService.ChangeSignUpType(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId, SignUpType.Maybe);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await RaidService.SignUp(Raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Maybe);
|
await RaidService.SignUp(_raid.RaidId, liebUser.LiebUserId, liebUser.GuildWars2Accounts.FirstOrDefault().GuildWars2AccountId, role.PlannedRaidRoleId, SignUpType.Maybe);
|
||||||
}
|
}
|
||||||
Raid = RaidService.GetRaid(Raid.RaidId);
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task SignOffClicked(PlannedRaidRole role, LiebUser liebUser)
|
async Task SignOffClicked(PlannedRaidRole role, LiebUser liebUser)
|
||||||
{
|
{
|
||||||
await RaidService.SignOff(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId);
|
await RaidService.SignOff(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId);
|
||||||
Raid = RaidService.GetRaid(Raid.RaidId);
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task RandomizeClicked()
|
||||||
|
{
|
||||||
|
await RaidRandomizerService.RandomizeRaid(_raid.RaidId);
|
||||||
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task ChangeAccount(LiebUser liebUser, ChangeEventArgs e)
|
async Task ChangeAccount(LiebUser liebUser, ChangeEventArgs e)
|
||||||
{
|
{
|
||||||
int accountId = int.Parse(e.Value.ToString());
|
int accountId = int.Parse(e.Value.ToString());
|
||||||
await RaidService.ChangeAccount(Raid.RaidId, liebUser.LiebUserId, accountId);
|
await RaidService.ChangeAccount(_raid.RaidId, liebUser.LiebUserId, accountId);
|
||||||
Raid = RaidService.GetRaid(Raid.RaidId);
|
_raid = RaidService.GetRaid(_raid.RaidId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
@foreach (var raid in raids) {
|
@foreach (var raid in raids) {
|
||||||
<br />
|
<br />
|
||||||
<RaidDetails Raid=@raid />
|
<RaidDetails _raid=@raid />
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ builder.Services.AddScoped<RaidService>();
|
||||||
builder.Services.AddScoped<UserService>();
|
builder.Services.AddScoped<UserService>();
|
||||||
builder.Services.AddScoped<GuildWars2AccountService>();
|
builder.Services.AddScoped<GuildWars2AccountService>();
|
||||||
builder.Services.AddScoped<GuildWars2BuildService>();
|
builder.Services.AddScoped<GuildWars2BuildService>();
|
||||||
|
builder.Services.AddScoped<RaidRandomizerService>();
|
||||||
|
|
||||||
|
|
||||||
builder.Services.AddAuthentication(opt =>
|
builder.Services.AddAuthentication(opt =>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue