sokko-g/services/cards/expedition-items.js
Shinuwa ae23adfdd9 Bring back objects from Sokko TCG Expeditions and open bonus and hidden destinations
- Expeditions bring back objects by weight, proportional to the loot; treasure-map fragments, burins and workshop plans need enough average Orientation.
- A guide star adds a fifth destination to the player's day, a sky chart a sixth with richer loot, and enough fragments open one of two hidden destinations: Le Passage Écarlate and L'Observatoire perdu.
- An anchor recalls a running solo expedition and keeps the Éclats and stardust already earned; a burin engraves a tag on a common to create the synergy a player wants.
- The Veilleur's bag lists objects with their use, reports show brought-back objects, and Administration › Guilde tunes object chances, weights, the Orientation threshold and hidden-destination loot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 17:28:05 +01:00

108 lines
7.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Rôle : objets des Expéditions Sokko TCG : inventaire, ancre (rappel), destinations bonus (étoile guide, carte céleste, carte au trésor)
// et gravure d'une étiquette sur un équipement. Ces méthodes s'ajoutent à ExpeditionService (Object.assign sur son prototype).
import { dailyDayKey } from "./daily.js";
import { ITEM_IDS } from "./expedition-rules.js";
function fail(status, code, message) {
throw Object.assign(new Error(message), { status, code });
}
// Objet consommé pour chaque destination bonus, et place de la destination dans la journée du joueur.
const BONUS_ITEMS = Object.freeze({ guideStar: "5", skyChart: "6", fragment: "hidden" });
export const itemMethods = {
itemsOf(playerId) {
const items = Object.fromEntries(ITEM_IDS.map((id) => [id, 0]));
for (const row of this.db.prepare("SELECT item_id, quantity FROM player_items WHERE player_id = ?").all(playerId)) items[row.item_id] = row.quantity;
return items;
},
addItemsInTx(playerId, itemIds) {
const add = this.db.prepare(`INSERT INTO player_items(player_id, item_id, quantity) VALUES (?, ?, 1)
ON CONFLICT(player_id, item_id) DO UPDATE SET quantity = quantity + 1`);
for (const itemId of itemIds) add.run(playerId, itemId);
},
consumeItemInTx(playerId, itemId, count = 1) {
const used = this.db.prepare("UPDATE player_items SET quantity = quantity - ? WHERE player_id = ? AND item_id = ? AND quantity >= ?").run(count, playerId, itemId, count);
if (!used.changes) fail(409, "NOT_ENOUGH_ITEMS", "Vous n’avez pas assez de cet objet.");
},
// Destinations bonus du joueur pour un jour : 5e, 6e ou cachée, avec leur première condition et leurs deux premiers dangers.
bonusEntries(playerId, day) {
return this.db.prepare("SELECT slot, destination_id FROM player_bonus_destinations WHERE player_id = ? AND day = ? ORDER BY slot").all(playerId, day).map((row) => {
const destination = this.destinations.find((item) => item.id === row.destination_id);
return destination && { destination, entry: { condition: destination.conditions[0], dangers: destination.dangers.slice(0, 2) }, bonusSlot: row.slot };
}).filter(Boolean);
},
// Destinations jouables du joueur : celles du jour, puis ses destinations bonus. À appeler dans une transaction.
playerEntries(playerId, day, settings) {
return [...this.ensureDay(day, settings), ...this.bonusEntries(playerId, day)];
},
// Ajoute une destination bonus à la journée du joueur, en consommant l'objet correspondant (4 fragments pour une carte au trésor).
addBonusDestination(playerId, { item } = {}) {
const settings = this.settings;
const slot = BONUS_ITEMS[item];
if (!slot) fail(400, "ITEM_INVALID", "Cet objet n’ouvre pas de destination.");
this.cards.database.transaction(() => {
if (!settings.enabled) fail(409, "EXPEDITIONS_CLOSED", "Les Expéditions sont fermées pour le moment.");
const day = dailyDayKey(this.now());
const taken = this.playerEntries(playerId, day, settings);
if (taken.some((entry) => entry.bonusSlot === slot)) fail(409, "BONUS_ALREADY_USED", "Vous avez déjà ajouté cette destination aujourd’hui.");
const takenIds = new Set(taken.map((entry) => entry.destination.id));
const pool = this.destinations.filter((destination) => (slot === "hidden" ? destination.hidden : !destination.hidden && settings.destinations[destination.id]?.active !== false) && !takenIds.has(destination.id));
if (!pool.length) fail(409, "BONUS_UNAVAILABLE", "Aucune autre destination n’est disponible aujourd’hui.");
this.consumeItemInTx(playerId, item, item === "fragment" ? settings.items.fragmentsForMap : 1);
const destination = pool[this.random(pool.length)];
this.db.prepare("INSERT INTO player_bonus_destinations(player_id, day, slot, destination_id, created_at) VALUES (?, ?, ?, ?, ?)").run(playerId, day, slot, destination.id, this.now());
this.cards.audit(playerId, "item_used", item, { item, destinationId: destination.id, destinationName: destination.name });
});
return { expeditions: this.getExpeditions(playerId), state: this.cards.getState(playerId) };
},
// Ancre : une expédition solo en route revient tout de suite, avec la part de butin déjà gagnée (Éclats et poussière au prorata).
recallExpedition(playerId, expeditionId) {
this.cards.database.transaction(() => {
const row = this.db.prepare(`SELECT e.*, m.reward_json FROM expeditions e JOIN expedition_members m ON m.expedition_id = e.id AND m.player_id = ?
WHERE e.id = ? AND m.claimed_at IS NULL`).get(playerId, expeditionId);
if (!row || row.status !== "running") fail(404, "EXPEDITION_NOT_FOUND", "Cette expédition n’est pas en route.");
if (row.mode !== "solo") fail(409, "RECALL_SOLO_ONLY", "Seule une expédition solo peut être rappelée.");
const now = this.now();
if (row.returns_at <= now) fail(409, "EXPEDITION_RETURNED", "Cette expédition est déjà rentrée.");
this.consumeItemInTx(playerId, "anchor");
const share = Math.max(0, Math.min(1, (now - row.departs_at) / (row.returns_at - row.departs_at)));
const reward = JSON.parse(row.reward_json);
const kept = { eclats: Math.floor(reward.eclats * share), stardust: Math.floor(reward.stardust * share), packs: [], items: [], recalledPct: Math.round(share * 100) };
this.db.prepare("UPDATE expedition_members SET reward_json = ? WHERE expedition_id = ? AND player_id = ?").run(JSON.stringify(kept), row.id, playerId);
this.db.prepare("UPDATE expeditions SET returns_at = ? WHERE id = ?").run(now, row.id);
const setup = JSON.parse(row.setup_json);
this.cards.audit(playerId, "item_used", "anchor", { item: "anchor", destinationId: setup.destination.id, destinationName: setup.destination.name, recalledPct: kept.recalledPct });
});
return { expedition: this.expeditionView(expeditionId, playerId), expeditions: this.getExpeditions(playerId), state: this.cards.getState(playerId) };
},
engravingsOf(playerId) {
return new Map(this.db.prepare("SELECT edition_id, tag FROM equipment_engravings WHERE player_id = ?").all(playerId).map((row) => [row.edition_id, row.tag]));
},
// Burin : grave une étiquette du vocabulaire sur une commune, une seule fois, pour créer la synergie qu'on veut.
engrave(playerId, { editionId, tag } = {}) {
this.cards.database.transaction(() => {
const item = this.equipmentItem(editionId);
if (!item) fail(400, "EQUIPMENT_INVALID", "Seules les communes se gravent.");
if (!this.cards.quantityOf(playerId, editionId)) fail(404, "CARD_NOT_OWNED", "Vous ne possédez pas cette commune.");
if (!this.cards.catalog.expeditionTags.some((entry) => entry.id === tag) || item.tags.includes(tag)) fail(400, "ENGRAVING_INVALID", "Choisissez une étiquette que cette commune ne porte pas encore.");
if (this.db.prepare("SELECT 1 FROM equipment_engravings WHERE player_id = ? AND edition_id = ?").get(playerId, editionId)) fail(409, "ENGRAVING_EXISTS", "Cette commune est déjà gravée.");
this.consumeItemInTx(playerId, "engraving");
this.db.prepare("INSERT INTO equipment_engravings(player_id, edition_id, tag, engraved_at) VALUES (?, ?, ?, ?)").run(playerId, editionId, tag, this.now());
this.cards.audit(playerId, "item_used", "engraving", { item: "engraving", editionId, tag });
});
return this.cards.getState(playerId);
},
forgetItemsInTx(playerId) {
for (const table of ["player_items", "equipment_engravings", "player_bonus_destinations"]) this.db.prepare(`DELETE FROM ${table} WHERE player_id = ?`).run(playerId);
}
};