122 lines
4.8 KiB
C#
122 lines
4.8 KiB
C#
using NeonSprawl.Server.Game.PositionState;
|
|
using NeonSprawl.Server.Game.Targeting;
|
|
using NeonSprawl.Server.Game.World;
|
|
|
|
namespace NeonSprawl.Server.Game.AbilityInput;
|
|
|
|
/// <summary>Maps prototype ability cast POST (NEO-31 + NEO-28 target authority).</summary>
|
|
public static class AbilityCastApi
|
|
{
|
|
public const string ReasonSlotOutOfBounds = "slot_out_of_bounds";
|
|
public const string ReasonSlotUnbound = "slot_unbound";
|
|
public const string ReasonLoadoutMismatch = "loadout_mismatch";
|
|
public const string ReasonUnknownAbility = "unknown_ability";
|
|
|
|
/// <summary>Request target missing, unknown to the prototype registry, or not the server lock (NEO-28).</summary>
|
|
public const string ReasonInvalidTarget = TargetValidity.InvalidTarget;
|
|
|
|
/// <summary>Locked prototype target is outside horizontal reach of authoritative position (NEO-28).</summary>
|
|
public const string ReasonOutOfRange = TargetValidity.OutOfRange;
|
|
|
|
public static WebApplication MapAbilityCastApi(this WebApplication app)
|
|
{
|
|
app.MapPost(
|
|
"/game/players/{id}/ability-cast",
|
|
(
|
|
string id,
|
|
AbilityCastRequest? body,
|
|
IPositionStateStore positions,
|
|
IPlayerHotbarLoadoutStore store,
|
|
IPlayerTargetLockStore locks) =>
|
|
{
|
|
if (body is null || body.SchemaVersion != AbilityCastRequest.CurrentSchemaVersion)
|
|
{
|
|
return Results.BadRequest();
|
|
}
|
|
|
|
if (!positions.TryGetPosition(id, out var snap))
|
|
{
|
|
return Results.NotFound();
|
|
}
|
|
|
|
if (!store.TryGetBindings(id, out var bindings))
|
|
{
|
|
return Results.NotFound();
|
|
}
|
|
|
|
if (body.SlotIndex < 0 || body.SlotIndex >= HotbarLoadoutResponse.SlotCountV1)
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse
|
|
{
|
|
Accepted = false,
|
|
ReasonCode = ReasonSlotOutOfBounds,
|
|
});
|
|
}
|
|
|
|
if (!bindings.TryGetValue(body.SlotIndex, out var boundAbility) ||
|
|
string.IsNullOrWhiteSpace(boundAbility))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse
|
|
{
|
|
Accepted = false,
|
|
ReasonCode = ReasonSlotUnbound,
|
|
});
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(body.AbilityId) ||
|
|
!PrototypeAbilityRegistry.TryNormalizeKnown(body.AbilityId, out var normalizedRequest))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse
|
|
{
|
|
Accepted = false,
|
|
ReasonCode = ReasonUnknownAbility,
|
|
});
|
|
}
|
|
|
|
if (!string.Equals(normalizedRequest, boundAbility, StringComparison.Ordinal))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse
|
|
{
|
|
Accepted = false,
|
|
ReasonCode = ReasonLoadoutMismatch,
|
|
});
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(body.TargetId))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse { Accepted = false, ReasonCode = ReasonInvalidTarget });
|
|
}
|
|
|
|
var lookupKey = body.TargetId.Trim().ToLowerInvariant();
|
|
if (!PrototypeTargetRegistry.TryGet(lookupKey, out var entry))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse { Accepted = false, ReasonCode = ReasonInvalidTarget });
|
|
}
|
|
|
|
var (lockIdLowercase, _) = locks.GetLockState(id);
|
|
if (string.IsNullOrEmpty(lockIdLowercase) ||
|
|
!string.Equals(lookupKey, lockIdLowercase, StringComparison.Ordinal))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse { Accepted = false, ReasonCode = ReasonInvalidTarget });
|
|
}
|
|
|
|
if (!HorizontalReach.IsWithinHorizontalRadius(snap.X, snap.Z, entry.X, entry.Z, entry.LockRadius))
|
|
{
|
|
return Results.Json(
|
|
new AbilityCastResponse { Accepted = false, ReasonCode = ReasonOutOfRange });
|
|
}
|
|
|
|
return Results.Json(new AbilityCastResponse { Accepted = true });
|
|
});
|
|
|
|
return app;
|
|
}
|
|
}
|