/* Robot Clash - ranks and achievements (no DOM, runs in Node for the tests). Needs parts.js. Everything is read from / written to the save, so the campaign and the online game keep their own ranks and their own achievements. RC.Progress.rankOf(fame) -> { i, key, at, next, pct } next = null at the top rank RC.Progress.check(save, ctx) -> [{ id, key, reward }] newly earned (marks them in save.ach) RC.Progress.earned(save) -> number of achievements done ctx after a fight: { won, reason, hpPct, lost, dealt, training, exhibition, champion } Texts: lang.js keys 'rank_', 'ach_' (name) and 'achd_' (what to do). */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || (typeof require === 'function' ? require('./parts.js') : {})); // fame needed for each rank const RANKS = [ { at: 0, key: 'scrap' }, { at: 150, key: 'rookie' }, { at: 400, key: 'contender' }, { at: 900, key: 'pro' }, { at: 1800, key: 'veteran' }, { at: 3200, key: 'champion' }, { at: 5500, key: 'master' }, { at: 9000, key: 'legend' }, ]; function rankOf(fame) { const f = Math.max(0, fame | 0); let i = 0; while (i + 1 < RANKS.length && f >= RANKS[i + 1].at) i++; const cur = RANKS[i], next = RANKS[i + 1] || null; const pct = next ? Math.min(1, (f - cur.at) / (next.at - cur.at)) : 1; return { i, key: cur.key, at: cur.at, next, pct }; } // test(save, ctx) - ctx is empty when we only re-check the save (e.g. after a purchase) const ACHIEVEMENTS = [ { id: 'first_win', reward: 100, test: (s, c) => c.won && !c.training }, { id: 'ko', reward: 150, test: (s, c) => c.won && c.reason === 'ko' }, { id: 'pit', reward: 150, test: (s, c) => c.won && c.reason === 'pit' }, { id: 'tough', reward: 250, test: (s, c) => c.won && c.hpPct > 0 && c.hpPct < 0.15 }, { id: 'flawless', reward: 250, test: (s, c) => c.won && !c.training && c.lost === 0 && c.hpPct >= 0.9 }, { id: 'demolition', reward: 200, test: (s, c) => c.dealt >= 600 }, { id: 'champion', reward: 1000, test: (s, c) => !!c.champion }, { id: 'tourney', reward: 800, test: (s, c) => !!c.tourWon }, { id: 'veteran', reward: 300, test: s => (s.games | 0) >= 25 }, { id: 'rich', reward: 200, test: s => (s.money | 0) >= 5000 }, { id: 'collector', reward: 300, test: s => (s.owned || []).length >= 20 }, { id: 'garage', reward: 200, test: s => (s.robots || []).length >= 3 }, { id: 'artist', reward: 150, test: s => !!(s.paint && s.paint.decal) }, ]; function check(save, ctx) { if (!save) return []; const c = ctx || {}; if (!save.ach) save.ach = {}; const got = []; for (const a of ACHIEVEMENTS) { if (save.ach[a.id]) continue; let ok = false; try { ok = !!a.test(save, c); } catch (e) { ok = false; } if (!ok) continue; save.ach[a.id] = true; got.push({ id: a.id, key: a.id, reward: a.reward }); } return got; } const earned = save => ACHIEVEMENTS.filter(a => save && save.ach && save.ach[a.id]).length; RC.Progress = { RANKS, rankOf, ACHIEVEMENTS, check, earned, total: ACHIEVEMENTS.length }; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);