sokko-g/services/cards/expedition-equipment.js
Shinuwa c73a118ffb
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 22m30s
Validate and deploy Sokko G / deploy (push) Successful in 13s
Let players who join a group set up their card and say when they are ready
Joining a group froze the card's stats at once and locked its
equipment, so a joining player could not equip anything. Joining now
uses the same card frame as the preparation: card, equipment and
"Améliorer la carte". Everything stays editable after joining, and the
group counts the card as it is, until the player clicks "Prêt". That
freezes the card's stats. "Modifier ma préparation" reopens it.

The creator sees each member as "Prêt" or "En préparation", with a
ready count, and can still leave at any time. A player who is not
ready leaves with their card as it is at departure. A migration marks
existing members as ready, since their stats were already frozen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 19:24:23 +01:00

88 lines
6.7 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 : équipements des Expéditions Sokko TCG : communes posées sur les cartes, stats apportées et synergies.
// Ces méthodes s'ajoutent à ExpeditionService (Object.assign sur son prototype) et utilisent `this` comme lui.
import { addStats, editionExpeditionData, equipmentBonus, unlockedSlots } from "./expedition-rules.js";
function fail(status, code, message) {
throw Object.assign(new Error(message), { status, code });
}
export const equipmentMethods = {
// Équipements posés par le joueur : carte → [{ slot, equipmentEditionId }].
equipmentOf(playerId) {
const map = new Map();
for (const row of this.db.prepare("SELECT edition_id, slot, equipment_edition_id FROM card_equipment WHERE player_id = ? ORDER BY slot").all(playerId)) {
map.set(row.edition_id, [...(map.get(row.edition_id) || []), { slot: row.slot, equipmentEditionId: row.equipment_edition_id }]);
}
return map;
},
// Une commune et ce qu'elle apporte comme équipement : ses stats et ses étiquettes, plus celle gravée par le joueur (`engravings`).
equipmentItem(editionId, engravings = null) {
const edition = this.cards.index.editions.get(editionId);
const card = edition && this.cards.index.cards.get(edition.cardId);
if (!card?.expedition?.equipment) return null;
const engraved = engravings?.get(editionId) ?? null;
const { stats = {}, power = null, synergyStat = null } = card.expedition.equipment;
return { editionId, cardId: card.id, name: card.name, stats, power, synergyStat, tags: engraved ? [...card.expedition.tags, engraved] : card.expedition.tags, engraved };
},
// Une carte prête à partir : stats de sa Puissance, plus ses équipements (dans ses emplacements ouverts) et leurs synergies.
// `progress` : { level, plan, allocation } de la carte chez le joueur.
loadout(editionId, progress, equipped, settings, engravings = null) {
const data = editionExpeditionData(this.cards.catalog, editionId, progress?.level ?? 0, settings, progress);
if (!data) return null;
const slots = unlockedSlots(data.power, settings);
const items = (equipped || []).filter((item) => item.slot < slots).map((item) => ({ slot: item.slot, ...this.equipmentItem(item.equipmentEditionId, engravings) })).filter((item) => item.editionId);
const bonus = equipmentBonus(items, data.tags, settings);
return {
...data, baseStats: data.stats, stats: addStats(data.stats, bonus.stats), slots,
powers: items.map((item) => item.power).filter(Boolean),
equipment: items.map((item, index) => ({ slot: item.slot, editionId: item.editionId, cardId: item.cardId, name: item.name, stats: item.stats, power: item.power, engraved: item.engraved, synergyTag: bonus.pairs[index],
synergyStat: bonus.synergies[index]?.stat ?? null, synergyBonus: bonus.synergies[index]?.value ?? 0 }))
};
},
// Pose (ou retire, avec `equipmentEditionId` nul) une commune sur un emplacement ouvert d'une carte. Une commune déjà posée
// ailleurs y est retirée, sauf si cette autre carte est en expédition.
equip(playerId, { editionId, slot, equipmentEditionId = null } = {}) {
const settings = this.settings;
this.cards.database.transaction(() => {
const levels = this.levelsOf(playerId);
const owned = new Map(this.cards.collectionOf(playerId).map((entry) => [entry.editionId, entry]));
if (!owned.has(editionId)) fail(404, "CARD_NOT_OWNED", "Vous ne possédez pas cette carte.");
const data = editionExpeditionData(this.cards.catalog, editionId, levels.get(editionId)?.level ?? 0, settings);
if (!data) fail(400, "EXPEDITION_CARD_INVALID", "Seules les cartes rares, épiques, légendaires et mythiques s’équipent.");
if (!Number.isInteger(slot) || slot < 0 || slot >= unlockedSlots(data.power, settings)) fail(400, "EQUIPMENT_SLOT_LOCKED", "Cet emplacement n’est pas encore ouvert : améliorez la carte.");
const busy = this.lockedEditions(playerId);
if (busy.has(editionId)) fail(409, "EXPEDITION_CARD_BUSY", "Cette carte est en expédition : son équipement ne change pas avant son retour.");
if (equipmentEditionId == null) {
this.db.prepare("DELETE FROM card_equipment WHERE player_id = ? AND edition_id = ? AND slot = ?").run(playerId, editionId, slot);
return;
}
const item = this.equipmentItem(equipmentEditionId, this.engravingsOf(playerId));
if (!item) fail(400, "EQUIPMENT_INVALID", "Seules les communes servent d’équipement.");
if (!owned.has(equipmentEditionId)) fail(404, "CARD_NOT_OWNED", "Vous ne possédez pas cette commune.");
// Un seul pouvoir par carte : une commune de pouvoir ne remplace qu'un emplacement qui en portait déjà un, ou s'ajoute s'il n'y en a aucun.
if (item.power) {
const powered = this.db.prepare("SELECT slot, equipment_edition_id FROM card_equipment WHERE player_id = ? AND edition_id = ?").all(playerId, editionId)
.filter((row) => row.slot !== slot && row.equipment_edition_id !== equipmentEditionId && this.equipmentItem(row.equipment_edition_id)?.power);
if (powered.length) fail(409, "POWER_LIMIT", "Cette carte porte déjà un pouvoir : retirez-le d’abord.");
}
const elsewhere = this.db.prepare("SELECT edition_id FROM card_equipment WHERE player_id = ? AND equipment_edition_id = ?").get(playerId, equipmentEditionId);
if (elsewhere && elsewhere.edition_id !== editionId && busy.has(elsewhere.edition_id)) fail(409, "EQUIPMENT_IN_USE", "Cette commune équipe une carte partie en expédition.");
this.db.prepare("DELETE FROM card_equipment WHERE player_id = ? AND (equipment_edition_id = ? OR (edition_id = ? AND slot = ?))").run(playerId, equipmentEditionId, editionId, slot);
this.db.prepare("INSERT INTO card_equipment(player_id, edition_id, slot, equipment_edition_id, equipped_at) VALUES (?, ?, ?, ?, ?)").run(playerId, editionId, slot, equipmentEditionId, this.now());
});
return { progress: this.cardProgressView(playerId, settings).find((entry) => entry.editionId === editionId), state: this.cards.getState(playerId) };
},
// Communes du joueur utilisables comme équipement, avec la carte qu'elles équipent.
equipmentChoices(playerId) {
const placed = new Map(this.db.prepare("SELECT equipment_edition_id, edition_id FROM card_equipment WHERE player_id = ?").all(playerId).map((row) => [row.equipment_edition_id, row.edition_id]));
const engravings = this.engravingsOf(playerId);
return this.cards.collectionOf(playerId).map((entry) => {
const item = this.equipmentItem(entry.editionId, engravings);
return item && { editionId: item.editionId, cardId: item.cardId, stats: item.stats, power: item.power, tags: item.tags, engraved: item.engraved, equippedOn: placed.get(item.editionId) ?? null };
}).filter(Boolean);
}
};