public async Task UpdateItemDataAsync_WhenQuantityChange_ShouldLogItInCharacterHistory(int?currentQuantity, int?newQuantity) { const int itemId = 4; const int characterId = 8; var itemData = new ItemData { Quantity = newQuantity }; var item = GivenAnItem(new ItemData { Quantity = currentQuantity }, characterId); var characterHistoryEntry = new CharacterHistoryEntry(); _jsonUtil.Serialize(itemData) .Returns("some-new-item-data-json"); _unitOfWorkFactory.GetUnitOfWork().Items.GetWithOwnerAsync(itemId) .Returns(item); _characterHistoryUtil.CreateLogChangeItemQuantity(characterId, item, currentQuantity, newQuantity) .Returns(characterHistoryEntry); _unitOfWorkFactory.GetUnitOfWork().When(x => x.SaveChangesAsync()) .Do(_ => item.Character.HistoryEntries.Should().Contain(characterHistoryEntry)); await _service.UpdateItemDataAsync(new NaheulbookExecutionContext(), itemId, itemData); await _unitOfWorkFactory.GetUnitOfWork().Received(1).SaveChangesAsync(); }
public void ApplyChangesAndNotifyAsync_WhenMankdebolIsSet_UpdateValue_LogInGroupHistory() { var group = new Group { Data = GroupDataJson }; var groupData = new GroupData(); var groupHistoryEntry = new GroupHistoryEntry(); var request = new PatchGroupRequest { Mankdebol = 4 }; _groupHistoryUtil.CreateLogChangeMankdebol(group, null, 4) .Returns(groupHistoryEntry); _jsonUtil.Deserialize <GroupData>(GroupDataJson) .Returns(groupData); _jsonUtil.Serialize(groupData) .Returns(UpdatedGroupDataJson); _util.ApplyChangesAndNotify(group, request, _notificationSession); group.Data.Should().BeEquivalentTo(UpdatedGroupDataJson); groupData.Mankdebol.Should().Be(4); group.HistoryEntries.Should().Contain(groupHistoryEntry); }
public async Task CreateMonsterAsync_ShouldCreateNewEntryInDatabase_AndLinkItToGroup() { const int groupId = 42; var request = CreateRequest(); var executionContext = new NaheulbookExecutionContext(); var group = new Group { Id = groupId }; _unitOfWorkFactory.GetUnitOfWork().Groups.GetAsync(groupId) .Returns(group); _jsonUtil.Serialize(request.Data) .Returns("some-json-data"); _jsonUtil.Serialize(request.Modifiers) .Returns("some-json-modifiers"); var actualMonster = await _service.CreateMonsterAsync(executionContext, groupId, request); Received.InOrder(() => { _activeStatsModifierUtil.InitializeModifierIds(request.Modifiers); _unitOfWorkFactory.GetUnitOfWork().Monsters.Add(actualMonster); _unitOfWorkFactory.GetUnitOfWork().SaveChangesAsync(); }); actualMonster.Should().BeEquivalentTo(new Monster { Name = "some-monster-name", Data = "some-json-data", Modifiers = "some-json-modifiers", Group = group }); }
public GroupHistoryEntry CreateLogChangeMankdebol(Group group, int?oldValue, int newValue) { return(new GroupHistoryEntry { Group = group, Action = UpdateMankdebolActionName, Date = DateTime.Now, Gm = true, Data = _jsonUtil.Serialize(new { oldValue, newValue }) }); }
private void UpdateMonsterDuration(Monster monster, IList <IDurationChange> changes, INotificationSession notificationSession) { var modifiers = _jsonUtil.DeserializeOrCreate <List <ActiveStatsModifier> >(monster.Modifiers); foreach (var change in changes.OfType <ModifierDurationChange>()) { ApplyChangeOnMonsterModifier(monster, modifiers, change, notificationSession); } UpdateItemsDuration(monster.Items, changes.OfType <IITemDurationChange>(), notificationSession); monster.Modifiers = _jsonUtil.Serialize(modifiers); }
public void Save(IGeneralConfig config, IFileSystem disk, IJsonUtil jsonUtil) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (disk == null) { throw new ArgumentNullException(nameof(disk)); } if (jsonUtil == null) { throw new ArgumentNullException(nameof(jsonUtil)); } var path = config.ResolvePath(UserPath); var dir = Path.GetDirectoryName(path); if (!disk.DirectoryExists(dir)) { disk.CreateDirectory(dir); } var json = jsonUtil.Serialize(this); disk.WriteAllText(path, json); }
public T Serdes(T existing, AssetInfo info, AssetMapping mapping, ISerializer s, IJsonUtil jsonUtil) { if (s == null) { throw new ArgumentNullException(nameof(s)); } if (jsonUtil == null) { throw new ArgumentNullException(nameof(jsonUtil)); } if (s.IsWriting()) { if (existing == null) { throw new ArgumentNullException(nameof(existing)); } var json = Encoding.UTF8.GetBytes(jsonUtil.Serialize(existing)); s.Bytes(null, json, json.Length); return(existing); } else { var json = s.Bytes(null, null, (int)s.BytesRemaining); return(jsonUtil.Deserialize <T>(json)); } }
public async Task SendPacketsAsync(IEnumerable <INotificationPacket> packets) { foreach (var notificationPacket in packets) { await _hubContext.Clients.Group(notificationPacket.GroupName) .SendAsync("event", _jsonUtil.Serialize(notificationPacket.Payload)); } }
public CharacterHistoryEntry CreateLogChangeEv(Character character, int?oldValue, int?newValue) { return(new CharacterHistoryEntry { Character = character, Action = ChangeEvActionName, Date = DateTime.Now, Data = _jsonUtil.Serialize(new { oldValue, newValue }) }); }
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); } }
public void ApplyChangesAndNotify(Group group, PatchGroupRequest request, INotificationSession notificationSession) { var groupData = _jsonUtil.Deserialize <GroupData>(group.Data) ?? new GroupData(); if (request.Mankdebol.HasValue) { group.AddHistoryEntry(_groupHistoryUtil.CreateLogChangeMankdebol(group, groupData.Mankdebol, request.Mankdebol.Value)); groupData.Mankdebol = request.Mankdebol.Value; } if (request.Debilibeuk.HasValue) { group.AddHistoryEntry(_groupHistoryUtil.CreateLogChangeDebilibeuk(group, groupData.Debilibeuk, request.Debilibeuk.Value)); groupData.Debilibeuk = request.Debilibeuk.Value; } if (request.Date != null) { var newDate = new NhbkDate(request.Date); group.AddHistoryEntry(_groupHistoryUtil.CreateLogChangeDate(group, groupData.Date, newDate)); groupData.Date = newDate; } if (request.FighterIndex != null) { groupData.CurrentFighterIndex = request.FighterIndex.Value; } if (request.Name != null) { group.Name = request.Name; } notificationSession.NotifyGroupChangeGroupData(group.Id, groupData); group.Data = _jsonUtil.Serialize(groupData); }
public void Save(IFileSystem disk, IJsonUtil jsonUtil) { if (disk == null) { throw new ArgumentNullException(nameof(disk)); } if (jsonUtil == null) { throw new ArgumentNullException(nameof(jsonUtil)); } var configPath = Path.Combine(_basePath, "data", "input.json"); var json = jsonUtil.Serialize(this); disk.WriteAllText(configPath, json); }
public ISerializer Read(string path, AssetInfo info, IFileSystem disk, IJsonUtil jsonUtil) { if (info == null) { throw new ArgumentNullException(nameof(info)); } if (disk == null) { throw new ArgumentNullException(nameof(disk)); } if (jsonUtil == null) { throw new ArgumentNullException(nameof(jsonUtil)); } if (!disk.FileExists(path)) { return(null); } var dict = Load(path, disk, jsonUtil); if (!dict.TryGetValue(info.AssetId, out var token)) { return(null); } var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonUtil.Serialize(token))); var br = new BinaryReader(ms); return(new GenericBinaryReader( br, ms.Length, Encoding.UTF8.GetString, ApiUtil.Assert, () => { br.Dispose(); ms.Dispose(); })); }
public void ApplyCharactersChange(NaheulbookExecutionContext executionContext, PatchCharacterRequest request, Character character, INotificationSession notificationSession) { if (request.Debilibeuk.HasValue) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); var gmData = _jsonUtil.Deserialize <CharacterGmData>(character.GmData) ?? new CharacterGmData(); gmData.Debilibeuk = request.Debilibeuk.Value; character.GmData = _jsonUtil.Serialize(gmData); notificationSession.NotifyCharacterGmChangeData(character, gmData); } if (request.Mankdebol.HasValue) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); var gmData = _jsonUtil.Deserialize <CharacterGmData>(character.GmData) ?? new CharacterGmData(); gmData.Mankdebol = request.Mankdebol.Value; character.GmData = _jsonUtil.Serialize(gmData); notificationSession.NotifyCharacterGmChangeData(character, gmData); } if (request.IsActive.HasValue) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); character.IsActive = request.IsActive.Value; notificationSession.NotifyCharacterGmChangeActive(character); } if (request.Color != null) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); character.Color = request.Color; notificationSession.NotifyCharacterGmChangeColor(character); } if (request.OwnerId != null) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); character.OwnerId = request.OwnerId.Value; } if (request.Target != null) { _authorizationUtil.EnsureIsGroupOwner(executionContext, character.Group); if (request.Target.IsMonster) { character.TargetedCharacterId = null; character.TargetedMonsterId = request.Target.Id; } else { character.TargetedMonsterId = null; character.TargetedCharacterId = request.Target.Id; } notificationSession.NotifyCharacterGmChangeTarget(character, request.Target); } if (request.Ev.HasValue) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeEv(character, character.Ev, request.Ev)); character.Ev = request.Ev; notificationSession.NotifyCharacterChangeEv(character); } if (request.Ea.HasValue) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeEa(character, character.Ea, request.Ea)); character.Ea = request.Ea; notificationSession.NotifyCharacterChangeEa(character); } if (request.FatePoint.HasValue) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeFatePoint(character, character.FatePoint, request.FatePoint)); character.FatePoint = request.FatePoint.Value; notificationSession.NotifyCharacterChangeFatePoint(character); } if (request.Experience.HasValue) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeExperience(character, character.Experience, request.Experience)); character.Experience = request.Experience.Value; notificationSession.NotifyCharacterChangeExperience(character); } if (request.Sex != null) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeSex(character, character.Sex, request.Sex)); character.Sex = request.Sex; notificationSession.NotifyCharacterChangeSex(character); } if (request.Name != null) { character.AddHistoryEntry(_characterHistoryUtil.CreateLogChangeName(character, character.Name, request.Name)); character.Name = request.Name; notificationSession.NotifyCharacterChangeName(character); } if (request.Notes != null) { character.Notes = request.Notes; notificationSession.NotifyCharacterChangeNotes(character); } }