Skip to content

Commit

Permalink
Use [Record] attribute for refresh/warp data objects and remove inter…
Browse files Browse the repository at this point in the history
…faces
  • Loading branch information
ethanmoffat committed Apr 28, 2022
1 parent c1249b6 commit 28edf4c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 151 deletions.
58 changes: 7 additions & 51 deletions EOLib/Domain/Map/RefreshReplyPacketData.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,23 @@
using EOLib.Net.Translators;
using Amadevus.RecordGenerator;
using EOLib.Net.Translators;
using System.Collections.Generic;

namespace EOLib.Domain.Map
{
public class RefreshReplyData : IRefreshReplyData
[Record]
public sealed partial class RefreshReplyData : ITranslatedData
{
public IReadOnlyList<Character.Character> Characters { get; private set; }
public IReadOnlyList<Character.Character> Characters { get; }

public IReadOnlyList<NPC.NPC> NPCs { get; private set; }
public IReadOnlyList<NPC.NPC> NPCs { get; }

public IReadOnlyList<MapItem> Items { get; private set; }
public IReadOnlyList<MapItem> Items { get; }

public RefreshReplyData()
{
Characters = new List<Character.Character>();
NPCs = new List<NPC.NPC>();
Items = new List<MapItem>();
}

public IRefreshReplyData WithCharacters(IEnumerable<Character.Character> characters)
{
var newData = MakeCopy(this);
newData.Characters = new List<Character.Character>(characters);
return newData;
}

public IRefreshReplyData WithNPCs(IEnumerable<NPC.NPC> npcs)
{
var newData = MakeCopy(this);
newData.NPCs = new List<NPC.NPC>(npcs);
return newData;
}

public IRefreshReplyData WithItems(IEnumerable<MapItem> items)
{
var newData = MakeCopy(this);
newData.Items = new List<MapItem>(items);
return newData;
}

private static RefreshReplyData MakeCopy(IRefreshReplyData source)
{
return new RefreshReplyData
{
Characters = new List<Character.Character>(source.Characters),
NPCs = new List<NPC.NPC>(source.NPCs),
Items = new List<MapItem>(source.Items)
};
}
}

public interface IRefreshReplyData : ITranslatedData
{
IReadOnlyList<Character.Character> Characters { get; }

IReadOnlyList<NPC.NPC> NPCs { get; }

IReadOnlyList<MapItem> Items { get; }

IRefreshReplyData WithCharacters(IEnumerable<Character.Character> characters);

IRefreshReplyData WithNPCs(IEnumerable<NPC.NPC> npcs);

IRefreshReplyData WithItems(IEnumerable<MapItem> items);
}
}
85 changes: 8 additions & 77 deletions EOLib/Domain/Map/WarpAgreePacketData.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,27 @@
using EOLib.Domain.Character;
using Amadevus.RecordGenerator;
using EOLib.Net.Translators;
using System.Collections.Generic;

namespace EOLib.Domain.Map
{
public class WarpAgreePacketData : IWarpAgreePacketData
[Record]
public sealed partial class WarpAgreePacketData : ITranslatedData
{
public short MapID { get; private set; }
public short MapID { get; }

public WarpAnimation WarpAnimation { get; private set; }
public WarpAnimation WarpAnimation { get; }

public IReadOnlyList<Character.Character> Characters { get; private set; }
public IReadOnlyList<Character.Character> Characters { get; }

public IReadOnlyList<NPC.NPC> NPCs { get; private set; }
public IReadOnlyList<NPC.NPC> NPCs { get; }

public IReadOnlyList<MapItem> Items { get; private set; }
public IReadOnlyList<MapItem> Items { get; }

public WarpAgreePacketData()
{
Characters = new List<Character.Character>();
NPCs = new List<NPC.NPC>();
Items = new List<MapItem>();
}

public IWarpAgreePacketData WithMapID(short mapID)
{
var newData = MakeCopy(this);
newData.MapID = mapID;
return newData;
}

public IWarpAgreePacketData WithWarpAnimation(WarpAnimation warpAnimation)
{
var newData = MakeCopy(this);
newData.WarpAnimation = warpAnimation;
return newData;
}

public IWarpAgreePacketData WithCharacters(IEnumerable<Character.Character> characters)
{
var newData = MakeCopy(this);
newData.Characters = new List<Character.Character>(characters);
return newData;
}

public IWarpAgreePacketData WithNPCs(IEnumerable<NPC.NPC> npcs)
{
var newData = MakeCopy(this);
newData.NPCs = new List<NPC.NPC>(npcs);
return newData;
}

public IWarpAgreePacketData WithItems(IEnumerable<MapItem> items)
{
var newData = MakeCopy(this);
newData.Items = new List<MapItem>(items);
return newData;
}

private static WarpAgreePacketData MakeCopy(IWarpAgreePacketData source)
{
return new WarpAgreePacketData
{
MapID = source.MapID,
WarpAnimation = source.WarpAnimation,
Characters = new List<Character.Character>(source.Characters),
NPCs = new List<NPC.NPC>(source.NPCs),
Items = new List<MapItem>(source.Items)
};
}
}

public interface IWarpAgreePacketData : ITranslatedData
{
short MapID { get; }

WarpAnimation WarpAnimation { get; }

IReadOnlyList<Character.Character> Characters { get; }

IReadOnlyList<NPC.NPC> NPCs { get; }

IReadOnlyList<MapItem> Items { get; }

IWarpAgreePacketData WithMapID(short mapID);

IWarpAgreePacketData WithWarpAnimation(WarpAnimation warpAnimation);

IWarpAgreePacketData WithCharacters(IEnumerable<Character.Character> characters);

IWarpAgreePacketData WithNPCs(IEnumerable<NPC.NPC> npcs);

IWarpAgreePacketData WithItems(IEnumerable<MapItem> items);
}
}
16 changes: 9 additions & 7 deletions EOLib/Net/Translators/RefreshReplyPacketTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using AutomaticTypeMapper;
using EOLib.Domain.Map;
using System.Linq;

namespace EOLib.Net.Translators
{
[AutoMappedType]
public class RefreshReplyPacketTranslator : MapStatePacketTranslator<IRefreshReplyData>
public class RefreshReplyPacketTranslator : MapStatePacketTranslator<RefreshReplyData>
{
public RefreshReplyPacketTranslator(ICharacterFromPacketFactory characterFromPacketFactory)
: base(characterFromPacketFactory) { }

public override IRefreshReplyData TranslatePacket(IPacket packet)
public override RefreshReplyData TranslatePacket(IPacket packet)
{
var characters = GetCharacters(packet);
var npcs = GetNPCs(packet);
var items = GetMapItems(packet);

IRefreshReplyData data = new RefreshReplyData();

return data.WithCharacters(characters)
.WithNPCs(npcs)
.WithItems(items);
return new RefreshReplyData.Builder
{
Characters = characters.ToList(),
NPCs = npcs.ToList(),
Items = items.ToList(),
}.ToImmutable();
}
}
}
21 changes: 11 additions & 10 deletions EOLib/Net/Translators/WarpAgreePacketTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using AutomaticTypeMapper;
using EOLib.Domain.Map;
using System.Linq;

namespace EOLib.Net.Translators
{
[AutoMappedType]
public class WarpAgreePacketTranslator : MapStatePacketTranslator<IWarpAgreePacketData>
public class WarpAgreePacketTranslator : MapStatePacketTranslator<WarpAgreePacketData>
{
public WarpAgreePacketTranslator(ICharacterFromPacketFactory characterFromPacketFactory)
: base(characterFromPacketFactory) { }

public override IWarpAgreePacketData TranslatePacket(IPacket packet)
public override WarpAgreePacketData TranslatePacket(IPacket packet)
{
IWarpAgreePacketData retData = new WarpAgreePacketData();

if (packet.ReadChar() != 2)
throw new MalformedPacketException("Missing initial marker value of 2", packet);

Expand All @@ -23,12 +22,14 @@ public override IWarpAgreePacketData TranslatePacket(IPacket packet)
var npcs = GetNPCs(packet);
var items = GetMapItems(packet);

return retData
.WithMapID(newMapID)
.WithWarpAnimation(warpAnim)
.WithCharacters(characters)
.WithNPCs(npcs)
.WithItems(items);
return new WarpAgreePacketData.Builder
{
MapID = newMapID,
WarpAnimation = warpAnim,
Characters = characters.ToList(),
NPCs = npcs.ToList(),
Items = items.ToList(),
}.ToImmutable();
}
}
}
6 changes: 3 additions & 3 deletions EOLib/PacketHandlers/EndPlayerWarpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace EOLib.PacketHandlers
[AutoMappedType]
public class EndPlayerWarpHandler : InGameOnlyPacketHandler
{
private readonly IPacketTranslator<IWarpAgreePacketData> _warpAgreePacketTranslator;
private readonly IPacketTranslator<WarpAgreePacketData> _warpAgreePacketTranslator;
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICurrentMapProvider _currentMapProvider;
Expand All @@ -30,7 +30,7 @@ public class EndPlayerWarpHandler : InGameOnlyPacketHandler
public override PacketAction Action => PacketAction.Agree;

public EndPlayerWarpHandler(IPlayerInfoProvider playerInfoProvider,
IPacketTranslator<IWarpAgreePacketData> warpAgreePacketTranslator,
IPacketTranslator<WarpAgreePacketData> warpAgreePacketTranslator,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
ICurrentMapProvider currentMapProvider,
Expand Down Expand Up @@ -62,7 +62,7 @@ public override bool HandlePacket(IPacket packet)
.WithAppliedData(updatedMainCharacter, _eifFileProvider.EIFFile.IsRangedWeapon(updatedMainCharacter.RenderProperties.WeaponGraphic));

var withoutMainCharacter = warpAgreePacketData.Characters.Where(x => !MainCharacterIDMatches(x));
warpAgreePacketData = warpAgreePacketData.WithCharacters(withoutMainCharacter);
warpAgreePacketData = warpAgreePacketData.WithCharacters(withoutMainCharacter.ToList());

var differentMapID = _currentMapStateRepository.CurrentMapID != warpAgreePacketData.MapID;

Expand Down
6 changes: 3 additions & 3 deletions EOLib/PacketHandlers/RefreshMapStateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace EOLib.PacketHandlers
[AutoMappedType]
public class RefreshMapStateHandler : InGameOnlyPacketHandler
{
private readonly IPacketTranslator<IRefreshReplyData> _refreshReplyTranslator;
private readonly IPacketTranslator<RefreshReplyData> _refreshReplyTranslator;
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IEnumerable<IMapChangedNotifier> _mapChangedNotifiers;
Expand All @@ -25,7 +25,7 @@ public class RefreshMapStateHandler : InGameOnlyPacketHandler
public override PacketAction Action => PacketAction.Reply;

public RefreshMapStateHandler(IPlayerInfoProvider playerInfoProvider,
IPacketTranslator<IRefreshReplyData> refreshReplyTranslator,
IPacketTranslator<RefreshReplyData> refreshReplyTranslator,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
IEnumerable<IMapChangedNotifier> mapChangedNotifiers)
Expand All @@ -48,7 +48,7 @@ public override bool HandlePacket(IPacket packet)
.WithMapY(updatedMainCharacter.RenderProperties.MapY);

var withoutMainCharacter = data.Characters.Where(x => !IDMatches(x));
data = data.WithCharacters(withoutMainCharacter);
data = data.WithCharacters(withoutMainCharacter.ToList());

_characterRepository.MainCharacter = _characterRepository.MainCharacter
.WithRenderProperties(updatedRenderProperties);
Expand Down

0 comments on commit 28edf4c

Please sign in to comment.