[Activity] Update component style

- Add icons
- Disable button when activity is full
This commit is contained in:
2023-03-22 12:26:16 +01:00
parent 8dcefeeb39
commit ef948dba27
5 changed files with 33 additions and 8 deletions

View File

@@ -38,13 +38,16 @@ public class ActivityFormatter
public EmbedBuilder ActivityEmbed(Activity activity, IReadOnlyCollection<ActivityPlayer> players)
{
// Activity full
bool activityFull = players.Count >= activity.MaxPlayers;
// Compute padding using player with longest name
var namePadding = players.Count > 0 ? players.Max(p => p.Name.Length) : 0;
// Players field
var playersField = new EmbedFieldBuilder()
.WithName("Joueurs inscrits")
.WithValue($"{(!players.Any() ? "*Aucun joueur inscrit*" : string.Join("\n\n", players.Select(p => FormatActivityPlayer(p, namePadding))))}");
.WithValue($"{(!players.Any() ? "*Aucun joueur inscrit*" : string.Join("\n", players.Select(p => FormatActivityPlayer(p, namePadding))))}");
var title = activity switch
{
@@ -59,8 +62,10 @@ public class ActivityFormatter
string bannerUrl = $"https://sage.cdn.ilysix.fr/assets/Cocotte/banner/{GetActivityCode(activity.Name)}.webp";
var color = activityFull ? Colors.CocotteOrange : Colors.CocotteBlue;
return new EmbedBuilder()
.WithColor(Colors.CocotteBlue)
.WithColor(color)
.WithTitle(title)
.WithDescription(description)
.WithImageUrl(bannerUrl)

View File

@@ -94,7 +94,7 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
await _activitiesRepository.SaveChanges();
// Add components
var components = ActivityRoleComponent(activity.ActivityId);
var components = ActivityComponents(activity.ActivityId);
await ModifyOriginalResponseAsync(m =>
{
@@ -162,7 +162,7 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
await _activitiesRepository.SaveChanges();
// Update activity embed
await UpdateActivityEmbed(activity);
await UpdateActivityEmbed(activity, ActivityUpdateReason.PlayerJoin);
await RespondAsync(
ephemeral: true,
@@ -203,7 +203,7 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
await _activitiesRepository.SaveChanges();
// Update activity embed
await UpdateActivityEmbed(activity);
await UpdateActivityEmbed(activity, ActivityUpdateReason.PlayerLeave);
await RespondAsync(
ephemeral: true,
@@ -211,7 +211,7 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
);
}
private async Task UpdateActivityEmbed(Activity activity)
private async Task UpdateActivityEmbed(Activity activity, ActivityUpdateReason updateReason)
{
// Get channel
var channel = await Context.Interaction.GetChannelAsync();
@@ -227,10 +227,19 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
await channel.ModifyMessageAsync(activity.ActivityId, properties =>
{
properties.Embed = _activityFormatter.ActivityEmbed(activity, players).Build();
// Disable join button if the activity is full on join, enable it on leave if activity is not full anymore
var isActivityFull = players.Count >= activity.MaxPlayers;
properties.Components = updateReason switch
{
ActivityUpdateReason.PlayerJoin when isActivityFull => ActivityComponents(activity.ActivityId, disabled: true).Build(),
ActivityUpdateReason.PlayerLeave when !isActivityFull => ActivityComponents(activity.ActivityId, disabled: false).Build(),
_ => Optional<MessageComponent>.Unspecified
};
});
}
private static ComponentBuilder ActivityRoleComponent(ulong activityId)
private static ComponentBuilder ActivityComponents(ulong activityId, bool disabled = false)
{
return new ComponentBuilder()
.AddRow(new ActionRowBuilder()
@@ -239,6 +248,7 @@ public partial class ActivityModule : InteractionModuleBase<SocketInteractionCon
.WithCustomId($"activity join:{activityId}")
.WithEmote(":white_check_mark:".ToEmote())
.WithStyle(ButtonStyle.Primary)
.WithDisabled(disabled)
)
.WithButton(new ButtonBuilder()
.WithLabel("Se désinscrire de l'activité")

View File

@@ -29,7 +29,7 @@ public partial class ActivityModule
activity.ActivityPlayers.Add(player);
await _activitiesRepository.SaveChanges();
await UpdateActivityEmbed(activity);
await UpdateActivityEmbed(activity, ActivityUpdateReason.PlayerJoin);
}
}

View File

@@ -0,0 +1,8 @@
namespace Cocotte.Modules.Activities;
public enum ActivityUpdateReason
{
PlayerJoin,
PlayerLeave,
Update
}

View File

@@ -6,6 +6,8 @@ public static class Colors
{
// Main Cocotte colors
public static Color CocotteBlue => new(0x3196c8);
public static Color CocotteRed => new(0xe40808);
public static Color CocotteOrange => new(0xff6d01);
// Colors used in embeds
public static Color ErrorColor => new(0xFB6060);