/* 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.; 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) => `
${opts.map(([v, label]) => ``).join('')}
`; const who = fbAcc() || (accounts.current && !accounts.current.guest ? accounts.current : null); const item = (label, ctl) => `${label}${ctl}`; card(`

${t('settings')}

${item(t('s_lang'), seg('sLang', L.lang, L.langs.map(l => [l, window.LANG[l].lang_self])))} ${G.save ? item(t('s_country'), ``) : ''} ${item(t('s_music'), ``)} ${item(t('s_sfx'), ``)} ${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')]]))}
${G.isDev && G.save ? `
๐Ÿ›  Dev ยท Coins
` : ''}
${t('s_account')} ${who ? '๐Ÿ‘ค ' + esc(who.name) + '' + (who.email ? ' ' + esc(who.email) + '' : '') : '๐Ÿ‘ค ' + t('guest')} ${G.save ? `` : ''}
${G.save ? `` : ''}
`, { 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 `
W A S D / โ†‘ โ† โ†“ โ†’${t('key_drive')}${t('key_space')}${t('key_weapon')} Shift / E${t('key_tower')}C${t('key_cam')}P / Esc${t('pause')}M${t('sound_on')} / ${t('sound_off')} ${t('key_pad')}${t('key_pad_text')}
`; } // 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(`

${t('tut_title')} ${n}/${TUT.length}

${t(TUT[i])}

`, { 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(`

๐ŸŒ ${t('country_title')}

${t('country_text')}

${FLAGS.map(f => ``).join('')}
`, { 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(`
${t('event')}${esc(n)}${extra}
`); } const m = liveText(G.LIVE.motd); if (m) out.push(`

${esc(m)}

`); 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 `
${done ? '๐Ÿ…' : '๐Ÿ”’'} ${t('ach_' + a.id)}
${t('achd_' + a.id)}
${Kit.fmt(a.reward)}
`; }).join(''); card(`

๐Ÿ… ${t('achievements')}

${t('rank_' + r.key)}${nextLine}

${t('ach_done', { n: P.earned(G.save), m: P.total })}

${rows}
`, { 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 `
${m.done ? 'โœ…' : '๐ŸŽฏ'}
${t('mission_' + m.id)}
${t('missd_' + m.id)}
${rew}${prog}/${m.goal}
`; }; card(`

๐ŸŽฏ ${t('missions')}

${t('miss_daily')} ยท ${t('miss_reset_in', { n: hrs(toDay) })}

${daily.map(row).join('')}

${t('miss_weekly')} ยท ${t('miss_reset_in', { n: hrs(toWeek) })}

${weekly.map(row).join('')}
`, { wide: true, bg: 'bg_title.jpg' }); $('bBack').onclick = () => { SFX.click(); back(); }; } function showHelp(back) { overlayEnter(); card(`

${t('how_to')}

${t('help_text')}

${keysHtml()}

${t('help_arena')}

`, { 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 => ``).join(''); card(`

${t('legal')}

${seg}
${t(which + '_text')}
`, { 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(`

๐Ÿ† ${t('ranking')} ${rank.enabled ? 'โ— ' + t('rank_live') + '' : ''}

  1. ${t('loading')}
`, { 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 = `
  • ${t('no_scores')}
  • `; 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 ? 'โ–ฒ' : 'โ–ผ') : ''; return `
  • ${i + 1}${flagHtml(r.flag || 'XX')} ${esc(r.name)}${me ? ` (${t('you')})` : ''}${arr} 0
  • `; }).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(`

    ${t('reset')}

    ${t('reset_text')}

    `); $('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') ? `

    ${t('ranking')}

    1. ${t('loading')}
    ` : ''; } 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) => `
  • ${i + 1}.${esc(r.name)}${Kit.fmt(r.fame)}
  • `).join('') : `
  • ${t('no_scores')}
  • `; } catch (e) { el.innerHTML = `
  • ${t('scores_offline')}
  • `; } } return { showSettings, keysHtml, TUT, startTutorial, chooseFlag, ensureFlag, liveBanner, showAchievements, showMissions, showHelp, showLegal, closeRank, showRanking, animNum, drawBoard, confirmReset, boardHtml, fillBoard }; }; })(window);