| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | /** |
| 5 | * Append-only block registry shared with the JS client (server/lib/blocks.json). |
| 6 | * Never reorder entries; new blocks are appended. Thresholds stay in lockstep |
| 7 | * with src/blocks.js: Terra's first five are free; index 5 costs 500 lifetime |
| 8 | * points, each next index costs 14.5% more (rounded to a strictly increasing int). |
| 9 | */ |
| 10 | final class Blocks |
| 11 | { |
| 12 | public const TERRA_COUNT = 5; |
| 13 | public const FIRST_THRESHOLD = 500; |
| 14 | public const GROWTH = 1.145; |
| 15 | |
| 16 | private static ?array $all = null; |
| 17 | private static ?array $byId = null; |
| 18 | |
| 19 | public static function path(): string |
| 20 | { |
| 21 | return __DIR__ . '/blocks.json'; |
| 22 | } |
| 23 | |
| 24 | /** @return list<array> */ |
| 25 | public static function all(): array |
| 26 | { |
| 27 | if (self::$all !== null) { |
| 28 | return self::$all; |
| 29 | } |
| 30 | $raw = file_get_contents(self::path()); |
| 31 | if ($raw === false) { |
| 32 | throw new RuntimeException('blocks.json ontbreekt.'); |
| 33 | } |
| 34 | $data = json_decode($raw, true); |
| 35 | if (!is_array($data) || !isset($data['blocks']) || !is_array($data['blocks'])) { |
| 36 | throw new RuntimeException('blocks.json is ongeldig.'); |
| 37 | } |
| 38 | self::$all = []; |
| 39 | self::$byId = []; |
| 40 | foreach ($data['blocks'] as $i => $row) { |
| 41 | if (!is_array($row) || empty($row['id'])) { |
| 42 | continue; |
| 43 | } |
| 44 | $row['_index'] = $i; |
| 45 | self::$all[] = $row; |
| 46 | self::$byId[(string) $row['id']] = $row; |
| 47 | } |
| 48 | if (count(self::$all) < self::TERRA_COUNT) { |
| 49 | throw new RuntimeException('blocks.json mist de Terra-blokjes.'); |
| 50 | } |
| 51 | return self::$all; |
| 52 | } |
| 53 | |
| 54 | public static function byId(string $id): ?array |
| 55 | { |
| 56 | if (self::$byId === null) { |
| 57 | self::all(); |
| 58 | } |
| 59 | return self::$byId[$id] ?? null; |
| 60 | } |
| 61 | |
| 62 | public static function known(string $id): bool |
| 63 | { |
| 64 | return self::byId($id) !== null; |
| 65 | } |
| 66 | |
| 67 | public static function indexOf(string $id): ?int |
| 68 | { |
| 69 | $b = self::byId($id); |
| 70 | return $b !== null ? (int) $b['_index'] : null; |
| 71 | } |
| 72 | |
| 73 | public static function isTerra(string $id): bool |
| 74 | { |
| 75 | $i = self::indexOf($id); |
| 76 | return $i !== null && $i < self::TERRA_COUNT; |
| 77 | } |
| 78 | |
| 79 | /** Lifetime points required to unlock the block at $index (0-based). */ |
| 80 | public static function threshold(int $index): int |
| 81 | { |
| 82 | if ($index < self::TERRA_COUNT) { |
| 83 | return 0; |
| 84 | } |
| 85 | $t = self::FIRST_THRESHOLD; |
| 86 | for ($i = self::TERRA_COUNT; $i < $index; $i++) { |
| 87 | $next = (int) round($t * self::GROWTH); |
| 88 | $t = max($t + 1, $next); |
| 89 | } |
| 90 | return $t; |
| 91 | } |
| 92 | |
| 93 | public static function unlocked(int $index, int $points): bool |
| 94 | { |
| 95 | return $points >= self::threshold($index); |
| 96 | } |
| 97 | |
| 98 | public static function unlockedId(string $id, int $points): bool |
| 99 | { |
| 100 | $i = self::indexOf($id); |
| 101 | return $i !== null && self::unlocked($i, $points); |
| 102 | } |
| 103 | |
| 104 | /** Public list for the client: every block plus its threshold and unlock state. */ |
| 105 | public static function present(int $points): array |
| 106 | { |
| 107 | $out = []; |
| 108 | foreach (self::all() as $i => $b) { |
| 109 | $need = self::threshold($i); |
| 110 | $out[] = [ |
| 111 | 'id' => (string) $b['id'], |
| 112 | 'name' => (string) ($b['name'] ?? $b['id']), |
| 113 | 'hex' => (string) ($b['hex'] ?? '#888'), |
| 114 | 'shape' => (string) ($b['shape'] ?? ''), |
| 115 | 'image' => (string) ($b['image'] ?? ''), |
| 116 | 'terra' => !empty($b['terra']) || $i < self::TERRA_COUNT, |
| 117 | 'index' => $i, |
| 118 | 'threshold' => $need, |
| 119 | 'unlocked' => $points >= $need, |
| 120 | ]; |
| 121 | } |
| 122 | return $out; |
| 123 | } |
| 124 | |
| 125 | /** Resolve a registry id to a planet-slot colour (with a 0–4 slot id). */ |
| 126 | public static function asColor(string $id, int $slot): ?array |
| 127 | { |
| 128 | $b = self::byId($id); |
| 129 | if ($b === null) { |
| 130 | return null; |
| 131 | } |
| 132 | return [ |
| 133 | 'id' => $slot, |
| 134 | 'block' => (string) $b['id'], |
| 135 | 'name' => (string) ($b['name'] ?? $b['id']), |
| 136 | 'hex' => (string) ($b['hex'] ?? '#888'), |
| 137 | 'shape' => (string) ($b['shape'] ?? ''), |
| 138 | 'image' => (string) ($b['image'] ?? ''), |
| 139 | ]; |
| 140 | } |
| 141 | } |
projects-timfalken-com meteors