Exemplo n.º 1
0
        public async Task <OperationResult> UpdateAsync(
            long guildId,
            long divisionId,
            CharacterGuildDivisionUpdateModel updateModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDivisionUpdating(_logger, guildId, divisionId, updateModel, performedById);

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var guildIdValidationResult = await ValidateGuildIdAsync(guildId, cancellationToken);

            if (guildIdValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildIdValidationFailed(_logger, guildId, guildIdValidationResult);
                return(guildIdValidationResult);
            }
            CharactersLogMessages.CharacterGuildIdValidationSucceeded(_logger, guildId);

            var nameValidationResult = await ValidateDivisionNameAsync(guildId, updateModel.Name, divisionId, cancellationToken);

            if (nameValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionNameValidationFailed(_logger, updateModel.Name, nameValidationResult);
                return(nameValidationResult);
            }
            CharactersLogMessages.CharacterGuildDivisionNameValidationSucceeded(_logger, updateModel.Name);

            var now = _systemClock.UtcNow;

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)CharacterManagementAdministrationActionType.DivisionModified,
                now,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var updateResult = await _characterGuildDivisionsRepository.UpdateAsync(
                divisionId : divisionId,
                actionId : actionId,
                name : updateModel.Name,
                cancellationToken : cancellationToken);

            if (updateResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionUpdateFailed(_logger, guildId, divisionId, updateResult);
                return(updateResult);
            }
            CharactersLogMessages.CharacterGuildDivisionUpdated(_logger, guildId, divisionId);

            transactionScope.Complete();
            TransactionsLogMessages.TransactionScopeCommitted(_logger);

            return(OperationResult.Success);
        }
        public async Task <IReadOnlyCollection <CharacterGuildIdentityViewModel> > GetCurrentIdentitiesAsync(
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildIdentitiesFetchingCurrent(_logger);

            var identities = await _characterGuildsRepository.AsyncEnumerateIdentities(
                isDeleted : false)
                             .ToArrayAsync(cancellationToken);

            CharactersLogMessages.CharacterGuildIdentitiesFetchedCurrent(_logger);

            return(identities);
        }
        protected override async Task OnStartingAsync(
            IServiceProvider serviceProvider,
            CancellationToken cancellationToken)
        {
            if (!_yastahAutoMigrationStartupAction.WhenDone.IsCompletedSuccessfully)
            {
                YastahDbContextLogMessages.ContextMigrationAwaiting(_logger);
            }
            await _yastahAutoMigrationStartupAction.WhenDone;

            CharactersLogMessages.CharacterLevelDefinitionsInitializing(_logger);

            var auditableActionsRepository = serviceProvider.GetRequiredService <IAuditableActionsRepository>();
            var characterLevelsRepository  = serviceProvider.GetRequiredService <ICharacterLevelsRepository>();

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var level1Exists = await characterLevelsRepository.AnyDefinitionsAsync(
                level :               1,
                experienceThreshold : 0,
                isDeleted :           false,
                cancellationToken :   cancellationToken);

            if (!level1Exists)
            {
                CharactersLogMessages.CharacterLevelsNotInitialized(_logger);

                var actionId = await auditableActionsRepository.CreateAsync(
                    (int)CharacterManagementAdministrationActionType.LevelDefinitionsInitialized,
                    _systemClock.UtcNow,
                    null,
                    cancellationToken);

                AuditingLogMessages.AuditingActionCreated(_logger, actionId);

                await characterLevelsRepository.MergeDefinitionAsync(
                    1,
                    0,
                    false,
                    actionId,
                    cancellationToken);

                CharactersLogMessages.CharacterLevelDefinition1Created(_logger);
            }

            TransactionsLogMessages.TransactionScopeCommitting(_logger);
            transactionScope.Complete();

            CharactersLogMessages.CharacterLevelDefinitionsInitialized(_logger);
        }
Exemplo n.º 4
0
        public async Task <OperationResult <long> > CreateAsync(
            long guildId,
            CharacterGuildDivisionCreationModel creationModel,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDivisionCreating(_logger, guildId, creationModel, performedById);

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var guildIdValidationResult = await ValidateGuildIdAsync(guildId, cancellationToken);

            if (guildIdValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildIdValidationFailed(_logger, guildId, guildIdValidationResult);
                return(guildIdValidationResult.Error);
            }
            CharactersLogMessages.CharacterGuildIdValidationSucceeded(_logger, guildId);

            var nameValidationResult = await ValidateDivisionNameAsync(guildId, creationModel.Name, null, cancellationToken);

            if (nameValidationResult.IsFailure)
            {
                CharactersLogMessages.CharacterGuildDivisionNameValidationFailed(_logger, creationModel.Name, nameValidationResult);
                return(nameValidationResult.Error);
            }
            CharactersLogMessages.CharacterGuildDivisionNameValidationSucceeded(_logger, creationModel.Name);

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)CharacterManagementAdministrationActionType.DivisionCreated,
                _systemClock.UtcNow,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var divisionId = await _characterGuildDivisionsRepository.CreateAsync(
                guildId,
                creationModel.Name,
                actionId,
                cancellationToken);

            CharactersLogMessages.CharacterGuildDivisionCreated(_logger, guildId, divisionId);

            transactionScope.Complete();
            TransactionsLogMessages.TransactionScopeCommitted(_logger);

            return(divisionId.ToSuccess());
        }
        public ValueTask <IReadOnlyList <CharacterLevelDefinitionViewModel> > GetCurrentDefinitionsAsync(
            CancellationToken cancellationToken)
        => _memoryCache.OptimisticGetOrCreateAsync(_getCurrentDefinitionsCacheKey, async entry =>
        {
            CharactersLogMessages.CharacterLevelDefinitionsFetchingCurrent(_logger);

            entry.Priority = CacheItemPriority.High;

            var definitions = await _characterLevelsRepository.AsyncEnumerateDefinitions(
                isDeleted: false)
                              .ToArrayAsync(cancellationToken)
                              as IReadOnlyList <CharacterLevelDefinitionViewModel>;
            CharactersLogMessages.CharacterLevelDefinitionsFetchedCurrent(_logger);

            return(definitions);
        });
        public async Task <OperationResult> DeleteAsync(
            long guildId,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterGuildDeleting(_logger, guildId, performedById);

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)CharacterManagementAdministrationActionType.GuildDeleted,
                _systemClock.UtcNow,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var updateResult = await _characterGuildsRepository.UpdateAsync(
                guildId : guildId,
                actionId : actionId,
                isDeleted : true,
                cancellationToken : cancellationToken);

            if (updateResult.IsSuccess)
            {
                CharactersLogMessages.CharacterGuildDeleted(_logger, guildId);
                transactionScope.Complete();
                TransactionsLogMessages.TransactionScopeCommitted(_logger);
            }
            else
            {
                CharactersLogMessages.CharacterGuildDeleteFailed(_logger, guildId, updateResult);
            }

            return(updateResult);
        }
        public async Task <OperationResult> UpdateExperienceDiffsAsync(
            IReadOnlyList <int> experienceDiffs,
            ulong performedById,
            CancellationToken cancellationToken)
        {
            CharactersLogMessages.CharacterLevelDefinitionsUpdating(_logger);

            var totalExperience     = 0;
            var proposedDefinitions = experienceDiffs
                                      .Select((experienceDiff, index) => (
                                                  level: index + 2,
                                                  previousExperienceThreshold: totalExperience,
                                                  experienceThreshold: totalExperience += experienceDiff))
                                      .Prepend((
                                                   level: 0,
                                                   previousExperienceThreshold: -1,
                                                   experienceThreshold: 0))
                                      .ToArray();

            foreach (var(level, previousExperienceThreshold, experienceThreshold) in proposedDefinitions)
            {
                CharactersLogMessages.CharacterLevelDefinitionProposed(_logger, level, experienceThreshold, previousExperienceThreshold);
                if (experienceThreshold <= previousExperienceThreshold)
                {
                    CharactersLogMessages.CharacterLevelDefinitionValidationFailed(_logger, level, experienceThreshold, previousExperienceThreshold);
                    return(new InvalidLevelDefinitionError(
                               level,
                               experienceThreshold,
                               previousExperienceThreshold));
                }
            }

            using var transactionScope = _transactionScopeFactory.CreateScope();
            TransactionsLogMessages.TransactionScopeCreated(_logger);

            var currentDefinitions = await GetCurrentDefinitionsAsync(cancellationToken);

            CharactersLogMessages.CharacterLevelDefinitionsFetchedCurrent(_logger);

            var sequenceLength   = Math.Max(experienceDiffs.Count + 1, currentDefinitions.Count);
            var pairwiseSequence = Enumerable.Zip(
                currentDefinitions
                .PadEnd <CharacterLevelDefinitionViewModel?>(sequenceLength, null),
                proposedDefinitions
                .Select(x => x.ToNullable())
                .PadEnd(sequenceLength, null),
                (current, proposed) => (current, proposed));

            var actionId = await _auditableActionsRepository.CreateAsync(
                (int)CharacterManagementAdministrationActionType.LevelDefinitionsUpdated,
                _systemClock.UtcNow,
                performedById,
                cancellationToken);

            AuditingLogMessages.AuditingActionCreated(_logger, actionId);

            var anyChangesMade = false;

            foreach (var(current, proposed) in pairwiseSequence)
            {
                if (proposed is null)
                {
                    CharactersLogMessages.CharacterLevelDefinitionDeleting(_logger, current !.Level);
                    await _characterLevelsRepository.MergeDefinitionAsync(
                        current !.Level,
                        current !.ExperienceThreshold,
                        true,
                        actionId,
                        cancellationToken);

                    CharactersLogMessages.CharacterLevelDefinitionDeleted(_logger, current !.Level);

                    anyChangesMade = true;
                }
                else if ((current is null) || (current.ExperienceThreshold != proposed.Value.experienceThreshold))
                {
                    CharactersLogMessages.CharacterLevelDefinitionUpdating(_logger, proposed !.Value.level, proposed !.Value.experienceThreshold);
                    await _characterLevelsRepository.MergeDefinitionAsync(
                        proposed !.Value.level,
                        proposed !.Value.experienceThreshold,
                        false,
                        actionId,
                        cancellationToken);

                    CharactersLogMessages.CharacterLevelDefinitionUpdated(_logger, proposed !.Value.level, proposed !.Value.experienceThreshold);

                    anyChangesMade = true;
                }
            }

            if (!anyChangesMade)
            {
                CharactersLogMessages.CharacterLevelDefinitionsNoChangesGiven(_logger);
                return(new NoChangesGivenError("Character Level Definitions"));
            }

            transactionScope.Complete();
            TransactionsLogMessages.TransactionScopeCommitted(_logger);

            _memoryCache.Remove(_getCurrentDefinitionsCacheKey);
            CharactersLogMessages.CharacterLevelDefinitionsCacheCleared(_logger);

            return(OperationResult.Success);
        }