diff --git a/Lieb/Data/RaidRandomizerService.cs b/Lieb/Data/RaidRandomizerService.cs new file mode 100644 index 0000000..cafcdb5 --- /dev/null +++ b/Lieb/Data/RaidRandomizerService.cs @@ -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 _contextFactory; + + public RaidRandomizerService(IDbContextFactory 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 possibleClasses = new HashSet(); + 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 possibleEliteSpecs = new HashSet(); + 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 rolesToDelete = new List(); + 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); + } + + } +} diff --git a/Lieb/Models/GuildWars2/Raid/Raid.cs b/Lieb/Models/GuildWars2/Raid/Raid.cs index 523a6cc..97d8a71 100644 --- a/Lieb/Models/GuildWars2/Raid/Raid.cs +++ b/Lieb/Models/GuildWars2/Raid/Raid.cs @@ -46,6 +46,8 @@ namespace Lieb.Models.GuildWars2.Raid [Required] public RaidType RaidType { get; set; } + public bool IsRandomized { get; set; } = false; + public int Frequency { get; set; } public string RequiredRole { get; set; } = String.Empty; diff --git a/Lieb/Pages/Raids/RaidDetails.razor b/Lieb/Pages/Raids/RaidDetails.razor index f900b7a..5ad5a10 100644 --- a/Lieb/Pages/Raids/RaidDetails.razor +++ b/Lieb/Pages/Raids/RaidDetails.razor @@ -4,67 +4,72 @@ @using Lieb.Models.GuildWars2.Raid @inject UserService UserService @inject RaidService RaidService +@inject RaidRandomizerService RaidRandomizerService -
@Raid.Title
+
@_raid.Title
-
@Raid.Description
+
@_raid.Description
Date
-

@Raid.Date.ToLongDateString()

+

@_raid.Date.ToLongDateString()

Time
-

from: @Raid.StartTime.LocalDateTime.ToShortTimeString() to: @Raid.EndTime.LocalDateTime.ToShortTimeString()

+

from: @_raid.StartTime.LocalDateTime.ToShortTimeString() to: @_raid.EndTime.LocalDateTime.ToShortTimeString()

Organizer
-

@Raid.Organizer

+

@_raid.Organizer

Guild
-

@Raid.Guild

+

@_raid.Guild

Voice chat
-

@Raid.VoiceChat

+

@_raid.VoiceChat

@{ - ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); - LiebUser user = UserService.GetLiebUserSmall(discordId); - RaidSignUp userRole = Raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault(); - bool isSignedUp = userRole != null; + ulong discordId = ulong.Parse(@context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value); + LiebUser user = UserService.GetLiebUser(discordId); + RaidSignUp userRole = _raid.SignUps.Where(s => s.LiebUserId == user.LiebUserId).FirstOrDefault(); + 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); - @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(); - - @if (@usedSpots < @role.Spots) - { - - - } - else - { - - - } - - - + + @if (isSignUoAllowed) + { + @if (@usedSpots < @role.Spots) + { + + + } + else + { + + + } + + } + @foreach (var signUp in signUps) @@ -72,16 +77,19 @@ @if(signUp.SignUpType != SignUpType.SignedOff) { - - @{bool isUser = isSignedUp && userRole.PlannedRaidRole.PlannedRaidRoleId == role.PlannedRaidRoleId && signUp.LiebUserId == user.LiebUserId;} - @if(isUser) - { - - } - else + @if (isSignUoAllowed) { + + @if(isUser) + { + + } + else + { + + } } @{string signUpStatus = string.Empty;} @if (signUp.SignUpType != SignUpType.SignedUp) signUpStatus = $" - {signUp.SignUpType}"; @@ -112,9 +120,9 @@
@role.Name: @role.Description (@usedSpots /@role.Spots)
@role.Name: @role.Description (@usedSpots /@role.Spots)
- @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(); @@ -139,68 +147,78 @@ + @if(_raid.RaidType != RaidType.Planned && !_raid.IsRandomized) + { + + } @code { [Parameter] - public Raid Raid { get; set; } + public Raid _raid { get; set; } async Task SignUpClicked(PlannedRaidRole role, LiebUser liebUser, bool 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 { - 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) { 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 { - 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) { 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 { - 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) { - await RaidService.SignOff(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId); - Raid = RaidService.GetRaid(Raid.RaidId); + await RaidService.SignOff(_raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId); + _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) { int accountId = int.Parse(e.Value.ToString()); - await RaidService.ChangeAccount(Raid.RaidId, liebUser.LiebUserId, accountId); - Raid = RaidService.GetRaid(Raid.RaidId); + await RaidService.ChangeAccount(_raid.RaidId, liebUser.LiebUserId, accountId); + _raid = RaidService.GetRaid(_raid.RaidId); } } diff --git a/Lieb/Pages/Raids/RaidOverview.razor b/Lieb/Pages/Raids/RaidOverview.razor index ef84a45..18226f0 100644 --- a/Lieb/Pages/Raids/RaidOverview.razor +++ b/Lieb/Pages/Raids/RaidOverview.razor @@ -17,7 +17,7 @@ @foreach (var raid in raids) {
- + } diff --git a/Lieb/Program.cs b/Lieb/Program.cs index e6a1e45..0e09fc0 100644 --- a/Lieb/Program.cs +++ b/Lieb/Program.cs @@ -24,6 +24,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddAuthentication(opt =>