47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
/**
|
|
* Fonctions réutilisables pour afficher une carte de monde dans le chat.
|
|
*/
|
|
import { SectorMapApp } from './SectorMapApp.js';
|
|
|
|
const BASE_URL = 'https://travellermap.com';
|
|
|
|
/**
|
|
* Récupère les données d'un monde via l'API et poste une carte détaillée dans le chat.
|
|
* @param {string} sector Nom du secteur
|
|
* @param {string} hex Code hex sur 4 chiffres
|
|
* @param {object} [options] { whisper: bool }
|
|
*/
|
|
export async function postWorldCardToChat(sector, hex, options = {}) {
|
|
if (!sector || !hex) {
|
|
ui.notifications.error('Secteur et hex requis');
|
|
return;
|
|
}
|
|
|
|
const url = `${BASE_URL}/data/${encodeURIComponent(sector)}/${encodeURIComponent(hex)}`;
|
|
let resp;
|
|
try {
|
|
resp = await fetch(url);
|
|
} catch (err) {
|
|
console.error('worldCard | fetch error:', err);
|
|
ui.notifications.error('Erreur réseau');
|
|
return;
|
|
}
|
|
if (!resp.ok) {
|
|
ui.notifications.error(`Monde introuvable : ${sector} ${hex}`);
|
|
return;
|
|
}
|
|
|
|
const data = await resp.json();
|
|
const world = data.Worlds?.[0];
|
|
if (!world) {
|
|
ui.notifications.error(`Aucune donnée pour ${sector} ${hex}`);
|
|
return;
|
|
}
|
|
|
|
const html = SectorMapApp._buildWorldCardHTML(world);
|
|
const msgData = { content: html };
|
|
if (options.whisper !== false) msgData.whisper = [game.user.id];
|
|
|
|
await ChatMessage.create(msgData);
|
|
}
|