Card liquefication

This commit is contained in:
gamer147
2026-05-24 14:42:44 -04:00
parent d9ef9fe1fc
commit 12fb2f4801
9 changed files with 862 additions and 0 deletions

View File

@@ -0,0 +1,130 @@
using Microsoft.AspNetCore.Mvc;
using SVSim.Database.Repositories.Card;
using SVSim.EmulatedEntrypoint.Models.Dtos;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Card;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Card;
using System.Text.Json;
namespace SVSim.EmulatedEntrypoint.Controllers;
/// <summary>
/// /card/* — viewer card-inventory mutations. v1 implements /card/destruct only; reserved
/// for /card/create, /card/protect, /card/create-foil-card.
/// </summary>
[Route("card")]
public class CardController : SVSimController
{
private readonly ICardInventoryRepository _inventory;
private readonly ILogger<CardController> _log;
public CardController(ICardInventoryRepository inventory, ILogger<CardController> log)
{
_inventory = inventory;
_log = log;
}
[HttpPost("destruct")]
public async Task<ActionResult<CardDestructResponse>> Destruct(CardDestructRequest request)
{
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
if (!TryParseDestructDict(request.CardIdNumberArray, out var destructCounts, out var snapshots, out var parseError))
return BadRequest(new { error = parseError });
if (destructCounts.Count == 0)
return BadRequest(new { error = "malformed_request" });
var outcome = await _inventory.DestructCards(viewerId, destructCounts);
if (!outcome.IsSuccess)
return BadRequest(new { error = ErrorKey(outcome.Error!.Value) });
// Client snapshot mismatch is warn-log only; never blocks the request.
foreach (var (cardId, snapshot) in snapshots)
{
// We don't carry pre-state counts back, but post + destructed = pre.
int destructedNum = destructCounts[cardId];
int reconstructedPre = outcome.Result!.NewOwnedCounts[cardId] + destructedNum;
if (reconstructedPre != snapshot)
{
_log.LogWarning(
"Destruct possession-snapshot mismatch: card={CardId} client_snapshot={Snapshot} server_pre={ServerPre}",
cardId, snapshot, reconstructedPre);
}
}
// Wire spec is int; clamp the ulong total so a hypothetical 2B+ balance can't underflow
// to a negative wire value. Realistic balances are well under int.MaxValue.
int redEtherWire = outcome.Result!.NewRedEtherTotal > int.MaxValue
? int.MaxValue
: (int)outcome.Result!.NewRedEtherTotal;
var rewardList = new List<RewardListEntry>
{
new() { RewardType = 1, RewardId = 0, RewardNum = redEtherWire },
};
foreach (var (cardId, postCount) in outcome.Result!.NewOwnedCounts)
{
rewardList.Add(new RewardListEntry { RewardType = 5, RewardId = cardId, RewardNum = postCount });
}
return new CardDestructResponse { RewardList = rewardList };
}
private static string ErrorKey(DestructError error) => error switch
{
DestructError.UnknownCard => "unknown_card",
DestructError.NotDestructible => "not_destructible",
DestructError.CardProtected => "card_protected",
DestructError.InsufficientCards => "insufficient_cards",
_ => "malformed_request",
};
/// <summary>
/// Decodes the inner JSON of <c>card_id_number_array</c>. Values are
/// <c>"&lt;num_to_destruct&gt;,&lt;client_possession_snapshot&gt;"</c> — both strings.
/// Returns false (and sets <paramref name="errorKey"/>) on any structural problem.
/// </summary>
private static bool TryParseDestructDict(
string raw,
out Dictionary<long, int> destructCounts,
out Dictionary<long, int> clientSnapshots,
out string errorKey)
{
destructCounts = new();
clientSnapshots = new();
errorKey = "malformed_request";
if (string.IsNullOrWhiteSpace(raw))
return false;
JsonDocument? doc;
try { doc = JsonDocument.Parse(raw); }
catch (JsonException) { return false; }
using (doc)
{
if (doc.RootElement.ValueKind != JsonValueKind.Object)
return false;
foreach (var prop in doc.RootElement.EnumerateObject())
{
if (!long.TryParse(prop.Name, out long cardId))
return false;
if (prop.Value.ValueKind != JsonValueKind.String)
return false;
var pair = prop.Value.GetString()!.Split(',');
if (pair.Length != 2)
return false;
if (!int.TryParse(pair[0], out int num) || num <= 0)
return false;
if (!int.TryParse(pair[1], out int snapshot) || snapshot < 0)
return false;
destructCounts[cardId] = num;
clientSnapshots[cardId] = snapshot;
}
}
return true;
}
}

View File

@@ -0,0 +1,18 @@
using MessagePack;
using System.Text.Json.Serialization;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Card;
/// <summary>
/// POST /card/destruct. The single payload field is a JSON-encoded STRING (double-encoded
/// — see docs/api-spec/endpoints/post-login/card-destruct.md). The inner object maps
/// cardId → "&lt;num_to_destruct&gt;,&lt;client_possession_snapshot&gt;". Both inner values
/// are strings. This DTO keeps it as a single string; parsing happens in CardController.
/// </summary>
[MessagePackObject]
public class CardDestructRequest : BaseRequest
{
[JsonPropertyName("card_id_number_array")]
[Key("card_id_number_array")]
public string CardIdNumberArray { get; set; } = string.Empty;
}

View File

@@ -0,0 +1,19 @@
using MessagePack;
using SVSim.EmulatedEntrypoint.Models.Dtos;
using System.Text.Json.Serialization;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Card;
/// <summary>
/// /card/destruct response data. reward_list entries are POST-STATE TOTALS (the client's
/// PlayerStaticData.UpdateHaveUserGoodsNumByJsonData does direct assignment) — one
/// RewardType=1 RedEther entry plus one RewardType=5 Card entry per destructed cardId.
/// </summary>
[MessagePackObject]
public class CardDestructResponse
{
[JsonPropertyName("reward_list")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
[Key("reward_list")]
public List<RewardListEntry> RewardList { get; set; } = new();
}

View File

@@ -59,6 +59,7 @@ public class Program
});
builder.Services.AddTransient<IViewerRepository, ViewerRepository>();
builder.Services.AddTransient<ICardRepository, CardRepository>();
builder.Services.AddTransient<ICardInventoryRepository, CardInventoryRepository>();
builder.Services.AddTransient<ICollectionRepository, CollectionRepository>();
builder.Services.AddTransient<IGlobalsRepository, GlobalsRepository>();
builder.Services.AddTransient<IDeckRepository, DeckRepository>();