Let administrators change the finish of an existing mythic variation
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:
parent
a52f89e9d5
commit
3f70a5638d
11 changed files with 265 additions and 38 deletions
|
|
@ -492,6 +492,11 @@ export function createCardsApiHandler({
|
|||
json(res, 200, service.setCardMythicConfig(session.player.id, cardMythic[0], { finishIds: body.finishIds, opening: { particleShapeId: body.opening?.particleShapeId ?? null, particlePaletteId: body.opening?.particlePaletteId ?? null } }));
|
||||
return true;
|
||||
}
|
||||
const mythicFinish = routeMatch(url.pathname, /^\/api\/cards\/admin\/catalog\/mythics\/([^/]+)\/finish$/);
|
||||
if (req.method === "PUT" && mythicFinish) {
|
||||
json(res, 200, service.setMythicFinish(session.player.id, mythicFinish[0], (await readBody(req)).finishId));
|
||||
return true;
|
||||
}
|
||||
if (req.method === "POST" && url.pathname === "/api/cards/recycle") {
|
||||
const body = await readBody(req);
|
||||
json(res, 200, service.recycleDuplicates(session.player.id, { editionId: body.editionId ?? null, maxRarity: body.maxRarity ?? "legendary" }));
|
||||
|
|
|
|||
|
|
@ -375,10 +375,16 @@ export function legendaryParentEdition(catalog, cardId) {
|
|||
return catalog.editions.find((edition) => edition.cardId === cardId && edition.rarity === "legendary" && !edition.promotional) || null;
|
||||
}
|
||||
|
||||
// Identifiant donné à une variation à sa création. Il ne change plus ensuite, même si sa finition change :
|
||||
// il ne faut donc pas en déduire la finition (lire `finishIds`).
|
||||
export function mythicEditionId(cardId, finishId) {
|
||||
return `${cardId}-${finishId}`;
|
||||
}
|
||||
|
||||
export function isMythicEditionIdOf(editionId, cardId) {
|
||||
return typeof editionId === "string" && editionId.startsWith(`${cardId}-`);
|
||||
}
|
||||
|
||||
// Construit le catalogue publié : catalogue versionné + variations mythiques et réglages d'ouverture enregistrés en base.
|
||||
// `variations` : [{ editionId, cardId, finishId, status: "active" | "retired" }]
|
||||
// `openingSettings` : [{ scope: "collection" | "card", targetId, particleShapeId | null, particlePaletteId | null }]
|
||||
|
|
@ -397,12 +403,16 @@ export function buildPublishedCatalog(versionedCatalog, { variations = [], openi
|
|||
}
|
||||
const holographic = holographicFinishIds(catalog);
|
||||
const knownIds = new Set(catalog.editions.map((edition) => edition.id));
|
||||
const cardFinishes = new Set();
|
||||
const mythics = variations.map((variation) => {
|
||||
const parent = legendaryParentEdition(catalog, variation.cardId);
|
||||
if (!parent || !holographic.has(variation.finishId) || variation.editionId !== mythicEditionId(variation.cardId, variation.finishId) || knownIds.has(variation.editionId)) {
|
||||
const cardFinish = `${variation.cardId}\n${variation.finishId}`;
|
||||
if (!parent || !holographic.has(variation.finishId) || !isMythicEditionIdOf(variation.editionId, variation.cardId) || knownIds.has(variation.editionId)) {
|
||||
throw new TypeError(`Mythic variation ${variation.editionId} needs a legendary card and a holographic finish.`);
|
||||
}
|
||||
if (cardFinishes.has(cardFinish)) throw new TypeError(`Mythic variation ${variation.editionId} repeats the finish ${variation.finishId}.`);
|
||||
knownIds.add(variation.editionId);
|
||||
cardFinishes.add(cardFinish);
|
||||
return {
|
||||
id: variation.editionId,
|
||||
cardId: variation.cardId,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { DatabaseSync } from "node:sqlite";
|
|||
import { INITIAL_COLLECTION_STATES, INITIAL_DROP_RATES, INITIAL_RECHARGE_SETTING, INITIAL_SHOP_SETTING } from "./admin-seed.js";
|
||||
import { DEFAULT_EXPEDITION_SETTINGS } from "./expedition-settings.js";
|
||||
|
||||
const MIGRATIONS = [
|
||||
export const MIGRATIONS = [
|
||||
`
|
||||
CREATE TABLE players (
|
||||
id TEXT PRIMARY KEY,
|
||||
|
|
@ -482,7 +482,12 @@ const MIGRATIONS = [
|
|||
DROP TABLE recharge_settings;
|
||||
ALTER TABLE recharge_settings_next RENAME TO recharge_settings;
|
||||
`
|
||||
}
|
||||
},
|
||||
// La finition d'une variation mythique peut changer sans toucher à son identifiant (les exemplaires des joueurs restent) :
|
||||
// l'identifiant ne garantit donc plus qu'une carte a une seule variation par finition, cet index le fait.
|
||||
`
|
||||
CREATE UNIQUE INDEX mythic_variations_card_finish_idx ON mythic_variations(card_id, finish_id);
|
||||
`
|
||||
];
|
||||
|
||||
export class CardsDatabase {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
import { createHash, randomBytes, randomInt, randomUUID } from "node:crypto";
|
||||
import { normalizeReward } from "./rewards.js";
|
||||
import { MASS_RECYCLE_RARITIES, planRecycling, spareQuantity, validateShopSetting } from "./stardust.js";
|
||||
import { FULL_ART_FORMAT, buildPublishedCatalog, collectionDrawableEditions, createCatalogIndex, holographicFinishIds, legendaryParentEdition, MATCH_HOLO_PALETTE_ID, mythicEditionId, publicCatalog, validateRarityWeights } from "./catalog.js";
|
||||
import { FULL_ART_FORMAT, buildPublishedCatalog, collectionDrawableEditions, createCatalogIndex, holographicFinishIds, isMythicEditionIdOf, legendaryParentEdition, MATCH_HOLO_PALETTE_ID, mythicEditionId, publicCatalog, validateRarityWeights } from "./catalog.js";
|
||||
import { computeStreak, DAILY_REPEAT_WINDOW_DAYS, dailyCandidates, dailyDayKey, dailyZoom, drawDailyPuzzle, guessHint, isDayKey, MAX_DAILY_GUESSES, nextDailyAt, shiftDayKey } from "./daily.js";
|
||||
import { calculateRecharge, pinnedRecharge, drawPack, drawShopPack, MAX_RECHARGE_INTERVAL_MS, MAX_RESERVE_OPENINGS, MIN_RECHARGE_INTERVAL_MS, spendOpening } from "./game-rules.js";
|
||||
import { DEFAULT_DESTINATIONS_PATH, loadExpeditionDestinations } from "./expedition-content.js";
|
||||
|
|
@ -155,7 +155,7 @@ export class CardsService {
|
|||
const paletteIds = new Set([...versioned.particlePalettes.map((palette) => palette.id), MATCH_HOLO_PALETTE_ID]);
|
||||
const variations = this.db.prepare("SELECT edition_id, card_id, finish_id, status FROM mythic_variations").all()
|
||||
.map((row) => ({ editionId: row.edition_id, cardId: row.card_id, finishId: row.finish_id, status: row.status }))
|
||||
.filter((row) => holographic.has(row.finishId) && legendaryParentEdition(versioned, row.cardId) && row.editionId === mythicEditionId(row.cardId, row.finishId));
|
||||
.filter((row) => holographic.has(row.finishId) && legendaryParentEdition(versioned, row.cardId) && isMythicEditionIdOf(row.editionId, row.cardId));
|
||||
const openingSettings = this.db.prepare("SELECT scope, target_id, particle_shape_id, particle_palette_id FROM mythic_opening_settings ORDER BY scope, target_id").all()
|
||||
.map((row) => ({
|
||||
scope: row.scope,
|
||||
|
|
@ -245,18 +245,21 @@ export class CardsService {
|
|||
// Une seule règle pour toutes les variations : retirée des tirages si un joueur la possède ou si une campagne
|
||||
// la cite (sinon ses codes et paquets offerts ne fonctionneraient plus), supprimée sinon.
|
||||
const campaignCounts = this.editionCampaignCounts();
|
||||
// Une variation se retrouve par sa finition actuelle : son identifiant garde la finition de sa création.
|
||||
const rows = new Map(this.db.prepare("SELECT edition_id, finish_id, status FROM mythic_variations WHERE card_id = ?").all(cardId).map((row) => [row.finish_id, row]));
|
||||
for (const finishId of holographic) {
|
||||
const editionId = mythicEditionId(cardId, finishId);
|
||||
const row = this.db.prepare("SELECT status FROM mythic_variations WHERE edition_id = ?").get(editionId);
|
||||
const row = rows.get(finishId);
|
||||
if (wanted.has(finishId)) {
|
||||
if (row?.status !== "active") {
|
||||
this.db.prepare(`INSERT INTO mythic_variations(edition_id, card_id, finish_id, status, updated_at, updated_by) VALUES (?, ?, ?, 'active', ?, ?)
|
||||
ON CONFLICT(edition_id) DO UPDATE SET status = 'active', updated_at = excluded.updated_at, updated_by = excluded.updated_by`).run(editionId, cardId, finishId, now, playerId);
|
||||
if (!row) {
|
||||
this.db.prepare("INSERT INTO mythic_variations(edition_id, card_id, finish_id, status, updated_at, updated_by) VALUES (?, ?, ?, 'active', ?, ?)")
|
||||
.run(this.freeMythicEditionId(cardId, finishId), cardId, finishId, now, playerId);
|
||||
} else if (row.status !== "active") {
|
||||
this.db.prepare("UPDATE mythic_variations SET status = 'active', updated_at = ?, updated_by = ? WHERE edition_id = ?").run(now, playerId, row.edition_id);
|
||||
}
|
||||
} else if (row && (this.editionReferenced(editionId) || campaignCounts[editionId] > 0)) {
|
||||
if (row.status !== "retired") this.db.prepare("UPDATE mythic_variations SET status = 'retired', updated_at = ?, updated_by = ? WHERE edition_id = ?").run(now, playerId, editionId);
|
||||
} else if (row && (this.editionReferenced(row.edition_id) || campaignCounts[row.edition_id] > 0)) {
|
||||
if (row.status !== "retired") this.db.prepare("UPDATE mythic_variations SET status = 'retired', updated_at = ?, updated_by = ? WHERE edition_id = ?").run(now, playerId, row.edition_id);
|
||||
} else if (row) {
|
||||
this.db.prepare("DELETE FROM mythic_variations WHERE edition_id = ?").run(editionId);
|
||||
this.db.prepare("DELETE FROM mythic_variations WHERE edition_id = ?").run(row.edition_id);
|
||||
}
|
||||
}
|
||||
this.writeOpeningSetting(playerId, "card", cardId, openingStyle);
|
||||
|
|
@ -266,6 +269,42 @@ export class CardsService {
|
|||
return { catalog: publicCatalog(this.catalog) };
|
||||
}
|
||||
|
||||
// Identifiant d'une nouvelle variation : `carte-finition`, suffixé si une variation dont la finition a changé le porte déjà
|
||||
// ou si d'anciens exemplaires, ouvertures, transferts ou campagnes le citent encore.
|
||||
freeMythicEditionId(cardId, finishId) {
|
||||
const base = mythicEditionId(cardId, finishId);
|
||||
const versionedIds = new Set(this.versionedCatalog.editions.map((edition) => edition.id));
|
||||
const campaignCounts = this.editionCampaignCounts();
|
||||
const taken = (id) => versionedIds.has(id) || campaignCounts[id] > 0 || this.editionReferenced(id)
|
||||
|| Boolean(this.db.prepare("SELECT 1 FROM mythic_variations WHERE edition_id = ?").get(id));
|
||||
let candidate = base;
|
||||
for (let index = 2; taken(candidate); index += 1) candidate = `${base}-${index}`;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
// Change la finition holographique d'une variation existante. Son identifiant ne change pas : les exemplaires des joueurs,
|
||||
// leur nombre, leur progression d'expédition, leurs équipements et les campagnes qui la citent restent les mêmes.
|
||||
setMythicFinish(playerId, editionId, finishId) {
|
||||
this.requireAdmin(playerId);
|
||||
const versioned = this.versionedCatalog;
|
||||
const row = this.db.prepare("SELECT card_id, finish_id FROM mythic_variations WHERE edition_id = ?").get(editionId);
|
||||
const card = row && versioned.cards.find((item) => item.id === row.card_id);
|
||||
if (!card) fail(404, "MYTHIC_NOT_FOUND", "Variation mythique introuvable.");
|
||||
const finish = versioned.finishes.find((item) => item.id === finishId);
|
||||
if (!finish || !holographicFinishIds(versioned).has(finishId)) fail(400, "FINISH_INVALID", "Choisissez une finition holographique.");
|
||||
if (finish.formatId === FULL_ART_FORMAT && !card.fullImage) fail(400, "FULL_IMAGE_REQUIRED", "Cette carte n’a pas d’illustration pleine.");
|
||||
if (row.finish_id === finishId) fail(400, "FINISH_UNCHANGED", "La variation a déjà cette finition.");
|
||||
if (this.db.prepare("SELECT 1 FROM mythic_variations WHERE card_id = ? AND finish_id = ?").get(row.card_id, finishId)) {
|
||||
fail(409, "FINISH_TAKEN", "Cette carte a déjà une variation avec cette finition.");
|
||||
}
|
||||
this.database.transaction(() => {
|
||||
this.db.prepare("UPDATE mythic_variations SET finish_id = ?, updated_at = ?, updated_by = ? WHERE edition_id = ?").run(finishId, this.now(), playerId, editionId);
|
||||
this.audit(playerId, "catalog_mythic_finish_changed", row.card_id, { editionId, previousFinishId: row.finish_id, finishId });
|
||||
this.reloadCatalog();
|
||||
});
|
||||
return { catalog: publicCatalog(this.catalog) };
|
||||
}
|
||||
|
||||
requireCollection(collectionId) {
|
||||
if (!this.versionedCatalog.collections.some((collection) => collection.id === collectionId)) fail(404, "COLLECTION_NOT_FOUND", "Collection introuvable.");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue