From 4098fc38ac0b183a2e5a49ce72fb4be174da361f Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 7 Jan 2023 20:49:33 +0100 Subject: [PATCH] feat(game): add inventory api --- .../Apis/NostaleInventoryPacketApi.cs | 101 ++++++++++++++++++ Core/NosSmooth.Game/Data/Info/Position.cs | 4 + Core/NosSmooth.Game/Errors/NotInRangeError.cs | 18 ++++ .../Errors/NotInitializedError.cs | 15 +++ Core/NosSmooth.Game/NosSmooth.Game.csproj | 4 - 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs create mode 100644 Core/NosSmooth.Game/Errors/NotInRangeError.cs create mode 100644 Core/NosSmooth.Game/Errors/NotInitializedError.cs diff --git a/Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs b/Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs new file mode 100644 index 0000000000000000000000000000000000000000..efcd3de69752aa70d5aa129a844a588b7a48d4b4 --- /dev/null +++ b/Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs @@ -0,0 +1,101 @@ +// +// NostaleInventoryPacketApi.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Core.Client; +using NosSmooth.Packets.Client.Inventory; +using NosSmooth.Packets.Enums.Inventory; +using Remora.Results; + +namespace NosSmooth.Game.Apis; + +/// +/// Packet api for managing items in inventory. +/// +public class NostaleInventoryPacketApi +{ + private readonly INostaleClient _client; + + /// + /// Initializes a new instance of the class. + /// + /// The nostale client. + public NostaleInventoryPacketApi(INostaleClient client) + { + _client = client; + } + + /// + /// Drop the given item. + /// + /// The bag where the item is located. + /// The slot the item is at. + /// The amount to drop. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + public async Task DropItemAsync + ( + BagType bag, + short slot, + short amount, + CancellationToken ct = default + ) + => await _client.SendPacketAsync(new PutPacket(bag, slot, amount), ct); + + /// + /// Move the given item within one bag. + /// + /// The bag the item is in. + /// The source slot to move the item from. + /// The destination slot to move the item to. + /// The amount to move. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + public async Task MoveItemAsync + ( + BagType bag, + short sourceSlot, + short destinationSlot, + short amount, + CancellationToken ct = default + ) + => await MoveItemAsync + ( + bag, + sourceSlot, + bag, + destinationSlot, + amount, + ct + ); + + /// + /// Move an item from the given source bag and slot to the given destination bag and slot. + /// + /// The bag the item is in. + /// The source slot to move the item from. + /// The destination bag to move the item to. + /// The destination slot to move the item to. + /// The amount to move. + /// The cancellation token used for cancelling the operation. + /// A result that may or may not have succeeded. + public async Task MoveItemAsync + ( + BagType sourceBag, + short sourceSlot, + BagType destinationBag, + short destinationSlot, + short amount, + CancellationToken ct = default + ) + { + if (sourceBag == destinationBag) + { + return await _client.SendPacketAsync(new MviPacket(sourceBag, sourceSlot, amount, destinationSlot), ct); + } + + return await _client.SendPacketAsync(new MvePacket(sourceBag, sourceSlot, destinationBag, destinationSlot), ct); + } +} \ No newline at end of file diff --git a/Core/NosSmooth.Game/Data/Info/Position.cs b/Core/NosSmooth.Game/Data/Info/Position.cs index e233a18e771d95d0a63beeae65ec88ea1de06336..c4ce667dfbe05bfb44a41a23e1536a6fbd0a8dfc 100644 --- a/Core/NosSmooth.Game/Data/Info/Position.cs +++ b/Core/NosSmooth.Game/Data/Info/Position.cs @@ -89,4 +89,8 @@ public record struct Position(short X, short Y) { return new Position((short)(left.X - right.X), (short)(left.Y - right.Y)); } + + /// + public override string ToString() + => $"{X}, {Y}"; } \ No newline at end of file diff --git a/Core/NosSmooth.Game/Errors/NotInRangeError.cs b/Core/NosSmooth.Game/Errors/NotInRangeError.cs new file mode 100644 index 0000000000000000000000000000000000000000..070ddebfe23dfd3efcd6ef2059540df5f1c45b01 --- /dev/null +++ b/Core/NosSmooth.Game/Errors/NotInRangeError.cs @@ -0,0 +1,18 @@ +// +// NotInRangeError.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Game.Data.Info; +using Remora.Results; + +namespace NosSmooth.Game.Errors; + +public record NotInRangeError +( + string Entity, + Position EntityPosition, + Position TargetPosition, + short Range +) : ResultError($"Entity {Entity} ({EntityPosition}) is not in range ({Range}) of ({TargetPosition})."); \ No newline at end of file diff --git a/Core/NosSmooth.Game/Errors/NotInitializedError.cs b/Core/NosSmooth.Game/Errors/NotInitializedError.cs new file mode 100644 index 0000000000000000000000000000000000000000..641fe62dc7c5bdf4902fcd96fe22adc604aae63f --- /dev/null +++ b/Core/NosSmooth.Game/Errors/NotInitializedError.cs @@ -0,0 +1,15 @@ +// +// NotInitializedError.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Remora.Results; + +namespace NosSmooth.Game.Errors; + +/// +/// A game field that had to be accessed was not initialized. +/// +/// The field. +public record NotInitializedError(string Field) : ResultError($"The {Field} is not initialized."); \ No newline at end of file diff --git a/Core/NosSmooth.Game/NosSmooth.Game.csproj b/Core/NosSmooth.Game/NosSmooth.Game.csproj index 20a8e4da021183a1f421af1f835a8b9caf5ee318..5ef65adae42a8a58eea0e1b2a8a41371ad13e5de 100644 --- a/Core/NosSmooth.Game/NosSmooth.Game.csproj +++ b/Core/NosSmooth.Game/NosSmooth.Game.csproj @@ -15,10 +15,6 @@ Move Friends, Group, Family, Inventory to Game 2.0.0 - - - -