Пример #1
0
        public async Task <Item> AddItemToCharacterAsync(NaheulbookExecutionContext executionContext, int characterId, CreateItemRequest request)
        {
            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var character = await uow.Characters.GetWithGroupAsync(characterId);

                if (character == null)
                {
                    throw new CharacterNotFoundException(characterId);
                }

                _authorizationUtil.EnsureCharacterAccess(executionContext, character);
            }

            var item = await _itemService.AddItemToAsync(ItemOwnerType.Character, characterId, request);

            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                uow.CharacterHistoryEntries.Add(_characterHistoryUtil.CreateLogAddItem(characterId, item));
                await uow.SaveChangesAsync();
            }

            var session = _notificationSessionFactory.CreateSession();

            session.NotifyCharacterAddItem(characterId, item);
            await session.CommitAsync();

            return(item);
        }
Пример #2
0
        public async Task UpdateLootVisibilityAsync(NaheulbookExecutionContext executionContext, int lootId, PutLootVisibilityRequest request)
        {
            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var loot = await uow.Loots.GetAsync(lootId);

                if (loot == null)
                {
                    throw new LootNotFoundException(lootId);
                }

                var group = await uow.Groups.GetGroupsWithCharactersAsync(loot.GroupId);

                if (group == null)
                {
                    throw new GroupNotFoundException(loot.GroupId);
                }

                _authorizationUtil.EnsureIsGroupOwner(executionContext, loot.Group);

                loot.IsVisibleForPlayer = request.VisibleForPlayer;

                var session = _notificationSessionFactory.CreateSession();
                session.NotifyLootUpdateVisibility(lootId, request.VisibleForPlayer);

                if (loot.IsVisibleForPlayer)
                {
                    var fullLootData = await uow.Loots.GetWithAllDataAsync(lootId);

                    foreach (var character in group.Characters)
                    {
                        session.NotifyCharacterShowLoot(character.Id, fullLootData);
                    }
                }
                else
                {
                    foreach (var character in group.Characters)
                    {
                        session.NotifyCharacterHideLoot(character.Id, loot.Id);
                    }
                }

                await uow.SaveChangesAsync();

                await session.CommitAsync();
            }
        }
Пример #3
0
        public async Task <Monster> CreateMonsterAsync(NaheulbookExecutionContext executionContext, int groupId, CreateMonsterRequest request)
        {
            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var group = await uow.Groups.GetAsync(groupId);

                if (group == null)
                {
                    throw new GroupNotFoundException(groupId);
                }

                _authorizationUtil.EnsureIsGroupOwner(executionContext, group);

                _activeStatsModifierUtil.InitializeModifierIds(request.Modifiers);

                var monster = new Monster
                {
                    Group     = group,
                    Name      = request.Name,
                    Data      = _jsonUtil.Serialize(request.Data),
                    Modifiers = _jsonUtil.Serialize(request.Modifiers)
                };

                // FIXME: test this
                uow.Monsters.Add(monster);
                monster.Items = await _itemService.CreateItemsAsync(request.Items);

                await uow.SaveChangesAsync();

                monster.Items = await uow.Items.GetWithAllDataByIdsAsync(monster.Items.Select(x => x.Id));

                var notificationSession = _notificationSessionFactory.CreateSession();
                notificationSession.NotifyGroupAddMonster(group.Id, monster);
                await notificationSession.CommitAsync();

                return(monster);
            }
        }
Пример #4
0
        public async Task EditGroupPropertiesAsync(NaheulbookExecutionContext executionContext, int groupId, PatchGroupRequest request)
        {
            var notificationSession = _notificationSessionFactory.CreateSession();

            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var group = await uow.Groups.GetAsync(groupId);

                if (group == null)
                {
                    throw new GroupNotFoundException(groupId);
                }

                _authorizationUtil.EnsureIsGroupOwner(executionContext, group);

                _groupUtil.ApplyChangesAndNotify(group, request, notificationSession);

                await uow.SaveChangesAsync();
            }

            await notificationSession.CommitAsync();
        }
Пример #5
0
        public async Task UpdateDurationAsync(int groupId, IList <FighterDurationChanges> request)
        {
            var notificationSession = _notificationSessionFactory.CreateSession();

            using (var uow = _unitOfWorkFactory.CreateUnitOfWork())
            {
                var monsterUpdateDurations = request.OfType <MonsterUpdateDuration>().ToList();
                var monsterIds             = monsterUpdateDurations.Select(f => f.MonsterId).ToList();
                var monsters = await uow.Monsters.GetWithItemsByGroupAndByIdAsync(groupId, monsterIds);

                if (monsterIds.Count != monsters.Count)
                {
                    throw new MonsterNotFoundException(monsters.First(x => !monsterIds.Contains(x.Id)).Id);
                }

                var characterUpdateDurations = request.OfType <CharacterUpdateDuration>().ToList();
                var characterIds             = characterUpdateDurations.Select(f => f.CharacterId).ToList();
                var characters = await uow.Characters.GetWithItemsWithModifiersByGroupAndByIdAsync(groupId, characterIds);

                if (characterIds.Count != characters.Count)
                {
                    throw new CharacterNotFoundException(characters.First(x => !characterIds.Contains(x.Id)).Id);
                }

                foreach (var(character, changes) in characters.Join(characterUpdateDurations, c => c.Id, c => c.CharacterId, (character, updateDuration) => (character, updateDuration.Changes)))
                {
                    UpdateCharacterDuration(character, changes, notificationSession);
                }

                foreach (var(monster, changes) in monsters.Join(monsterUpdateDurations, c => c.Id, c => c.MonsterId, (monster, updateDuration) => (monster, updateDuration.Changes)))
                {
                    UpdateMonsterDuration(monster, changes, notificationSession);
                }

                await uow.SaveChangesAsync();
            }

            await notificationSession.CommitAsync();
        }