sokko-g/services/cards/alert-messages.js
Shinuwa 82cebd0fd1
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 23m48s
Validate and deploy Sokko G / deploy (push) Successful in 16s
Show the destination, result and loot in Sokko TCG Discord alerts, with a button to the expedition
- Each queued alert keeps an embed and a link button: the destination illustration as thumbnail, the tier, score and loot for a return, the difficulty and automatic departure for a new group.
- The button and the message link open #/cards/expeditions/<id>, which scrolls to the player's expedition or group and highlights it; Discord login now returns to that link.
- Without an https CARDS_PUBLIC_ORIGIN the link stays in the text, without button or thumbnail, which Discord would refuse.
- Delivery docs name palico-bot's NOTIFICATIONS_API_KEY and its local-only route on port 3101.

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

85 lines
5.2 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 : construit les messages privés des alertes Discord des Expéditions : le texte (montré par la notification du téléphone),
// un embed (destination, résultat, butin, vignette de la destination) et un bouton qui ouvre directement l'expédition dans Sokko TCG.
// Module pur : il reçoit le catalogue et l'adresse publique, sans base ni horloge.
// Libellés des paliers et des objets, les mêmes que l'interface (site.json, cards.expeditions) ; un test garde les listes égales.
export const TIER_LABELS = Object.freeze({ struggle: "Retour difficile", success: "Réussite", triumph: "Triomphe" });
export const ITEM_LABELS = Object.freeze({
fragment: "Fragment de carte au trésor", engraving: "Burin de graveur", plan: "Plan d’atelier", anchor: "Ancre des Veilleurs", guideStar: "Étoile guide",
skyChart: "Carte céleste", oblivion: "Parchemin d’oubli", windRose: "Rose des vents", balm: "Baume des Veilleurs"
});
// Couleur de l'embed : or pour un Triomphe, vert pour une Réussite, gris pour un Retour difficile, sarcelle pour un groupe ouvert.
const TIER_COLORS = Object.freeze({ triumph: 0xf6c453, success: 0x34d399, struggle: 0x94a3b8 });
const GROUP_COLOR = 0x0f766e;
const FOOTER = "Sokko TCG · Guilde des Veilleurs";
// Lien direct vers une expédition (#/cards/expeditions/<id>) : la page la met en vue. Sans adresse publique, pas de lien.
export function expeditionUrl(origin, expeditionId) {
return origin ? `${origin}/#/cards/expeditions/${expeditionId}` : null;
}
// Vignette : l'illustration de la destination, ou celle de sa carte de couverture ; seulement en https, que Discord télécharge.
function thumbnailUrl(origin, catalog, destination) {
const coverImage = destination.coverCardId ? catalog.cards.find((card) => card.id === destination.coverCardId)?.image : null;
const image = destination.image || coverImage;
return origin?.startsWith("https://") && image ? `${origin}${image}` : null;
}
const meta = (setup) => `${"★".repeat(setup.stars)} · ${setup.hours} h`;
function withLink(message, url, label) {
if (!url) return message;
return { ...message, embed: { ...message.embed, url: url.startsWith("https://") ? url : undefined }, link: url.startsWith("https://") ? { label, url } : null };
}
// Groupe ouvert : qui l'ouvre, où, à quelle difficulté, et quand il partira seul.
export function groupOpenedMessage({ creatorName, setup, expeditionId, origin, catalog }) {
const url = expeditionUrl(origin, expeditionId);
const name = creatorName || "Un joueur";
const content = `${name} ouvre un groupe d’expédition vers ${setup.destination.name} (${meta(setup)}). Rejoignez-le sur le tableau de la Guilde :${url ? ` ${url}` : ""}`;
const fields = [{ name: "Difficulté", value: "★".repeat(setup.stars), inline: true }, { name: "Durée", value: `${setup.hours} h`, inline: true }];
if (setup.recruitUntil) fields.push({ name: "Départ automatique", value: `<t:${Math.floor(setup.recruitUntil / 1000)}:R>`, inline: true });
const embed = {
title: `Nouveau groupe vers ${setup.destination.name}`,
description: `${name} cherche des compagnons pour cette expédition.`,
color: GROUP_COLOR,
thumbnail: thumbnailUrl(origin, catalog, setup.destination) ?? undefined,
fields,
footer: FOOTER
};
return withLink({ content, embed }, url, "Rejoindre le groupe");
}
// Butin d'un retour, en une ligne : Éclats, poussière, cartes et objets.
export function lootLine(reward, catalog) {
const parts = [];
if (reward.eclats) parts.push(`+${reward.eclats} Éclats`);
if (reward.stardust) parts.push(`+${reward.stardust} poussière${reward.stardust > 1 ? "s" : ""}`);
for (const editionId of reward.cards || []) {
const edition = catalog.editions.find((item) => item.id === editionId);
parts.push(catalog.cards.find((card) => card.id === edition?.cardId)?.name || editionId);
}
const items = new Map();
for (const item of reward.items || []) items.set(item, (items.get(item) || 0) + 1);
for (const [item, count] of items) parts.push(`${ITEM_LABELS[item] || item}${count > 1 ? ` ×${count}` : ""}`);
const packs = reward.packs?.length || 0;
if (packs) parts.push(`${packs} paquet${packs > 1 ? "s" : ""} offert${packs > 1 ? "s" : ""}`);
return parts.join(" · ") || "Rien cette fois";
}
// Expédition de retour : destination, palier et score, butin à récupérer.
export function expeditionReturnedMessage({ setup, result, reward, expeditionId, origin, catalog }) {
const url = expeditionUrl(origin, expeditionId);
const content = `Votre expédition vers ${setup.destination.name} est de retour ! Récupérez son butin :${url ? ` ${url}` : ""}`;
const fields = [];
if (result?.tier) fields.push({ name: "Résultat", value: `${TIER_LABELS[result.tier] || result.tier}${result.scorePct != null ? ` · ${String(result.scorePct).replace(".", ",")} %` : ""}`, inline: true });
if (reward) fields.push({ name: "Butin", value: lootLine(reward, catalog) });
const embed = {
title: "Votre expédition est de retour !",
description: `${setup.destination.name} · ${meta(setup)}`,
color: TIER_COLORS[result?.tier] ?? GROUP_COLOR,
thumbnail: thumbnailUrl(origin, catalog, setup.destination) ?? undefined,
fields,
footer: FOOTER
};
return withLink({ content, embed }, url, "Voir l’expédition");
}