Let administrators change the finish of an existing mythic variation
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 22m37s
Validate and deploy Sokko G / deploy (push) Successful in 16s

A variation keeps its edition id when its holographic finish changes, so
players keep their copies, duplicates, expedition progress and equipment.
A unique index on (card_id, finish_id) replaces the id-derived uniqueness,
and new variations get a suffixed id when the original one is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-21 17:50:15 +01:00
parent a52f89e9d5
commit 3f70a5638d
11 changed files with 265 additions and 38 deletions

View file

@ -185,6 +185,16 @@ test("cards API authenticates dev sessions and protects mutations", async (t) =>
const updated = await call(handler, { method: "PUT", path: mythicPath, headers: { cookie: sessionCookie, "x-csrf-token": csrfToken }, body: mythicBody });
assert.equal(updated.status, 200);
assert.ok(updated.json.catalog.editions.some((edition) => edition.id === "mythic-card-pull-spectral-prism-cosmic"));
const finishPath = "/api/cards/admin/catalog/mythics/mythic-card-pull-spectral-prism-solar/finish";
const regularFinish = await call(handler, { method: "PUT", path: finishPath, headers: { cookie: `sokkog_cards_session=${regularSession.token}`, "x-csrf-token": regularSession.csrfToken }, body: { finishId: "spectral-prism-aurora" } });
assert.equal(regularFinish.status, 403);
const takenFinish = await call(handler, { method: "PUT", path: finishPath, headers: { cookie: sessionCookie, "x-csrf-token": csrfToken }, body: { finishId: "spectral-prism-cosmic" } });
assert.deepEqual([takenFinish.status, takenFinish.json.error.code], [409, "FINISH_TAKEN"]);
const changedFinish = await call(handler, { method: "PUT", path: finishPath, headers: { cookie: sessionCookie, "x-csrf-token": csrfToken }, body: { finishId: "spectral-prism-aurora" } });
assert.equal(changedFinish.status, 200);
assert.deepEqual(changedFinish.json.catalog.editions.find((edition) => edition.id === "mythic-card-pull-spectral-prism-solar").finishIds, ["spectral-prism-aurora"]);
const restoredFinish = await call(handler, { method: "PUT", path: finishPath, headers: { cookie: sessionCookie, "x-csrf-token": csrfToken }, body: { finishId: "spectral-prism-solar" } });
assert.equal(restoredFinish.status, 200);
const collectionOpening = await call(handler, { method: "PUT", path: "/api/cards/admin/catalog/collections/astral-genesis/mythic-opening", headers: { cookie: sessionCookie, "x-csrf-token": csrfToken }, body: { particleShapeId: null, particlePaletteId: "match-holo" } });
assert.equal(collectionOpening.status, 200);
const refreshedConfig = await call(handler, { path: "/api/cards/config" });

View file

@ -11,7 +11,7 @@ import { INITIAL_COLLECTION_STATES, INITIAL_DROP_RATES, INITIAL_RECHARGE_SETTING
import { planRecycling } from "../services/cards/stardust.js";
import { resolveMythicOpeningStyle, resolveOpeningEffect } from "../website/src/features/cards/openingEffects.js";
import { summarizeReward } from "../website/src/features/cards/rewardText.js";
import { CardsDatabase } from "../services/cards/database.js";
import { CardsDatabase, MIGRATIONS } from "../services/cards/database.js";
import { calculateRecharge, drawPack, drawShopPack } from "../services/cards/game-rules.js";
import { CardsService, DEV_DISCORD_ID } from "../services/cards/service.js";
import { rarityPercentages } from "../website/src/features/cards/dropRates.js";
@ -446,8 +446,9 @@ test("rebuilding the players table keeps every row that references a player", as
service.completeOpening(player.id, opened.opening.id);
const counts = () => Object.fromEntries(["players", "sessions", "openings", "collection_cards", "audit_log"].map((table) => [table, database.db.prepare(`SELECT COUNT(*) AS count FROM ${table}`).get().count]));
const before = counts();
const version = database.db.prepare("SELECT MAX(version) AS version FROM schema_migrations").get().version;
// La reconstruction est la dernière migration : la rejouer refait la copie sur des données existantes.
// Rejouer la reconstruction (et elle seule) refait la copie sur des données existantes.
const version = MIGRATIONS.findIndex((migration) => migration.withoutForeignKeys && migration.sql.includes("CREATE TABLE players_next")) + 1;
assert.ok(version > 0);
database.db.prepare("DELETE FROM schema_migrations WHERE version = ?").run(version);
database.close();
@ -1135,6 +1136,65 @@ test("mythic variations live only in the database and a new database starts with
database.close();
});
test("changing a mythic finish keeps its id, the players' copies and their expedition progress", () => {
const now = 1_800_000_000_000;
const database = new CardsDatabase(":memory:");
const withoutImage = structuredClone(baseCatalog);
delete withoutImage.cards.find((card) => card.id === "aurora-wolf").fullImage;
const service = new CardsService({ database, catalog: withoutImage, now: () => now, random: () => 0 });
const admin = service.upsertPlayer({ discordId: "finish-admin", displayName: "Admin", isAdmin: true });
const player = service.upsertPlayer({ discordId: "finish-player", displayName: "Player" });
const solar = "mythic-card-pull-spectral-prism-solar";
service.setCardMythicConfig(admin.id, "mythic-card-pull", { finishIds: ["spectral-prism-solar", "spectral-prism-aurora"] });
service.setCardMythicConfig(admin.id, "aurora-wolf", { finishIds: ["spectral-prism-solar"] });
service.setDropRates(admin.id, "astral-genesis", { common: 0, rare: 0, epic: 0, legendary: 0, mythic: 1 });
const opening = service.openPack(player.id, "astral-genesis", "finish-pack");
service.completeOpening(player.id, opening.opening.id);
const before = service.collectionOf(player.id).find((entry) => entry.editionId === solar);
assert.ok(before?.quantity >= 1);
database.db.prepare("INSERT INTO card_progress(player_id, edition_id, level, field_xp, updated_at) VALUES (?, ?, 4, 17, 0)").run(player.id, solar);
assert.throws(() => service.setMythicFinish(player.id, solar, "spectral-prism-cosmic"), /administrateur/);
assert.throws(() => service.setMythicFinish(admin.id, "unknown-mythic", "spectral-prism-cosmic"), /introuvable/);
assert.throws(() => service.setMythicFinish(admin.id, solar, "standard"), /holographique/);
assert.throws(() => service.setMythicFinish(admin.id, "aurora-wolf-spectral-prism-solar", "full-art-spectral-prism-aurora"), /illustration pleine/);
assert.throws(() => service.setMythicFinish(admin.id, solar, "spectral-prism-solar"), /déjà cette finition/);
assert.throws(() => service.setMythicFinish(admin.id, solar, "spectral-prism-aurora"), /déjà une variation/);
const result = service.setMythicFinish(admin.id, solar, "spectral-prism-cosmic");
const changed = result.catalog.editions.find((edition) => edition.id === solar);
assert.deepEqual([changed.finishIds, changed.rarity, changed.retired], [["spectral-prism-cosmic"], "mythic", undefined]);
assert.deepEqual(service.collectionOf(player.id).find((entry) => entry.editionId === solar), before);
assert.deepEqual({ ...database.db.prepare("SELECT level, field_xp AS fieldXp FROM card_progress WHERE player_id = ? AND edition_id = ?").get(player.id, solar) }, { level: 4, fieldXp: 17 });
const audit = database.db.prepare("SELECT entity_id, details_json FROM audit_log WHERE event_type = 'catalog_mythic_finish_changed'").get();
assert.deepEqual([audit.entity_id, JSON.parse(audit.details_json)], ["mythic-card-pull", { editionId: solar, previousFinishId: "spectral-prism-solar", finishId: "spectral-prism-cosmic" }]);
// La configuration par finitions retrouve la variation par sa finition actuelle, pas par son identifiant.
service.setCardMythicConfig(admin.id, "mythic-card-pull", { finishIds: ["spectral-prism-cosmic", "spectral-prism-aurora"] });
const mythicsOf = () => service.catalog.editions.filter((edition) => edition.cardId === "mythic-card-pull" && edition.rarity === "mythic");
assert.deepEqual(mythicsOf().map((edition) => [edition.id, edition.finishIds[0], Boolean(edition.retired)]).sort(), [
["mythic-card-pull-spectral-prism-aurora", "spectral-prism-aurora", false],
[solar, "spectral-prism-cosmic", false]
]);
// Reprendre l'ancienne finition crée une nouvelle variation : l'identifiant d'origine est déjà pris, il est suffixé.
service.setCardMythicConfig(admin.id, "mythic-card-pull", { finishIds: ["spectral-prism-cosmic", "spectral-prism-aurora", "spectral-prism-solar"] });
assert.equal(mythicsOf().find((edition) => edition.finishIds[0] === "spectral-prism-solar").id, `${solar}-2`);
// Retirer la finition actuelle retire la variation possédée, sous son identifiant d'origine.
service.setCardMythicConfig(admin.id, "mythic-card-pull", { finishIds: ["spectral-prism-aurora", "spectral-prism-solar"] });
assert.equal(mythicsOf().find((edition) => edition.id === solar).retired, true);
// Le changement survit au redémarrage et la base refuse deux variations d'une carte sur la même finition.
const reloaded = new CardsService({ database, catalog: withoutImage, now: () => now });
assert.deepEqual(reloaded.catalog.editions.find((edition) => edition.id === solar).finishIds, ["spectral-prism-cosmic"]);
assert.throws(() => database.db.prepare("INSERT INTO mythic_variations(edition_id, card_id, finish_id, status, updated_at) VALUES ('mythic-card-pull-copy', 'mythic-card-pull', 'spectral-prism-cosmic', 'active', 0)").run(), /UNIQUE/);
assert.throws(() => buildPublishedCatalog(baseCatalog, { variations: [
{ editionId: "mythic-card-pull-a", cardId: "mythic-card-pull", finishId: "spectral-prism-solar", status: "active" },
{ editionId: "mythic-card-pull-b", cardId: "mythic-card-pull", finishId: "spectral-prism-solar", status: "active" }
] }), /repeats the finish/);
assert.throws(() => buildPublishedCatalog(baseCatalog, { variations: [{ editionId: "aurora-wolf-x", cardId: "mythic-card-pull", finishId: "spectral-prism-solar", status: "active" }] }), /needs a legendary card/);
database.close();
});
test("pack availability and drop rates live in the database and apply immediately", () => {
assert.equal("dropTables" in rawCatalog, false);
assert.ok(rawCatalog.collections.every((collection) => !("active" in collection) && !("dropWeights" in collection)));