Modules are classic-script factories RC.Game<Part>(G); G gives live access to the shared state. Guarded by new browser tests for every moved page and workshop tool (tests/e2e/screens.spec.js).
272 lines
18 KiB
JavaScript
272 lines
18 KiB
JavaScript
/* Robot Clash - menu pages: settings, help, tutorial, country, achievements, missions, legal, ranking board.
|
|
Part of game.js (split for size): created once by game.js with the shared context G.
|
|
Live state (save, mode, F, ...) is read as G.<name>; services/helpers are taken from G at creation. */
|
|
(function (root) {
|
|
'use strict';
|
|
const RC = (root.RC = root.RC || {});
|
|
RC.GamePages = function (G) {
|
|
const {
|
|
$, CAMS, FLAGS, L, SFX, accounts, act, audio, card, currentBack, ensureMissions, esc, fbAcc, flagHtml,
|
|
hud, layer, liveText, logout, net, newSave, openGarage, overlay, overlayEnter, persist, rank,
|
|
saveSettings, settings, shop, showLeague, showTournament, skipIntro, t, togglePause, tourState
|
|
} = G;
|
|
|
|
// ---------------------------------------------------------------- settings / help / ranking
|
|
function showSettings(back) {
|
|
overlayEnter();
|
|
const prev = G.mode;
|
|
const seg = (id, val, opts) => `<div class="seg" id="${id}">${opts.map(([v, label]) => `<button data-v="${v}" class="${v === val ? 'on' : ''}">${label}</button>`).join('')}</div>`;
|
|
const who = fbAcc() || (accounts.current && !accounts.current.guest ? accounts.current : null);
|
|
const item = (label, ctl) => `<span class="sl">${label}</span>${ctl}`;
|
|
card(`<h2>${t('settings')}</h2><div class="set2">
|
|
${item(t('s_lang'), seg('sLang', L.lang, L.langs.map(l => [l, window.LANG[l].lang_self])))}
|
|
${G.save ? item(t('s_country'), `<select id="sCountry" class="selset">${FLAGS.map(f => `<option value="${f}" ${f === G.save.flag ? 'selected' : ''}>${t('cc_' + f)}</option>`).join('')}</select>`) : ''}
|
|
${item(t('s_music'), `<input type="range" id="sMusic" min="0" max="1" step="0.05" value="${audio.musicVol}">`)}
|
|
${item(t('s_sfx'), `<input type="range" id="sSfx" min="0" max="1" step="0.05" value="${audio.sfxVol}">`)}
|
|
${item(t('s_gfx'), seg('sGfx', settings.gfx, [['low', t('gfx_low')], ['medium', t('gfx_medium')], ['high', t('gfx_high')]]))}
|
|
${item(t('s_cam'), seg('sCam', settings.cam, CAMS.map(c => [c, t('cam_' + c)])))}
|
|
${item(t('s_comfort'), seg('sComfort', settings.comfort ? '1' : '0', [['1', t('on')], ['0', t('off')]]))}
|
|
${item(t('s_steer'), seg('sSteer', settings.steer, [['tank', t('steer_tank')], ['simple', t('steer_simple')]]))}
|
|
${item(t('s_autofire'), seg('sAuto', settings.autofire ? '1' : '0', [['1', t('on')], ['0', t('off')]]))}
|
|
${item(t('s_quiet'), seg('sQuiet', settings.quiet ? '1' : '0', [['0', t('off')], ['1', t('on')]]))}
|
|
${item(t('s_arena'), seg('sArena', settings.arena, [['random', t('arena_random')]].concat(RC.ARENAS.map(a => [a.id, t('arena_' + a.id)]))))}
|
|
${item(t('s_intro'), seg('sIntro', settings.intro ? '1' : '0', [['1', t('on')], ['0', t('off')]]))}
|
|
</div>
|
|
${G.isDev && G.save ? `<div class="setdev"><span class="sl">🛠 Dev · Coins</span><div class="row">
|
|
<button data-coin="10000">+10k</button><button data-coin="100000">+100k</button>
|
|
<button data-coin="1000000">+1M</button><button data-coin="set800">Reset 800</button></div></div>` : ''}
|
|
<div class="setacc"><span class="sl">${t('s_account')}</span>
|
|
<span class="who">${who ? '👤 <b>' + esc(who.name) + '</b>' + (who.email ? ' <span class="muted">' + esc(who.email) + '</span>' : '') : '👤 ' + t('guest')}</span>
|
|
${G.save ? `<button id="sOut">${who ? t('logout') : t('to_title')}</button>` : ''}</div>
|
|
<div class="row"><button id="sFull">⛶ ${t('win_full')}</button><button id="sKeys">🎮 ${t('s_controls')}</button>
|
|
<button id="sLegal">§ ${t('legal')}</button>${G.save ? `<button id="sReset">${t('reset')}</button>` : ''}<button class="primary" id="bBack">${t('back')}</button></div>`, { wide: true, clear: prev === 'title' || prev === 'intro' });
|
|
const segOn = (id, f) => $(id).querySelectorAll('button').forEach(b => { b.onclick = () => { SFX.click(); f(b.dataset.v); $(id).querySelectorAll('button').forEach(x => x.classList.toggle('on', x === b)); }; });
|
|
segOn('sLang', v => { L.set(v); showSettings(back); });
|
|
segOn('sGfx', v => { settings.gfx = v; saveSettings(); if (G.view) G.view.setQuality(v); });
|
|
segOn('sIntro', v => { settings.intro = v === '1'; saveSettings(); });
|
|
segOn('sSteer', v => { settings.steer = v; saveSettings(); });
|
|
segOn('sCam', v => { settings.cam = v; saveSettings(); if (G.view) G.view.setCam(v); });
|
|
segOn('sComfort', v => { settings.comfort = v === '1'; saveSettings(); if (G.view && G.view.setComfort) G.view.setComfort(settings.comfort); });
|
|
segOn('sAuto', v => { settings.autofire = v === '1'; saveSettings(); });
|
|
segOn('sQuiet', v => { settings.quiet = v === '1'; saveSettings(); });
|
|
segOn('sArena', v => { settings.arena = v; saveSettings(); });
|
|
if ($('sCountry')) $('sCountry').onchange = e => { G.save.flag = e.target.value; G.save.flagSet = true; persist(); hud(); };
|
|
$('sMusic').oninput = e => audio.setVolume(+e.target.value, audio.sfxVol);
|
|
$('sSfx').oninput = e => audio.setVolume(audio.musicVol, +e.target.value);
|
|
$('sSfx').onchange = () => SFX.click();
|
|
$('sFull').onclick = () => { SFX.click(); Kit.host.fullscreen(); };
|
|
if (G.isDev && G.save) overlay.querySelectorAll('[data-coin]').forEach(b => b.onclick = () => {
|
|
SFX.buy(); const v = b.dataset.coin;
|
|
act({ type: 'devCoins', money: v === 'set800' ? 800 : Math.min(1e9, (G.save.money | 0) + (+v || 0)) });
|
|
persist(); hud(); showSettings(back);
|
|
});
|
|
if ($('sReset')) $('sReset').onclick = () => { SFX.click(); confirmReset(() => showSettings(back)); };
|
|
if ($('sOut')) $('sOut').onclick = () => { SFX.click(); logout(); };
|
|
$('sKeys').onclick = () => { SFX.click(); showHelp(() => showSettings(back)); };
|
|
if ($('sLegal')) $('sLegal').onclick = () => { SFX.click(); showLegal(() => showSettings(back)); };
|
|
$('bBack').onclick = () => { SFX.click(); back(); };
|
|
}
|
|
function keysHtml() {
|
|
return `<div class="keys"><b>W A S D / ↑ ← ↓ →</b><span>${t('key_drive')}</span><b>${t('key_space')}</b><span>${t('key_weapon')}</span>
|
|
<b>Shift / E</b><span>${t('key_tower')}</span><b>C</b><span>${t('key_cam')}</span><b>P / Esc</b><span>${t('pause')}</span><b>M</b><span>${t('sound_on')} / ${t('sound_off')}</span>
|
|
<b>${t('key_pad')}</b><span>${t('key_pad_text')}</span></div>`;
|
|
}
|
|
// first start: four short cards instead of the full help page
|
|
const TUT = ['tut_1', 'tut_2', 'tut_3', 'tut_4'];
|
|
function startTutorial(done) {
|
|
let i = 0;
|
|
const step = () => {
|
|
if (i >= TUT.length) { done(); return; }
|
|
const n = i + 1;
|
|
overlayEnter();
|
|
card(`<h2>${t('tut_title')} <span class="muted">${n}/${TUT.length}</span></h2>
|
|
<p>${t(TUT[i])}</p>
|
|
<div class="row"><button id="tSkip">${t('tut_skip')}</button>
|
|
<button class="primary" id="tNext">${n === TUT.length ? t('tut_go') : t('tut_next')}</button></div>`,
|
|
{ bg: 'bg_workshop.jpg' });
|
|
$('tNext').onclick = () => { SFX.click(); i++; step(); };
|
|
$('tSkip').onclick = () => { SFX.click(); done(); };
|
|
};
|
|
step();
|
|
}
|
|
|
|
// first start: pick the country whose flag shows on the ranking / in the lobby (changeable later in settings)
|
|
function chooseFlag(done) {
|
|
overlayEnter();
|
|
let pick = (G.save && FLAGS.includes(G.save.flag)) ? G.save.flag : 'XX';
|
|
card(`<h2>🌍 ${t('country_title')}</h2><p>${t('country_text')}</p>
|
|
<div class="flaggrid">${FLAGS.map(f => `<button class="flagbtn ${f === pick ? 'on' : ''}" data-f="${f}">${flagHtml(f)}<span>${t('cc_' + f)}</span></button>`).join('')}</div>
|
|
<div class="row"><button class="primary" id="cfGo">${t('country_go')}</button></div>`, { bg: 'bg_title.jpg', wide: true });
|
|
overlay.querySelectorAll('.flagbtn').forEach(b => b.onclick = () => { pick = b.dataset.f; SFX.click(); overlay.querySelectorAll('.flagbtn').forEach(x => x.classList.toggle('on', x.dataset.f === pick)); });
|
|
$('cfGo').onclick = () => { if (G.save) { G.save.flag = pick; G.save.flagSet = true; persist(); } SFX.click(); done(); };
|
|
}
|
|
function ensureFlag(done) { if (G.save && !G.save.flagSet) chooseFlag(done); else done(); }
|
|
|
|
// one line for the running event and one for the admin's message
|
|
function liveBanner() {
|
|
const out = [];
|
|
if (G.LIVE.event) {
|
|
const n = liveText(G.LIVE.event.name) || t('event_running');
|
|
const extra = G.LIVE.event.prizeMul !== 1 ? ` · ${t('event_prize', { n: G.LIVE.event.prizeMul })}` : '';
|
|
out.push(`<div class="evbar"><b>${t('event')}</b><span>${esc(n)}${extra}</span></div>`);
|
|
}
|
|
const m = liveText(G.LIVE.motd);
|
|
if (m) out.push(`<p class="motd">${esc(m)}</p>`);
|
|
return out.join('');
|
|
}
|
|
|
|
function showAchievements(back) {
|
|
overlayEnter();
|
|
const P = RC.Progress, r = P.rankOf(G.save.fame);
|
|
const pct = Math.round(r.pct * 100);
|
|
const nextLine = r.next
|
|
? t('rank_next', { n: t('rank_' + r.next.key), f: Kit.fmt(r.next.at - G.save.fame) })
|
|
: t('rank_top');
|
|
const rows = P.ACHIEVEMENTS.map(a => {
|
|
const done = !!(G.save.ach && G.save.ach[a.id]);
|
|
return `<div class="achrow ${done ? 'done' : 'todo'}"><span class="am">${done ? '🏅' : '🔒'}</span>
|
|
<span><span class="an">${t('ach_' + a.id)}</span><br><span class="ad">${t('achd_' + a.id)}</span></span>
|
|
<span class="${done ? 'plus' : 'muted'}">${Kit.fmt(a.reward)}</span></div>`;
|
|
}).join('');
|
|
card(`<h2>🏅 ${t('achievements')}</h2>
|
|
<div class="rankbar"><div class="rt"><b>${t('rank_' + r.key)}</b><span class="muted">${nextLine}</span></div>
|
|
<div class="bar"><i style="width:${pct}%"></i></div></div>
|
|
<p class="muted">${t('ach_done', { n: P.earned(G.save), m: P.total })}</p>
|
|
<div class="achlist">${rows}</div>
|
|
<div class="row"><button class="primary" id="bBack">${t('back')}</button></div>`, { wide: true, bg: 'bg_title.jpg' });
|
|
$('bBack').onclick = () => { SFX.click(); back(); };
|
|
}
|
|
|
|
// campaign missions page: 3 daily + 1 weekly with progress bars + reset countdown
|
|
function showMissions(back) {
|
|
overlayEnter();
|
|
ensureMissions();
|
|
const daily = (G.save.missions && G.save.missions.daily) || [], weekly = (G.save.missions && G.save.missions.weekly) || [];
|
|
const hrs = ms => Math.max(1, Math.ceil(ms / 3600000));
|
|
const toDay = 86400000 - (Date.now() % 86400000), toWeek = 604800000 - (Date.now() % 604800000);
|
|
const row = m => {
|
|
const prog = Math.min(m.prog || 0, m.goal), pct = Math.round(prog / m.goal * 100);
|
|
const rew = `🪙 ${Kit.fmt(m.reward)}${m.fame ? ' · ⭐' + m.fame : ''}${typeof m.unlock === 'number' ? ' · 🎨' : ''}`;
|
|
return `<div class="missrow ${m.done ? 'done' : ''}"><span class="mm">${m.done ? '✅' : '🎯'}</span>
|
|
<div class="mi"><div class="mn">${t('mission_' + m.id)}</div><div class="md">${t('missd_' + m.id)}</div>
|
|
<div class="bar"><i style="width:${pct}%"></i></div></div>
|
|
<div class="mr"><span class="${m.done ? 'plus' : 'muted'}">${rew}</span><span class="mp">${prog}/${m.goal}</span></div></div>`;
|
|
};
|
|
card(`<h2>🎯 ${t('missions')}</h2>
|
|
<h3>${t('miss_daily')} <span class="muted">· ${t('miss_reset_in', { n: hrs(toDay) })}</span></h3>
|
|
<div class="misslist">${daily.map(row).join('')}</div>
|
|
<h3>${t('miss_weekly')} <span class="muted">· ${t('miss_reset_in', { n: hrs(toWeek) })}</span></h3>
|
|
<div class="misslist">${weekly.map(row).join('')}</div>
|
|
<div class="row"><button class="primary" id="bBack">${t('back')}</button></div>`, { wide: true, bg: 'bg_title.jpg' });
|
|
$('bBack').onclick = () => { SFX.click(); back(); };
|
|
}
|
|
|
|
function showHelp(back) {
|
|
overlayEnter();
|
|
card(`<h2>${t('how_to')}</h2><p>${t('help_text')}</p>${keysHtml()}
|
|
<p style="margin-top:12px">${t('help_arena')}</p><button class="primary" id="bOk">${t('ok')}</button>`, { bg: 'bg_title.jpg' });
|
|
$('bOk').onclick = () => { SFX.click(); back(); };
|
|
}
|
|
// legal pages (Impressum / Datenschutz) — placeholders, Michael fills his data in lang.js
|
|
function showLegal(back, which) {
|
|
overlayEnter();
|
|
which = which === 'privacy' ? 'privacy' : 'impressum';
|
|
const seg = ['impressum', 'privacy'].map(k => `<button data-l="${k}" class="${k === which ? 'on' : ''}">${t('legal_' + k)}</button>`).join('');
|
|
card(`<h2>${t('legal')}</h2><div class="seg legalseg">${seg}</div>
|
|
<div class="legaltext">${t(which + '_text')}</div>
|
|
<div class="row"><button class="primary" id="bOk">${t('ok')}</button></div>`, { wide: true, bg: 'bg_title.jpg' });
|
|
overlay.querySelectorAll('.legalseg button').forEach(b => b.onclick = () => { SFX.click(); showLegal(back, b.dataset.l); });
|
|
$('bOk').onclick = () => { SFX.click(); back(); };
|
|
}
|
|
|
|
const closeRank = () => { if (G.rankSub) { G.rankSub.close(); G.rankSub = null; } };
|
|
function showRanking(back) {
|
|
overlayEnter();
|
|
card(`<h2>🏆 ${t('ranking')} ${rank.enabled ? '<span class="livedot">● ' + t('rank_live') + '</span>' : ''}</h2>
|
|
<ol class="board live" id="board"><li class="muted">${t('loading')}</li></ol>
|
|
<div class="row"><button class="primary" id="bBack">${t('back')}</button></div>`, { bg: 'bg_title.jpg' });
|
|
$('bBack').onclick = () => { SFX.click(); closeRank(); back(); };
|
|
G.boardFame = {}; G.boardIdx = {};
|
|
const myUid = (fbAcc() && fbAcc().uid) || net.uid;
|
|
closeRank();
|
|
if (rank.enabled) G.rankSub = rank.watch(rows => drawBoard($('board'), rows, myUid));
|
|
else drawBoard($('board'), [], myUid);
|
|
}
|
|
// animated live board: FLIP row reordering, fame bars, count-up numbers, rank ▲/▼
|
|
function animNum(el, from, to) {
|
|
if (!el) return; const t0 = performance.now(), dur = 520;
|
|
(function step(now) {
|
|
const k = Math.min(1, (now - t0) / dur), e = 1 - Math.pow(1 - k, 3);
|
|
el.textContent = Kit.fmt(Math.round(from + (to - from) * e));
|
|
if (k < 1 && el.isConnected) requestAnimationFrame(step);
|
|
})(t0);
|
|
}
|
|
function drawBoard(el, rows, myUid) {
|
|
if (!el) return;
|
|
if (!rows.length) { el.innerHTML = `<li class="muted">${t('no_scores')}</li>`; G.boardFame = {}; G.boardIdx = {}; return; }
|
|
const max = Math.max(1, rows[0].fame);
|
|
const prevTop = {};
|
|
el.querySelectorAll('.brow').forEach(li => { prevTop[li.dataset.key] = li.getBoundingClientRect().top; });
|
|
el.innerHTML = rows.map((r, i) => {
|
|
const me = r.key === myUid;
|
|
const was = G.boardIdx[r.key];
|
|
const arr = (was != null && was !== i) ? (i < was ? '<span class="arrow up">▲</span>' : '<span class="arrow dn">▼</span>') : '';
|
|
return `<li class="brow ${me ? 'me' : ''}" data-key="${r.key}" data-fame="${r.fame}">
|
|
<span class="rk">${i + 1}</span><span class="fl">${flagHtml(r.flag || 'XX')}</span>
|
|
<span class="nm">${esc(r.name)}${me ? ` <span class="muted">(${t('you')})</span>` : ''}${arr}</span>
|
|
<span class="val"><i class="fill" style="width:${(r.fame / max * 100).toFixed(1)}%"></i><b>0</b></span></li>`;
|
|
}).join('');
|
|
el.querySelectorAll('.brow').forEach(li => {
|
|
const key = li.dataset.key, to = +li.dataset.fame, from = G.boardFame[key] != null ? G.boardFame[key] : to;
|
|
animNum(li.querySelector('.val b'), from, to); G.boardFame[key] = to;
|
|
if (prevTop[key] != null) {
|
|
const d = prevTop[key] - li.getBoundingClientRect().top;
|
|
if (d) { li.style.transform = `translateY(${d}px)`; li.style.transition = 'none';
|
|
requestAnimationFrame(() => { li.style.transition = 'transform .45s cubic-bezier(.2,.8,.2,1)'; li.style.transform = ''; }); }
|
|
}
|
|
});
|
|
G.boardIdx = {}; rows.forEach((r, i) => G.boardIdx[r.key] = i);
|
|
}
|
|
function confirmReset(back) {
|
|
card(`<h2>${t('reset')}</h2><p>${t('reset_text')}</p><div class="row"><button id="bYes">${t('reset_yes')}</button><button class="primary" id="bNo">${t('cancel')}</button></div>`);
|
|
$('bNo').onclick = () => { SFX.click(); back(); };
|
|
$('bYes').onclick = () => { Object.assign(G.save, newSave()); RC.layerFromData(layer, null); G.save.tutorialSeen = true; persist(); hud(); shop.reset(); SFX.click(); openGarage(); };
|
|
}
|
|
// the rank badge opens the achievements page
|
|
$('hTour').onclick = () => { SFX.click(); if (G.gameMode === 'campaign') { if (tourState()) showTournament(); else showLeague(); } }; // online tournament: server-side later (plan P2-6)
|
|
$('chipFame').onclick = () => {
|
|
if (!G.save || G.mode === 'intro' || G.mode === 'title') return;
|
|
if (G.mode === 'fight') togglePause();
|
|
SFX.click();
|
|
showAchievements(G.mode === 'pause' ? () => { G.mode = 'fight'; togglePause(); } : currentBack());
|
|
};
|
|
$('bSettings').onclick = () => {
|
|
if (G.mode === 'fight') togglePause();
|
|
if (G.mode === 'intro') skipIntro();
|
|
SFX.click();
|
|
const back = G.mode === 'pause' ? () => { G.mode = 'fight'; togglePause(); } : currentBack();
|
|
showSettings(back);
|
|
};
|
|
|
|
function boardHtml() { return (rank.enabled && G.gameMode === 'online') ? `<h3>${t('ranking')}</h3><ol class="board" id="board"><li class="muted">${t('loading')}</li></ol>` : ''; }
|
|
async function fillBoard() {
|
|
const el = $('board');
|
|
if (!el) return;
|
|
const myUid = (fbAcc() && fbAcc().uid) || net.uid;
|
|
try {
|
|
const rows = await rank.once(10);
|
|
el.innerHTML = rows.length
|
|
? rows.map((r, i) => `<li class="${r.key === myUid ? 'me' : ''}"><span>${i + 1}.</span><span class="nm">${esc(r.name)}</span><span>${Kit.fmt(r.fame)}</span></li>`).join('')
|
|
: `<li class="muted">${t('no_scores')}</li>`;
|
|
} catch (e) { el.innerHTML = `<li class="muted">${t('scores_offline')}</li>`; }
|
|
}
|
|
|
|
|
|
return {
|
|
showSettings, keysHtml, TUT, startTutorial, chooseFlag, ensureFlag, liveBanner, showAchievements,
|
|
showMissions, showHelp, showLegal, closeRank, showRanking, animNum, drawBoard, confirmReset, boardHtml,
|
|
fillBoard
|
|
};
|
|
};
|
|
})(window);
|