Skip to content

Commit

Permalink
Implement UseItem client action and quest item handlers. Remove old i…
Browse files Browse the repository at this point in the history
…tem related network code
  • Loading branch information
ethanmoffat committed Mar 29, 2022
1 parent 1c51aed commit 07d5019
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 96 deletions.
2 changes: 1 addition & 1 deletion EOBot/TrainerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ await TrySend(() =>
private async Task JunkItem(IItem item)
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.JunkItem, $"{item.Amount,7} - {_itemData.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _itemActions.JunkItem(item));
await TrySend(() => _itemActions.JunkItem(item.ItemID, item.Amount));
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
}

Expand Down
45 changes: 0 additions & 45 deletions EOLib/Net/API/Item.cs

This file was deleted.

1 change: 0 additions & 1 deletion EOLib/Net/API/PacketAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public PacketAPI(EOClient client)
_createChestMembers();
_createEmoteMembers();
_createInitMembers();
_createItemMembers();
_createLockerMembers();
_createMessageMembers();
_createMusicMembers();
Expand Down
83 changes: 83 additions & 0 deletions EOLib/PacketHandlers/Items/QuestItemChangeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Notifiers;
using EOLib.Net;
using EOLib.Net.Handlers;
using Optional.Collections;
using System.Collections.Generic;

namespace EOLib.PacketHandlers.Items
{
public abstract class QuestItemChangeHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICharacterInventoryRepository _inventoryRepository;

public override PacketFamily Family => PacketFamily.Item;

protected QuestItemChangeHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICharacterInventoryRepository inventoryRepository)
: base(playerInfoProvider)
{
_characterRepository = characterRepository;
_inventoryRepository = inventoryRepository;
}

public override bool HandlePacket(IPacket packet)
{
var id = packet.ReadShort();
var amount = packet.ReadThree();
var weight = packet.ReadChar();

var inventoryItem = _inventoryRepository.ItemInventory.SingleOrNone(x => x.ItemID == id);
inventoryItem.MatchSome(x => _inventoryRepository.ItemInventory.Remove(x));

if (amount > 0)
{
var amountRemaining = inventoryItem.Match(
some: x => Action == PacketAction.Kick ? x.Amount - amount : x.Amount + amount,
none: () => Action == PacketAction.Kick ? 0 : amount);

if (amountRemaining > 0)
{
inventoryItem.Map(x => x.WithAmount(amount))
.MatchSome(x => _inventoryRepository.ItemInventory.Add(x));
}
}

var stats = _characterRepository.MainCharacter.Stats;
stats = stats.WithNewStat(CharacterStat.Weight, weight);
_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);

return true;
}
}

[AutoMappedType]
public class ItemObtainHandler : QuestItemChangeHandler
{
public override PacketAction Action => PacketAction.Obtain;

public ItemObtainHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICharacterInventoryRepository inventoryRepository)
: base(playerInfoProvider, characterRepository, inventoryRepository)
{
}
}

[AutoMappedType]
public class ItemKickHandler : QuestItemChangeHandler
{
public override PacketAction Action => PacketAction.Kick;

public ItemKickHandler(IPlayerInfoProvider playerInfoProvider,
ICharacterRepository characterRepository,
ICharacterInventoryRepository inventoryRepository)
: base(playerInfoProvider, characterRepository, inventoryRepository)
{
}
}
}
90 changes: 50 additions & 40 deletions EndlessClient/Controllers/InventoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class InventoryController : IInventoryController
private readonly IPubFileProvider _pubFileProvider;
private readonly IHudControlProvider _hudControlProvider;
private readonly ICurrentMapStateProvider _currentMapStateProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IEIFFileProvider _eifFileProvider;
private readonly IActiveDialogProvider _activeDialogProvider;
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IItemTransferDialogFactory _itemTransferDialogFactory;
private readonly IEOMessageBoxFactory _eoMessageBoxFactory;
Expand All @@ -41,6 +44,9 @@ public InventoryController(IItemActions itemActions,
IPubFileProvider pubFileProvider,
IHudControlProvider hudControlProvider,
ICurrentMapStateProvider currentMapStateProvider,
ICurrentMapProvider currentMapProvider,
IEIFFileProvider eifFileProvider,
IActiveDialogProvider activeDialogProvider,
IStatusLabelSetter statusLabelSetter,
IItemTransferDialogFactory itemTransferDialogFactory,
IEOMessageBoxFactory eoMessageBoxFactory)
Expand All @@ -52,6 +58,9 @@ public InventoryController(IItemActions itemActions,
_pubFileProvider = pubFileProvider;
_hudControlProvider = hudControlProvider;
_currentMapStateProvider = currentMapStateProvider;
_currentMapProvider = currentMapProvider;
_eifFileProvider = eifFileProvider;
_activeDialogProvider = activeDialogProvider;
_statusLabelSetter = statusLabelSetter;
_itemTransferDialogFactory = itemTransferDialogFactory;
_eoMessageBoxFactory = eoMessageBoxFactory;
Expand All @@ -65,56 +74,60 @@ public void ShowPaperdollDialog()

public void UseItem(EIFRecord record)
{
var useItem = false;

var character = _characterProvider.MainCharacter;

switch (record.Type)
{
//usable items
case ItemType.Teleport:
//if (!OldWorld.Instance.ActiveMapRenderer.MapRef.Properties.CanScroll)
//{
// EOGame.Instance.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, EOResourceID.STATUS_LABEL_NOTHING_HAPPENED);
// break;
//}
//if (m_itemData.ScrollMap == OldWorld.Instance.MainPlayer.ActiveCharacter.CurrentMap &&
// m_itemData.ScrollX == OldWorld.Instance.MainPlayer.ActiveCharacter.X &&
// m_itemData.ScrollY == OldWorld.Instance.MainPlayer.ActiveCharacter.Y)
break; //already there - no need to scroll!
//useItem = true;
if (!_currentMapProvider.CurrentMap.Properties.CanScroll)
{
_statusLabelSetter.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_ACTION, EOResourceID.STATUS_LABEL_NOTHING_HAPPENED);
break;
}

if (record.ScrollMap == character.MapID && record.ScrollX == character.RenderProperties.MapX && record.ScrollY == character.RenderProperties.MapY)
break;

useItem = true;
break;
case ItemType.Heal:
case ItemType.HairDye:
case ItemType.Beer:
//useItem = true;
useItem = true;
break;

case ItemType.CureCurse:
//note: don't actually set the useItem bool here. Call API.UseItem if the dialog result is OK.
//if (c.PaperDoll.Select(id => OldWorld.Instance.EIF[id])
// .Any(rec => rec.Special == ItemSpecial.Cursed))
//{
// EOMessageBox.Show(DialogResourceID.ITEM_CURSE_REMOVE_PROMPT, EODialogButtons.OkCancel, EOMessageBoxStyle.SmallDialogSmallHeader,
// (o, e) =>
// {
// //if (e.Result == XNADialogResult.OK && !m_api.UseItem((short)m_itemData.ID))
// //{
// // ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
// //}
// });
//}
var paperdollItems = _paperdollProvider.VisibleCharacterPaperdolls[_characterProvider.MainCharacter.ID].Paperdoll.Values;
if (paperdollItems.Select(id => _eifFileProvider.EIFFile[id].Special).Any(s => s == ItemSpecial.Cursed))
{
var msgBox = _eoMessageBoxFactory.CreateMessageBox(DialogResourceID.ITEM_CURSE_REMOVE_PROMPT, EODialogButtons.OkCancel, EOMessageBoxStyle.SmallDialogSmallHeader);
msgBox.DialogClosing += (o, e) =>
{
if (e.Result == XNADialogResult.OK)
{
_itemActions.UseItem((short)record.ID);
}
};
msgBox.ShowDialog();
}
break;
case ItemType.EffectPotion:
case ItemType.EXPReward:
//useItem = true;
useItem = true;
break;
case ItemType.EffectPotion:
//useItem = true;
// Not implemented server - side
case ItemType.SkillReward:
case ItemType.StatReward:
break;
//Not implemented server-side
//case ItemType.SkillReward:
// break;
//case ItemType.StatReward:
// break;
}

//if (useItem && !m_api.UseItem((short)m_itemData.ID))
// ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
if (useItem)
{
_itemActions.UseItem((short)record.ID);
}
}

public void EquipItem(EIFRecord itemData)
Expand Down Expand Up @@ -205,14 +218,11 @@ public void UnequipItem(EquipLocation equipLocation)

public void DropItem(EIFRecord itemData, IInventoryItem inventoryItem)
{
/*
if (((OldEOInventory)parent).IsOverDrop() || (OldWorld.Instance.ActiveMapRenderer.MouseOver
//&& ChestDialog.Instance == null && EOPaperdollDialog.Instance == null && LockerDialog.Instance == null
&& BankAccountDialog.Instance == null && TradeDialog.Instance == null))*/
var mapRenderer = _hudControlProvider.GetComponent<IMapRenderer>(HudControlIdentifier.MapRenderer);
if (_activeDialogProvider.ActiveDialogs.Any() && mapRenderer.MouseOver)
return;

var rp = _characterProvider.MainCharacter.RenderProperties;

var mapRenderer = _hudControlProvider.GetComponent<IMapRenderer>(HudControlIdentifier.MapRenderer);
var dropPoint = mapRenderer.MouseOver
? mapRenderer.GridCoordinates
: new MapCoordinate(rp.MapX, rp.MapY);
Expand Down
9 changes: 0 additions & 9 deletions EndlessClient/Old/PacketAPICallbackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public void AssignCallbacks()
m_packetAPI.OnRecoverReply += _recoverReply;
m_packetAPI.OnPlayerHeal += _playerHeal;

//item related
m_packetAPI.OnItemChange += _itemChange;

m_packetAPI.OnMapMutation += _mapMutate;

//npc related
Expand Down Expand Up @@ -143,12 +140,6 @@ private void _playerHeal(short playerid, int healamount, byte percenthealth)
OldWorld.Instance.ActiveMapRenderer.OtherPlayerHeal(playerid, healamount, percenthealth);
}

private void _itemChange(bool wasItemObtained, short id, int amount, byte weight)
{
//OldWorld.Instance.MainPlayer.ActiveCharacter.UpdateInventoryItem(id, amount, weight,
// OldWorld.Instance.MainPlayer.ActiveCharacter.MaxWeight, wasItemObtained);
}

private void _mapMutate()
{
if (File.Exists("maps\\00000.emf"))
Expand Down

0 comments on commit 07d5019

Please sign in to comment.