From 8b76570e7ad09ac2a7d22ddb460678da4bc60300 Mon Sep 17 00:00:00 2001 From: Shinuwa Date: Thu, 19 Nov 2026 09:54:56 +0100 Subject: [PATCH] Fix pack openings failing when they draw a destination card already owned The server builds the card service without a random source. Pack draws fall back to crypto randomness, but replacing a destination card the player already owns called the missing source and threw "this.random is not a function". The service now defaults to crypto randomness itself. Co-Authored-By: Claude Opus 5.5 --- services/cards/expedition-service.js | 4 ++-- services/cards/service.js | 5 +++-- tests/nebula-cards-expedition-service.test.mjs | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/services/cards/expedition-service.js b/services/cards/expedition-service.js index 269470f..ffc91d4 100644 --- a/services/cards/expedition-service.js +++ b/services/cards/expedition-service.js @@ -1,6 +1,6 @@ // Rôle : applique les règles des Expéditions Sokko TCG avec la base (destinations du jour, aperçu, retour, réglages d'administration). // Branché sur CardsService (`service.expeditions`) : il réutilise sa base, son horloge, son hasard, son audit et sa collection. -import { randomInt, randomUUID } from "node:crypto"; +import { randomUUID } from "node:crypto"; import { dailyDayKey, nextDailyAt, shiftDayKey } from "./daily.js"; import { drawCollectionCard } from "./game-rules.js"; import { @@ -42,7 +42,7 @@ export class ExpeditionService { } random(max) { - return this.cards.random ? this.cards.random(max) : randomInt(max); + return this.cards.random(max); } // Réglages en vigueur : un champ stocké invalide reprend sa valeur par défaut, pour que le jeu reste jouable. diff --git a/services/cards/service.js b/services/cards/service.js index 4624c12..d0463ae 100644 --- a/services/cards/service.js +++ b/services/cards/service.js @@ -1,5 +1,5 @@ // Rôle : applique les règles serveur de propriété, paquets, transferts et distributions Sokko TCG. -import { createHash, randomBytes, randomUUID } from "node:crypto"; +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"; @@ -59,7 +59,8 @@ export class CardsService { // Catalogue versionné préparé par loadCardsCatalog / prepareVersionedCatalog (finitions générées, sans mythiques). this.versionedCatalog = catalog; this.now = now; - this.random = random; + // Tirage d'un entier de 0 à max − 1. Sans source fournie (le serveur), le hasard cryptographique, comme les paquets. + this.random = random ?? ((max) => randomInt(max)); this.reloadCatalog(); this.expeditions = new ExpeditionService(this, destinations || loadExpeditionDestinations(DEFAULT_DESTINATIONS_PATH, catalog), alerts); } diff --git a/tests/nebula-cards-expedition-service.test.mjs b/tests/nebula-cards-expedition-service.test.mjs index ca19a6f..8154625 100644 --- a/tests/nebula-cards-expedition-service.test.mjs +++ b/tests/nebula-cards-expedition-service.test.mjs @@ -690,6 +690,20 @@ test("cards level up with Éclats or a duplicate, gain field experience and stro assert.equal(database.db.prepare("SELECT COUNT(*) AS count FROM card_progress WHERE player_id = ?").get(player.id).count, 0); }); +test("a pack that draws a destination card already owned replaces it, also without an injected random source", () => { + // Le serveur crée le service sans source de hasard : le remplacement d'une carte de destination déjà possédée doit tout de même tirer. + const database = new CardsDatabase(":memory:"); + const service = new CardsService({ database, catalog: baseCatalog, now: () => Date.parse("2026-11-15T09:00:00Z") }); + const player = service.upsertPlayer({ discordId: "destination-owner", displayName: "Player" }); + const [destination] = destinationEditions; + database.transaction(() => service.addCard(player.id, destination)); + const [replaced] = service.uniqueDestinationCards(player.id, [destination]); + const edition = published.editions.find((item) => item.id === replaced); + assert.notEqual(replaced, destination); + assert.equal(edition.rarity, published.editions.find((item) => item.id === destination).rarity); + database.close(); +}); + test("a card at an awakening tier awakens with its own duplicate or duplicates of its collection and rarity, never with Éclats", () => { const { service, player, open, rares, database } = expeditionService(); open();