From e334e027681f0283c6d15f61f8f68942434c9f80 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Thu, 24 Nov 2022 10:28:42 +0100 Subject: [PATCH] [Raid] Add date parameter in start command --- Cocotte/Modules/Raids/RaidFormatter.cs | 6 +-- Cocotte/Modules/Raids/RaidModule.cs | 64 ++++++++++++++++++++------ Cocotte/Modules/Raids/RosterPlayer.cs | 6 +-- Cocotte/Utils/DateTimeUtils.cs | 16 +++++++ 4 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 Cocotte/Utils/DateTimeUtils.cs diff --git a/Cocotte/Modules/Raids/RaidFormatter.cs b/Cocotte/Modules/Raids/RaidFormatter.cs index 93ebb68..455e231 100644 --- a/Cocotte/Modules/Raids/RaidFormatter.cs +++ b/Cocotte/Modules/Raids/RaidFormatter.cs @@ -27,7 +27,7 @@ public class RaidFormatter _ => $"{fc/1000}k" }; - public string FormatRosterPlayer(RosterPlayer player) => player.Substitue switch + public string FormatRosterPlayer(RosterPlayer player) => player.Substitute switch { false => $"{RoleToEmote(player.Role)} {player.Name} ({FcFormat(player.Fc)} FC)", true => $"*{RoleToEmote(player.Role)} {player.Name} ({FcFormat(player.Fc)} FC)*" @@ -38,8 +38,8 @@ public class RaidFormatter EmbedFieldBuilder RosterEmbed(int rosterNumber, IEnumerable players) { var rosterPlayers = players.ToList(); - var nonSubstitute = rosterPlayers.Where(p => !p.Substitue); - var substitute = rosterPlayers.Where(p => p.Substitue); + var nonSubstitute = rosterPlayers.Where(p => !p.Substitute); + var substitute = rosterPlayers.Where(p => p.Substitute); var separatorLength = Math.Max(nonSubstitute.Select(p => p.Name.Length).Max(), substitute.Select(p => p.Name.Length).Max()); separatorLength = (int) ((separatorLength + 13) * 0.49); // Don't ask why, it just works diff --git a/Cocotte/Modules/Raids/RaidModule.cs b/Cocotte/Modules/Raids/RaidModule.cs index c235403..39622b4 100644 --- a/Cocotte/Modules/Raids/RaidModule.cs +++ b/Cocotte/Modules/Raids/RaidModule.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using Cocotte.Options; using Cocotte.Utils; using Discord; using Discord.Interactions; @@ -26,8 +25,19 @@ public class RaidModule : InteractionModuleBase [EnabledInDm(false)] [SlashCommand("start", "Start a raid formation")] - public async Task Start() + public async Task Start(DayOfWeek day, string time) { + // Check if time is valid + if (!TimeOnly.TryParse(time, out var timeOnly)) + { + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.ErrorEmbed("Invalid time, try using format 'hh:mm'").Build() + ); + + return; + } + // Raids are identified using their original message id await RespondAsync("`Creating a new raid...`"); @@ -36,14 +46,21 @@ public class RaidModule : InteractionModuleBase _logger.LogInformation("Created new raid with id {RaidId}", raidId); + // Calculate date + var date = DateTime.Now.Date; + date = date.AddDays(DateTimeUtils.CalculateDayOfWeekOffset(date.DayOfWeek, day)) + .Add(timeOnly.ToTimeSpan()); + // New raid instance - // TODO: Ask for date - if (!_raidsRepository.AddNewRaid(raidId, DateTime.Now)) + if (!_raidsRepository.AddNewRaid(raidId, date)) { // A raid with this message id already exists, how?? _logger.LogWarning("Tried to create a new raid with already existing id: {RaidId}", raidId); - await FollowupAsync(ephemeral: true, embed: EmbedUtils.ErrorEmbed("Can't create a new raid with same raid id").Build()); + await FollowupAsync( + ephemeral: true, + embed: EmbedUtils.ErrorEmbed("Can't create a new raid with same raid id").Build() + ); await DeleteOriginalResponseAsync(); return; @@ -67,16 +84,21 @@ public class RaidModule : InteractionModuleBase { if (!_raidsRepository.TryGetRaid(raidId, out var raid)) { - await RespondAsync(ephemeral: true, embed: EmbedUtils.ErrorEmbed("This raid does not exist").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.ErrorEmbed("This raid does not exist").Build() + ); return; } // Todo: Ask role, FC, substitute - var user = (IGuildUser)Context.User; + var user = (IGuildUser) Context.User; if (!raid.AddPlayer(user.Id, user.DisplayName, PlayerRole.Dps, 10000)) { - await RespondAsync(ephemeral: true, embed: EmbedUtils.InfoEmbed("You're already registered for this raid").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.InfoEmbed("You're already registered for this raid").Build()); return; } @@ -84,7 +106,10 @@ public class RaidModule : InteractionModuleBase _logger.LogInformation("User {User} joined raid {Raid}", Context.User.Username, raid); await UpdateRaidRosterEmbed(raid); - await RespondAsync(ephemeral: true, embed: EmbedUtils.SuccessEmbed($"Successfully joined the raid as {raid.GetPlayer(user.Id).Role}").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.SuccessEmbed($"Successfully joined the raid as {raid.GetPlayer(user.Id).Role}").Build() + ); } [ComponentInteraction("raid raid_leave:*", ignoreGroupNames: true)] @@ -92,21 +117,30 @@ public class RaidModule : InteractionModuleBase { if (!_raidsRepository.TryGetRaid(raidId, out var raid)) { - await RespondAsync(ephemeral: true, embed: EmbedUtils.ErrorEmbed("This raid does not exist").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.ErrorEmbed("This raid does not exist").Build() + ); return; } - var user = (IGuildUser)Context.User; + var user = (IGuildUser) Context.User; if (!raid.RemovePlayer(user.Id)) { - await RespondAsync(ephemeral: true, embed: EmbedUtils.WarningEmbed("You're not registered for this raid").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.WarningEmbed("You're not registered for this raid").Build() + ); return; } await UpdateRaidRosterEmbed(raid); - await RespondAsync(ephemeral: true, embed: EmbedUtils.SuccessEmbed("Successfully left the raid").Build()); + await RespondAsync( + ephemeral: true, + embed: EmbedUtils.SuccessEmbed("Successfully left the raid").Build() + ); } public async Task UpdateRaidRosterEmbed(Raid raid) @@ -115,7 +149,9 @@ public class RaidModule : InteractionModuleBase if (message is SocketUserMessage userMessage) { - await userMessage.ModifyAsync(m => m.Embed = _raidFormatter.RaidEmbed(raid).Build()); + await userMessage.ModifyAsync( + m => m.Embed = _raidFormatter.RaidEmbed(raid).Build() + ); } } diff --git a/Cocotte/Modules/Raids/RosterPlayer.cs b/Cocotte/Modules/Raids/RosterPlayer.cs index 983a57b..4fa53c8 100644 --- a/Cocotte/Modules/Raids/RosterPlayer.cs +++ b/Cocotte/Modules/Raids/RosterPlayer.cs @@ -1,8 +1,6 @@ -using Cocotte.Options; +namespace Cocotte.Modules.Raids; -namespace Cocotte.Modules.Raids; - -public record RosterPlayer(ulong Id, string Name, PlayerRole Role, int Fc, bool Substitue = false) +public record RosterPlayer(ulong Id, string Name, PlayerRole Role, int Fc, bool Substitute = false) { public int RosterNumber { get; set; } diff --git a/Cocotte/Utils/DateTimeUtils.cs b/Cocotte/Utils/DateTimeUtils.cs new file mode 100644 index 0000000..175c116 --- /dev/null +++ b/Cocotte/Utils/DateTimeUtils.cs @@ -0,0 +1,16 @@ +namespace Cocotte.Utils; + +public static class DateTimeUtils +{ + // Source: https://stackoverflow.com/a/50033489 + public static int CalculateDayOfWeekOffset(DayOfWeek current, DayOfWeek desired) { + // f( c, d ) = [7 - (c - d)] mod 7 + // f( c, d ) = [7 - c + d] mod 7 + // c is current day of week and 0 <= c < 7l + // d is desired day of the week and 0 <= d < 7 + int c = (int)current; + int d = (int)desired; + int offset = (7 - c + d) % 7; + return offset; + } +} \ No newline at end of file