[Raid] Add date parameter in start command

This commit is contained in:
2022-11-24 10:28:42 +01:00
parent 4f0ccb9464
commit e334e02768
4 changed files with 71 additions and 21 deletions

View File

@@ -27,7 +27,7 @@ public class RaidFormatter
_ => $"{fc/1000}k" _ => $"{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)", false => $"{RoleToEmote(player.Role)} {player.Name} ({FcFormat(player.Fc)} FC)",
true => $"*{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<RosterPlayer> players) EmbedFieldBuilder RosterEmbed(int rosterNumber, IEnumerable<RosterPlayer> players)
{ {
var rosterPlayers = players.ToList(); var rosterPlayers = players.ToList();
var nonSubstitute = rosterPlayers.Where(p => !p.Substitue); var nonSubstitute = rosterPlayers.Where(p => !p.Substitute);
var substitute = rosterPlayers.Where(p => p.Substitue); 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()); 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 separatorLength = (int) ((separatorLength + 13) * 0.49); // Don't ask why, it just works

View File

@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Cocotte.Options;
using Cocotte.Utils; using Cocotte.Utils;
using Discord; using Discord;
using Discord.Interactions; using Discord.Interactions;
@@ -26,8 +25,19 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
[EnabledInDm(false)] [EnabledInDm(false)]
[SlashCommand("start", "Start a raid formation")] [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 // Raids are identified using their original message id
await RespondAsync("`Creating a new raid...`"); await RespondAsync("`Creating a new raid...`");
@@ -36,14 +46,21 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
_logger.LogInformation("Created new raid with id {RaidId}", raidId); _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 // New raid instance
// TODO: Ask for date if (!_raidsRepository.AddNewRaid(raidId, date))
if (!_raidsRepository.AddNewRaid(raidId, DateTime.Now))
{ {
// A raid with this message id already exists, how?? // A raid with this message id already exists, how??
_logger.LogWarning("Tried to create a new raid with already existing id: {RaidId}", raidId); _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(); await DeleteOriginalResponseAsync();
return; return;
@@ -67,16 +84,21 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
{ {
if (!_raidsRepository.TryGetRaid(raidId, out var raid)) 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; return;
} }
// Todo: Ask role, FC, substitute // 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)) 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; return;
} }
@@ -84,7 +106,10 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
_logger.LogInformation("User {User} joined raid {Raid}", Context.User.Username, raid); _logger.LogInformation("User {User} joined raid {Raid}", Context.User.Username, raid);
await UpdateRaidRosterEmbed(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)] [ComponentInteraction("raid raid_leave:*", ignoreGroupNames: true)]
@@ -92,21 +117,30 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
{ {
if (!_raidsRepository.TryGetRaid(raidId, out var raid)) 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; return;
} }
var user = (IGuildUser)Context.User; var user = (IGuildUser) Context.User;
if (!raid.RemovePlayer(user.Id)) 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; return;
} }
await UpdateRaidRosterEmbed(raid); 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) public async Task UpdateRaidRosterEmbed(Raid raid)
@@ -115,7 +149,9 @@ public class RaidModule : InteractionModuleBase<SocketInteractionContext>
if (message is SocketUserMessage userMessage) 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()
);
} }
} }

View File

@@ -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 Substitute = false)
public record RosterPlayer(ulong Id, string Name, PlayerRole Role, int Fc, bool Substitue = false)
{ {
public int RosterNumber { get; set; } public int RosterNumber { get; set; }

View File

@@ -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;
}
}