[Activity] Add full check

This commit is contained in:
2023-03-21 22:52:47 +01:00
parent bffa7d06bb
commit 21996a2e03
4 changed files with 63 additions and 6 deletions

View File

@@ -63,7 +63,7 @@ public class ActivityFormatter
public string FormatActivityPlayer(ActivityPlayer player) => player switch
{
ActivityRolePlayer rolePlayer => $"{player.Name} {RolesToEmotes(rolePlayer.Roles)}",
ActivityRolePlayer rolePlayer => $"{player.Name} - {RolesToEmotes(rolePlayer.Roles)}",
_ => $"{player.Name})"
};
@@ -73,22 +73,22 @@ public class ActivityFormatter
if (rolePlayerRoles.HasFlag(ActivityRoles.Helper))
{
emotesBuilder.Append($"{_options.HelperEmote}/");
emotesBuilder.Append($" {_options.HelperEmote} ");
}
if (rolePlayerRoles.HasFlag(ActivityRoles.Dps))
{
emotesBuilder.Append(_options.DpsEmote);
emotesBuilder.Append($" {_options.DpsEmote} ");
}
if (rolePlayerRoles.HasFlag(ActivityRoles.Tank))
{
emotesBuilder.Append(_options.TankEmote);
emotesBuilder.Append($" {_options.TankEmote} ");
}
if (rolePlayerRoles.HasFlag(ActivityRoles.Support))
{
emotesBuilder.Append(_options.SupportEmote);
emotesBuilder.Append($" {_options.SupportEmote} ");
}
return emotesBuilder.ToString();

View File

@@ -14,7 +14,7 @@ namespace Cocotte.Modules.Activities;
/// </summary>
[Group("activite", "Organise des activités")]
[SuppressMessage("ReSharper", "UnusedMember.Local")]
public class ActivityModule : InteractionModuleBase<SocketInteractionContext>
public partial class ActivityModule : InteractionModuleBase<SocketInteractionContext>
{
private readonly ILogger<ActivityModule> _logger;
private readonly ActivityOptions _options;
@@ -129,6 +129,17 @@ public class ActivityModule : InteractionModuleBase<SocketInteractionContext>
return;
}
// Check if activity is full
if (await _activitiesRepository.ActivityPlayerCount(activity) >= activity.MaxPlayers)
{
await RespondAsync(
ephemeral: true,
embed: EmbedUtils.ErrorEmbed("L'activité est complète").Build()
);
return;
}
_logger.LogTrace("Player {Player} joined activity {Id}", user.DisplayName, activityId);
var roles = _activityHelper.GetPlayerRoles(user.Roles);

View File

@@ -0,0 +1,42 @@
using Cocotte.Modules.Activities.Models;
using Cocotte.Utils;
using Discord;
using Discord.Interactions;
namespace Cocotte.Modules.Activities;
#if DEBUG
public partial class ActivityModule
{
[MessageCommand("Add role player")]
public async Task AddRolePlayer(IMessage message)
{
if (message is IUserMessage userMessage && userMessage.Author.IsBot)
{
if (await _activitiesRepository.FindActivity(message.Id) is { } activity)
{
// Generate random player
var player = new ActivityRolePlayer
{
Activity = activity,
Name = $"Player{Random.Shared.Next(1, 100)}",
DiscordId = (ulong) Random.Shared.NextInt64(),
Roles = (ActivityRoles) Random.Shared.Next((int) (ActivityRoles.Dps | ActivityRoles.Helper |
ActivityRoles.Support | ActivityRoles.Tank) + 1)
};
// Add the player to the activity
activity.ActivityPlayers.Add(player);
await _activitiesRepository.SaveChanges();
await UpdateActivityEmbed(activity);
}
}
await RespondAsync(
embed: EmbedUtils.SuccessEmbed($"Successfully added a player").Build(),
ephemeral: true
);
}
}
#endif

View File

@@ -1,4 +1,5 @@
using Cocotte.Services;
using Microsoft.EntityFrameworkCore;
namespace Cocotte.Modules.Activities.Models;
@@ -31,6 +32,9 @@ public class ActivitiesRepository
return await _cocotteDbContext.ActivityRolePlayers.FindAsync(activityId, playerId);
}
public async Task<int> ActivityPlayerCount(Activity activity) =>
await _cocotteDbContext.ActivityPlayers.Where(player => player.ActivityId == activity.ActivityId).CountAsync();
public async Task<IEnumerable<ActivityPlayer>> LoadActivityPlayers(Activity activity)
{
await _cocotteDbContext