feat(sleeve): shop catalog + /sleeve/{info,buy} endpoints
Schema: SleeveShopSeries -> SleeveShopProducts -> Rewards (owned).
Migration AddSleeveShop creates 3 tables with FK cascade.
Importer mirrors BuildDeck pattern: find-or-create per series/product,
rewards replaced wholesale on rerun (owned collection). 10 series,
270 products imported from seeds/sleeve-shop.json.
Controller:
- /sleeve/info returns wire-faithful dict-keyed shape
({sleeve_list: {<series_id>: {product_info: {<product_id>: ...}}}}).
is_purchased_product derived from viewer.Sleeves.Contains(sleeve_id).
- /sleeve/buy: sales_type 0=free / 1=crystal / 2=rupy / 3=ticket(501).
Validates series_product mismatch, currency, already-purchased.
Currency debited with post-state-total reward_list entry; cosmetic
grants dispatched through RewardGrantService.ApplyAsync (covers
sleeve + emblem bundled grants per product).
476 tests pass (was 466; +10 sleeve tests).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
189
SVSim.EmulatedEntrypoint/Controllers/SleeveController.cs
Normal file
189
SVSim.EmulatedEntrypoint/Controllers/SleeveController.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Enums;
|
||||
using SVSim.Database.Models;
|
||||
using SVSim.Database.Services;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Sleeve;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Sleeve;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Controllers;
|
||||
|
||||
/// <summary>
|
||||
/// /sleeve/* — the sleeve shop. Catalog + single-product purchase. No series-completion bonus
|
||||
/// (sleeves are sold individually; the leader-skin shop is the family with set-buys).
|
||||
/// </summary>
|
||||
[Route("sleeve")]
|
||||
public class SleeveController : SVSimController
|
||||
{
|
||||
private readonly SVSimDbContext _db;
|
||||
private readonly RewardGrantService _rewards;
|
||||
|
||||
public SleeveController(SVSimDbContext db, RewardGrantService rewards)
|
||||
{
|
||||
_db = db;
|
||||
_rewards = rewards;
|
||||
}
|
||||
|
||||
[HttpPost("info")]
|
||||
public async Task<ActionResult<SleeveInfoResponse>> Info()
|
||||
{
|
||||
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
||||
|
||||
// is_purchased_product is "viewer owns at least one sleeve granted by this product".
|
||||
// Loading the viewer's sleeve-id set once and checking each product against it avoids
|
||||
// an N+1 over products.
|
||||
var ownedSleeveIds = (await _db.Viewers
|
||||
.Where(v => v.Id == viewerId)
|
||||
.SelectMany(v => v.Sleeves.Select(s => (long)s.Id))
|
||||
.ToListAsync()).ToHashSet();
|
||||
|
||||
var series = await _db.SleeveShopSeries
|
||||
.Where(s => s.IsEnabled)
|
||||
.Include(s => s.Products.Where(p => p.IsEnabled)).ThenInclude(p => p.Rewards)
|
||||
.OrderBy(s => s.Id)
|
||||
.ToListAsync();
|
||||
|
||||
var sleeveList = new Dictionary<string, SleeveSeriesDto>();
|
||||
foreach (var s in series)
|
||||
{
|
||||
var products = new Dictionary<string, SleeveProductDto>();
|
||||
foreach (var p in s.Products.OrderBy(p => p.Id))
|
||||
{
|
||||
products[p.Id.ToString()] = new SleeveProductDto
|
||||
{
|
||||
ProductId = p.Id,
|
||||
Name = p.NameKey,
|
||||
PriceCrystal = p.PriceCrystal,
|
||||
PriceRupy = p.PriceRupy,
|
||||
IsPurchasedProduct = IsProductPurchased(p, ownedSleeveIds),
|
||||
Rewards = p.Rewards.OrderBy(r => r.OrderIndex).Select(r => new SleeveProductRewardDto
|
||||
{
|
||||
RewardType = r.RewardType,
|
||||
RewardDetailId = r.RewardDetailId,
|
||||
RewardNumber = r.RewardNumber,
|
||||
}).ToList(),
|
||||
};
|
||||
}
|
||||
|
||||
sleeveList[s.Id.ToString()] = new SleeveSeriesDto
|
||||
{
|
||||
SeriesId = s.Id,
|
||||
IsNew = s.IsNew,
|
||||
ProductInfo = products,
|
||||
};
|
||||
}
|
||||
|
||||
return new SleeveInfoResponse { SleeveList = sleeveList };
|
||||
}
|
||||
|
||||
[HttpPost("buy")]
|
||||
public async Task<ActionResult<SleeveBuyResponse>> Buy(SleeveBuyRequest request)
|
||||
{
|
||||
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
||||
|
||||
if (request.SalesType is 3)
|
||||
return StatusCode(StatusCodes.Status501NotImplemented,
|
||||
new { error = "ticket_currency_path_not_implemented" });
|
||||
if (request.SalesType is < 0 or > 3)
|
||||
return BadRequest(new { error = "invalid_sales_type" });
|
||||
|
||||
var product = await _db.SleeveShopProducts
|
||||
.Include(p => p.Rewards)
|
||||
.Include(p => p.Series)
|
||||
.FirstOrDefaultAsync(p => p.Id == request.ProductId);
|
||||
if (product is null) return NotFound(new { error = "unknown_product" });
|
||||
|
||||
if (!product.IsEnabled || product.Series is not { IsEnabled: true })
|
||||
return BadRequest(new { error = "product_not_available" });
|
||||
|
||||
// Defence-in-depth: client also sends series_id; reject mismatches so a misencoded
|
||||
// request can't accidentally bypass per-series state we'll later add (e.g. series-new flag).
|
||||
if (product.SeriesId != request.SeriesId)
|
||||
return BadRequest(new { error = "series_product_mismatch" });
|
||||
|
||||
var viewer = await LoadViewerGraphAsync(viewerId);
|
||||
|
||||
if (IsProductPurchased(product, viewer.Sleeves.Select(s => (long)s.Id).ToHashSet()))
|
||||
return BadRequest(new { error = "already_purchased" });
|
||||
|
||||
// Pricing: capture-confirmed shape is single-price-per-currency (no intro/regular tiers
|
||||
// like BuildDeck). At least one of crystal/rupy must match the chosen sales_type;
|
||||
// sales_type==0 means "free", which requires both prices == 0.
|
||||
var rewardList = new List<RewardListEntry>();
|
||||
switch (request.SalesType)
|
||||
{
|
||||
case 0: // free
|
||||
if (!(product.PriceCrystal == 0 && product.PriceRupy == 0))
|
||||
return BadRequest(new { error = "price_not_available_for_currency" });
|
||||
break;
|
||||
case 1: // crystal
|
||||
if (product.PriceCrystal is null)
|
||||
return BadRequest(new { error = "price_not_available_for_currency" });
|
||||
var crystalCost = (ulong)product.PriceCrystal.Value;
|
||||
if (viewer.Currency.Crystals < crystalCost)
|
||||
return BadRequest(new { error = "insufficient_crystals" });
|
||||
viewer.Currency.Crystals -= crystalCost;
|
||||
rewardList.Add(new RewardListEntry { RewardType = 2, RewardId = 0, RewardNum = (int)viewer.Currency.Crystals });
|
||||
break;
|
||||
case 2: // rupy
|
||||
if (product.PriceRupy is null)
|
||||
return BadRequest(new { error = "price_not_available_for_currency" });
|
||||
var rupyCost = (ulong)product.PriceRupy.Value;
|
||||
if (viewer.Currency.Rupees < rupyCost)
|
||||
return BadRequest(new { error = "insufficient_rupees" });
|
||||
viewer.Currency.Rupees -= rupyCost;
|
||||
rewardList.Add(new RewardListEntry { RewardType = 9, RewardId = 0, RewardNum = (int)viewer.Currency.Rupees });
|
||||
break;
|
||||
}
|
||||
|
||||
// Grant each catalog reward through the central dispatcher — covers sleeve (6), emblem
|
||||
// (7), and any future bundled grants. ApplyAsync returns post-state-aware reward entries
|
||||
// suitable for emission as-is.
|
||||
foreach (var r in product.Rewards.OrderBy(r => r.OrderIndex))
|
||||
{
|
||||
var granted = await _rewards.ApplyAsync(viewer, (UserGoodsType)r.RewardType, r.RewardDetailId, r.RewardNumber);
|
||||
foreach (var g in granted)
|
||||
{
|
||||
rewardList.Add(new RewardListEntry
|
||||
{
|
||||
RewardType = g.RewardType,
|
||||
RewardId = g.RewardId,
|
||||
RewardNum = g.RewardNum,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new SleeveBuyResponse { RewardList = rewardList };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A product is "purchased" once the viewer owns at least one of its sleeve-typed reward
|
||||
/// grants. Emblem/other grants aren't load-bearing for this check — a viewer who somehow
|
||||
/// ended up with the emblem but not the sleeve (e.g. partial gift) should still be allowed
|
||||
/// to buy the product to pick up the sleeve.
|
||||
/// </summary>
|
||||
private static bool IsProductPurchased(SleeveShopProductEntry product, HashSet<long> ownedSleeveIds)
|
||||
{
|
||||
foreach (var r in product.Rewards)
|
||||
{
|
||||
if (r.RewardType == (int)UserGoodsType.Sleeve && ownedSleeveIds.Contains(r.RewardDetailId))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Task<Viewer> LoadViewerGraphAsync(long viewerId) => _db.Viewers
|
||||
.Include(v => v.Sleeves)
|
||||
.Include(v => v.Emblems)
|
||||
.Include(v => v.LeaderSkins)
|
||||
.Include(v => v.Degrees)
|
||||
.Include(v => v.MyPageBackgrounds)
|
||||
.Include(v => v.Items).ThenInclude(i => i.Item)
|
||||
.Include(v => v.Cards).ThenInclude(c => c.Card)
|
||||
.AsSplitQuery()
|
||||
.FirstAsync(v => v.Id == viewerId);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using MessagePack;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Sleeve;
|
||||
|
||||
/// <summary>
|
||||
/// /sleeve/buy request body. sales_type is ShopCommonUtility.SalesType:
|
||||
/// 0=free, 1=crystal, 2=rupy, 3=ticket (v1: 3 returns 501, no ticket-priced sleeve captured).
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public class SleeveBuyRequest : BaseRequest
|
||||
{
|
||||
[JsonPropertyName("series_id")]
|
||||
[Key("series_id")]
|
||||
public int SeriesId { get; set; }
|
||||
|
||||
[JsonPropertyName("product_id")]
|
||||
[Key("product_id")]
|
||||
public int ProductId { get; set; }
|
||||
|
||||
[JsonPropertyName("sales_type")]
|
||||
[Key("sales_type")]
|
||||
public int SalesType { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using MessagePack;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Sleeve;
|
||||
|
||||
/// <summary>
|
||||
/// /sleeve/buy response. <c>reward_list</c> items use reward_id/reward_num
|
||||
/// (POST-STATE-TOTAL for currencies, grant id+count for cosmetics) — driven by
|
||||
/// <c>PlayerStaticData.UpdateHaveUserGoodsNumByJsonData</c>. Mirrors the /pack/open +
|
||||
/// /build_deck/buy reward_list semantics.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public class SleeveBuyResponse
|
||||
{
|
||||
[JsonPropertyName("reward_list")]
|
||||
[Key("reward_list")]
|
||||
public List<RewardListEntry> RewardList { get; set; } = new();
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using MessagePack;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Sleeve;
|
||||
|
||||
/// <summary>
|
||||
/// /sleeve/info response. Wire shape: <c>{sleeve_list: {<series_id_str>: SleeveSeriesDto}}</c>.
|
||||
/// Dict-keyed (not array) to match the prod capture exactly — LitJson's numeric indexer in
|
||||
/// <c>SleevePurchaseInfoTask.Parse()</c> iterates dict values by inserted order, so either
|
||||
/// shape would work, but mirroring the wire avoids surprise.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public class SleeveInfoResponse
|
||||
{
|
||||
[JsonPropertyName("sleeve_list")]
|
||||
[Key("sleeve_list")]
|
||||
public Dictionary<string, SleeveSeriesDto> SleeveList { get; set; } = new();
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
public class SleeveSeriesDto
|
||||
{
|
||||
[JsonPropertyName("series_id")]
|
||||
[Key("series_id")]
|
||||
public int SeriesId { get; set; }
|
||||
|
||||
[JsonPropertyName("is_new")]
|
||||
[Key("is_new")]
|
||||
public bool IsNew { get; set; }
|
||||
|
||||
/// <summary>Dict keyed by product_id string — same iteration convention as sleeve_list.</summary>
|
||||
[JsonPropertyName("product_info")]
|
||||
[Key("product_info")]
|
||||
public Dictionary<string, SleeveProductDto> ProductInfo { get; set; } = new();
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
public class SleeveProductDto
|
||||
{
|
||||
[JsonPropertyName("product_id")]
|
||||
[Key("product_id")]
|
||||
public int ProductId { get; set; }
|
||||
|
||||
/// <summary>SystemText key (e.g. "sleeve_138") — client resolves via GetSleeveProductText.</summary>
|
||||
[JsonPropertyName("name")]
|
||||
[Key("name")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("rewards")]
|
||||
[Key("rewards")]
|
||||
public List<SleeveProductRewardDto> Rewards { get; set; } = new();
|
||||
|
||||
[JsonPropertyName("sales_period_info")]
|
||||
[Key("sales_period_info")]
|
||||
public List<object> SalesPeriodInfo { get; set; } = new(); // always [] in v1
|
||||
|
||||
[JsonPropertyName("is_purchased_product")]
|
||||
[Key("is_purchased_product")]
|
||||
public bool IsPurchasedProduct { get; set; }
|
||||
|
||||
[JsonPropertyName("price_crystal")]
|
||||
[Key("price_crystal")]
|
||||
public int? PriceCrystal { get; set; }
|
||||
|
||||
[JsonPropertyName("price_rupy")]
|
||||
[Key("price_rupy")]
|
||||
public int? PriceRupy { get; set; }
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
public class SleeveProductRewardDto
|
||||
{
|
||||
[JsonPropertyName("reward_type")]
|
||||
[Key("reward_type")]
|
||||
public int RewardType { get; set; }
|
||||
|
||||
[JsonPropertyName("reward_detail_id")]
|
||||
[Key("reward_detail_id")]
|
||||
public long RewardDetailId { get; set; }
|
||||
|
||||
[JsonPropertyName("reward_number")]
|
||||
[Key("reward_number")]
|
||||
public int RewardNumber { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user