R Core/NosSmooth.Game/Apis/NostaleChatPacketApi.cs => Core/NosSmooth.Game/Apis/Safe/NostaleChatApi.cs +5 -5
@@ 1,5 1,5 @@
//
-// NostaleChatPacketApi.cs
+// NostaleChatApi.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.
@@ 10,21 10,21 @@ using NosSmooth.Packets.Enums.Entities;
using NosSmooth.Packets.Server.Chat;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Safe;
/// <summary>
/// Packet api for sending and receiving messages.
/// </summary>
-public class NostaleChatPacketApi
+public class NostaleChatApi
{
// TODO: check length of the messages
private readonly INostaleClient _client;
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleChatPacketApi"/> class.
+ /// Initializes a new instance of the <see cref="NostaleChatApi"/> class.
/// </summary>
/// <param name="client">The nostale client.</param>
- public NostaleChatPacketApi(INostaleClient client)
+ public NostaleChatApi(INostaleClient client)
{
_client = client;
}
R Core/NosSmooth.Game/Apis/NostaleMapPacketApi.cs => Core/NosSmooth.Game/Apis/Safe/NostaleMapApi.cs +13 -74
@@ 1,55 1,41 @@
//
-// NostaleMapPacketApi.cs
+// NostaleMapApi.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.Game.Attributes;
+using NosSmooth.Game.Apis.Unsafe;
using NosSmooth.Game.Data.Entities;
-using NosSmooth.Game.Data.Items;
using NosSmooth.Game.Errors;
-using NosSmooth.Packets.Client.Inventory;
-using NosSmooth.Packets.Client.Movement;
-using NosSmooth.Packets.Enums.Entities;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Safe;
/// <summary>
/// Packet api for managing maps in inventory.
/// </summary>
-public class NostaleMapPacketApi
+public class NostaleMapApi
{
+ private readonly Game _game;
+ private readonly UnsafeMapApi _unsafeMapApi;
+
/// <summary>
/// The range the player may pick up items in.
/// </summary>
public static short PickUpRange => 5;
- private readonly Game _game;
- private readonly INostaleClient _client;
-
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleMapPacketApi"/> class.
+ /// Initializes a new instance of the <see cref="NostaleMapApi"/> class.
/// </summary>
/// <param name="game">The game.</param>
- /// <param name="client">The client.</param>
- public NostaleMapPacketApi(Game game, INostaleClient client)
+ /// <param name="unsafeMapApi">The unsafe map api.</param>
+ public NostaleMapApi(Game game, UnsafeMapApi unsafeMapApi)
{
_game = game;
- _client = client;
+ _unsafeMapApi = unsafeMapApi;
}
/// <summary>
- /// Use the given portal.
- /// </summary>
- /// <param name="ct">The cancellation token for cancelling the operation.</param>
- /// <returns>A result that may or may not have succeeded.</returns>
- [Unsafe("Portal position not checked.")]
- public Task<Result> UsePortalAsync(CancellationToken ct = default)
- => _client.SendPacketAsync(new PreqPacket(), ct);
-
- /// <summary>
/// Pick up the given item.
/// </summary>
/// <remarks>
@@ 86,28 72,7 @@ public class NostaleMapPacketApi
return new NotInRangeError("Character", characterPosition.Value, itemPosition.Value, PickUpRange);
}
- return await CharacterPickUpAsync(item.Id, ct);
- }
-
- /// <summary>
- /// Pick up the given item by character.
- /// </summary>
- /// <remarks>
- /// Unsafe, does not check anything.
- /// </remarks>
- /// <param name="itemId">The id of the item.</param>
- /// <param name="ct">The cancellation token used for cancelling the operation.</param>
- /// <returns>A result that may or may not have succeeded.</returns>
- [Unsafe("Nor character distance, nor the existence of item is checked.")]
- public async Task<Result> CharacterPickUpAsync(long itemId, CancellationToken ct = default)
- {
- var character = _game.Character;
- if (character is null)
- {
- return new NotInitializedError("Character");
- }
-
- return await _client.SendPacketAsync(new GetPacket(EntityType.Player, character.Id, itemId), ct);
+ return await _unsafeMapApi.CharacterPickUpAsync(item.Id, ct);
}
/// <summary>
@@ 160,33 125,7 @@ public class NostaleMapPacketApi
return new NotInRangeError("Pet", petPosition.Value, itemPosition.Value, PickUpRange);
}
- return await PetPickUpAsync(item.Id, ct);
+ return await _unsafeMapApi.PetPickUpAsync(item.Id, ct);
}
- /// <summary>
- /// Pick up the given item by pet.
- /// </summary>
- /// <remarks>
- /// Unsafe, does not check anything.
- /// </remarks>
- /// <param name="itemId">The id of the item.</param>
- /// <param name="ct">The cancellation token used for cancelling the operation.</param>
- /// <returns>A result that may or may not have succeeded.</returns>
- [Unsafe("Nor pet distance to item nor whether the item exists is checked.")]
- public async Task<Result> PetPickUpAsync(long itemId, CancellationToken ct = default)
- {
- var mates = _game.Mates;
- if (mates is null)
- {
- return new NotInitializedError("Game.Mates");
- }
-
- var pet = mates.CurrentPet;
- if (pet is null)
- {
- return new NotInitializedError("Game.Mates.CurrentPet");
- }
-
- return await _client.SendPacketAsync(new GetPacket(EntityType.Player, pet.Pet.MateId, itemId), ct);
- }
}=
\ No newline at end of file
A Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs => Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs +426 -0
@@ 0,0 1,426 @@
+//
+// NostaleSkillsApi.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.Core.Contracts;
+using NosSmooth.Data.Abstractions.Enums;
+using NosSmooth.Game.Apis.Unsafe;
+using NosSmooth.Game.Contracts;
+using NosSmooth.Game.Data.Characters;
+using NosSmooth.Game.Data.Entities;
+using NosSmooth.Game.Data.Info;
+using NosSmooth.Game.Errors;
+using NosSmooth.Game.Events.Battle;
+using NosSmooth.Packets.Client.Battle;
+using Remora.Results;
+
+namespace NosSmooth.Game.Apis.Safe;
+
+/// <summary>
+/// A safe NosTale api for using character skills.
+/// </summary>
+public class NostaleSkillsApi
+{
+ private readonly Game _game;
+ private readonly INostaleClient _client;
+ private readonly Contractor _contractor;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="NostaleSkillsApi"/> class.
+ /// </summary>
+ /// <param name="game">The game.</param>
+ /// <param name="client">The NosTale client.</param>
+ /// <param name="contractor">The contractor.</param>
+ public NostaleSkillsApi(Game game, INostaleClient client, Contractor contractor)
+ {
+ _game = game;
+ _client = client;
+ _contractor = contractor;
+ }
+
+ /// <summary>
+ /// Use the given (targetable) skill on character himself.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="ct">The cancellation token for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public Task<Result> UseSkillOnCharacter
+ (
+ Skill skill,
+ CancellationToken ct = default
+ )
+ {
+ var character = _game.Character;
+ if (character is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("Game.Character"));
+ }
+
+ var skills = _game.Skills;
+ if (skills is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("Game.Skills"));
+ }
+
+ if (skill.IsOnCooldown)
+ {
+ return Task.FromResult<Result>(new SkillOnCooldownError(skill));
+ }
+
+ if (skill.Info is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("skill info"));
+ }
+
+ if (skill.Info.TargetType is not(TargetType.Self or TargetType.SelfOrTarget))
+ {
+ return Task.FromResult<Result>(new WrongSkillTargetError(skill, character));
+ }
+
+ return _client.SendPacketAsync
+ (
+ new UseSkillPacket
+ (
+ skill.Info.CastId,
+ character.Type,
+ character.Id,
+ null,
+ null
+ ),
+ ct
+ );
+ }
+
+ /// <summary>
+ /// Use the given (targetable) skill on specified entity.
+ /// </summary>
+ /// <remarks>
+ /// The skill won't be used if it is on cooldown.
+ /// For skills that can be used only on self, use <paramref name="entity"/> of the character.
+ /// For skills that cannot be targeted on an entity, proceed to UseSkillAt.
+ /// </remarks>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="entity">The entity to use the skill on.</param>
+ /// <param name="mapX">The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <param name="mapY">The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <param name="ct">The cancellation token for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public Task<Result> UseSkillOn
+ (
+ Skill skill,
+ ILivingEntity entity,
+ short? mapX = default,
+ short? mapY = default,
+ CancellationToken ct = default
+ )
+ {
+ if (entity == _game.Character)
+ {
+ return UseSkillOnCharacter(skill, ct);
+ }
+
+ var skills = _game.Skills;
+ if (skills is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("Game.Skills"));
+ }
+
+ if (skill.IsOnCooldown)
+ {
+ return Task.FromResult<Result>(new SkillOnCooldownError(skill));
+ }
+
+ if (skill.Info is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("skill info"));
+ }
+
+ if (skill.Info.TargetType is not(TargetType.Target or TargetType.SelfOrTarget))
+ {
+ return Task.FromResult<Result>(new WrongSkillTargetError(skill, entity));
+ }
+
+ var entityPosition = entity.Position;
+ if (entityPosition is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("entity position"));
+ }
+
+ var characterPosition = _game.Character?.Position;
+ if (characterPosition is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("character position"));
+ }
+
+ if (!entityPosition.Value.IsInRange(characterPosition.Value, skill.Info.Range))
+ {
+ return Task.FromResult<Result>
+ (
+ new NotInRangeError
+ (
+ "Character",
+ characterPosition.Value,
+ entityPosition.Value,
+ skill.Info.Range
+ )
+ );
+ }
+
+ if (mapX != null && mapY != null)
+ {
+ var mapPosition = new Position(mapX.Value, mapY.Value);
+ if (!mapPosition.IsInRange(characterPosition.Value, skill.Info.Range))
+ {
+ return Task.FromResult<Result>
+ (
+ new NotInRangeError
+ (
+ "Character",
+ characterPosition.Value,
+ mapPosition,
+ skill.Info.Range
+ )
+ );
+ }
+ }
+
+ return _client.SendPacketAsync
+ (
+ new UseSkillPacket
+ (
+ skill.Info.CastId,
+ entity.Type,
+ entity.Id,
+ mapX,
+ mapY
+ ),
+ ct
+ );
+ }
+
+ /// <summary>
+ /// Use the given (targetable) skill on specified entity.
+ /// </summary>
+ /// <remarks>
+ /// For skills that can be used only on self, use <paramref name="entityId"/> of the character.
+ /// For skills that cannot be targeted on an entity, proceed to UseSkillAt.
+ /// </remarks>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="entityId">The id of the entity to use the skill on.</param>
+ /// <param name="mapX">The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <param name="mapY">The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <param name="ct">The cancellation token for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public Task<Result> UseSkillOn
+ (
+ Skill skill,
+ long entityId,
+ short? mapX = default,
+ short? mapY = default,
+ CancellationToken ct = default
+ )
+ {
+ var map = _game.CurrentMap;
+ if (map is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("Game.Map"));
+ }
+
+ var entity = map.Entities.GetEntity<ILivingEntity>(entityId);
+ if (entity is null)
+ {
+ return Task.FromResult<Result>(new NotFoundError($"Entity with id {entityId} was not found on the map."));
+ }
+
+ return UseSkillOn
+ (
+ skill,
+ entity,
+ mapX,
+ mapY,
+ ct
+ );
+ }
+
+ /// <summary>
+ /// Use the given (aoe) skill on the specified place.
+ /// </summary>
+ /// <remarks>
+ /// For skills that can have targets, proceed to UseSkillOn.
+ /// </remarks>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="mapX">The x coordinate to use the skill at.</param>
+ /// <param name="mapY">The y coordinate to use the skill at.</param>
+ /// <param name="ct">The cancellation token for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public Task<Result> UseSkillAt
+ (
+ Skill skill,
+ short mapX,
+ short mapY,
+ CancellationToken ct = default
+ )
+ {
+ var skills = _game.Skills;
+ if (skills is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("Game.Skills"));
+ }
+
+ if (skill.IsOnCooldown)
+ {
+ return Task.FromResult<Result>(new SkillOnCooldownError(skill));
+ }
+
+ if (skill.Info is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("skill info"));
+ }
+
+ if (skill.Info.TargetType is not TargetType.NoTarget)
+ {
+ return Task.FromResult<Result>(new WrongSkillTargetError(skill, null));
+ }
+
+ var characterPosition = _game.Character?.Position;
+ if (characterPosition is null)
+ {
+ return Task.FromResult<Result>(new NotInitializedError("character position"));
+ }
+
+ var target = new Position(mapX, mapY);
+ if (!target.IsInRange(characterPosition.Value, skill.Info.Range))
+ {
+ return Task.FromResult<Result>
+ (
+ new NotInRangeError
+ (
+ "Character",
+ characterPosition.Value,
+ target,
+ skill.Info.Range
+ )
+ );
+ }
+
+ return _client.SendPacketAsync
+ (
+ new UseAOESkillPacket(skill.Info.CastId, mapX, mapY),
+ ct
+ );
+ }
+
+ /// <summary>
+ /// Creates a contract for using a skill on character himself.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <returns>The contract or an error.</returns>
+ public Result<IContract<SkillUsedEvent, UseSkillStates>> ContractUseSkillOnCharacter
+ (
+ Skill skill
+ )
+ {
+ var characterId = _game?.Character?.Id;
+ if (characterId is null)
+ {
+ return new NotInitializedError("Game.Character");
+ }
+
+ return Result<IContract<SkillUsedEvent, UseSkillStates>>.FromSuccess
+ (
+ UnsafeSkillsApi.CreateUseSkillContract
+ (
+ _contractor,
+ skill.SkillVNum,
+ characterId.Value,
+ ct => UseSkillOnCharacter
+ (
+ skill,
+ ct
+ )
+ )
+ );
+ }
+
+ /// <summary>
+ /// Creates a contract for using a skill on the given entity.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="entity">The entity to use the skill on.</param>
+ /// <param name="mapX">The x coordinate to use the skill at.</param>
+ /// <param name="mapY">The y coordinate to use the skill at.</param>
+ /// <returns>The contract or an error.</returns>
+ public Result<IContract<SkillUsedEvent, UseSkillStates>> ContractUseSkillOn
+ (
+ Skill skill,
+ ILivingEntity entity,
+ short? mapX = default,
+ short? mapY = default
+ )
+ {
+ var characterId = _game?.Character?.Id;
+ if (characterId is null)
+ {
+ return new NotInitializedError("Game.Character");
+ }
+
+ return Result<IContract<SkillUsedEvent, UseSkillStates>>.FromSuccess
+ (
+ UnsafeSkillsApi.CreateUseSkillContract
+ (
+ _contractor,
+ skill.SkillVNum,
+ characterId.Value,
+ ct => UseSkillOn
+ (
+ skill,
+ entity,
+ mapX,
+ mapY,
+ ct
+ )
+ )
+ );
+ }
+
+ /// <summary>
+ /// Creates a contract for using a skill at the given location.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="mapX">The x coordinate to use the skill at.</param>
+ /// <param name="mapY">The y coordinate to use the skill at.</param>
+ /// <returns>The contract or an error.</returns>
+ public Result<IContract<SkillUsedEvent, UseSkillStates>> ContractUseSkillAt
+ (
+ Skill skill,
+ short mapX,
+ short mapY
+ )
+ {
+ var characterId = _game?.Character?.Id;
+ if (characterId is null)
+ {
+ return new NotInitializedError("Game.Character");
+ }
+
+ return Result<IContract<SkillUsedEvent, UseSkillStates>>.FromSuccess
+ (
+ UnsafeSkillsApi.CreateUseSkillContract
+ (
+ _contractor,
+ skill.SkillVNum,
+ characterId.Value,
+ ct => UseSkillAt
+ (
+ skill,
+ mapX,
+ mapY,
+ ct
+ )
+ )
+ );
+ }
+}<
\ No newline at end of file
R Core/NosSmooth.Game/Apis/NostaleInventoryPacketApi.cs => Core/NosSmooth.Game/Apis/Unsafe/UnsafeInventoryApi.cs +56 -5
@@ 1,30 1,40 @@
//
-// NostaleInventoryPacketApi.cs
+// UnsafeInventoryApi.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 System.Data;
using NosSmooth.Core.Client;
+using NosSmooth.Core.Contracts;
+using NosSmooth.Game.Data.Entities;
+using NosSmooth.Game.Data.Inventory;
+using NosSmooth.Game.Events.Entities;
using NosSmooth.Packets.Client.Inventory;
using NosSmooth.Packets.Enums.Inventory;
+using NosSmooth.Packets.Server.Maps;
+using OneOf.Types;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Unsafe;
/// <summary>
/// Packet api for managing items in inventory.
/// </summary>
-public class NostaleInventoryPacketApi
+public class UnsafeInventoryApi
{
private readonly INostaleClient _client;
+ private readonly Contractor _contractor;
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleInventoryPacketApi"/> class.
+ /// Initializes a new instance of the <see cref="UnsafeInventoryApi"/> class.
/// </summary>
/// <param name="client">The nostale client.</param>
- public NostaleInventoryPacketApi(INostaleClient client)
+ /// <param name="contractor">The contractor.</param>
+ public UnsafeInventoryApi(INostaleClient client, Contractor contractor)
{
_client = client;
+ _contractor = contractor;
}
/// <summary>
@@ 45,6 55,47 @@ public class NostaleInventoryPacketApi
=> _client.SendPacketAsync(new PutPacket(bag, slot, amount), ct);
/// <summary>
+ /// Creates a contract for dropping an item.
+ /// Returns the ground item that was thrown on the ground.
+ /// </summary>
+ /// <param name="bag">The inventory bag.</param>
+ /// <param name="slot">The inventory slot.</param>
+ /// <param name="amount">The amount to drop.</param>
+ /// <returns>A contract representing the drop operation.</returns>
+ public IContract<GroundItem, DefaultStates> ContractDropItem
+ (
+ BagType bag,
+ InventorySlot slot,
+ short amount
+ )
+ {
+ // TODO: confirm dialog.
+ return new ContractBuilder<GroundItem, DefaultStates, NoErrors>(_contractor, DefaultStates.None)
+ .SetMoveAction
+ (
+ DefaultStates.None,
+ async (_, ct) =>
+ {
+ await DropItemAsync(bag, slot.Slot, amount, ct);
+ return true;
+ },
+ DefaultStates.Requested
+ )
+ .SetMoveFilter<ItemDroppedEvent>
+ (
+ DefaultStates.Requested,
+ data => data.Item.Amount == amount && data.Item.VNum == slot.Item?.ItemVNum,
+ DefaultStates.ResponseObtained
+ )
+ .SetFillData<ItemDroppedEvent>
+ (
+ DefaultStates.Requested,
+ data => data.Item
+ )
+ .Build();
+ }
+
+ /// <summary>
/// Move the given item within one bag.
/// </summary>
/// <param name="bag">The bag the item is in.</param>
A Core/NosSmooth.Game/Apis/Unsafe/UnsafeMapApi.cs => Core/NosSmooth.Game/Apis/Unsafe/UnsafeMapApi.cs +88 -0
@@ 0,0 1,88 @@
+//
+// UnsafeMapApi.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.Game.Errors;
+using NosSmooth.Packets.Client.Inventory;
+using NosSmooth.Packets.Client.Movement;
+using NosSmooth.Packets.Enums.Entities;
+using Remora.Results;
+
+namespace NosSmooth.Game.Apis.Unsafe;
+
+/// <summary>
+/// Packet api for managing maps in inventory.
+/// </summary>
+public class UnsafeMapApi
+{
+ private readonly Game _game;
+ private readonly INostaleClient _client;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="UnsafeMapApi"/> class.
+ /// </summary>
+ /// <param name="game">The game.</param>
+ /// <param name="client">The client.</param>
+ public UnsafeMapApi(Game game, INostaleClient client)
+ {
+ _game = game;
+ _client = client;
+ }
+
+ /// <summary>
+ /// Use the given portal.
+ /// </summary>
+ /// <param name="ct">The cancellation token for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public Task<Result> UsePortalAsync(CancellationToken ct = default)
+ => _client.SendPacketAsync(new PreqPacket(), ct);
+
+ /// <summary>
+ /// Pick up the given item by character.
+ /// </summary>
+ /// <remarks>
+ /// Unsafe, does not check anything.
+ /// </remarks>
+ /// <param name="itemId">The id of the item.</param>
+ /// <param name="ct">The cancellation token used for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public async Task<Result> CharacterPickUpAsync(long itemId, CancellationToken ct = default)
+ {
+ var character = _game.Character;
+ if (character is null)
+ {
+ return new NotInitializedError("Character");
+ }
+
+ return await _client.SendPacketAsync(new GetPacket(EntityType.Player, character.Id, itemId), ct);
+ }
+
+ /// <summary>
+ /// Pick up the given item by pet.
+ /// </summary>
+ /// <remarks>
+ /// Unsafe, does not check anything.
+ /// </remarks>
+ /// <param name="itemId">The id of the item.</param>
+ /// <param name="ct">The cancellation token used for cancelling the operation.</param>
+ /// <returns>A result that may or may not have succeeded.</returns>
+ public async Task<Result> PetPickUpAsync(long itemId, CancellationToken ct = default)
+ {
+ var mates = _game.Mates;
+ if (mates is null)
+ {
+ return new NotInitializedError("Game.Mates");
+ }
+
+ var pet = mates.CurrentPet;
+ if (pet is null)
+ {
+ return new NotInitializedError("Game.Mates.CurrentPet");
+ }
+
+ return await _client.SendPacketAsync(new GetPacket(EntityType.Player, pet.Pet.MateId, itemId), ct);
+ }
+}<
\ No newline at end of file
R Core/NosSmooth.Game/Apis/NostaleMatePacketApi.cs => Core/NosSmooth.Game/Apis/Unsafe/UnsafeMateApi.cs +5 -6
@@ 1,30 1,29 @@
//
-// NostaleMatePacketApi.cs
+// UnsafeMateApi.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.Game.Attributes;
using NosSmooth.Packets.Client;
using NosSmooth.Packets.Enums.Entities;
using NosSmooth.Packets.Enums.NRun;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Unsafe;
/// <summary>
/// Packet api for managing mates, company, stay, sending them back.
/// </summary>
-public class NostaleMatePacketApi
+public class UnsafeMateApi
{
private readonly INostaleClient _client;
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleMatePacketApi"/> class.
+ /// Initializes a new instance of the <see cref="UnsafeMateApi"/> class.
/// </summary>
/// <param name="client">The client.</param>
- public NostaleMatePacketApi(INostaleClient client)
+ public UnsafeMateApi(INostaleClient client)
{
_client = client;
}
R Core/NosSmooth.Game/Apis/NostaleMateSkillsPacketApi.cs => Core/NosSmooth.Game/Apis/Unsafe/UnsafeMateSkillsApi.cs +5 -5
@@ 1,5 1,5 @@
//
-// NostaleMateSkillsPacketApi.cs
+// UnsafeMateSkillsApi.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.
@@ 9,20 9,20 @@ using NosSmooth.Packets.Client.Battle;
using NosSmooth.Packets.Enums.Entities;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Unsafe;
/// <summary>
/// Packet api for using mate skills.
/// </summary>
-public class NostaleMateSkillsPacketApi
+public class UnsafeMateSkillsApi
{
private readonly INostaleClient _client;
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleMateSkillsPacketApi"/> class.
+ /// Initializes a new instance of the <see cref="UnsafeMateSkillsApi"/> class.
/// </summary>
/// <param name="client">The client.</param>
- public NostaleMateSkillsPacketApi(INostaleClient client)
+ public UnsafeMateSkillsApi(INostaleClient client)
{
_client = client;
}
R Core/NosSmooth.Game/Apis/NostaleSkillsPacketApi.cs => Core/NosSmooth.Game/Apis/Unsafe/UnsafeSkillsApi.cs +151 -24
@@ 1,36 1,43 @@
//
-// NostaleSkillsPacketApi.cs
+// UnsafeSkillsApi.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.Core.Contracts;
+using NosSmooth.Game.Contracts;
using NosSmooth.Game.Data.Characters;
using NosSmooth.Game.Data.Entities;
using NosSmooth.Game.Errors;
+using NosSmooth.Game.Events.Battle;
using NosSmooth.Packets.Client.Battle;
using NosSmooth.Packets.Enums.Entities;
+using NosSmooth.Packets.Server.Skills;
using Remora.Results;
-namespace NosSmooth.Game.Apis;
+namespace NosSmooth.Game.Apis.Unsafe;
/// <summary>
/// Packet api for using character skills.
/// </summary>
-public class NostaleSkillsPacketApi
+public class UnsafeSkillsApi
{
private readonly INostaleClient _client;
private readonly Game _game;
+ private readonly Contractor _contractor;
/// <summary>
- /// Initializes a new instance of the <see cref="NostaleSkillsPacketApi"/> class.
+ /// Initializes a new instance of the <see cref="UnsafeSkillsApi"/> class.
/// </summary>
/// <param name="client">The nostale client.</param>
/// <param name="game">The game.</param>
- public NostaleSkillsPacketApi(INostaleClient client, Game game)
+ /// <param name="contractor">The contractor.</param>
+ public UnsafeSkillsApi(INostaleClient client, Game game, Contractor contractor)
{
_client = client;
_game = game;
+ _contractor = contractor;
}
/// <summary>
@@ 150,7 157,6 @@ public class NostaleSkillsPacketApi
/// Use the given (targetable) skill on specified entity.
/// </summary>
/// <remarks>
- /// The skill won't be used if it is on cooldown.
/// For skills that can be used only on self, use <paramref name="entity"/> of the character.
/// For skills that cannot be targeted on an entity, proceed to UseSkillAt.
/// </remarks>
@@ 169,11 175,6 @@ public class NostaleSkillsPacketApi
CancellationToken ct = default
)
{
- if (skill.IsOnCooldown)
- {
- return Task.FromResult<Result>(new SkillOnCooldownError(skill));
- }
-
if (skill.Info is null)
{
return Task.FromResult<Result>(new NotInitializedError("skill info"));
@@ 217,12 218,8 @@ public class NostaleSkillsPacketApi
CancellationToken ct = default
)
{
- if (skill.IsOnCooldown)
- {
- return Task.FromResult<Result>(new SkillOnCooldownError(skill));
- }
-
- if (skill.Info is null)
+ var info = skill.Info;
+ if (info is null)
{
return Task.FromResult<Result>(new NotInitializedError("skill info"));
}
@@ 231,7 228,7 @@ public class NostaleSkillsPacketApi
(
new UseSkillPacket
(
- skill.Info.CastId,
+ info.CastId,
entityType,
entityId,
mapX,
@@ 247,14 244,14 @@ public class NostaleSkillsPacketApi
/// <remarks>
/// For skills that can have targets, proceed to UseSkillOn.
/// </remarks>
- /// <param name="skillVNum">The id of the skill.</param>
+ /// <param name="castId">The id of the skill.</param>
/// <param name="mapX">The x coordinate to use the skill at.</param>
/// <param name="mapY">The y coordinate to use the skill at.</param>
/// <param name="ct">The cancellation token for cancelling the operation.</param>
/// <returns>A result that may or may not have succeeded.</returns>
public Task<Result> UseSkillAt
(
- long skillVNum,
+ long castId,
short mapX,
short mapY,
CancellationToken ct = default
@@ 262,7 259,7 @@ public class NostaleSkillsPacketApi
{
return _client.SendPacketAsync
(
- new UseAOESkillPacket(skillVNum, mapX, mapY),
+ new UseAOESkillPacket(castId, mapX, mapY),
ct
);
}
@@ 286,15 283,145 @@ public class NostaleSkillsPacketApi
CancellationToken ct = default
)
{
- if (skill.IsOnCooldown)
+ var info = skill.Info;
+ if (info is null)
{
- return Task.FromResult<Result>(new SkillOnCooldownError(skill));
+ return Task.FromResult<Result>(new NotInitializedError("skill info"));
}
return _client.SendPacketAsync
(
- new UseAOESkillPacket(skill.SkillVNum, mapX, mapY),
+ new UseAOESkillPacket(info.CastId, mapX, mapY),
ct
);
}
+
+ /// <summary>
+ /// Creates a contract for using a skill on the given entity.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="entityId">The id of the entity to use the skill on.</param>
+ /// <param name="entityType">The type of the supplied entity.</param>
+ /// <param name="mapX">The x coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <param name="mapY">The y coordinate on the map. (Used for non targeted dashes etc., says where the dash will be to.)</param>
+ /// <returns>The contract or an error.</returns>
+ public Result<IContract<SkillUsedEvent, UseSkillStates>> ContractUseSkillOn
+ (
+ Skill skill,
+ long entityId,
+ EntityType entityType,
+ short? mapX = default,
+ short? mapY = default
+ )
+ {
+ var characterId = _game?.Character?.Id;
+ if (characterId is null)
+ {
+ return new NotInitializedError("Game.Character");
+ }
+
+ if (skill.Info is null)
+ {
+ return new NotInitializedError("skill info");
+ }
+
+ return Result<IContract<SkillUsedEvent, UseSkillStates>>.FromSuccess
+ (
+ CreateUseSkillContract
+ (
+ _contractor,
+ skill.SkillVNum,
+ characterId.Value,
+ ct => UseSkillOn
+ (
+ skill.Info.CastId,
+ entityId,
+ entityType,
+ mapX,
+ mapY,
+ ct
+ )
+ )
+ );
+ }
+
+ /// <summary>
+ /// Creates a contract for using a skill at the given location.
+ /// </summary>
+ /// <param name="skill">The skill to use.</param>
+ /// <param name="mapX">The x coordinate to use the skill at.</param>
+ /// <param name="mapY">The y coordinate to use the skill at.</param>
+ /// <returns>The contract or an error.</returns>
+ public Result<IContract<SkillUsedEvent, UseSkillStates>> ContractUseSkillAt
+ (
+ Skill skill,
+ short mapX,
+ short mapY
+ )
+ {
+ var characterId = _game?.Character?.Id;
+ if (characterId is null)
+ {
+ return new NotInitializedError("Game.Character");
+ }
+
+ if (skill.Info is null)
+ {
+ return new NotInitializedError("skill info");
+ }
+
+ return Result<IContract<SkillUsedEvent, UseSkillStates>>.FromSuccess
+ (
+ CreateUseSkillContract
+ (
+ _contractor,
+ skill.SkillVNum,
+ characterId.Value,
+ ct =>
+ {
+ return UseSkillAt(skill.Info.CastId, mapX, mapY, ct);
+ }
+ )
+ );
+ }
+
+ /// <summary>
+ /// Creates a use skill contract,
+ /// casting the skill using the given action.
+ /// </summary>
+ /// <param name="contractor">The contractor to register the contract at.</param>
+ /// <param name="skillVNum">The vnum of the casting skill.</param>
+ /// <param name="characterId">The id of the caster, character.</param>
+ /// <param name="useSkill">The used skill event.</param>
+ /// <returns>A contract for using the given skill.</returns>
+ public static IContract<SkillUsedEvent, UseSkillStates> CreateUseSkillContract
+ (
+ Contractor contractor,
+ int skillVNum,
+ long characterId,
+ Func<CancellationToken, Task<Result>> useSkill
+ )
+ {
+ return new ContractBuilder<SkillUsedEvent, UseSkillStates, UseSkillErrors>(contractor, UseSkillStates.None)
+ .SetMoveAction
+ (
+ UseSkillStates.None,
+ async (data, ct) => (await useSkill(ct)).Map(true),
+ UseSkillStates.SkillUseRequested
+ )
+ .SetMoveFilter<SkillUsedEvent>
+ (
+ UseSkillStates.SkillUseRequested,
+ data => data.Skill.SkillVNum == skillVNum && data.Caster.Id == characterId,
+ UseSkillStates.SkillUsedResponse
+ )
+ .SetFillData<SkillUsedEvent>
+ (
+ UseSkillStates.SkillUsedResponse,
+ skillUseEvent => skillUseEvent
+ )
+ .SetError<CancelPacket>(UseSkillStates.SkillUseRequested, _ => UseSkillErrors.Unknown)
+ .SetTimeout(UseSkillStates.SkillUsedResponse, TimeSpan.FromSeconds(1), UseSkillStates.CharacterRestored)
+ .Build();
+ }
}=
\ No newline at end of file
D Core/NosSmooth.Game/Attributes/UnsafeAttribute.cs => Core/NosSmooth.Game/Attributes/UnsafeAttribute.cs +0 -28
@@ 1,28 0,0 @@
-//
-// UnsafeAttribute.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.
-
-namespace NosSmooth.Game.Attributes;
-
-/// <summary>
-/// The given method does not do some checks.
-/// </summary>
-[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Struct)]
-public class UnsafeAttribute : Attribute
-{
- /// <summary>
- /// Initializes a new instance of the <see cref="UnsafeAttribute"/> class.
- /// </summary>
- /// <param name="reason">The reason.</param>
- public UnsafeAttribute(string reason)
- {
- Reason = reason;
- }
-
- /// <summary>
- /// Gets the unsafe reason.
- /// </summary>
- public string Reason { get; }
-}>
\ No newline at end of file
A Core/NosSmooth.Game/Contracts/UseSkillErrors.cs => Core/NosSmooth.Game/Contracts/UseSkillErrors.cs +28 -0
@@ 0,0 1,28 @@
+//
+// UseSkillErrors.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.
+
+namespace NosSmooth.Game.Contracts;
+
+/// <summary>
+/// Errors for using a skill.
+/// </summary>
+public enum UseSkillErrors
+{
+ /// <summary>
+ /// An unknown error has happened.
+ /// </summary>
+ Unknown,
+
+ /// <summary>
+ /// The character does not have enough ammo.
+ /// </summary>
+ NoAmmo,
+
+ /// <summary>
+ /// The character does not have enough mana.
+ /// </summary>
+ NoMana
+}<
\ No newline at end of file
A Core/NosSmooth.Game/Contracts/UseSkillStates.cs => Core/NosSmooth.Game/Contracts/UseSkillStates.cs +37 -0
@@ 0,0 1,37 @@
+//
+// UseSkillStates.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.
+
+namespace NosSmooth.Game.Contracts;
+
+/// <summary>
+/// States for contract for using a skill.
+/// </summary>
+public enum UseSkillStates
+{
+ /// <summary>
+ /// Skill use was not executed yet.
+ /// </summary>
+ None,
+
+ /// <summary>
+ /// A skill use packet (u_s, u_as, etc.) was used,
+ /// awaiting a response from the server.
+ /// </summary>
+ SkillUseRequested,
+
+ /// <summary>
+ /// The server has responded with a skill use,
+ /// (after cast time), the information about
+ /// the caster, target is filled.
+ /// </summary>
+ SkillUsedResponse,
+
+ /// <summary>
+ /// Fired 1000 ms after <see cref="SkillUsedResponse"/>,
+ /// the character may move after this.
+ /// </summary>
+ CharacterRestored
+}<
\ No newline at end of file
A Core/NosSmooth.Game/Errors/WrongSkillTargetError.cs => Core/NosSmooth.Game/Errors/WrongSkillTargetError.cs +19 -0
@@ 0,0 1,19 @@
+//
+// WrongSkillTargetError.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.Characters;
+using NosSmooth.Game.Data.Entities;
+using Remora.Results;
+
+namespace NosSmooth.Game.Errors;
+
+/// <summary>
+/// Skill was used at wrong target.
+/// </summary>
+/// <param name="Skill">The skill that was used.</param>
+/// <param name="Target">The target of the skill.</param>
+public record WrongSkillTargetError(Skill Skill, ILivingEntity? Target)
+ : ResultError($"The skill {Skill.SkillVNum} was used at wrong target {Target}");<
\ No newline at end of file
M Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs => Core/NosSmooth.Game/Extensions/ServiceCollectionExtensions.cs +8 -6
@@ 8,6 8,8 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NosSmooth.Core.Extensions;
using NosSmooth.Game.Apis;
+using NosSmooth.Game.Apis.Safe;
+using NosSmooth.Game.Apis.Unsafe;
using NosSmooth.Game.Contracts;
using NosSmooth.Game.Events.Core;
using NosSmooth.Game.Events.Inventory;
@@ 66,12 68,12 @@ public static class ServiceCollectionExtensions
.AddPacketResponder<EqResponder>();
serviceCollection
- .AddTransient<NostaleMapPacketApi>()
- .AddTransient<NostaleInventoryPacketApi>()
- .AddTransient<NostaleMatePacketApi>()
- .AddTransient<NostaleMateSkillsPacketApi>()
- .AddTransient<NostaleChatPacketApi>()
- .AddTransient<NostaleSkillsPacketApi>();
+ .AddTransient<UnsafeMapApi>()
+ .AddTransient<UnsafeInventoryApi>()
+ .AddTransient<UnsafeMateApi>()
+ .AddTransient<UnsafeMateSkillsApi>()
+ .AddTransient<NostaleChatApi>()
+ .AddTransient<UnsafeSkillsApi>();
serviceCollection
.AddScoped<IEveryGameResponder, ContractEventResponder>();