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:
gamer147
2026-05-27 22:09:45 -04:00
parent 6a03ff1bf6
commit f237851e42
18 changed files with 10316 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,89 @@
using Microsoft.EntityFrameworkCore;
using SVSim.Bootstrap.Models.Seed;
using SVSim.Database;
using SVSim.Database.Models;
namespace SVSim.Bootstrap.Importers;
/// <summary>
/// Idempotent upsert of the sleeve-shop catalog from <c>seeds/sleeve-shop.json</c>.
/// Source is the wire <c>/sleeve/info</c> response, extracted via
/// <c>data_dumps/extract/extract-sleeve-shop.py</c>. Mirror of the BuildDeck importer pattern.
/// Rows missing from the seed are LEFT INTACT (so manual test fixtures survive re-runs).
/// </summary>
public class SleeveShopImporter
{
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
{
string path = Path.Combine(seedDir, "sleeve-shop.json");
var seed = SeedLoader.LoadList<SleeveShopSeriesSeed>(path);
if (seed.Count == 0)
{
Console.WriteLine("[SleeveShopImporter] No seed rows; skipping.");
return 0;
}
var existingSeries = await context.SleeveShopSeries
.Include(s => s.Products).ThenInclude(p => p.Rewards)
.ToDictionaryAsync(s => s.Id);
int createdSeries = 0, updatedSeries = 0, createdProducts = 0, updatedProducts = 0;
foreach (var s in seed)
{
if (s.SeriesId == 0) continue;
if (!existingSeries.TryGetValue(s.SeriesId, out var series))
{
series = new SleeveShopSeriesEntry { Id = s.SeriesId };
context.SleeveShopSeries.Add(series);
existingSeries[s.SeriesId] = series;
createdSeries++;
}
else updatedSeries++;
series.IsNew = s.IsNew;
series.IsEnabled = true;
var existingProducts = series.Products.ToDictionary(p => p.Id);
foreach (var p in s.Products)
{
if (p.ProductId == 0) continue;
if (!existingProducts.TryGetValue(p.ProductId, out var product))
{
product = new SleeveShopProductEntry { Id = p.ProductId };
series.Products.Add(product);
createdProducts++;
}
else updatedProducts++;
product.SeriesId = s.SeriesId;
product.NameKey = p.NameKey;
product.PriceCrystal = p.PriceCrystal;
product.PriceRupy = p.PriceRupy;
product.IsEnabled = true;
// Rewards: replace wholesale (owned collection — EF will issue DELETE+INSERT
// anyway, and the wire shape is canonical per re-extract).
product.Rewards.Clear();
foreach (var r in p.Rewards.OrderBy(r => r.OrderIndex))
{
product.Rewards.Add(new SleeveShopProductRewardEntry
{
OrderIndex = r.OrderIndex,
RewardType = r.RewardType,
RewardDetailId = r.RewardDetailId,
RewardNumber = r.RewardNumber,
});
}
}
}
await context.SaveChangesAsync();
Console.WriteLine(
$"[SleeveShopImporter] series +{createdSeries}/~{updatedSeries}, " +
$"products +{createdProducts}/~{updatedProducts}");
return createdSeries + updatedSeries;
}
}

View File

@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;
namespace SVSim.Bootstrap.Models.Seed;
public sealed class SleeveShopSeriesSeed
{
[JsonPropertyName("series_id")] public int SeriesId { get; set; }
[JsonPropertyName("is_new")] public bool IsNew { get; set; }
[JsonPropertyName("products")] public List<SleeveShopProductSeed> Products { get; set; } = new();
}
public sealed class SleeveShopProductSeed
{
[JsonPropertyName("product_id")] public int ProductId { get; set; }
[JsonPropertyName("name_key")] public string NameKey { get; set; } = "";
[JsonPropertyName("price_crystal")] public int? PriceCrystal { get; set; }
[JsonPropertyName("price_rupy")] public int? PriceRupy { get; set; }
[JsonPropertyName("rewards")] public List<SleeveShopRewardSeed> Rewards { get; set; } = new();
}
public sealed class SleeveShopRewardSeed
{
[JsonPropertyName("order_index")] public int OrderIndex { get; set; }
[JsonPropertyName("reward_type")] public int RewardType { get; set; }
[JsonPropertyName("reward_detail_id")] public long RewardDetailId { get; set; }
[JsonPropertyName("reward_number")] public int RewardNumber { get; set; }
}

View File

@@ -98,6 +98,7 @@ public static class Program
await new PracticeOpponentImporter().ImportAsync(context, opts.SeedDir);
await new PaymentItemImporter().ImportAsync(context, opts.SeedDir);
await new ItemImporter().ImportAsync(context, opts.SeedDir);
await new SleeveShopImporter().ImportAsync(context, opts.SeedDir);
var puzzleImporter = new PuzzleImporter();
await puzzleImporter.ImportGroupsAsync(context, opts.SeedDir);
await puzzleImporter.ImportPuzzlesAsync(context, opts.SeedDir);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,96 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace SVSim.Database.Migrations
{
/// <inheritdoc />
public partial class AddSleeveShop : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SleeveShopSeries",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false),
IsNew = table.Column<bool>(type: "boolean", nullable: false),
IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
DateCreated = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
DateUpdated = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SleeveShopSeries", x => x.Id);
});
migrationBuilder.CreateTable(
name: "SleeveShopProducts",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false),
SeriesId = table.Column<int>(type: "integer", nullable: false),
NameKey = table.Column<string>(type: "text", nullable: false),
PriceCrystal = table.Column<int>(type: "integer", nullable: true),
PriceRupy = table.Column<int>(type: "integer", nullable: true),
IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
DateCreated = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
DateUpdated = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SleeveShopProducts", x => x.Id);
table.ForeignKey(
name: "FK_SleeveShopProducts_SleeveShopSeries_SeriesId",
column: x => x.SeriesId,
principalTable: "SleeveShopSeries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SleeveShopProductRewardEntry",
columns: table => new
{
SleeveShopProductEntryId = table.Column<int>(type: "integer", nullable: false),
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
OrderIndex = table.Column<int>(type: "integer", nullable: false),
RewardType = table.Column<int>(type: "integer", nullable: false),
RewardDetailId = table.Column<long>(type: "bigint", nullable: false),
RewardNumber = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SleeveShopProductRewardEntry", x => new { x.SleeveShopProductEntryId, x.Id });
table.ForeignKey(
name: "FK_SleeveShopProductRewardEntry_SleeveShopProducts_SleeveShopP~",
column: x => x.SleeveShopProductEntryId,
principalTable: "SleeveShopProducts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_SleeveShopProducts_SeriesId",
table: "SleeveShopProducts",
column: "SeriesId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SleeveShopProductRewardEntry");
migrationBuilder.DropTable(
name: "SleeveShopProducts");
migrationBuilder.DropTable(
name: "SleeveShopSeries");
}
}
}

View File

@@ -1964,6 +1964,62 @@ namespace SVSim.Database.Migrations
b.ToTable("Sleeves");
});
modelBuilder.Entity("SVSim.Database.Models.SleeveShopProductEntry", b =>
{
b.Property<int>("Id")
.HasColumnType("integer");
b.Property<DateTime>("DateCreated")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("DateUpdated")
.HasColumnType("timestamp with time zone");
b.Property<bool>("IsEnabled")
.HasColumnType("boolean");
b.Property<string>("NameKey")
.IsRequired()
.HasColumnType("text");
b.Property<int?>("PriceCrystal")
.HasColumnType("integer");
b.Property<int?>("PriceRupy")
.HasColumnType("integer");
b.Property<int>("SeriesId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("SeriesId");
b.ToTable("SleeveShopProducts");
});
modelBuilder.Entity("SVSim.Database.Models.SleeveShopSeriesEntry", b =>
{
b.Property<int>("Id")
.HasColumnType("integer");
b.Property<DateTime>("DateCreated")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("DateUpdated")
.HasColumnType("timestamp with time zone");
b.Property<bool>("IsEnabled")
.HasColumnType("boolean");
b.Property<bool>("IsNew")
.HasColumnType("boolean");
b.HasKey("Id");
b.ToTable("SleeveShopSeries");
});
modelBuilder.Entity("SVSim.Database.Models.SpecialDeckFormatEntry", b =>
{
b.Property<int>("Id")
@@ -2828,6 +2884,50 @@ namespace SVSim.Database.Migrations
b.Navigation("Sleeve");
});
modelBuilder.Entity("SVSim.Database.Models.SleeveShopProductEntry", b =>
{
b.HasOne("SVSim.Database.Models.SleeveShopSeriesEntry", "Series")
.WithMany("Products")
.HasForeignKey("SeriesId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.OwnsMany("SVSim.Database.Models.SleeveShopProductRewardEntry", "Rewards", b1 =>
{
b1.Property<int>("SleeveShopProductEntryId")
.HasColumnType("integer");
b1.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b1.Property<int>("Id"));
b1.Property<int>("OrderIndex")
.HasColumnType("integer");
b1.Property<long>("RewardDetailId")
.HasColumnType("bigint");
b1.Property<int>("RewardNumber")
.HasColumnType("integer");
b1.Property<int>("RewardType")
.HasColumnType("integer");
b1.HasKey("SleeveShopProductEntryId", "Id");
b1.ToTable("SleeveShopProductRewardEntry");
b1.WithOwner()
.HasForeignKey("SleeveShopProductEntryId");
});
b.Navigation("Rewards");
b.Navigation("Series");
});
modelBuilder.Entity("SVSim.Database.Models.Viewer", b =>
{
b.OwnsMany("SVSim.Database.Models.OwnedCardEntry", "Cards", b1 =>
@@ -3259,6 +3359,11 @@ namespace SVSim.Database.Migrations
b.Navigation("Cards");
});
modelBuilder.Entity("SVSim.Database.Models.SleeveShopSeriesEntry", b =>
{
b.Navigation("Products");
});
modelBuilder.Entity("SVSim.Database.Models.Viewer", b =>
{
b.Navigation("Achievements");

View File

@@ -0,0 +1,32 @@
using SVSim.Database.Common;
namespace SVSim.Database.Models;
/// <summary>
/// One purchasable sleeve product. PK = wire product_id (e.g. 301901). FK SeriesId.
/// <para>
/// Both <see cref="PriceCrystal"/> and <see cref="PriceRupy"/> are nullable. At least one must be
/// populated for an enabled product (both zero = free, both null = invalid). Sleeves don't have
/// the two-tier intro/regular pricing that BuildDeck products use — one price per currency.
/// </para>
/// <para>
/// <see cref="Rewards"/> drives both the catalog display (in /sleeve/info) and the actual grant
/// list (in /sleeve/buy). The capture shows each sleeve product grants a sleeve (type=6) and an
/// emblem (type=7) — both faithful reward_detail_ids that exist in the cosmetic catalogs.
/// </para>
/// </summary>
public class SleeveShopProductEntry : BaseEntity<int>
{
public int SeriesId { get; set; }
/// <summary>Wire `name` field — SystemText key like "sleeve_138". Localised client-side.</summary>
public string NameKey { get; set; } = string.Empty;
public int? PriceCrystal { get; set; }
public int? PriceRupy { get; set; }
public bool IsEnabled { get; set; }
public List<SleeveShopProductRewardEntry> Rewards { get; set; } = new();
public SleeveShopSeriesEntry? Series { get; set; }
}

View File

@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
namespace SVSim.Database.Models;
/// <summary>
/// One per-buy reward attached to a sleeve product. Owned by <see cref="SleeveShopProductEntry"/>.
/// Wire shape: one entry of the product-level `rewards` array in /sleeve/info. Order is
/// preserved by <see cref="OrderIndex"/> since the wire shape is an ordered array, not a dict.
/// </summary>
[Owned]
public class SleeveShopProductRewardEntry
{
public int OrderIndex { get; set; }
public int RewardType { get; set; } // Wizard.UserGoods.Type
public long RewardDetailId { get; set; }
public int RewardNumber { get; set; }
}

View File

@@ -0,0 +1,16 @@
using SVSim.Database.Common;
namespace SVSim.Database.Models;
/// <summary>
/// One sleeve-shop series (a themed collection — e.g. series 3019 "BattlePass sleeves",
/// series 3004 "Granblue Fantasy collab"). PK = wire series_id. IsEnabled gates whether
/// /sleeve/info renders this series.
/// </summary>
public class SleeveShopSeriesEntry : BaseEntity<int>
{
public bool IsNew { get; set; }
public bool IsEnabled { get; set; }
public List<SleeveShopProductEntry> Products { get; set; } = new();
}

View File

@@ -70,6 +70,8 @@ public class SVSimDbContext : DbContext
public DbSet<PackConfigEntry> Packs => Set<PackConfigEntry>();
public DbSet<BuildDeckSeriesEntry> BuildDeckSeries => Set<BuildDeckSeriesEntry>();
public DbSet<BuildDeckProductEntry> BuildDeckProducts => Set<BuildDeckProductEntry>();
public DbSet<SleeveShopSeriesEntry> SleeveShopSeries => Set<SleeveShopSeriesEntry>();
public DbSet<SleeveShopProductEntry> SleeveShopProducts => Set<SleeveShopProductEntry>();
public DbSet<MaintenanceCardEntry> MaintenanceCards => Set<MaintenanceCardEntry>();
public DbSet<FeatureMaintenanceEntry> FeatureMaintenances => Set<FeatureMaintenanceEntry>();
public DbSet<PreReleaseInfo> PreReleaseInfos => Set<PreReleaseInfo>();
@@ -180,6 +182,14 @@ public class SVSimDbContext : DbContext
modelBuilder.Entity<BuildDeckProductEntry>().HasIndex(p => p.SeriesId);
modelBuilder.Entity<SleeveShopProductEntry>().OwnsMany(p => p.Rewards);
modelBuilder.Entity<SleeveShopProductEntry>()
.HasOne(p => p.Series)
.WithMany(s => s.Products)
.HasForeignKey(p => p.SeriesId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<SleeveShopProductEntry>().HasIndex(p => p.SeriesId);
modelBuilder.Entity<CardCosmeticReward>(b =>
{
b.HasKey(r => new { r.CardId, r.Type, r.CosmeticId });

View 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);
}

View File

@@ -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; }
}

View File

@@ -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();
}

View File

@@ -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; }
}

View File

@@ -0,0 +1,217 @@
using System.Net;
using System.Text;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Controllers;
public class SleeveControllerTests
{
private static StringContent JsonBody(string json) => new(json, Encoding.UTF8, "application/json");
/// <summary>
/// Seeds: series 9001 (enabled) with one crystal-priced product 900101 granting
/// sleeve 9000011 + emblem 9000011. Caller sets viewer crystals.
/// Sleeve + emblem catalog rows are inserted with placeholder names so RewardGrantService
/// can resolve them.
/// </summary>
private static async Task SeedCrystalProduct(SVSimTestFactory f, long viewerId, ulong crystals)
{
using var scope = f.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
// Sleeve + emblem catalog must exist so RewardGrantService.ApplyAsync can find them.
// Using ids outside the 1k-1.6k cosmetic seed range so they can't collide with reference data.
const int testSleeveId = 9000011;
const int testEmblemId = 9000011;
if (!await db.Sleeves.AnyAsync(s => s.Id == testSleeveId))
db.Sleeves.Add(new SleeveEntry { Id = testSleeveId });
if (!await db.Emblems.AnyAsync(e => e.Id == testEmblemId))
db.Emblems.Add(new EmblemEntry { Id = testEmblemId });
db.SleeveShopSeries.Add(new SleeveShopSeriesEntry
{
Id = 9001, IsEnabled = true, IsNew = false,
Products =
{
new SleeveShopProductEntry
{
Id = 900101, SeriesId = 9001, NameKey = "sleeve_test", PriceCrystal = 400,
IsEnabled = true,
Rewards =
{
new SleeveShopProductRewardEntry { OrderIndex = 0, RewardType = 7, RewardDetailId = testEmblemId, RewardNumber = 1 },
new SleeveShopProductRewardEntry { OrderIndex = 1, RewardType = 6, RewardDetailId = testSleeveId, RewardNumber = 1 },
},
},
},
});
var v = await db.Viewers.FirstAsync(x => x.Id == viewerId);
v.Currency.Crystals = crystals;
await db.SaveChangesAsync();
}
[Test]
public async Task Info_returns_dict_keyed_by_series_id_and_product_id()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 0);
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/sleeve/info",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":""}"""));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
var sleeveList = doc.RootElement.GetProperty("sleeve_list");
Assert.That(sleeveList.ValueKind, Is.EqualTo(JsonValueKind.Object), "wire shape is dict-keyed by series_id string");
var series = sleeveList.GetProperty("9001");
Assert.That(series.GetProperty("series_id").GetInt32(), Is.EqualTo(9001));
var productInfo = series.GetProperty("product_info");
Assert.That(productInfo.ValueKind, Is.EqualTo(JsonValueKind.Object), "product_info is dict-keyed by product_id string");
var product = productInfo.GetProperty("900101");
Assert.That(product.GetProperty("product_id").GetInt32(), Is.EqualTo(900101));
Assert.That(product.GetProperty("name").GetString(), Is.EqualTo("sleeve_test"));
Assert.That(product.GetProperty("price_crystal").GetInt32(), Is.EqualTo(400));
Assert.That(product.GetProperty("is_purchased_product").GetBoolean(), Is.False);
Assert.That(product.GetProperty("rewards").GetArrayLength(), Is.EqualTo(2));
}
[Test]
public async Task Buy_with_crystals_debits_currency_and_grants_cosmetics()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 1000);
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9001,"product_id":900101,"sales_type":1}"""));
var body = await response.Content.ReadAsStringAsync();
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK), body);
using var doc = JsonDocument.Parse(body);
var rewardList = doc.RootElement.GetProperty("reward_list");
Assert.That(rewardList.GetArrayLength(), Is.EqualTo(3)); // crystal post-state + emblem + sleeve
// First entry: crystal balance post-debit. reward_type=2 (Crystal), reward_id=0, num=600 (1000-400).
var crystal = rewardList[0];
Assert.That(crystal.GetProperty("reward_type").GetInt32(), Is.EqualTo(2));
Assert.That(crystal.GetProperty("reward_id").GetInt64(), Is.EqualTo(0));
Assert.That(crystal.GetProperty("reward_num").GetInt32(), Is.EqualTo(600));
// Viewer state: crystals decremented; sleeve + emblem in owned collections.
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
var viewer = await db.Viewers
.Include(v => v.Sleeves)
.Include(v => v.Emblems)
.FirstAsync(v => v.Id == viewerId);
Assert.That(viewer.Currency.Crystals, Is.EqualTo(600UL));
Assert.That(viewer.Sleeves.Any(s => s.Id == 9000011), Is.True);
Assert.That(viewer.Emblems.Any(e => e.Id == 9000011), Is.True);
}
[Test]
public async Task Buy_with_insufficient_crystals_rejects_with_400()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 100);
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9001,"product_id":900101,"sales_type":1}"""));
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Buy_with_series_product_mismatch_rejects_with_400()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 1000);
using var client = factory.CreateAuthenticatedClient(viewerId);
// product 900101 is in series 9001, not 9999
var response = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9999,"product_id":900101,"sales_type":1}"""));
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Buy_already_purchased_sleeve_rejects_with_400()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 1000);
// First buy succeeds
using var client = factory.CreateAuthenticatedClient(viewerId);
var first = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9001,"product_id":900101,"sales_type":1}"""));
Assert.That(first.StatusCode, Is.EqualTo(HttpStatusCode.OK));
// Second buy rejected
var second = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9001,"product_id":900101,"sales_type":1}"""));
Assert.That(second.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
[Test]
public async Task Buy_ticket_sales_type_returns_501()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 1000);
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/sleeve/buy",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":"","series_id":9001,"product_id":900101,"sales_type":3}"""));
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotImplemented));
}
[Test]
public async Task Info_marks_already_owned_sleeve_as_purchased()
{
using var factory = new SVSimTestFactory();
long viewerId = await factory.SeedViewerAsync();
await SeedCrystalProduct(factory, viewerId, crystals: 1000);
// Pre-grant the sleeve so /info should flag is_purchased_product=true
using (var scope = factory.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
var viewer = await db.Viewers.Include(v => v.Sleeves).FirstAsync(v => v.Id == viewerId);
var sleeve = await db.Sleeves.FindAsync(9000011);
viewer.Sleeves.Add(sleeve!);
await db.SaveChangesAsync();
}
using var client = factory.CreateAuthenticatedClient(viewerId);
var response = await client.PostAsync("/sleeve/info",
JsonBody("""{"viewer_id":"0","steam_id":0,"steam_session_ticket":""}"""));
var body = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(body);
var product = doc.RootElement
.GetProperty("sleeve_list").GetProperty("9001")
.GetProperty("product_info").GetProperty("900101");
Assert.That(product.GetProperty("is_purchased_product").GetBoolean(), Is.True);
}
}

View File

@@ -0,0 +1,87 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SVSim.Bootstrap.Importers;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.UnitTests.Infrastructure;
namespace SVSim.UnitTests.Importers;
public class SleeveShopImporterTests
{
private static string SeedDir => Path.Combine(AppContext.BaseDirectory, "Data", "seeds");
[Test]
public async Task Imports_series_and_products_from_seed_file()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await new SleeveShopImporter().ImportAsync(db, SeedDir);
var series = await db.SleeveShopSeries
.Include(s => s.Products).ThenInclude(p => p.Rewards)
.OrderBy(s => s.Id)
.ToListAsync();
Assert.That(series.Count, Is.GreaterThan(0), "seed file should contain series");
// Spot-check series 3019 (BattlePass sleeves) — captured at 6 products with crystal pricing.
var bp = series.FirstOrDefault(s => s.Id == 3019);
Assert.That(bp, Is.Not.Null, "series 3019 should be present");
Assert.That(bp!.Products.Count, Is.GreaterThan(0));
var firstProduct = bp.Products.OrderBy(p => p.Id).First();
Assert.That(firstProduct.NameKey, Does.StartWith("sleeve_"), "name should be a SystemText key");
Assert.That(firstProduct.Rewards, Is.Not.Empty, "products should have catalog rewards");
Assert.That(firstProduct.IsEnabled, Is.True);
}
[Test]
public async Task Is_idempotent_on_rerun()
{
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await new SleeveShopImporter().ImportAsync(db, SeedDir);
int seriesBefore = await db.SleeveShopSeries.CountAsync();
int productsBefore = await db.SleeveShopProducts.CountAsync();
await new SleeveShopImporter().ImportAsync(db, SeedDir);
Assert.That(await db.SleeveShopSeries.CountAsync(), Is.EqualTo(seriesBefore));
Assert.That(await db.SleeveShopProducts.CountAsync(), Is.EqualTo(productsBefore));
}
[Test]
public async Task Replaces_rewards_wholesale_on_rerun()
{
// Owned rewards collection: importer clears and re-adds. A stale catalog reward should
// not survive a re-import. (Hand-tamper one row, re-import, check the tamper is gone.)
using var factory = new SVSimTestFactory();
using var scope = factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
await new SleeveShopImporter().ImportAsync(db, SeedDir);
var product = await db.SleeveShopProducts
.Include(p => p.Rewards)
.OrderBy(p => p.Id)
.FirstAsync();
int originalCount = product.Rewards.Count;
product.Rewards.Add(new SleeveShopProductRewardEntry
{
OrderIndex = 99, RewardType = 99, RewardDetailId = 99, RewardNumber = 99,
});
await db.SaveChangesAsync();
await new SleeveShopImporter().ImportAsync(db, SeedDir);
var reloaded = await db.SleeveShopProducts
.Include(p => p.Rewards)
.FirstAsync(p => p.Id == product.Id);
Assert.That(reloaded.Rewards.Count, Is.EqualTo(originalCount), "extra reward should be wiped on re-import");
Assert.That(reloaded.Rewards.Any(r => r.RewardType == 99), Is.False);
}
}

View File

@@ -209,6 +209,7 @@ internal sealed class SVSimTestFactory : WebApplicationFactory<Program>
await new PracticeOpponentImporter().ImportAsync(ctx, seedDir);
await new PaymentItemImporter().ImportAsync(ctx, seedDir);
await new ItemImporter().ImportAsync(ctx, seedDir);
await new SleeveShopImporter().ImportAsync(ctx, seedDir);
var puzzleImporter = new PuzzleImporter();
await puzzleImporter.ImportGroupsAsync(ctx, seedDir);
await puzzleImporter.ImportPuzzlesAsync(ctx, seedDir);