From 2288e48308d20796bda51de48e4d10462be5ffb2 Mon Sep 17 00:00:00 2001 From: Sarah Faey Date: Sat, 10 Dec 2022 19:51:05 +0100 Subject: [PATCH] Random Messages don't show empty roles No Flex Button in random raids Reworked random sign up to keep roles after signing off --- DiscordBot/Messages/RaidMessage.cs | 9 +- Lieb/Controllers/DiscordBotController.cs | 25 ++- Lieb/Data/DiscordService.cs | 8 +- Lieb/Data/RaidService.cs | 177 ++++++++++++------ Lieb/Data/UserService.cs | 3 +- Lieb/Pages/Raids/RaidOverview/RaidRoles.razor | 7 +- SharedClasses/SharedModels/ApiRaid.cs | 2 + 7 files changed, 161 insertions(+), 70 deletions(-) diff --git a/DiscordBot/Messages/RaidMessage.cs b/DiscordBot/Messages/RaidMessage.cs index 3a63204..764b5b1 100644 --- a/DiscordBot/Messages/RaidMessage.cs +++ b/DiscordBot/Messages/RaidMessage.cs @@ -36,9 +36,12 @@ namespace DiscordBot.Messages var builder = new ComponentBuilder() .WithButton("SignUp", $"{Constants.ComponentIds.SIGN_UP_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Success) .WithButton("Maybe", $"{Constants.ComponentIds.MAYBE_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Secondary) - .WithButton("Backup", $"{Constants.ComponentIds.BACKUP_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Secondary) - .WithButton("Flex", $"{Constants.ComponentIds.FLEX_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Secondary) - .WithButton("SignOff", $"{Constants.ComponentIds.SIGN_OFF_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Danger); + .WithButton("Backup", $"{Constants.ComponentIds.BACKUP_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Secondary); + if(raid.AllowFlexRole) + { + builder.WithButton("Flex", $"{Constants.ComponentIds.FLEX_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Secondary); + } + builder.WithButton("SignOff", $"{Constants.ComponentIds.SIGN_OFF_BUTTON}-{raid.RaidId.ToString()}", ButtonStyle.Danger); MessageComponent components = builder.Build(); Embed raidMessage = CreateRaidMessage(raid); diff --git a/Lieb/Controllers/DiscordBotController.cs b/Lieb/Controllers/DiscordBotController.cs index a6c3287..c627fc3 100644 --- a/Lieb/Controllers/DiscordBotController.cs +++ b/Lieb/Controllers/DiscordBotController.cs @@ -73,14 +73,27 @@ namespace Lieb.Controllers Raid raid = _raidService.GetRaid(raidId); List apiRoles = new List(); - foreach(RaidRole role in raid.Roles) + if(raid.RaidType == RaidType.Planned) { + foreach(RaidRole role in raid.Roles) + { + apiRoles.Add(new ApiRole(){ + Name = role.Name, + Description = role.Description, + IsSignUpAllowed = _raidService.IsRoleSignUpAllowed(raidId, userId, role.RaidRoleId, SignUpType.SignedUp, false), + roleId = role.RaidRoleId + }); + } + } + else + { + RaidRole role = raid.Roles.First(r => r.IsRandomSignUpRole); apiRoles.Add(new ApiRole(){ - Name = role.Name, - Description = role.Description, - IsSignUpAllowed = _raidService.IsRoleSignUpAllowed(userId, role.RaidRoleId, SignUpType.SignedUp), - roleId = role.RaidRoleId - }); + Name = role.Name, + Description = role.Description, + IsSignUpAllowed = _raidService.IsRoleSignUpAllowed(raidId, userId, role.RaidRoleId, SignUpType.SignedUp, false), + roleId = role.RaidRoleId + }); } return apiRoles; } diff --git a/Lieb/Data/DiscordService.cs b/Lieb/Data/DiscordService.cs index 077098e..96201fe 100644 --- a/Lieb/Data/DiscordService.cs +++ b/Lieb/Data/DiscordService.cs @@ -314,7 +314,8 @@ namespace Lieb.Data VoiceChat = raid.VoiceChat, StartTimeUTC = raid.StartTimeUTC, EndTimeUTC = raid.EndTimeUTC, - RaidId = raid.RaidId + RaidId = raid.RaidId, + AllowFlexRole = raid.RaidType == RaidType.Planned }; apiRaid.DisocrdMessages = ConvertMessages(raid.DiscordRaidMessages); apiRaid.Roles = new List(); @@ -353,7 +354,10 @@ namespace Lieb.Data } } } - apiRaid.Roles.Add(apiRole); + if(raid.RaidType == RaidType.Planned || role.IsRandomSignUpRole || apiRole.Users.Count > 0) + { + apiRaid.Roles.Add(apiRole); + } } return apiRaid; } diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index 8d7061a..e7c6bb9 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -1,4 +1,5 @@ using Lieb.Models; +using Lieb.Models.GuildWars2; using Lieb.Models.GuildWars2.Raid; using Microsoft.EntityFrameworkCore; @@ -107,11 +108,22 @@ namespace Lieb.Data public async Task SignUp(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType, ulong signedUpByUserId = 0) { + 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; + if(raid.RaidType != RaidType.Planned) + { + return await SignUpRandom(raidId, liebUserId, guildWars2AccountId, signUpType, signedUpByUserId); + } + if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true)) { return false; } - using var context = _contextFactory.CreateDbContext(); + LiebUser user = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId); if(user == null) return false; @@ -119,7 +131,7 @@ namespace Lieb.Data 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()) { - await ChangeSignUpType(raidId, liebUserId, plannedRoleId, signUpType, false); + await ChangeSignUpType(raidId, liebUserId, plannedRoleId, signUpType, raid.RaidType, false); await ChangeAccount(raidId, liebUserId, guildWars2AccountId, false); } else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any()) @@ -139,6 +151,46 @@ namespace Lieb.Data return true; } + public async Task SignUpRandom(int raidId, ulong liebUserId, int guildWars2AccountId, SignUpType signUpType, ulong signedUpByUserId = 0) + { + using var context = _contextFactory.CreateDbContext(); + if(signUpType == SignUpType.Flex) return false; + + Raid? raid = context.Raids + .Include(r => r.Roles) + .Include(r => r.SignUps) + .FirstOrDefault(r => r.RaidId == raidId); + + GuildWars2Account account = context.GuildWars2Accounts + .Include(a => a.EquippedBuilds) + .ThenInclude(b => b.GuildWars2Build) + .FirstOrDefault(a => a.GuildWars2AccountId == guildWars2AccountId); + + if(account == null) return false; + if(!account.EquippedBuilds.Where(b => b.GuildWars2Build.UseInRandomRaid).Any()) return false; + + if(IsRandomRoleSignUpAllowed(raid, liebUserId, signUpType)) + { + RaidSignUp signUp; + if(raid.SignUps.Where(s => s.LiebUserId == liebUserId).Any()) + { + signUp = raid.SignUps.First(s => s.LiebUserId == liebUserId); + signUp.SignUpType = signUpType; + } + else + { + RaidRole randomRole = raid.Roles.First(r => r.IsRandomSignUpRole); + signUp = new RaidSignUp(raid.RaidId, liebUserId, guildWars2AccountId, randomRole.RaidRoleId , signUpType); + context.RaidSignUps.Add(signUp); + } + await context.SaveChangesAsync(); + await LogSignUp(signUp, context.LiebUsers.FirstOrDefault(u => u.Id == liebUserId)?.Name, signedUpByUserId); + await _discordService.PostRaidMessage(raidId); + return true; + } + return false; + } + public async Task SignUpExternalUser(int raidId, string userName, int plannedRoleId, SignUpType signUpType, ulong signedUpByUserId) { if (!IsRoleSignUpAllowed(raidId, ulong.MaxValue, plannedRoleId, signUpType, true)) @@ -168,14 +220,6 @@ namespace Lieb.Data if (signUp != null) { signUp.SignUpType = SignUpType.SignedOff; - - //change and delete Role for Random Raids - Raid? raid = context.Raids.Include(r => r.Roles).FirstOrDefault(r => r.RaidId == raidId); - if(raid != null && raid.RaidType != RaidType.Planned && !signUp.RaidRole.IsRandomSignUpRole) - { - context.RaidRoles.Remove(signUp.RaidRole); - signUp.RaidRole = raid.Roles.FirstOrDefault(r => r.IsRandomSignUpRole, CreateRandomSignUpRole(raid.RaidType)); - } await LogSignUp(signUp, signUp.LiebUser.Name, signedOffByUserId); } await context.SaveChangesAsync(); @@ -214,7 +258,7 @@ namespace Lieb.Data } } - public async Task ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool postChanges = true) + public async Task ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, RaidType raidType, bool postChanges = true) { if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true)) { @@ -237,7 +281,10 @@ namespace Lieb.Data //change to new role if (signUp != null) { - signUp.RaidRoleId = plannedRoleId; + if(raidType == RaidType.Planned) + { + signUp.RaidRoleId = plannedRoleId; + } signUp.SignUpType = signUpType; if(signUp.IsExternalUser) { @@ -255,7 +302,35 @@ namespace Lieb.Data } } - public bool IsRoleSignUpAllowed(ulong liebUserId, int plannedRoleId, SignUpType signUpType) + public bool IsRoleSignUpAllowed(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser) + { + 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; + + if (raid.RaidType == RaidType.Planned) + { + if (raid.MoveFlexUsers) + { + //moveFlexUser parameter is for only checking or really moving + return IsRoleSignUpAllowedMoveFlex(raid, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List()).Result; + } + else + { + return IsRoleSignUpAllowedBasicCheck(liebUserId, plannedRoleId, signUpType); + } + } + else + { + return IsRandomRoleSignUpAllowed(raid, liebUserId, signUpType, plannedRoleId); + } + } + + private bool IsRoleSignUpAllowedBasicCheck(ulong liebUserId, int plannedRoleId, SignUpType signUpType) { if(signUpType == SignUpType.Backup || signUpType == SignUpType.Flex || signUpType == SignUpType.SignedOff) { @@ -278,47 +353,9 @@ namespace Lieb.Data return signUps.Where(s => s.LiebUserId == liebUserId && s.SignUpType == SignUpType.SignedUp).Any(); } - public bool IsRoleSignUpAllowed(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser) + private async Task IsRoleSignUpAllowedMoveFlex(Raid raid, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser, List checkedRoleIds) { - 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; - - if (raid.RaidType == RaidType.Planned) - { - if (raid.MoveFlexUsers) - { - return IsRoleSignUpAllowed(raid, liebUserId, plannedRoleId, signUpType, moveFlexUser, new List()).Result; - } - else - { - return IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType); - } - } - else - { - RaidRole? role = context.RaidRoles - .AsNoTracking() - .FirstOrDefault(r => r.RaidRoleId == plannedRoleId); - if(role == null) return false; - if (role.IsRandomSignUpRole) - { - // new sign up is available if there are free spots and the user is not signed up or still in the random role - RaidSignUp? signUp = raid.SignUps.FirstOrDefault(s => s.LiebUserId == liebUserId); - return raid.SignUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count() < role.Spots - && (signUp == null || signUp.RaidRoleId == plannedRoleId || signUp.SignUpType == SignUpType.SignedOff); - } - return raid.SignUps.Where(s => s.LiebUserId == liebUserId && s.RaidRoleId == plannedRoleId).Any(); - } - } - - private async Task IsRoleSignUpAllowed(Raid raid, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser, List checkedRoleIds) - { - if (IsRoleSignUpAllowed(liebUserId, plannedRoleId, signUpType)) + if (IsRoleSignUpAllowedBasicCheck(liebUserId, plannedRoleId, signUpType)) return true; if (checkedRoleIds == null) @@ -335,11 +372,11 @@ namespace Lieb.Data foreach (RaidSignUp signUp in raid.SignUps.Where(s => s.LiebUserId == userId && s.SignUpType == SignUpType.Flex)) { if (!checkedRoleIds.Contains(signUp.RaidRoleId) - && await IsRoleSignUpAllowed(raid, userId, signUp.RaidRoleId, SignUpType.SignedUp, moveFlexUser, checkedRoleIds)) + && await IsRoleSignUpAllowedMoveFlex(raid, userId, signUp.RaidRoleId, SignUpType.SignedUp, moveFlexUser, checkedRoleIds)) { if (moveFlexUser) { - await ChangeSignUpType(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, false); + await ChangeSignUpType(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, raid.RaidType, false); } return true; } @@ -348,6 +385,33 @@ namespace Lieb.Data return false; } + public bool IsRandomRoleSignUpAllowed(Raid raid, ulong liebUserId, SignUpType signUpType, int plannedRoleId = 0) + { + RaidRole randomRole = raid.Roles.First(r => r.IsRandomSignUpRole); + int signedUpUsers = raid.SignUps.Where(s => s.SignUpType == SignUpType.SignedUp).Count(); + int randomSpots = randomRole.Spots; + + if(raid.SignUps.Where(s => s.LiebUserId == liebUserId).Any()) + { + int userRoleId = raid.SignUps.First(s => s.LiebUserId == liebUserId).RaidRoleId; + if(plannedRoleId != 0 && plannedRoleId != userRoleId) return false; + } + else + { + if(plannedRoleId != 0 && plannedRoleId != randomRole.RaidRoleId) return false; + } + + if(randomSpots - signedUpUsers > 0) return true; + + if(signUpType == SignUpType.Backup) return true; + + if(raid.SignUps.Where(s => s.LiebUserId == liebUserId).Any()) + { + return raid.SignUps.First(s => s.LiebUserId == liebUserId).SignUpType == SignUpType.SignedUp; + } + return false; + } + public bool IsRaidSignUpAllowed(ulong liebUserId, int raidId, out string errorMessage, bool ignoreRole = false) { errorMessage = string.Empty; @@ -365,6 +429,7 @@ namespace Lieb.Data .ThenInclude(a => a.LiebRole) .Include(u => u.GuildWars2Accounts) .ThenInclude(a => a.EquippedBuilds) + .ThenInclude(b => b.GuildWars2Build) .AsNoTracking() .FirstOrDefault(r => r.Id == liebUserId); if (user == null) @@ -379,7 +444,7 @@ namespace Lieb.Data return false; } - if (raid.RaidType != RaidType.Planned && !user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).Any()) + if (raid.RaidType != RaidType.Planned && !user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Where(b => b.GuildWars2Build.UseInRandomRaid).Count() > 0).Any()) { errorMessage = "No equipped Guild Wars 2 build found."; return false; diff --git a/Lieb/Data/UserService.cs b/Lieb/Data/UserService.cs index 3ad69c3..06b0760 100644 --- a/Lieb/Data/UserService.cs +++ b/Lieb/Data/UserService.cs @@ -55,6 +55,7 @@ namespace Lieb.Data return context.LiebUsers .Include(u => u.GuildWars2Accounts) .ThenInclude(a => a.EquippedBuilds) + .ThenInclude(b => b.GuildWars2Build) .FirstOrDefault(u => u.Id == userId); } else @@ -281,7 +282,7 @@ namespace Lieb.Data } else { - return user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Count > 0).ToList(); + return user.GuildWars2Accounts.Where(a => a.EquippedBuilds.Where(b => b.GuildWars2Build.UseInRandomRaid).Count() > 0).ToList(); } } diff --git a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor index c655a68..3f974af 100644 --- a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor +++ b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor @@ -36,7 +36,10 @@ } @foreach (ExpandableRole role in _expandableRoles.OrderBy(r => r.Role.RaidRoleId)) { - + //leere random Rolle verstecken, wenn sie nicht dem angemeldeten Benutzer gehört + bool roleHidden = _raid.RaidType != RaidType.Planned && !role.Role.IsRandomSignUpRole + && !_raid.SignUps.Where(s => s.RaidRoleId == role.Role.RaidRoleId && (s.SignUpType != SignUpType.SignedOff || s.LiebUserId == _liebUserId)).Any(); + @{ @if(@role.IsRowExpanded) @@ -135,7 +138,7 @@ { if(_raid.SignUps.Where(s => s.LiebUserId == _liebUserId).Any() && signUpType != SignUpType.Flex) { - await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType); + await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType, _raid.RaidType); } else { diff --git a/SharedClasses/SharedModels/ApiRaid.cs b/SharedClasses/SharedModels/ApiRaid.cs index 5938f5a..292dda7 100644 --- a/SharedClasses/SharedModels/ApiRaid.cs +++ b/SharedClasses/SharedModels/ApiRaid.cs @@ -15,6 +15,8 @@ namespace SharedClasses.SharedModels public int RaidId { get; set; } + public bool AllowFlexRole {get; set;} + public DateTimeOffset StartTimeUTC { get; set; } = DateTime.Now; public DateTimeOffset EndTimeUTC { get; set; }