'use strict'; /** * Append-only block registry. Thresholds and order stay in lockstep with * server/lib/Blocks.php: Terra's first five are free; the next costs 500 * lifetime points; each one after that costs 14.5% more (rounded, always * strictly increasing). Points are a requirement, not a spend. */ var Blocks = (function () { var TERRA_COUNT = 5; var FIRST_THRESHOLD = 500; var GROWTH = 1.145; var _all = null; function loadRegistry () { if (_all) { return _all; } var data = null; if (typeof BLOCK_REGISTRY !== 'undefined' && BLOCK_REGISTRY && BLOCK_REGISTRY.blocks) { data = BLOCK_REGISTRY; } else if (typeof require !== 'undefined') { try { data = require('../server/lib/blocks.json'); } catch (e) { data = null; } } _all = (data && data.blocks) ? data.blocks.slice() : []; return _all; } function all () { return loadRegistry(); } function byId (id) { var list = all(); for (var i = 0; i < list.length; i++) { if (list[i].id === id) { return list[i]; } } return null; } function indexOf (id) { var list = all(); for (var i = 0; i < list.length; i++) { if (list[i].id === id) { return i; } } return -1; } function threshold (index) { if (index < TERRA_COUNT) { return 0; } var t = FIRST_THRESHOLD; for (var i = TERRA_COUNT; i < index; i++) { var next = Math.round(t * GROWTH); t = Math.max(t + 1, next); } return t; } function unlocked (index, points) { points = points || 0; return points >= threshold(index); } function unlockedId (id, points) { var i = indexOf(id); return i >= 0 && unlocked(i, points); } function present (points) { points = points || 0; var list = all(); var out = []; for (var i = 0; i < list.length; i++) { var b = list[i]; var need = threshold(i); out.push({ id: b.id, name: b.name || b.id, hex: b.hex, shape: b.shape, image: b.image, terra: !!b.terra || i < TERRA_COUNT, index: i, threshold: need, unlocked: points >= need }); } return out; } function asColor (id, slot) { var b = byId(id); if (!b) { return null; } return { id: slot, block: b.id, name: b.name || b.id, hex: b.hex, shape: b.shape, image: b.image }; } function images () { var list = all(); var out = []; var seen = Object.create(null); for (var i = 0; i < list.length; i++) { var url = list[i].image; if (!url || seen[url]) { continue; } seen[url] = true; out.push({ name: 'block:' + list[i].id, url: url }); } return out; } return { TERRA_COUNT: TERRA_COUNT, FIRST_THRESHOLD: FIRST_THRESHOLD, GROWTH: GROWTH, all: all, byId: byId, indexOf: indexOf, threshold: threshold, unlocked: unlocked, unlockedId: unlockedId, present: present, asColor: asColor, images: images }; })(); if (typeof module !== 'undefined' && module.exports) { module.exports = Blocks; } else if (typeof globalThis !== 'undefined') { globalThis.Blocks = Blocks; }