Skip to content

Commit

Permalink
Add AbilityMassUp and AbilityUp request handlers (excluding maxhp and…
Browse files Browse the repository at this point in the history
… maxmp handling)
  • Loading branch information
Kaioru committed Jan 8, 2024
1 parent eee3a28 commit 7406a1e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Edelstein.Common.Gameplay.Handling;
using Edelstein.Protocol.Gameplay.Game.Contracts;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Protocol.Utilities.Packets;
using Edelstein.Protocol.Utilities.Pipelines;

namespace Edelstein.Common.Gameplay.Game.Handling.Packets;

public class UserAbilityMassUpRequestHandler : AbstractPipedFieldHandler<FieldOnPacketUserAbilityMassUpRequest>
{
public override short Operation => (short)PacketRecvOperations.UserAbilityMassUpRequest;

public UserAbilityMassUpRequestHandler(IPipeline<FieldOnPacketUserAbilityMassUpRequest> pipeline) : base(pipeline)
{
}

protected override FieldOnPacketUserAbilityMassUpRequest? Serialize(IFieldUser user, IPacketReader reader)
{
var count = reader.Skip(4).ReadInt();
var statUp = new Dictionary<ModifyStatType, int>();

for (var i = 0; i < count; i++)
statUp.Add(
(ModifyStatType)reader.ReadInt(),
reader.ReadInt()
);

return new(
user,
statUp
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Edelstein.Common.Gameplay.Handling;
using Edelstein.Protocol.Gameplay.Game.Contracts;
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Protocol.Utilities.Packets;
using Edelstein.Protocol.Utilities.Pipelines;

namespace Edelstein.Common.Gameplay.Game.Handling.Packets;

public class UserAbilityUpRequestHandler : AbstractPipedFieldHandler<FieldOnPacketUserAbilityUpRequest>
{
public override short Operation => (short)PacketRecvOperations.UserAbilityUpRequest;

public UserAbilityUpRequestHandler(IPipeline<FieldOnPacketUserAbilityUpRequest> pipeline) : base(pipeline)
{
}

protected override FieldOnPacketUserAbilityUpRequest? Serialize(IFieldUser user, IPacketReader reader)
=> new(
user,
(ModifyStatType)reader.Skip(4).ReadInt()
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Edelstein.Common.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Protocol.Gameplay.Game.Contracts;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Protocol.Utilities.Pipelines;
using Microsoft.Extensions.Logging;

namespace Edelstein.Common.Gameplay.Game.Handling.Plugs;

public class FieldOnPacketUserAbilityMassUpRequestPlug : IPipelinePlug<FieldOnPacketUserAbilityMassUpRequest>
{
private readonly ILogger _logger;

public FieldOnPacketUserAbilityMassUpRequestPlug(ILogger<FieldOnPacketUserAbilityMassUpRequestPlug> logger)
=> _logger = logger;

public async Task Handle(IPipelineContext ctx, FieldOnPacketUserAbilityMassUpRequest message)
{
var stats = new ModifyStatContext(message.User.Character);

foreach (var pair in message.StatUp)
{
try
{
HandleStatUp(stats, pair.Key, pair.Value);
}
catch (ArgumentOutOfRangeException)
{
_logger.LogWarning("Unhandled ability up stat type {Type}", pair.Key);
}
}

await message.User.ModifyStats(stats, exclRequest: true);
}

public static void HandleStatUp(ModifyStatContext stats, ModifyStatType type, int value = 1)
{
switch (type)
{
case ModifyStatType.STR:
if (stats.STR + value > short.MaxValue)
return;
stats.STR += (short)value;
break;
case ModifyStatType.DEX:
if (stats.DEX + value > short.MaxValue)
return;
stats.DEX += (short)value;
break;
case ModifyStatType.INT:
if (stats.INT + value > short.MaxValue)
return;
stats.INT += (short)value;
break;
case ModifyStatType.LUK:
if (stats.LUK + value > short.MaxValue)
return;
stats.LUK += (short)value;
break;
case ModifyStatType.MaxHP:
case ModifyStatType.MaxMP:
// TODO
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}

stats.AP -= (short)value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Edelstein.Protocol.Gameplay.Game.Contracts;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;
using Edelstein.Protocol.Utilities.Pipelines;

namespace Edelstein.Common.Gameplay.Game.Handling.Plugs;

public class FieldOnPacketUserAbilityUpRequestPlug : IPipelinePlug<FieldOnPacketUserAbilityUpRequest>
{
private readonly FieldOnPacketUserAbilityMassUpRequestPlug _plug;

public FieldOnPacketUserAbilityUpRequestPlug(FieldOnPacketUserAbilityMassUpRequestPlug plug)
=> _plug = plug;

public Task Handle(IPipelineContext ctx, FieldOnPacketUserAbilityUpRequest message)
=> _plug.Handle(ctx, new FieldOnPacketUserAbilityMassUpRequest(
message.User,
new Dictionary<ModifyStatType, int>
{
[message.Type] = 1
}
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;

namespace Edelstein.Protocol.Gameplay.Game.Contracts;

public record FieldOnPacketUserAbilityMassUpRequest(
IFieldUser User,
IDictionary<ModifyStatType, int> StatUp
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Edelstein.Protocol.Gameplay.Game.Objects.User;
using Edelstein.Protocol.Gameplay.Models.Characters.Stats.Modify;

namespace Edelstein.Protocol.Gameplay.Game.Contracts;

public record FieldOnPacketUserAbilityUpRequest(
IFieldUser User,
ModifyStatType Type
);

0 comments on commit 7406a1e

Please sign in to comment.