diff --git a/DiscordBot/CommandHandlers/ButtonHandler.cs b/DiscordBot/CommandHandlers/ButtonHandler.cs index 5dcfaea..3ebcf7d 100644 --- a/DiscordBot/CommandHandlers/ButtonHandler.cs +++ b/DiscordBot/CommandHandlers/ButtonHandler.cs @@ -25,17 +25,17 @@ namespace DiscordBot.CommandHandlers switch(ids[0]) { case Constants.ComponentIds.SIGN_UP_BUTTON: + await SignUpClicked(component, false, SharedConstants.SIGNED_UP); + break; case Constants.ComponentIds.MAYBE_BUTTON: - RaidMessage.ButtonParameters signUpParameters = RaidMessage.ParseButtonId(component.Data.CustomId); - await _handlerFunctions.SelectRole(component, signUpParameters.RaidId, signUpParameters.ButtonType, false, component.User.Id, 0); + await SignUpClicked(component, false, SharedConstants.MAYBE); break; case Constants.ComponentIds.BACKUP_BUTTON: - RaidMessage.ButtonParameters backupParameters = RaidMessage.ParseButtonId(component.Data.CustomId); - await _handlerFunctions.SelectRole(component, backupParameters.RaidId, backupParameters.ButtonType, true, component.User.Id, 0); + await SignUpClicked(component, true, SharedConstants.BACKUP); break; case Constants.ComponentIds.FLEX_BUTTON: RaidMessage.ButtonParameters flexParameters = RaidMessage.ParseButtonId(component.Data.CustomId); - if(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id)) + if(!string.IsNullOrEmpty(await _httpService.IsUserSignedUp(flexParameters.RaidId, component.User.Id))) { await _handlerFunctions.SelectRole(component, flexParameters.RaidId, flexParameters.ButtonType, true, component.User.Id, 0); } @@ -66,5 +66,36 @@ namespace DiscordBot.CommandHandlers break; } } + + public async Task SignUpClicked(SocketMessageComponent component, bool allRoles, string signUpType) + { + RaidMessage.ButtonParameters parameters = RaidMessage.ParseButtonId(component.Data.CustomId); + string currentSignUp = await _httpService.IsUserSignedUp(parameters.RaidId, component.User.Id); + if(string.IsNullOrEmpty(currentSignUp) || currentSignUp == signUpType) + { + await _handlerFunctions.SelectRole(component, parameters.RaidId, parameters.ButtonType, allRoles, component.User.Id, 0); + } + else + { + ApiSignUp signUp = new ApiSignUp() + { + raidId = parameters.RaidId, + userId = component.User.Id + }; + switch(signUpType) + { + case SharedConstants.SIGNED_UP: + await _httpService.ChangeToSignUp(signUp); + break; + case SharedConstants.MAYBE: + await _httpService.ChangeToMaybe(signUp); + break; + case SharedConstants.BACKUP: + await _httpService.ChangeToBackup(signUp); + break; + } + await component.RespondAsync("Sign up type changed.", ephemeral: true); + } + } } } \ No newline at end of file diff --git a/DiscordBot/Services/HttpService.cs b/DiscordBot/Services/HttpService.cs index b5d2007..d931350 100644 --- a/DiscordBot/Services/HttpService.cs +++ b/DiscordBot/Services/HttpService.cs @@ -67,7 +67,7 @@ namespace DiscordBot.Services return new Tuple(true, string.Empty); } - public async Task IsUserSignedUp(int raidId, ulong userId) + public async Task IsUserSignedUp(int raidId, ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); @@ -75,12 +75,9 @@ namespace DiscordBot.Services if (httpResponseMessage.IsSuccessStatusCode) { - using var contentStream = - await httpResponseMessage.Content.ReadAsStreamAsync(); - - return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); + return await httpResponseMessage.Content.ReadAsStringAsync(); } - return false; + return string.Empty; } public async Task> GetRoles(int raidId, ulong userId) @@ -124,6 +121,21 @@ namespace DiscordBot.Services await SendSignUp(signUp, "DiscordBot/SignOff"); } + public async Task ChangeToSignUp(ApiSignUp signUp) + { + return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToSignUp"); + } + + public async Task ChangeToMaybe(ApiSignUp signUp) + { + return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToMaybe"); + } + + public async Task ChangeToBackup(ApiSignUp signUp) + { + return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToBackup"); + } + private async Task SendSignUp(ApiSignUp signUp, string requestUri) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); diff --git a/Lieb/Controllers/DiscordBotController.cs b/Lieb/Controllers/DiscordBotController.cs index c627fc3..434c9ef 100644 --- a/Lieb/Controllers/DiscordBotController.cs +++ b/Lieb/Controllers/DiscordBotController.cs @@ -49,10 +49,22 @@ namespace Lieb.Controllers [HttpGet] [Route("[action]/{raidId}/{userId}")] - public bool IsUserSignedUp(int raidId, ulong userId) + public string IsUserSignedUp(int raidId, ulong userId) { Raid raid = _raidService.GetRaid(raidId); - return raid.SignUps.Where(s => s.LiebUserId == userId && s.SignUpType != SignUpType.Flex && s.SignUpType != SignUpType.SignedOff).Any(); + RaidSignUp signUp = raid.SignUps.FirstOrDefault(s => s.LiebUserId == userId && s.SignUpType != SignUpType.Flex); + if(signUp == null) return string.Empty; + + switch(signUp.SignUpType) + { + case SignUpType.SignedUp: + return SharedConstants.SIGNED_UP; + case SignUpType.Maybe: + return SharedConstants.MAYBE; + case SignUpType.Backup: + return SharedConstants.BACKUP; + } + return string.Empty; } [HttpGet] @@ -173,6 +185,30 @@ namespace Lieb.Controllers return true; } + [HttpPost] + [Route("[action]")] + public async Task ChangeSignUpTypeToSignUp(ApiSignUp signUp) + { + await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.SignedUp, true); + return true; + } + + [HttpPost] + [Route("[action]")] + public async Task ChangeSignUpTypeToMaybe(ApiSignUp signUp) + { + await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.Maybe, true); + return true; + } + + [HttpPost] + [Route("[action]")] + public async Task ChangeSignUpTypeToBackup(ApiSignUp signUp) + { + await _raidService.ChangeSignUpType(signUp.raidId, signUp.userId, SignUpType.Backup, true); + return true; + } + [HttpPost] [Route("[action]")] public async Task CreateAccount(ApiRaid.Role.User user) diff --git a/Lieb/Data/DbInitializer.cs b/Lieb/Data/DbInitializer.cs index e108d00..c4d076f 100644 --- a/Lieb/Data/DbInitializer.cs +++ b/Lieb/Data/DbInitializer.cs @@ -151,7 +151,8 @@ namespace Lieb.Data Roles = new[] { new RaidRole(){ Description = "WupWup", Name = "Ups", - Spots = 10 + Spots = 10, + IsRandomSignUpRole = true } } }; diff --git a/Lieb/Data/RaidService.cs b/Lieb/Data/RaidService.cs index d52d1a3..804b4f1 100644 --- a/Lieb/Data/RaidService.cs +++ b/Lieb/Data/RaidService.cs @@ -114,24 +114,41 @@ namespace Lieb.Data .Include(r => r.SignUps) .FirstOrDefault(r => r.RaidId == raidId); if(raid == null) return false; - if(raid.RaidType != RaidType.Planned) + + LiebUser user = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId); + if(user == null) return false; + + bool result = false; + if(raid.RaidType == RaidType.Planned) { - return await SignUpRandom(raidId, liebUserId, guildWars2AccountId, signUpType, signedUpByUserId); + result = await SignUpPlanned(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType, user.Name, signedUpByUserId); + } + else + { + result = await SignUpRandom(raidId, liebUserId, guildWars2AccountId, signUpType, user.Name, signedUpByUserId); } + user.LastSignUpAt = DateTime.UtcNow; + await context.SaveChangesAsync(); + await _discordService.PostRaidMessage(raidId); + return result; + } + + private async Task SignUpPlanned(int raidId, ulong liebUserId, int guildWars2AccountId, int plannedRoleId, SignUpType signUpType, string userName, ulong signedUpByUserId = 0) + { if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true)) { return false; } - LiebUser user = context.LiebUsers.FirstOrDefault(l => l.Id == liebUserId); + using var context = _contextFactory.CreateDbContext(); - if(user == null) return false; 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, raid.RaidType, false); + await ChangeSignUpType(raidId, liebUserId, signUpType, false); + await ChangeRole(raidId, liebUserId, plannedRoleId, signUpType, RaidType.Planned, false); await ChangeAccount(raidId, liebUserId, guildWars2AccountId, false); } else if (!signUps.Where(r => r.RaidRoleId == plannedRoleId).Any()) @@ -139,19 +156,16 @@ namespace Lieb.Data RaidSignUp signUp = new RaidSignUp(raidId, liebUserId, guildWars2AccountId, plannedRoleId, signUpType); context.RaidSignUps.Add(signUp); await context.SaveChangesAsync(); - await LogSignUp(signUp, user.Name, signedUpByUserId); + await LogSignUp(signUp, userName, signedUpByUserId); } else { return false; } - user.LastSignUpAt = DateTime.UtcNow; - await context.SaveChangesAsync(); - await _discordService.PostRaidMessage(raidId); return true; } - public async Task SignUpRandom(int raidId, ulong liebUserId, int guildWars2AccountId, SignUpType signUpType, ulong signedUpByUserId = 0) + private async Task SignUpRandom(int raidId, ulong liebUserId, int guildWars2AccountId, SignUpType signUpType, string userName, ulong signedUpByUserId = 0) { using var context = _contextFactory.CreateDbContext(); if(signUpType == SignUpType.Flex) return false; @@ -184,8 +198,7 @@ namespace Lieb.Data context.RaidSignUps.Add(signUp); } await context.SaveChangesAsync(); - await LogSignUp(signUp, context.LiebUsers.FirstOrDefault(u => u.Id == liebUserId)?.Name, signedUpByUserId); - await _discordService.PostRaidMessage(raidId); + await LogSignUp(signUp, userName, signedUpByUserId); return true; } return false; @@ -258,9 +271,9 @@ namespace Lieb.Data } } - public async Task ChangeSignUpType(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, RaidType raidType, bool postChanges = true) + public async Task ChangeRole(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, RaidType raidType, bool postChanges = true) { - if (!IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true)) + if (raidType != RaidType.Planned || !IsRoleSignUpAllowed(raidId, liebUserId, plannedRoleId, signUpType, true)) { return; } @@ -281,16 +294,8 @@ namespace Lieb.Data //change to new role if (signUp != null) { - if(raidType == RaidType.Planned) - { - signUp.RaidRoleId = plannedRoleId; - } - signUp.SignUpType = signUpType; - if(signUp.IsExternalUser) - { - await LogSignUp(signUp, signUp.ExternalUserName); - } - else + signUp.RaidRoleId = plannedRoleId; + if(!signUp.IsExternalUser) { await LogSignUp(signUp, signUp.LiebUser.Name); } @@ -302,6 +307,27 @@ namespace Lieb.Data } } + public async Task ChangeSignUpType(int raidId, ulong liebUserId, SignUpType signUpType, bool postChanges = true) + { + using var context = _contextFactory.CreateDbContext(); + + RaidSignUp? signUp = context.RaidSignUps + .Include(s => s.LiebUser) + .FirstOrDefault(x => x.RaidId == raidId && x.LiebUserId == liebUserId + && x.SignUpType != SignUpType.Flex); + + if (signUp != null && IsRoleSignUpAllowed(raidId, liebUserId, signUp.RaidRoleId, signUpType, true)) + { + signUp.SignUpType = signUpType; + await LogSignUp(signUp, signUp.LiebUser.Name); + context.SaveChanges(); + } + if(postChanges) + { + await _discordService.PostRaidMessage(raidId); + } + } + public bool IsRoleSignUpAllowed(int raidId, ulong liebUserId, int plannedRoleId, SignUpType signUpType, bool moveFlexUser) { using var context = _contextFactory.CreateDbContext(); @@ -376,7 +402,7 @@ namespace Lieb.Data { if (moveFlexUser) { - await ChangeSignUpType(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, raid.RaidType, false); + await ChangeRole(raid.RaidId, userId, signUp.RaidRoleId, SignUpType.SignedUp, raid.RaidType, false); } return true; } diff --git a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor index 3f974af..4ef669c 100644 --- a/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor +++ b/Lieb/Pages/Raids/RaidOverview/RaidRoles.razor @@ -138,7 +138,11 @@ { if(_raid.SignUps.Where(s => s.LiebUserId == _liebUserId).Any() && signUpType != SignUpType.Flex) { - await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType, _raid.RaidType); + if(_raid.RaidType == RaidType.Planned) + { + await RaidService.ChangeRole(_raid.RaidId, _liebUserId, role.RaidRoleId, signUpType, _raid.RaidType, false); + } + await RaidService.ChangeSignUpType(_raid.RaidId, _liebUserId, signUpType); } else { diff --git a/SharedClasses/SharedModels/SharedConstants.cs b/SharedClasses/SharedModels/SharedConstants.cs index 15c3b90..110ceed 100644 --- a/SharedClasses/SharedModels/SharedConstants.cs +++ b/SharedClasses/SharedModels/SharedConstants.cs @@ -2,6 +2,11 @@ namespace SharedClasses.SharedModels { public class SharedConstants { + public const string SIGNED_UP = "signedUp"; + public const string MAYBE = "maybe"; + public const string BACKUP = "backup"; + public const string FLEX = "flex"; + public class SlashCommands { public const string RAID = "raid";