From f86451268da9b70f6cb52dc913218462abb27c5a Mon Sep 17 00:00:00 2001 From: "t.ruspekhofer" Date: Fri, 25 Feb 2022 19:10:43 +0100 Subject: [PATCH] reworked role management to work with flex roles --- Lieb/Data/RaidService.cs | 133 +++++++++++++++++------------ Lieb/Pages/Raids/RaidDetails.razor | 4 +- 2 files changed, 80 insertions(+), 57 deletions(-) diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index 0933ede..6623ba8 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -54,46 +54,6 @@ namespace Lieb.Data } } - public async Task> GetFreeRoles(int raidId) - { - using var context = _contextFactory.CreateDbContext(); - Raid? raid = await context.Raids - .Include(r => r.Roles) - .Include(r => r.SignUps) - .FirstOrDefaultAsync(r => r.RaidId == raidId); - - List freeRoles = new List(); - if (raid != null) - { - List plannedRolesIds = raid.SignUps.Select(s => s.PlannedRaidRoleId).ToList(); - Dictionary addedIds = new Dictionary(); - - foreach (RaidSignUp signUp in raid.SignUps) - { - if (signUp.SignUpType == SignUpType.SignedUp) - { - int id = signUp.PlannedRaidRoleId; - if (addedIds.ContainsKey(id)) - { - addedIds[id] += 1; - } - else - { - addedIds.Add(id, 1); - } - } - } - foreach(PlannedRaidRole role in raid.Roles) - { - if(!addedIds.ContainsKey(role.PlannedRaidRoleId) || role.Spots > addedIds[role.PlannedRaidRoleId]) - { - freeRoles.Add(role); - } - } - } - return freeRoles; - } - public async Task SignUp(int raidId, int liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType) { if (!IsSignUpAllowed(liebUserId, plannedRoleId, signUpType)) @@ -101,21 +61,31 @@ namespace Lieb.Data return; } using var context = _contextFactory.CreateDbContext(); - context.RaidSignUps.Add(new RaidSignUp() + + List signUps = context.RaidSignUps.Where(r => r.RaidId == raidId && r.LiebUserId == liebUserId).ToList(); + if (signUpType != SignUpType.Flex && signUps.Where(r => r.SignUpType != SignUpType.Flex).Any()) { - GuildWars2AccountId = guildWars2AccountId, - RaidId = raidId, - LiebUserId = liebUserId, - PlannedRaidRoleId = plannedRoleId, - SignUpType = signUpType - }); - await context.SaveChangesAsync(); + await ChangeSignUpType(raidId, liebUserId, plannedRoleId, signUpType); + } + else if (!signUps.Where(r => r.PlannedRaidRoleId == plannedRoleId).Any()) + { + context.RaidSignUps.Add(new RaidSignUp() + { + GuildWars2AccountId = guildWars2AccountId, + RaidId = raidId, + LiebUserId = liebUserId, + PlannedRaidRoleId = plannedRoleId, + SignUpType = signUpType + }); + await context.SaveChangesAsync(); + } } - public async Task SignOff(int raidId, int liebUserId) + public async Task SignOff(int raidId, int liebUserId, int plannedRoleId) { + await ChangeSignUpType(raidId, liebUserId, plannedRoleId, SignUpType.SignedOff); using var context = _contextFactory.CreateDbContext(); - List signUps = context.RaidSignUps.Where(x => x.RaidId == raidId && x.LiebUserId == liebUserId).ToList(); + List signUps = context.RaidSignUps.Where(x => x.RaidId == raidId && x.LiebUserId == liebUserId && x.SignUpType == SignUpType.Flex).ToList(); context.RaidSignUps.RemoveRange(signUps); await context.SaveChangesAsync(); } @@ -140,10 +110,25 @@ namespace Lieb.Data using var context = _contextFactory.CreateDbContext(); - RaidSignUp signUp = await context.RaidSignUps.FirstOrDefaultAsync(x => x.RaidId == raidId && x.LiebUserId == liebUserId && x.SignUpType != SignUpType.SignedOff && x.SignUpType != SignUpType.Flex); - signUp.PlannedRaidRoleId = plannedRoleId; - signUp.SignUpType = signUpType; - await context.SaveChangesAsync(); + List signUps = context.RaidSignUps.Where(x => x.RaidId == raidId && x.LiebUserId == liebUserId).ToList(); + + RaidSignUp? signUp = signUps.FirstOrDefault(x => x.SignUpType != SignUpType.Flex); + RaidSignUp? flexSignUp = signUps.FirstOrDefault(x => x.SignUpType == SignUpType.Flex && x.PlannedRaidRoleId == plannedRoleId); + + //change Flex to current role + if (flexSignUp != null && signUp != null) + { + flexSignUp.PlannedRaidRoleId = signUp.PlannedRaidRoleId; + await context.SaveChangesAsync(); + } + + //change to new role + if (signUp != null) + { + signUp.PlannedRaidRoleId = plannedRoleId; + signUp.SignUpType = signUpType; + await context.SaveChangesAsync(); + } } public bool IsSignUpAllowed(int liebUserId, int plannedRoleId, SignUpType signUpType) @@ -157,7 +142,7 @@ namespace Lieb.Data List signUps = context.RaidSignUps.Where(s => s.PlannedRaidRoleId == plannedRoleId).ToList(); - PlannedRaidRole role = context.PlannedRaidRoles + PlannedRaidRole? role = context.PlannedRaidRoles .FirstOrDefault(r => r.PlannedRaidRoleId == plannedRoleId); if (role == null) @@ -168,5 +153,43 @@ namespace Lieb.Data return signUps.Where(s => s.LiebUserId == liebUserId && s.SignUpType == SignUpType.SignedUp).Any(); } + + public async Task IsSignUpAllowed(int raidId, int liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser, List checkedRoleIds) + { + if (IsSignUpAllowed(liebUserId, plannedRoleId, signUpType)) + return true; + + if (checkedRoleIds == null) + checkedRoleIds = new List(); + checkedRoleIds.Add(plannedRoleId); + + using var context = _contextFactory.CreateDbContext(); + Raid? raid = context.Raids + .Include(r => r.Roles) + .Include(r => r.SignUps) + .FirstOrDefault(r => r.RaidId == raidId); + + if (raid == null) + { + return false; + } + + foreach (int userId in raid.SignUps.Where(s => s.PlannedRaidRoleId == plannedRoleId && s.SignUpType == SignUpType.SignedUp).Select(s => s.LiebUserId)) + { + foreach (RaidSignUp signUp in raid.SignUps.Where(s => s.LiebUserId == userId && s.SignUpType == SignUpType.Flex)) + { + if (!checkedRoleIds.Contains(signUp.PlannedRaidRoleId) + && await IsSignUpAllowed(raidId, userId, signUp.PlannedRaidRoleId, SignUpType.SignedUp, moveFlexUser, checkedRoleIds)) + { + if (moveFlexUser) + { + await ChangeSignUpType(raidId, userId, signUp.PlannedRaidRoleId, SignUpType.SignedUp); + } + return true; + } + } + } + return false; + } } } diff --git a/Lieb/Pages/Raids/RaidDetails.razor b/Lieb/Pages/Raids/RaidDetails.razor index abbe173..cbbfae8 100644 --- a/Lieb/Pages/Raids/RaidDetails.razor +++ b/Lieb/Pages/Raids/RaidDetails.razor @@ -50,7 +50,7 @@ { 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) { @@ -175,7 +175,7 @@ async Task SignOffClicked(PlannedRaidRole role, LiebUser liebUser) { - await RaidService.SignOff(Raid.RaidId, liebUser.LiebUserId); + await RaidService.SignOff(Raid.RaidId, liebUser.LiebUserId, role.PlannedRaidRoleId); Raid = RaidService.GetRaid(Raid.RaidId); }