/* Client multijoueur MountyCrawl — polling de l'état serveur (/api/mp/*), * rendu canvas et actions. L'identité du troll (id + secret) est gardée en * localStorage : on retrouve son troll d'une session à l'autre. * Partage les globales de game.js (RACES, GEAR_SLOTS, fmtStatLine…). */ "use strict"; const MP_TILE = 48; const MP_KEY = "mc_mp_identity"; let MP = { id: null, secret: null, state: null, timer: null, seen: new Set(), // brouillard : cases déjà vues (mémoire locale) lastLogT: 0, pending: false, }; /* ---------- Identité ---------- */ function mpLoadIdentity() { try { return JSON.parse(localStorage.getItem(MP_KEY)); } catch { return null; } } function mpSaveIdentity(id, secret) { localStorage.setItem(MP_KEY, JSON.stringify({ id, secret })); } /* ---------- Réseau ---------- */ async function mpJoin(name, password) { const res = await fetch("api/mp/join", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, race: selectedRace, password }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || "connexion refusée"); MP.id = data.id; MP.secret = data.secret; mpSaveIdentity(data.id, data.secret); return data.state; } async function mpLogin(name, password) { const res = await fetch("api/mp/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, password }), }); const data = await res.json(); if (!res.ok) throw new Error(data.error || "connexion refusée"); MP.id = data.id; MP.secret = data.secret; mpSaveIdentity(data.id, data.secret); return data.state; } async function mpFetchState() { const res = await fetch(`api/mp/state?id=${MP.id}&secret=${MP.secret}`); if (res.status === 403) return null; // identité périmée (reset serveur ?) if (!res.ok) throw new Error("état indisponible"); return res.json(); } async function mpAction(action) { if (MP.pending) return; MP.pending = true; try { const res = await fetch("api/mp/action", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: MP.id, secret: MP.secret, action }), }); const data = await res.json(); if (data.error) mpToast(data.error); if (data.state) { MP.state = data.state; mpRender(); } } catch { mpToast("le serveur ne répond pas"); } MP.pending = false; } function mpToast(msg) { const el = document.getElementById("mp-status"); if (el) { el.textContent = `⚠️ ${msg}`; setTimeout(() => mpRenderStatus(), 2500); } } /* ---------- Entrée / sortie de l'écran ---------- */ function mpShowJoin(show) { document.getElementById("mp-join").classList.toggle("hidden", !show); // #layout et #bottom-row existent aussi dans l'écran solo : on scope au multi document.querySelector("#screen-mp #layout").classList.toggle("hidden", show); document.querySelector("#screen-mp #bottom-row").classList.toggle("hidden", show); if (show) { document.getElementById("mp-join-name").value = document.getElementById("troll-name").value.trim() || "Trõllinet"; document.getElementById("mp-join-race").textContent = `${RACES[selectedRace].emoji} ${selectedRace}`; document.getElementById("mp-join-msg").textContent = ""; } } async function mpEnter() { document.getElementById("screen-create").classList.add("hidden"); document.getElementById("screen-mp").classList.remove("hidden"); switchLeftTab("stats", "mp-"); // ouvrir sur les caractéristiques MP.seen = new Set(); try { const saved = mpLoadIdentity(); if (saved && saved.id) { MP.id = saved.id; MP.secret = saved.secret; MP.state = await mpFetchState(); } if (!MP.state) { mpShowJoin(true); return; } // pas d'identité valable : créer ou retrouver mpShowJoin(false); mpRender(); mpSchedule(); } catch (e) { mpToast(e.message || "connexion impossible"); setTimeout(mpLeave, 2500); } } /* Création ou reconnexion depuis le panneau « Rejoindre ». */ async function mpJoinSubmit(isLogin) { const name = document.getElementById("mp-join-name").value.trim() || "Trõllinet"; const password = document.getElementById("mp-join-pass").value; const msg = document.getElementById("mp-join-msg"); try { MP.state = isLogin ? await mpLogin(name, password) : await mpJoin(name, password); MP.seen = new Set(); mpShowJoin(false); mpRender(); mpSchedule(); } catch (e) { msg.textContent = `⚠️ ${e.message}`; } } function mpLeave() { if (MP.timer) { clearTimeout(MP.timer); MP.timer = null; } document.getElementById("screen-mp").classList.add("hidden"); document.getElementById("screen-create").classList.remove("hidden"); } function mpSchedule() { if (MP.timer) clearTimeout(MP.timer); const sec = MP.state?.config?.pollSec || 5; MP.timer = setTimeout(async () => { if (document.getElementById("screen-mp").classList.contains("hidden")) return; try { const st = await mpFetchState(); if (!st) { // identité refusée (reset serveur ?) : repasser par le panneau localStorage.removeItem(MP_KEY); MP.state = null; mpShowJoin(true); return; } MP.state = st; mpRender(); } catch { /* on retentera au prochain tour */ } mpSchedule(); }, sec * 1000); } /* ---------- Rendu ---------- */ function mpFmtDelay(ms) { const s = Math.ceil(ms / 1000); if (s < 60) return `${s} s`; return `${Math.floor(s / 60)} min ${String(s % 60).padStart(2, "0")} s`; } function mpRenderStatus() { const st = MP.state; const el = document.getElementById("mp-status"); if (!st || !el) return; el.textContent = st.you.dead ? `☠️ Terrassé — retour dans ${mpFmtDelay(st.you.respawnIn)}` : `DLA n°${st.you.dla} · PA dans ${mpFmtDelay(st.you.nextDlaIn)} · 🔄 ${st.config.pollSec} s`; } function mpRender() { const st = MP.state; if (!st) return; mpRenderStatus(); mpRenderMap(st); mpRenderPanels(st); mpRenderLogs(st); } function mpRenderMap(st) { const canvas = document.getElementById("mp-map"); const w = st.config.mapW, h = st.config.mapH; canvas.width = w * MP_TILE; canvas.height = h * MP_TILE; const ctx = canvas.getContext("2d"); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.font = "18px serif"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; const you = st.you; const vue = you.eff.vue; const visible = (x, y) => Math.max(Math.abs(x - you.x), Math.abs(y - you.y)) <= vue; for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { if (visible(x, y)) MP.seen.add(y * w + x); const px = x * MP_TILE, py = y * MP_TILE; if (!MP.seen.has(y * w + x)) { ctx.fillStyle = "#0d0a06"; ctx.fillRect(px, py, MP_TILE, MP_TILE); continue; } const vis = visible(x, y); const wall = st.map[y][x] === "#"; ctx.fillStyle = wall ? (vis ? "#4a3a22" : "#2c2315") : (vis ? "#7a6a45" : "#3d3522"); ctx.fillRect(px, py, MP_TILE - 1, MP_TILE - 1); } } // Empilement : une case peut contenir plusieurs trolls/monstres/trésors et un // lieu. On regroupe par case et on dessine chaque TYPE dans un coin — // troll ↖, monstre ↗, trésor ↙, lieu ↘ — avec un « ×N » si plusieurs. const cells = {}; const cell = (x, y) => (cells[x + "," + y] = cells[x + "," + y] || { x, y, trolls: [], monsters: [], items: [], place: null, youHere: false }); for (const i of st.items) cell(i.x, i.y).items.push(i); for (const m of st.monsters) cell(m.x, m.y).monsters.push(m); for (const o of st.trolls) cell(o.x, o.y).trolls.push(o); for (const pl of (st.places || [])) cell(pl.x, pl.y).place = pl; if (!you.dead) cell(you.x, you.y).youHere = true; const Q = MP_TILE / 4; // centre d'un coin const EMOJI_FONT = Math.round(MP_TILE / 3) + "px serif"; const COUNT_FONT = "bold " + Math.round(MP_TILE / 4.5) + "px sans-serif"; const corner = (c, qx, qy, color, emoji, count, mine) => { const cx = c.x * MP_TILE + qx, cy = c.y * MP_TILE + qy; ctx.fillStyle = color; ctx.beginPath(); ctx.arc(cx, cy, Q, 0, Math.PI * 2); ctx.fill(); if (mine) { ctx.strokeStyle = "#eaffdc"; ctx.lineWidth = 2; ctx.stroke(); } ctx.fillStyle = "#1a140e"; ctx.font = EMOJI_FONT; ctx.fillText(emoji, cx, cy + 1); if (count > 1) { ctx.font = COUNT_FONT; ctx.lineWidth = 3; ctx.strokeStyle = "#1a140e"; ctx.strokeText("×" + count, cx + Q * 0.9, cy + Q * 0.9); ctx.fillStyle = "#ffe"; ctx.fillText("×" + count, cx + Q * 0.9, cy + Q * 0.9); } }; for (const c of Object.values(cells)) { const nTrolls = c.trolls.length + (c.youHere ? 1 : 0); if (nTrolls) corner(c, Q, Q, c.youHere ? "#8fbf5a" : "#5a7abf", "🧌", nTrolls, c.youHere); // ↖ if (c.monsters.length) { corner(c, MP_TILE - Q, Q, "#8a3030", c.monsters[0].emoji, c.monsters.length); // ↗ const low = c.monsters.reduce((a, b) => (b.pvPct < a.pvPct ? b : a)); const bw = Math.max(3, Math.round((MP_TILE - 6) * low.pvPct)); ctx.fillStyle = "#b03030"; ctx.fillRect(c.x * MP_TILE + 3, c.y * MP_TILE + 2, bw, 3); } if (c.items.length) { const it = c.items[0]; const col = it.kind === "gold" ? "#caa53d" : (it.kind === "potion" || it.kind === "scroll") ? (it.color || "#5d8535") : "#7a8db0"; corner(c, Q, MP_TILE - Q, col, it.emoji, c.items.length); // ↙ } if (c.place) corner(c, MP_TILE - Q, MP_TILE - Q, "#6a5a8a", c.place.emoji, 1); // ↘ // nom d'un autre troll seul sur sa case (au-dessus) ; masqué si empilement if (c.trolls.length === 1 && !c.youHere) { ctx.fillStyle = "#dfe8ff"; ctx.font = Math.round(MP_TILE / 2.6) + "px sans-serif"; ctx.fillText(c.trolls[0].name, c.x * MP_TILE + MP_TILE / 2, c.y * MP_TILE - MP_TILE / 8); } } ctx.font = "18px serif"; if (!you.dead && you.camo) { ctx.strokeStyle = "#8fbf5a"; ctx.strokeRect(you.x * MP_TILE + 1, you.y * MP_TILE + 1, MP_TILE - 2, MP_TILE - 2); } } function mpRenderPanels(st) { const you = st.you; const e = you.eff; document.getElementById("mp-troll-title").textContent = `${RACES[you.race].emoji} ${you.name}, ${you.race} niv. ${you.level}`; const pct = Math.max(0, you.pv / you.pvMax); const hpClass = pct > 0.6 ? "high" : pct > 0.3 ? "mid" : ""; document.getElementById("mp-stats").innerHTML = `