Skip to content

Commit

Permalink
Implement inn sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed May 25, 2023
1 parent bd01cdf commit 913e5ff
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 18 deletions.
25 changes: 25 additions & 0 deletions EOLib/Domain/Interact/Citizen/CitizenActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ public CitizenActions(IPacketSendService packetSendService,
_currentMapStateProvider = currentMapStateProvider;
}

public void RequestSleep()
{
var packet = new PacketBuilder(PacketFamily.Citizen, PacketAction.Request)
.AddShort(_currentMapStateProvider.MapWarpSession.ValueOr(0))
.AddShort(_citizenDataProvider.BehaviorID.ValueOr(0))
.Build();

_packetSendService.SendPacket(packet);

}

public void ConfirmSleep()
{
var packet = new PacketBuilder(PacketFamily.Citizen, PacketAction.Accept)
.AddShort(_currentMapStateProvider.MapWarpSession.ValueOr(0))
.AddShort(_citizenDataProvider.BehaviorID.ValueOr(0))
.Build();

_packetSendService.SendPacket(packet);
}

public void SignUp(IReadOnlyList<string> answers)
{
var packet = new PacketBuilder(PacketFamily.Citizen, PacketAction.Reply)
Expand Down Expand Up @@ -49,6 +70,10 @@ public void Unsubscribe()

public interface ICitizenActions
{
void RequestSleep();

void ConfirmSleep();

void SignUp(IReadOnlyList<string> answers);

void Unsubscribe();
Expand Down
4 changes: 4 additions & 0 deletions EOLib/Domain/Interact/INPCInteractionNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface INPCInteractionNotifier
void NotifyCitizenUnsubscribe(CitizenUnsubscribeReply reply);

void NotifyCitizenSignUp(int questionsWrong);

void NotifyCitizenRequestSleep(int sleepCost);
}

[AutoMappedType]
Expand All @@ -38,5 +40,7 @@ public void NotifyStatReset() { }
public void NotifyCitizenUnsubscribe(CitizenUnsubscribeReply reply) { }

public void NotifyCitizenSignUp(int questionsWrong) { }

public void NotifyCitizenRequestSleep(int sleepCost) { }
}
}
7 changes: 7 additions & 0 deletions EOLib/Domain/Map/CurrentMapStateRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface ICurrentMapStateRepository

Option<DateTime> MapWarpTime { get; set; }

bool IsSleepWarp { get; set; }

HashSet<int> UnknownPlayerIDs { get; set; }

HashSet<int> UnknownNPCIndexes { get; set; }
Expand Down Expand Up @@ -70,6 +72,8 @@ public interface ICurrentMapStateProvider

Option<DateTime> MapWarpTime { get; }

bool IsSleepWarp { get; }

HashSet<int> UnknownPlayerIDs { get; }

HashSet<int> UnknownNPCIndexes { get; }
Expand Down Expand Up @@ -106,6 +110,8 @@ public class CurrentMapStateRepository : ICurrentMapStateRepository, ICurrentMap

public Option<DateTime> MapWarpTime { get; set; }

public bool IsSleepWarp { get; set; }

public HashSet<int> UnknownPlayerIDs { get; set; }

public HashSet<int> UnknownNPCIndexes { get; set; }
Expand Down Expand Up @@ -145,6 +151,7 @@ public void ResetState()
MapWarpState = WarpState.None;
MapWarpSession = Option.None<int>();
MapWarpID = Option.None<int>();
IsSleepWarp = false;
}
}
}
51 changes: 51 additions & 0 deletions EOLib/PacketHandlers/Citizen/CitizenAcceptHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
using EOLib.Net;
using EOLib.Net.Handlers;

namespace EOLib.PacketHandlers.Citizen
{
/// <summary>
/// Sent when the player has accepted the cost of sleeping at an inn
/// </summary>
[AutoMappedType]
public class CitizenAcceptHandler : InGameOnlyPacketHandler
{
private readonly ICharacterInventoryRepository _characterInventoryRepository;
private readonly ICurrentMapStateRepository _currentMapStateRepository;
private readonly ICharacterRepository _characterRepository;

public override PacketFamily Family => PacketFamily.Citizen;

public override PacketAction Action => PacketAction.Accept;

public CitizenAcceptHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterInventoryRepository characterInventoryRepository,
ICurrentMapStateRepository currentMapStateRepository,
ICharacterRepository characterRepository)
: base(playerInfoProvider)
{
_characterInventoryRepository = characterInventoryRepository;
_currentMapStateRepository = currentMapStateRepository;
_characterRepository = characterRepository;
}

public override bool HandlePacket(IPacket packet)
{
var goldRemaining = packet.ReadInt();
_characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == 1);
_characterInventoryRepository.ItemInventory.Add(new InventoryItem(1, goldRemaining));

var stats = _characterRepository.MainCharacter.Stats;
stats = stats.WithNewStat(CharacterStat.HP, stats[CharacterStat.MaxHP])
.WithNewStat(CharacterStat.TP, stats[CharacterStat.MaxTP]);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);

_currentMapStateRepository.IsSleepWarp = true;

return true;
}
}
}
39 changes: 39 additions & 0 deletions EOLib/PacketHandlers/Citizen/CitizenRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AutomaticTypeMapper;
using EOLib.Domain.Interact;
using EOLib.Domain.Login;
using EOLib.Net;
using EOLib.Net.Handlers;
using System.Collections.Generic;

namespace EOLib.PacketHandlers.Citizen
{
/// <summary>
/// Sent when requesting to sleep at an inn
/// </summary>
[AutoMappedType]
public class CitizenRequestHandler : InGameOnlyPacketHandler
{
private readonly IEnumerable<INPCInteractionNotifier> _npcInteractionNotifiers;

public override PacketFamily Family => PacketFamily.Citizen;

public override PacketAction Action => PacketAction.Request;

public CitizenRequestHandler(IPlayerInfoProvider playerInfoProvider,
IEnumerable<INPCInteractionNotifier> npcInteractionNotifiers)
: base(playerInfoProvider)
{
_npcInteractionNotifiers = npcInteractionNotifiers;
}

public override bool HandlePacket(IPacket packet)
{
var sleepCost = packet.ReadInt();

foreach (var notifier in _npcInteractionNotifiers)
notifier.NotifyCitizenRequestSleep(sleepCost);

return true;
}
}
}
1 change: 1 addition & 0 deletions EOLib/PacketHandlers/Warp/WarpAgreeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public override bool HandlePacket(IPacket packet)

_currentMapStateRepository.MapWarpState = WarpState.None;
_currentMapStateRepository.MapWarpTime = Option.Some(DateTime.Now);
_currentMapStateRepository.IsSleepWarp = false;

return true;
}
Expand Down
20 changes: 20 additions & 0 deletions EndlessClient/Dialogs/Actions/NpcInteractionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
using EOLib.IO;
using EOLib.IO.Repositories;
using EOLib.Localization;
using XNAControls;

namespace EndlessClient.Dialogs.Actions
{
[AutoMappedType]
public class NPCInteractionActions : INPCInteractionNotifier
{
private readonly IInGameDialogActions _inGameDialogActions;
private readonly ICitizenActions _citizenActions;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly ILocalizedStringFinder _localizedStringFinder;
private readonly IECFFileProvider _ecfFileProvider;
private readonly ISfxPlayer _sfxPlayer;

public NPCInteractionActions(IInGameDialogActions inGameDialogActions,
ICitizenActions citizenActions,
IEOMessageBoxFactory messageBoxFactory,
ILocalizedStringFinder localizedStringFinder,
IECFFileProvider ecfFileProvider,
ISfxPlayer sfxPlayer)
{
_inGameDialogActions = inGameDialogActions;
_citizenActions = citizenActions;
_messageBoxFactory = messageBoxFactory;
_localizedStringFinder = localizedStringFinder;
_ecfFileProvider = ecfFileProvider;
Expand Down Expand Up @@ -110,5 +114,21 @@ public void NotifyCitizenSignUp(int questionsWrong)
dlg.ShowDialog();
}
}

public void NotifyCitizenRequestSleep(int sleepCost)
{
var message = $"{_localizedStringFinder.GetString(EOResourceID.INN_A_GOOD_NIGHT_REST_WILL_COST_YOU)} {sleepCost} Gold";

var dlg = _messageBoxFactory.CreateMessageBox(message, _localizedStringFinder.GetString(EOResourceID.INN_SLEEP), EODialogButtons.OkCancel);
dlg.DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.OK)
{
_citizenActions.ConfirmSleep();
}
};

dlg.ShowDialog();
}
}
}
5 changes: 5 additions & 0 deletions EndlessClient/Dialogs/BaseEODialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public override void CenterInGameView()
if (_isInGame() && !Game.Window.AllowUserResizing)
DrawPosition = new Vector2(DrawPosition.X, (330 - DrawArea.Height)/2f);
}

public void Close()
{
Close(XNADialogResult.NO_BUTTON_PRESSED);
}
}
}
9 changes: 2 additions & 7 deletions EndlessClient/Dialogs/InnkeeperDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ private void SetState(InnkeeperDialogState state)
SubText = _localizedStringFinder.GetString(EOResourceID.INN_FULL_HP_RECOVERY),
OffsetY = 45,
};
sleepItem.LeftClick += Sleep_Click;
sleepItem.RightClick += Sleep_Click;
sleepItem.LeftClick += (_, _) => _citizenActions.RequestSleep();
sleepItem.RightClick += (_, _) => _citizenActions.RequestSleep();

SetItemList(new List<ListDialogItem> { registrationItem, sleepItem });
}
Expand Down Expand Up @@ -246,10 +246,5 @@ private void SetState(InnkeeperDialogState state)
case InnkeeperDialogState.Sleep: break;
}
}

private void Sleep_Click(object sender, MouseEventArgs e)
{
// todo: inn sleeping
}
}
}
5 changes: 5 additions & 0 deletions EndlessClient/Rendering/Factories/MapRendererFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutomaticTypeMapper;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EndlessClient.Rendering.Character;
using EndlessClient.Rendering.Effects;
Expand All @@ -21,6 +22,7 @@ public class MapRendererFactory : IMapRendererFactory
private readonly IMapEntityRendererProvider _mapEntityRendererProvider;
private readonly ICharacterProvider _characterProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IContentProvider _contentProvider;
private readonly IMapRenderDistanceCalculator _mapRenderDistanceCalculator;
private readonly ICharacterRendererUpdater _characterRendererUpdater;
private readonly INPCRendererUpdater _npcRendererUpdater;
Expand All @@ -37,6 +39,7 @@ public MapRendererFactory(IEndlessGameProvider endlessGameProvider,
IMapEntityRendererProvider mapEntityRendererProvider,
ICharacterProvider characterProvider,
ICurrentMapProvider currentMapProvider,
IContentProvider contentProvider,
IMapRenderDistanceCalculator mapRenderDistanceCalculator,
ICharacterRendererUpdater characterRendererUpdater,
INPCRendererUpdater npcRendererUpdater,
Expand All @@ -53,6 +56,7 @@ public MapRendererFactory(IEndlessGameProvider endlessGameProvider,
_mapEntityRendererProvider = mapEntityRendererProvider;
_characterProvider = characterProvider;
_currentMapProvider = currentMapProvider;
_contentProvider = contentProvider;
_mapRenderDistanceCalculator = mapRenderDistanceCalculator;
_characterRendererUpdater = characterRendererUpdater;
_npcRendererUpdater = npcRendererUpdater;
Expand All @@ -72,6 +76,7 @@ public IMapRenderer CreateMapRenderer()
_mapEntityRendererProvider,
_characterProvider,
_currentMapProvider,
_contentProvider,
_mapRenderDistanceCalculator,
_characterRendererUpdater,
_npcRendererUpdater,
Expand Down
2 changes: 1 addition & 1 deletion EndlessClient/Rendering/Map/IMapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IMapRenderer : IGameComponent, IDrawable

bool MouseOver { get; }

void StartMapTransition();
void StartMapTransition(bool isSleep);

void StartEarthquake(int strength);

Expand Down
Loading

0 comments on commit 913e5ff

Please sign in to comment.