| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Append-only block registry. Thresholds and order stay in lockstep with |
| 5 | * server/lib/Blocks.php: Terra's first five are free; the next costs 500 |
| 6 | * lifetime points; each one after that costs 14.5% more (rounded, always |
| 7 | * strictly increasing). Points are a requirement, not a spend. |
| 8 | */ |
| 9 | |
| 10 | var Blocks = (function () |
| 11 | { |
| 12 | var TERRA_COUNT = 5; |
| 13 | var FIRST_THRESHOLD = 500; |
| 14 | var GROWTH = 1.145; |
| 15 | var _all = null; |
| 16 | |
| 17 | function loadRegistry () |
| 18 | { |
| 19 | if (_all) |
| 20 | { return _all; } |
| 21 | var data = null; |
| 22 | if (typeof BLOCK_REGISTRY !== 'undefined' && BLOCK_REGISTRY && BLOCK_REGISTRY.blocks) |
| 23 | { data = BLOCK_REGISTRY; } |
| 24 | else if (typeof require !== 'undefined') |
| 25 | { |
| 26 | try |
| 27 | { data = require('../server/lib/blocks.json'); } |
| 28 | catch (e) { data = null; } |
| 29 | } |
| 30 | _all = (data && data.blocks) ? data.blocks.slice() : []; |
| 31 | return _all; |
| 32 | } |
| 33 | |
| 34 | function all () |
| 35 | { |
| 36 | return loadRegistry(); |
| 37 | } |
| 38 | |
| 39 | function byId (id) |
| 40 | { |
| 41 | var list = all(); |
| 42 | for (var i = 0; i < list.length; i++) |
| 43 | { |
| 44 | if (list[i].id === id) |
| 45 | { return list[i]; } |
| 46 | } |
| 47 | return null; |
| 48 | } |
| 49 | |
| 50 | function indexOf (id) |
| 51 | { |
| 52 | var list = all(); |
| 53 | for (var i = 0; i < list.length; i++) |
| 54 | { |
| 55 | if (list[i].id === id) |
| 56 | { return i; } |
| 57 | } |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | function threshold (index) |
| 62 | { |
| 63 | if (index < TERRA_COUNT) |
| 64 | { return 0; } |
| 65 | var t = FIRST_THRESHOLD; |
| 66 | for (var i = TERRA_COUNT; i < index; i++) |
| 67 | { |
| 68 | var next = Math.round(t * GROWTH); |
| 69 | t = Math.max(t + 1, next); |
| 70 | } |
| 71 | return t; |
| 72 | } |
| 73 | |
| 74 | function unlocked (index, points) |
| 75 | { |
| 76 | points = points || 0; |
| 77 | return points >= threshold(index); |
| 78 | } |
| 79 | |
| 80 | function unlockedId (id, points) |
| 81 | { |
| 82 | var i = indexOf(id); |
| 83 | return i >= 0 && unlocked(i, points); |
| 84 | } |
| 85 | |
| 86 | function present (points) |
| 87 | { |
| 88 | points = points || 0; |
| 89 | var list = all(); |
| 90 | var out = []; |
| 91 | for (var i = 0; i < list.length; i++) |
| 92 | { |
| 93 | var b = list[i]; |
| 94 | var need = threshold(i); |
| 95 | out.push({ |
| 96 | id: b.id, |
| 97 | name: b.name || b.id, |
| 98 | hex: b.hex, |
| 99 | shape: b.shape, |
| 100 | image: b.image, |
| 101 | terra: !!b.terra || i < TERRA_COUNT, |
| 102 | index: i, |
| 103 | threshold: need, |
| 104 | unlocked: points >= need |
| 105 | }); |
| 106 | } |
| 107 | return out; |
| 108 | } |
| 109 | |
| 110 | function asColor (id, slot) |
| 111 | { |
| 112 | var b = byId(id); |
| 113 | if (!b) |
| 114 | { return null; } |
| 115 | return { |
| 116 | id: slot, |
| 117 | block: b.id, |
| 118 | name: b.name || b.id, |
| 119 | hex: b.hex, |
| 120 | shape: b.shape, |
| 121 | image: b.image |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | function images () |
| 126 | { |
| 127 | var list = all(); |
| 128 | var out = []; |
| 129 | var seen = Object.create(null); |
| 130 | for (var i = 0; i < list.length; i++) |
| 131 | { |
| 132 | var url = list[i].image; |
| 133 | if (!url || seen[url]) |
| 134 | { continue; } |
| 135 | seen[url] = true; |
| 136 | out.push({ name: 'block:' + list[i].id, url: url }); |
| 137 | } |
| 138 | return out; |
| 139 | } |
| 140 | |
| 141 | return { |
| 142 | TERRA_COUNT: TERRA_COUNT, |
| 143 | FIRST_THRESHOLD: FIRST_THRESHOLD, |
| 144 | GROWTH: GROWTH, |
| 145 | all: all, |
| 146 | byId: byId, |
| 147 | indexOf: indexOf, |
| 148 | threshold: threshold, |
| 149 | unlocked: unlocked, |
| 150 | unlockedId: unlockedId, |
| 151 | present: present, |
| 152 | asColor: asColor, |
| 153 | images: images |
| 154 | }; |
| 155 | })(); |
| 156 | |
| 157 | if (typeof module !== 'undefined' && module.exports) |
| 158 | { module.exports = Blocks; } |
| 159 | else if (typeof globalThis !== 'undefined') |
| 160 | { globalThis.Blocks = Blocks; } |
projects-timfalken-com meteors