Skip to content

Commit

Permalink
Implement bank account dialog with Withdraw/Deposit/Upgrade actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 10, 2022
1 parent 96c2cce commit 8975081
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 224 deletions.
1 change: 1 addition & 0 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class Constants
public const byte ViewLength = 16;

public const int LockerMaxSingleItemAmount = 200;
public const int MaxLockerUpgrades = 7;
public const int PartyRequestTimeoutSeconds = 15;
public const int TradeRequestTimeoutSeconds = 15;
public const int MuteDefaultTimeMinutes = 5;
Expand Down
11 changes: 11 additions & 0 deletions EndlessClient/Controllers/NPCInteractionController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutomaticTypeMapper;
using EndlessClient.Dialogs;
using EndlessClient.Dialogs.Actions;
using EOLib.Domain.Interact;
using EOLib.Domain.NPC;
using EOLib.IO.Repositories;
Expand All @@ -11,14 +12,17 @@ namespace EndlessClient.Controllers
public class NPCInteractionController : INPCInteractionController
{
private readonly IMapNPCActions _mapNpcActions;
private readonly IInGameDialogActions _inGameDialogActions;
private readonly IENFFileProvider _enfFileProvider;
private readonly IActiveDialogProvider _activeDialogProvider;

public NPCInteractionController(IMapNPCActions mapNpcActions,
IInGameDialogActions inGameDialogActions,
IENFFileProvider enfFileProvider,
IActiveDialogProvider activeDialogProvider)
{
_mapNpcActions = mapNpcActions;
_inGameDialogActions = inGameDialogActions;
_enfFileProvider = enfFileProvider;
_activeDialogProvider = activeDialogProvider;
}
Expand All @@ -38,6 +42,13 @@ public void ShowNPCDialog(INPC npc)
case EOLib.IO.NPCType.Quest:
_mapNpcActions.RequestQuest(npc);
break;
case EOLib.IO.NPCType.Bank:
_mapNpcActions.RequestBank(npc);
// note: the npc action types rely on a server response to show the dialog because they are driven
// by config data on the server. Bank account dialog does not have this restriction;
// interaction with the NPC should *always* show the dialog
_inGameDialogActions.ShowBankAccountDialog();
break;
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions EndlessClient/Dialogs/Actions/InGameDialogActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using EOLib.Domain.Interact;
using EOLib.Domain.Interact.Quest;
using EOLib.Domain.Interact.Shop;
using EOLib.Domain.NPC;
using EOLib.IO;
using Optional;

Expand All @@ -22,6 +21,7 @@ public class InGameDialogActions : IInGameDialogActions, INPCInteractionNotifier
private readonly IQuestDataRepository _questDataRepository;
private readonly IChestDialogFactory _chestDialogFactory;
private readonly ILockerDialogFactory _lockerDialogFactory;
private readonly IBankAccountDialogFactory _bankAccountDialogFactory;
private readonly IShopDialogFactory _shopDialogFactory;
private readonly IQuestDialogFactory _questDialogFactory;

Expand All @@ -35,7 +35,8 @@ public InGameDialogActions(IFriendIgnoreListDialogFactory friendIgnoreListDialog
IShopDataRepository shopDataRepository,
IQuestDataRepository questDataRepository,
IChestDialogFactory chestDialogFactory,
ILockerDialogFactory lockerDialogFactory)
ILockerDialogFactory lockerDialogFactory,
IBankAccountDialogFactory bankAccountDialogFactory)
{
_friendIgnoreListDialogFactory = friendIgnoreListDialogFactory;
_paperdollDialogFactory = paperdollDialogFactory;
Expand All @@ -46,6 +47,7 @@ public InGameDialogActions(IFriendIgnoreListDialogFactory friendIgnoreListDialog
_questDataRepository = questDataRepository;
_chestDialogFactory = chestDialogFactory;
_lockerDialogFactory = lockerDialogFactory;
_bankAccountDialogFactory = bankAccountDialogFactory;
_shopDialogFactory = shopDialogFactory;
_questDialogFactory = questDialogFactory;
}
Expand Down Expand Up @@ -178,6 +180,18 @@ public void ShowLockerDialog()
dlg.Show();
});
}

public void ShowBankAccountDialog()
{
_activeDialogRepository.BankAccountDialog.MatchNone(() =>
{
var dlg = _bankAccountDialogFactory.Create();
dlg.DialogClosed += (_, _) => _activeDialogRepository.BankAccountDialog = Option.None<BankAccountDialog>();
_activeDialogRepository.BankAccountDialog = Option.Some(dlg);

dlg.Show();
});
}
}

public interface IInGameDialogActions
Expand All @@ -199,5 +213,7 @@ public interface IInGameDialogActions
void ShowChestDialog();

void ShowLockerDialog();

void ShowBankAccountDialog();
}
}
8 changes: 8 additions & 0 deletions EndlessClient/Dialogs/ActiveDialogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IActiveDialogProvider : IDisposable

Option<LockerDialog> LockerDialog { get; }

Option<BankAccountDialog> BankAccountDialog { get; }

IReadOnlyList<Option<IXNADialog>> ActiveDialogs { get; }
}

Expand All @@ -46,6 +48,8 @@ public interface IActiveDialogRepository : IDisposable

Option<LockerDialog> LockerDialog { get; set; }

Option<BankAccountDialog> BankAccountDialog { get; set; }

IReadOnlyList<Option<IXNADialog>> ActiveDialogs { get; }
}

Expand All @@ -68,6 +72,8 @@ public class ActiveDialogRepository : IActiveDialogRepository, IActiveDialogProv

public Option<LockerDialog> LockerDialog { get; set; }

public Option<BankAccountDialog> BankAccountDialog { get; set; }

IReadOnlyList<Option<IXNADialog>> ActiveDialogs
{
get
Expand All @@ -82,6 +88,7 @@ IReadOnlyList<Option<IXNADialog>> ActiveDialogs
QuestDialog.Map(d => (IXNADialog)d),
ChestDialog.Map(d => (IXNADialog)d),
LockerDialog.Map(d => (IXNADialog)d),
BankAccountDialog.Map(d => (IXNADialog)d),
}.ToList();
}
}
Expand All @@ -103,6 +110,7 @@ public void Dispose()
QuestDialog = Option.None<QuestDialog>();
ChestDialog = Option.None<ChestDialog>();
LockerDialog = Option.None<LockerDialog>();
BankAccountDialog = Option.None<BankAccountDialog>();
}
}
}
243 changes: 243 additions & 0 deletions EndlessClient/Dialogs/BankAccountDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
using EndlessClient.ControlSets;
using EndlessClient.Dialogs.Factories;
using EndlessClient.Dialogs.Services;
using EndlessClient.HUD;
using EndlessClient.HUD.Controls;
using EndlessClient.HUD.Panels;
using EOLib;
using EOLib.Domain.Character;
using EOLib.Domain.Interact.Bank;
using EOLib.Graphics;
using EOLib.IO.Repositories;
using EOLib.Localization;
using Microsoft.Xna.Framework;
using Optional;
using Optional.Collections;
using System;
using XNAControls;

namespace EndlessClient.Dialogs
{
public class BankAccountDialog : ScrollingListDialog
{
private readonly IBankActions _bankActions;
private readonly ILocalizedStringFinder _localizedStringFinder;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly IItemTransferDialogFactory _itemTransferDialogFactory;
private readonly IBankDataProvider _bankDataProvider;
private readonly ICharacterInventoryProvider _characterInventoryProvider;
private readonly IEIFFileProvider _eifFileProvider;
private readonly InventoryPanel _inventoryPanel;

private int _cachedValue;
private Option<int> _cachedUpgrades;

public BankAccountDialog(INativeGraphicsManager nativeGraphicsManager,
IBankActions bankActions,
IEODialogButtonService dialogButtonService,
IEODialogIconService dialogIconService,
ILocalizedStringFinder localizedStringFinder,
IStatusLabelSetter statusLabelSetter,
IEOMessageBoxFactory messageBoxFactory,
IItemTransferDialogFactory itemTransferDialogFactory,
IHudControlProvider hudControlProvider,
IBankDataProvider bankDataProvider,
ICharacterInventoryProvider characterInventoryProvider,
IEIFFileProvider eifFileProvider)
: base(nativeGraphicsManager, dialogButtonService, dialogSize: ScrollingListDialogSize.SmallNoScroll)
{
_bankActions = bankActions;
_localizedStringFinder = localizedStringFinder;
_statusLabelSetter = statusLabelSetter;
_messageBoxFactory = messageBoxFactory;
_itemTransferDialogFactory = itemTransferDialogFactory;
_bankDataProvider = bankDataProvider;
_characterInventoryProvider = characterInventoryProvider;
_eifFileProvider = eifFileProvider;
_inventoryPanel = hudControlProvider.GetComponent<InventoryPanel>(HudControlIdentifier.InventoryPanel);

ListItemType = ListDialogItem.ListItemStyle.Large;
Buttons = ScrollingListDialogButtons.Cancel;

_titleText.Text = "0";
_titleText.TextAlign = LabelAlignment.MiddleRight;
_titleText.AutoSize = false;

var currencyName = _eifFileProvider.EIFFile[1].Name;

var depositItem = new ListDialogItem(this, ListDialogItem.ListItemStyle.Large, 0)
{
PrimaryText = _localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_DEPOSIT),
SubText = $"{_localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_TRANSFER)} {currencyName} {_localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_TO_ACCOUNT)}",
IconGraphic = dialogIconService.IconSheet,
IconGraphicSource = dialogIconService.GetDialogIconSource(DialogIcon.BankDeposit),
OffsetY = 55,
ShowIconBackGround = false,
};
depositItem.LeftClick += Deposit;
depositItem.RightClick += Deposit;

var withdrawItem = new ListDialogItem(this, ListDialogItem.ListItemStyle.Large, 1)
{
PrimaryText = _localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_WITHDRAW),
SubText = $"{_localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_TRANSFER)} {currencyName} {_localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_FROM_ACCOUNT)}",
IconGraphic = dialogIconService.IconSheet,
IconGraphicSource = dialogIconService.GetDialogIconSource(DialogIcon.BankWithdraw),
OffsetY = 55,
ShowIconBackGround = false,
};
withdrawItem.LeftClick += Withdraw;
withdrawItem.RightClick += Withdraw;

var upgradeItem = new ListDialogItem(this, ListDialogItem.ListItemStyle.Large, 2)
{
PrimaryText = _localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_LOCKER_UPGRADE),
SubText = _localizedStringFinder.GetString(EOResourceID.DIALOG_BANK_MORE_SPACE),
IconGraphic = dialogIconService.IconSheet,
IconGraphicSource = dialogIconService.GetDialogIconSource(DialogIcon.BankLockerUpgrade),
OffsetY = 55,
ShowIconBackGround = false,
};
upgradeItem.LeftClick += Upgrade;
upgradeItem.RightClick += Upgrade;

AddItemToList(depositItem, sortList: false);
AddItemToList(withdrawItem, sortList: false);
AddItemToList(upgradeItem, sortList: false);

DrawPosition += new Vector2(0, 50);
}

protected override void OnUpdateControl(GameTime gameTime)
{
if (_bankDataProvider.AccountValue != _cachedValue)
{
_cachedValue = _bankDataProvider.AccountValue;
Title = $"{_bankDataProvider.AccountValue}";
}

_cachedUpgrades.Match(
some: c =>
{
_bankDataProvider.LockerUpgrades.MatchSome(
upgrades =>
{
if (upgrades != c)
{
_cachedUpgrades = _bankDataProvider.LockerUpgrades;
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_INFORMATION, EOResourceID.STATUS_LABEL_LOCKER_SPACE_INCREASED);
}
});
},
none: () => _cachedUpgrades = _bankDataProvider.LockerUpgrades);

SuppressClickDragEvent(!_inventoryPanel.NoItemsDragging());

base.OnUpdateControl(gameTime);
}

private void Deposit(object sender, EventArgs e)
{
_characterInventoryProvider.ItemInventory.SingleOrNone(x => x.ItemID == 1)
.Match(
some: characterGold =>
{
if (characterGold.Amount == 0)
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.BANK_ACCOUNT_UNABLE_TO_DEPOSIT);
dlg.ShowDialog();

// todo: show the dialog message as a warning in the status label
}
else if (characterGold.Amount == 1)
{
_bankActions.Deposit(1);
}
else if (characterGold.Amount > 1)
{
var dlg = _itemTransferDialogFactory.CreateItemTransferDialog(_eifFileProvider.EIFFile[1].Name, ItemTransferDialog.TransferType.BankTransfer, characterGold.Amount, EOResourceID.DIALOG_TRANSFER_DEPOSIT);
dlg.DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.OK)
_bankActions.Deposit(dlg.SelectedAmount);
};
dlg.ShowDialog();
}
},
none: () =>
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.BANK_ACCOUNT_UNABLE_TO_DEPOSIT);
dlg.ShowDialog();

// todo: show the dialog message as a warning in the status label
});
}

private void Withdraw(object sender, EventArgs e)
{
if (_bankDataProvider.AccountValue == 0)
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.BANK_ACCOUNT_UNABLE_TO_WITHDRAW);
dlg.ShowDialog();

// todo: show the dialog message as a warning in the status label
}
else if (_bankDataProvider.AccountValue == 1)
{
_bankActions.Withdraw(1);
}
else if (_bankDataProvider.AccountValue > 1)
{
var dlg = _itemTransferDialogFactory.CreateItemTransferDialog(_eifFileProvider.EIFFile[1].Name, ItemTransferDialog.TransferType.BankTransfer, _bankDataProvider.AccountValue, EOResourceID.DIALOG_TRANSFER_WITHDRAW);
dlg.DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.OK)
_bankActions.Withdraw(dlg.SelectedAmount);
};
dlg.ShowDialog();
}
}

private void Upgrade(object sender, EventArgs e)
{
_bankDataProvider.LockerUpgrades.MatchSome(lockerUpgrades =>
{
if (lockerUpgrades == Constants.MaxLockerUpgrades)
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.LOCKER_UPGRADE_IMPOSSIBLE);
dlg.ShowDialog();
return;
}

int requiredGold = (lockerUpgrades + 1) * 1000;

_characterInventoryProvider.ItemInventory.SingleOrNone(x => x.ItemID == 1)
.Match(
some: charaterGold =>
{
if (charaterGold.Amount < requiredGold)
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.WARNING_YOU_HAVE_NOT_ENOUGH, $" {_eifFileProvider.EIFFile[1].Name}");
dlg.ShowDialog();
}
else
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.LOCKER_UPGRADE_UNIT, $"{requiredGold} {_eifFileProvider.EIFFile[1].Name}?", EODialogButtons.OkCancel);
dlg.DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.OK)
_bankActions.BuyStorageUpgrade();
};
dlg.ShowDialog();
}
},
() =>
{
var dlg = _messageBoxFactory.CreateMessageBox(DialogResourceID.WARNING_YOU_HAVE_NOT_ENOUGH, $" {_eifFileProvider.EIFFile[1].Name}");
dlg.ShowDialog();
});
});
}
}
}
Loading

0 comments on commit 8975081

Please sign in to comment.