Skip to content

Commit

Permalink
Add API/handling for junking items. Junk picked up items in TrainerBo…
Browse files Browse the repository at this point in the history
…t if specified.
  • Loading branch information
ethanmoffat committed May 12, 2021
1 parent f3dc5ef commit 60194d8
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 65 deletions.
29 changes: 26 additions & 3 deletions EOBot/TrainerBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ internal class TrainerBot : BotBase
private const int WALK_BACKOFF_MS = 400;
private const int FACE_BACKOFF_MS = 120;

private static readonly int[] JunkItemIds = new[]
{
// Dragon Blade, enchanted boots (red/green/blue)
37, 124, 125, 126
};

private readonly string _account;
private readonly string _password;
private readonly string _character;
Expand Down Expand Up @@ -275,13 +281,30 @@ private async Task PickUpItems(IMapCellState cellState)
{
foreach (var item in cellState.Items)
{
Console.WriteLine($"[TAKE] {item.Amount,7} - {_itemData.Data.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _mapActions.PickUpItem(item));
await PickUpItem(item);

await Task.Delay(TimeSpan.FromMilliseconds(100));
if (JunkItemIds.Contains(item.ItemID))
{
await Task.Delay(TimeSpan.FromSeconds(1));
await JunkItem(item);
}
}
}

private async Task PickUpItem(IItem item)
{
Console.WriteLine($"[TAKE] {item.Amount,7} - {_itemData.Data.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _mapActions.PickUpItem(item));
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
}

private async Task JunkItem(IItem item)
{
Console.WriteLine($"[JUNK] {item.Amount,7} - {_itemData.Data.Single(x => x.ID == item.ItemID).Name}");
await TrySend(() => _mapActions.JunkItem(item));
await Task.Delay(TimeSpan.FromMilliseconds(ATTACK_BACKOFF_MS));
}

private async Task CastHealSpell(IEnumerable<IInventorySpell> healSpells)
{
var spellToUse = _spellData.Data
Expand Down
13 changes: 13 additions & 0 deletions EOLib/Domain/Map/MapActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,25 @@ public ItemPickupResult PickUpItem(IItem item)

return pickupResult;
}

// todo: item stuff probably belongs in its own action class
public void JunkItem(IItem item)
{
var packet = new PacketBuilder(PacketFamily.Item, PacketAction.Junk)
.AddShort(item.ItemID)
.AddInt(item.Amount)
.Build();

_packetSendService.SendPacket(packet);
}
}

public interface IMapActions
{
void RequestRefresh();

ItemPickupResult PickUpItem(IItem item);

void JunkItem(IItem item);
}
}
1 change: 1 addition & 0 deletions EOLib/EOLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
<Compile Include="PacketHandlers\Effects\MapQuakeHandler.cs" />
<Compile Include="PacketHandlers\EndPlayerWarpHandler.cs" />
<Compile Include="PacketHandlers\ItemPickupHandler.cs" />
<Compile Include="PacketHandlers\Items\JunkItemHandler.cs" />
<Compile Include="PacketHandlers\Items\UseItemHandler.cs" />
<Compile Include="PacketHandlers\PlayerSitHandler.cs" />
<Compile Include="PacketHandlers\MainPlayerWalkHandler.cs" />
Expand Down
40 changes: 0 additions & 40 deletions EOLib/Net/API/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,18 @@ partial class PacketAPI
/// </summary>
public event PlayerItemDropEvent OnDropItem;
public event RemoveMapItemEvent OnRemoveItemFromMap;
public event JunkItemEvent OnJunkItem;
public event ItemChangeEvent OnItemChange;

private void _createItemMembers()
{
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Drop), _handleItemDrop, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Add), _handleItemAdd, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Remove), _handleItemRemove, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Junk), _handleItemJunk, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Obtain), _handleItemObtain, true);
m_client.AddPacketHandler(new FamilyActionPair(PacketFamily.Item, PacketAction.Kick), _handleItemKick, true);
//todo: handle ITEM_ACCEPT (ExpReward item type) (I think it shows the level up animation?)
}

/// <summary>
/// Pick up the item with the specified UID
/// </summary>
public bool GetItem(short uid)
{
if (!m_client.ConnectedAndInitialized || !Initialized)
return false;

OldPacket pkt = new OldPacket(PacketFamily.Item, PacketAction.Get);
pkt.AddShort(uid);

return m_client.SendPacket(pkt);
}

public bool DropItem(short id, int amount, byte x = 255, byte y = 255) //255 means use character's current location
{
if (!m_client.ConnectedAndInitialized || !Initialized)
Expand All @@ -65,18 +49,6 @@ public bool GetItem(short uid)

return m_client.SendPacket(pkt);
}

public bool JunkItem(short id, int amount)
{
if (!m_client.ConnectedAndInitialized || !Initialized)
return false;

OldPacket pkt = new OldPacket(PacketFamily.Item, PacketAction.Junk);
pkt.AddShort(id);
pkt.AddInt(amount);

return m_client.SendPacket(pkt);
}

private void _handleItemDrop(OldPacket pkt)
{
Expand Down Expand Up @@ -124,18 +96,6 @@ private void _handleItemRemove(OldPacket pkt)
OnRemoveItemFromMap(pkt.GetShort());
}

private void _handleItemJunk(OldPacket pkt)
{
short id = pkt.GetShort();
int amountRemoved = pkt.GetThree();//don't really care - just math it
int amountRemaining = pkt.GetInt();
byte weight = pkt.GetChar();
byte maxWeight = pkt.GetChar();

if (OnJunkItem != null)
OnJunkItem(id, amountRemoved, amountRemaining, weight, maxWeight);
}

private void _handleItemObtain(OldPacket pkt)
{
if (OnItemChange == null) return;
Expand Down
60 changes: 60 additions & 0 deletions EOLib/PacketHandlers/Items/JunkItemHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Extensions;
using EOLib.Net;
using EOLib.Net.Handlers;

namespace EOLib.PacketHandlers.Items
{
[AutoMappedType]
public class JunkItemHandler : InGameOnlyPacketHandler
{
private readonly ICharacterRepository _characterRepository;
private readonly ICharacterInventoryRepository _inventoryRepository;

public override PacketFamily Family => PacketFamily.Item;

public override PacketAction Action => PacketAction.Junk;

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

public override bool HandlePacket(IPacket packet)
{
var id = packet.ReadShort();
var amountRemoved = packet.ReadThree();
var amountRemaining = packet.ReadInt();
var weight = packet.ReadChar();
var maxWeight = packet.ReadChar();

var inventoryItem = _inventoryRepository.ItemInventory.OptionalSingle(x => x.ItemID == id);
if (inventoryItem.HasValue)
{
_inventoryRepository.ItemInventory.Remove(inventoryItem.Value);

if (amountRemaining > 0)
{
var updatedItem = inventoryItem.Value.WithAmount(amountRemaining);
_inventoryRepository.ItemInventory.Add(updatedItem);
}
}

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

_characterRepository.MainCharacter = _characterRepository.MainCharacter.WithStats(stats);

// todo: notify client for status message (see commented out _junkItem() in PacketApiCallbackManager)

return true;
}
}
}
24 changes: 12 additions & 12 deletions EndlessClient/HUD/Inventory/OldEOInventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ public override void Update(GameTime gameTime)
}
else if (((OldEOInventory)parent).IsOverJunk())
{
if (m_inventory.Amount > 1)
{
ItemTransferDialog dlg = new ItemTransferDialog(m_itemData.Name, ItemTransferDialog.TransferType.JunkItems,
m_inventory.Amount, EOResourceID.DIALOG_TRANSFER_JUNK);
dlg.DialogClosing += (sender, args) =>
{
if (args.Result == XNADialogResult.OK && !m_api.JunkItem(m_inventory.ItemID, dlg.SelectedAmount))
((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
};
}
else if (!m_api.JunkItem(m_inventory.ItemID, 1))
((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
//if (m_inventory.Amount > 1)
//{
// ItemTransferDialog dlg = new ItemTransferDialog(m_itemData.Name, ItemTransferDialog.TransferType.JunkItems,
// m_inventory.Amount, EOResourceID.DIALOG_TRANSFER_JUNK);
// dlg.DialogClosing += (sender, args) =>
// {
// if (args.Result == XNADialogResult.OK && !m_api.JunkItem(m_inventory.ItemID, dlg.SelectedAmount))
// ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
// };
//}
//else if (!m_api.JunkItem(m_inventory.ItemID, 1))
// ((EOGame)Game).DoShowLostConnectionDialogAndReturnToMainMenu();
}
else if (ChestDialog.Instance != null && ChestDialog.Instance.MouseOver && ChestDialog.Instance.MouseOverPreviously)
{
Expand Down
19 changes: 9 additions & 10 deletions EndlessClient/Old/PacketAPICallbackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void AssignCallbacks()

//item related
m_packetAPI.OnRemoveItemFromMap += _removeItemFromMap;
m_packetAPI.OnJunkItem += _junkItem;
m_packetAPI.OnDropItem += _dropItem;
m_packetAPI.OnItemChange += _itemChange;

Expand Down Expand Up @@ -206,16 +205,16 @@ private void _playerHeal(short playerid, int healamount, byte percenthealth)
OldWorld.Instance.ActiveMapRenderer.OtherPlayerHeal(playerid, healamount, percenthealth);
}

private void _junkItem(short id, int amountRemoved, int amountRemaining, byte weight, byte maxWeight)
{
OldWorld.Instance.MainPlayer.ActiveCharacter.UpdateInventoryItem(id, amountRemaining, weight, maxWeight);
//private void _junkItem(short id, int amountRemoved, int amountRemaining, byte weight, byte maxWeight)
//{
// OldWorld.Instance.MainPlayer.ActiveCharacter.UpdateInventoryItem(id, amountRemaining, weight, maxWeight);

var rec = OldWorld.Instance.EIF[id];
m_game.Hud.AddChat(ChatTab.System, "",
$"{OldWorld.GetString(EOResourceID.STATUS_LABEL_ITEM_JUNK_YOU_JUNKED)} {amountRemoved} {rec.Name}", ChatIcon.DownArrow);
m_game.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_INFORMATION, EOResourceID.STATUS_LABEL_ITEM_JUNK_YOU_JUNKED,
$" {amountRemoved} {rec.Name}");
}
// var rec = OldWorld.Instance.EIF[id];
// m_game.Hud.AddChat(ChatTab.System, "",
// $"{OldWorld.GetString(EOResourceID.STATUS_LABEL_ITEM_JUNK_YOU_JUNKED)} {amountRemoved} {rec.Name}", ChatIcon.DownArrow);
// m_game.Hud.SetStatusLabel(EOResourceID.STATUS_LABEL_TYPE_INFORMATION, EOResourceID.STATUS_LABEL_ITEM_JUNK_YOU_JUNKED,
// $" {amountRemoved} {rec.Name}");
//}

private void _dropItem(int characterAmount, byte weight, byte maxWeight, OldMapItem item)
{
Expand Down

0 comments on commit 60194d8

Please sign in to comment.