-
Notifications
You must be signed in to change notification settings - Fork 16
/
RefreshReplyHandler.cs
80 lines (65 loc) · 3.44 KB
/
RefreshReplyHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Domain.Notifiers;
using EOLib.Net;
using EOLib.Net.Handlers;
using EOLib.Net.Translators;
using System.Collections.Generic;
using System.Linq;
using DomainNPC = EOLib.Domain.NPC.NPC;
namespace EOLib.PacketHandlers.Refresh
{
/// <summary>
/// Sent when the map state (characters, npcs, and map items) should be refreshed
/// </summary>
[AutoMappedType]
public class RefreshReplyHandler : InGameOnlyPacketHandler
{
private readonly IPacketTranslator<RefreshReplyData> _refreshReplyTranslator;
private readonly ICharacterRepository _characterRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly IEnumerable<IMapChangedNotifier> _mapChangedNotifiers;
public override PacketFamily Family => PacketFamily.Refresh;
public override PacketAction Action => PacketAction.Reply;
public RefreshReplyHandler(IPlayerInfoProvider playerInfoProvider,
IPacketTranslator<RefreshReplyData> refreshReplyTranslator,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
IEnumerable<IMapChangedNotifier> mapChangedNotifiers)
: base(playerInfoProvider)
{
_refreshReplyTranslator = refreshReplyTranslator;
_characterRepository = characterRepository;
_currentMapStateRepository = currentMapStateRepository;
_mapChangedNotifiers = mapChangedNotifiers;
}
//todo: this is almost identical to EndPlayerWarpHandler - see if there is some way to share
public override bool HandlePacket(IPacket packet)
{
var data = _refreshReplyTranslator.TranslatePacket(packet);
var updatedMainCharacter = data.Characters.Single(IDMatches);
var updatedRenderProperties = _characterRepository.MainCharacter.RenderProperties
.WithMapX(updatedMainCharacter.RenderProperties.MapX)
.WithMapY(updatedMainCharacter.RenderProperties.MapY);
var withoutMainCharacter = data.Characters.Where(x => !IDMatches(x));
_characterRepository.MainCharacter = _characterRepository.MainCharacter
.WithRenderProperties(updatedRenderProperties);
_currentMapStateRepository.Characters = withoutMainCharacter.ToDictionary(k => k.ID, v => v);
_currentMapStateRepository.NPCs = new HashSet<DomainNPC>(data.NPCs);
_currentMapStateRepository.MapItems = new MapEntityCollectionHashSet<MapItem>(item => item.UniqueID, item => new MapCoordinate(item.X, item.Y), data.Items);
_currentMapStateRepository.OpenDoors.Clear();
_currentMapStateRepository.PendingDoors.Clear();
_currentMapStateRepository.VisibleSpikeTraps.Clear();
_currentMapStateRepository.MapWarpTime = Optional.Option.Some(System.DateTime.Now.AddMilliseconds(-100));
foreach (var notifier in _mapChangedNotifiers)
notifier.NotifyMapChanged(differentMapID: false, warpAnimation: WarpAnimation.None);
return true;
}
private bool IDMatches(Character x)
{
return x.ID == _characterRepository.MainCharacter.ID;
}
}
}