controller(card): POST /card/create

This commit is contained in:
gamer147
2026-05-28 01:31:44 -04:00
parent 442399b268
commit 9b5fe6dd83

View File

@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using SVSim.Database.Enums;
using SVSim.Database.Repositories.Card;
using SVSim.Database.Services;
using SVSim.EmulatedEntrypoint.Models.Dtos;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Card;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Card;
@@ -8,8 +10,8 @@ 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.
/// /card/* — viewer card-inventory mutations. Ships /card/destruct, /card/create, /card/protect.
/// /card/create_foil_card is reserved for a follow-up slice.
/// </summary>
[Route("card")]
public class CardController : SVSimController
@@ -69,6 +71,67 @@ public class CardController : SVSimController
return new CardDestructResponse { RewardList = rewardList };
}
[HttpPost("create")]
public async Task<ActionResult<CardCreateResponse>> Create(CardCreateRequest request)
{
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
if (!TryParseCardCountDict(request.CardIdNumberArray, out var createCounts, out var snapshots, out var parseError))
return BadRequest(new { error = parseError });
if (createCounts.Count == 0)
return BadRequest(new { error = "malformed_request" });
var outcome = await _inventory.CreateCards(viewerId, createCounts);
if (!outcome.IsSuccess)
return BadRequest(new { error = CreateErrorKey(outcome.Error!.Value) });
// Snapshot mismatch is warn-log only. pre-state = post-state - num.
var grants = outcome.Result!.Grants;
foreach (var (cardId, snapshot) in snapshots)
{
int requestedNum = createCounts[cardId];
int postCount = grants.FirstOrDefault(g => g.RewardType == (int)UserGoodsType.Card && g.RewardId == cardId)?.RewardNum ?? 0;
int reconstructedPre = postCount - requestedNum;
if (reconstructedPre != snapshot)
{
_log.LogWarning(
"Create 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. Mirrors destruct's clamp.
int redEtherWire = outcome.Result!.NewRedEtherTotal > int.MaxValue
? int.MaxValue
: (int)outcome.Result!.NewRedEtherTotal;
var rewardList = new List<RewardListEntry>
{
new() { RewardType = (int)UserGoodsType.RedEther, RewardId = 0, RewardNum = redEtherWire },
};
foreach (var grant in grants)
{
rewardList.Add(new RewardListEntry
{
RewardType = grant.RewardType,
RewardId = grant.RewardId,
RewardNum = grant.RewardNum,
});
}
return new CardCreateResponse { RewardList = rewardList };
}
private static string CreateErrorKey(CreateError error) => error switch
{
CreateError.UnknownCard => "unknown_card",
CreateError.NotCraftable => "not_craftable",
CreateError.WouldExceedMaxCopies => "would_exceed_max_copies",
CreateError.InsufficientVials => "insufficient_vials",
_ => "malformed_request",
};
private static string ErrorKey(DestructError error) => error switch
{
DestructError.UnknownCard => "unknown_card",