Esempio n. 1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="commitData">Commit data</param>
 /// <param name="userManagementService">User management service</param>
 /// <param name="localizationService">Localization service</param>
 public RaidCommitDialogElement(LocalizationService localizationService, UserManagementService userManagementService, RaidCommitContainer commitData)
     : base(localizationService)
 {
     _localizationService   = localizationService;
     _userManagementService = userManagementService;
     _commitData            = commitData;
 }
Esempio n. 2
0
    /// <summary>
    /// Commit a raid appointment
    /// </summary>
    /// <param name="commandContext">Command context</param>
    /// <param name="aliasName">Alias name</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    public async Task CommitRaidAppointment(CommandContextContainer commandContext, string aliasName)
    {
        using (var dbFactory = RepositoryFactory.CreateInstance())
        {
            var now = DateTime.Now;

            var appointment = await dbFactory.GetRepository <RaidAppointmentRepository>()
                              .GetQuery()
                              .Where(obj => obj.TimeStamp < now &&
                                     obj.IsCommitted == false &&
                                     obj.RaidDayConfiguration.AliasName == aliasName)
                              .Select(obj => new
            {
                obj.Id,
                obj.ConfigurationId,
                obj.TimeStamp
            })
                              .FirstOrDefaultAsync()
                              .ConfigureAwait(false);

            if (appointment?.Id > 0)
            {
                var users = new List <RaidCommitUserData>();

                var experienceLevels = dbFactory.GetRepository <RaidExperienceLevelRepository>()
                                       .GetQuery()
                                       .Select(obj => new
                {
                    obj.Id,
                    obj.DiscordEmoji,
                    obj.Rank,
                    obj.ParticipationPoints
                })
                                       .ToList();

                var fallbackExperienceLevel = experienceLevels.OrderByDescending(obj => obj.Rank)
                                              .First();

                foreach (var entry in dbFactory.GetRepository <RaidRegistrationRepository>()
                         .GetQuery()
                         .Where(obj => obj.AppointmentId == appointment.Id)
                         .Select(obj => new
                {
                    UserId = obj.User
                             .DiscordAccounts
                             .Select(obj2 => obj2.Id)
                             .FirstOrDefault(),
                    obj.User.RaidExperienceLevelId,
                    obj.LineupExperienceLevelId
                })
                         .ToList())
                {
                    var experienceLevel = experienceLevels.FirstOrDefault(obj => obj.Id == entry.RaidExperienceLevelId) ?? fallbackExperienceLevel;

                    users.Add(new RaidCommitUserData
                    {
                        DiscordUserId = entry.UserId,
                        Points        = experienceLevel.ParticipationPoints * (entry.LineupExperienceLevelId == null ? 3.0 : 1.0),
                        DiscordEmoji  = experienceLevel.DiscordEmoji
                    });
                }

                var container = new RaidCommitContainer
                {
                    AppointmentId        = appointment.Id,
                    AppointmentTimeStamp = appointment.TimeStamp,
                    Users = users,
                };

                var dialogHandler = new DialogHandler(commandContext);
                await using (dialogHandler.ConfigureAwait(false))
                {
                    while (await dialogHandler.Run <RaidCommitDialogElement, bool>(new RaidCommitDialogElement(_localizationService, _userManagementService, container)).ConfigureAwait(false))
                    {
                    }

                    await commandContext.Channel
                    .SendMessageAsync(LocalizationGroup.GetText("CommitCompleted", "The raid appointment has been committed."))
                    .ConfigureAwait(false);

                    await _messageBuilder.RefreshMessageAsync(appointment.ConfigurationId)
                    .ConfigureAwait(false);
                }
            }
            else
            {
                await commandContext.Message
                .RespondAsync(LocalizationGroup.GetText("NoOpenAppointment", "There is no uncommitted appointment available."))
                .ConfigureAwait(false);
            }
        }
    }