| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | /** |
| 5 | * Planet catalog shared with the JS client (server/lib/planets.json). |
| 6 | * Power and miss-chance formulas must stay in lockstep with src/planet.js. |
| 7 | */ |
| 8 | final class Planets |
| 9 | { |
| 10 | private static ?array $all = null; |
| 11 | |
| 12 | public static function path(): string |
| 13 | { |
| 14 | return __DIR__ . '/planets.json'; |
| 15 | } |
| 16 | |
| 17 | /** @return list<array> raw catalog entries */ |
| 18 | public static function all(): array |
| 19 | { |
| 20 | if (self::$all !== null) { |
| 21 | return self::$all; |
| 22 | } |
| 23 | $raw = file_get_contents(self::path()); |
| 24 | if ($raw === false) { |
| 25 | throw new RuntimeException('planets.json ontbreekt.'); |
| 26 | } |
| 27 | $data = json_decode($raw, true); |
| 28 | if (!is_array($data) || !isset($data['planets']) || !is_array($data['planets'])) { |
| 29 | throw new RuntimeException('planets.json is ongeldig.'); |
| 30 | } |
| 31 | self::$all = []; |
| 32 | foreach ($data['planets'] as $row) { |
| 33 | if (!is_array($row) || empty($row['id'])) { |
| 34 | continue; |
| 35 | } |
| 36 | self::$all[] = $row; |
| 37 | } |
| 38 | return self::$all; |
| 39 | } |
| 40 | |
| 41 | public static function byId(string $id): ?array |
| 42 | { |
| 43 | foreach (self::all() as $p) { |
| 44 | if (($p['id'] ?? '') === $id) { |
| 45 | return $p; |
| 46 | } |
| 47 | } |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | public static function defaultId(): string |
| 52 | { |
| 53 | return 'terra'; |
| 54 | } |
| 55 | |
| 56 | public static function default(): array |
| 57 | { |
| 58 | return self::byId(self::defaultId()) ?? self::all()[0]; |
| 59 | } |
| 60 | |
| 61 | public static function known(string $id): bool |
| 62 | { |
| 63 | return self::byId($id) !== null; |
| 64 | } |
| 65 | |
| 66 | /** Difficulty in 0..1. Higher is harder. */ |
| 67 | public static function power(array $planet): float |
| 68 | { |
| 69 | $j = self::clamp((float) ($planet['jumpsToClear'] ?? 2), 1, 4); |
| 70 | $f = self::clamp((float) ($planet['fallTime'] ?? 3), 1, 5); |
| 71 | $r = self::clamp((float) ($planet['riseTime'] ?? 1.5), 0.5, 3); |
| 72 | $gravity = ($j - 1) / 3; |
| 73 | $air = (5 - $f) / 4; |
| 74 | $mass = (3 - $r) / 2.5; |
| 75 | return ($gravity + $air + $mass) / 3; |
| 76 | } |
| 77 | |
| 78 | /** Maximum miss chance; displayed Power is 1 − miss / MAX_MISS_CHANCE. */ |
| 79 | public const MAX_MISS_CHANCE = 0.60; |
| 80 | public const MIN_MISS_CHANCE = 0.05; |
| 81 | |
| 82 | /** Miss chance: 0.60 at power 0, 0.05 at power 1. Unchanged gameplay formula. */ |
| 83 | public static function missChance(array $planet): float |
| 84 | { |
| 85 | return self::MAX_MISS_CHANCE - (self::MAX_MISS_CHANCE - self::MIN_MISS_CHANCE) * self::power($planet); |
| 86 | } |
| 87 | |
| 88 | /** Displayed Power 0–100: 0% miss → 100% Power, 60% miss → 0% Power. */ |
| 89 | public static function displayPower(array $planet): int |
| 90 | { |
| 91 | return (int) round((1 - self::missChance($planet) / self::MAX_MISS_CHANCE) * 100); |
| 92 | } |
| 93 | |
| 94 | public static function missChanceForId(string $id): float |
| 95 | { |
| 96 | $p = self::byId($id); |
| 97 | if ($p === null) { |
| 98 | $p = self::default(); |
| 99 | } |
| 100 | return self::missChance($p); |
| 101 | } |
| 102 | |
| 103 | /** @return array{gravity:float,density:float,newtons:float,power:int,missChance:float} */ |
| 104 | public static function displayValues(array $planet): array |
| 105 | { |
| 106 | $j = self::clamp((float) ($planet['jumpsToClear'] ?? 2), 1, 4); |
| 107 | $f = self::clamp((float) ($planet['fallTime'] ?? 3), 1, 5); |
| 108 | $r = self::clamp((float) ($planet['riseTime'] ?? 1.5), 0.5, 3); |
| 109 | return [ |
| 110 | 'gravity' => 9.81 * $j / 2, |
| 111 | 'density' => 0.25 + ((5 - $f) / 4) * 1.60, |
| 112 | 'newtons' => 40 + (($r - 0.5) / 2.5) * 160, |
| 113 | 'power' => self::displayPower($planet), |
| 114 | 'missChance' => self::missChance($planet), |
| 115 | ]; |
| 116 | } |
| 117 | |
| 118 | /** Public subset sent to clients (no need to ship the full catalog again). */ |
| 119 | public static function present(?string $id): array |
| 120 | { |
| 121 | $p = $id ? self::byId($id) : null; |
| 122 | if ($p === null) { |
| 123 | $p = self::default(); |
| 124 | } |
| 125 | $d = self::displayValues($p); |
| 126 | $colors = []; |
| 127 | foreach ($p['colors'] ?? [] as $c) { |
| 128 | if (!is_array($c)) { |
| 129 | continue; |
| 130 | } |
| 131 | $colors[] = [ |
| 132 | 'id' => (int) ($c['id'] ?? 0), |
| 133 | 'name' => (string) ($c['name'] ?? ''), |
| 134 | 'hex' => (string) ($c['hex'] ?? '#888'), |
| 135 | 'shape' => (string) ($c['shape'] ?? ''), |
| 136 | 'image' => (string) ($c['image'] ?? ''), |
| 137 | ]; |
| 138 | } |
| 139 | return [ |
| 140 | 'id' => (string) $p['id'], |
| 141 | 'name' => (string) $p['name'], |
| 142 | 'people' => (string) $p['people'], |
| 143 | 'jumpsToClear' => (float) $p['jumpsToClear'], |
| 144 | 'fallTime' => (float) $p['fallTime'], |
| 145 | 'riseTime' => (float) $p['riseTime'], |
| 146 | 'miss_chance' => $d['missChance'], |
| 147 | 'display' => $d, |
| 148 | 'colors' => $colors, |
| 149 | ]; |
| 150 | } |
| 151 | |
| 152 | /** Present a raw planet array (catalog or a validated custom snapshot). */ |
| 153 | public static function presentData(array $p): array |
| 154 | { |
| 155 | $d = self::displayValues($p); |
| 156 | $colors = []; |
| 157 | foreach ($p['colors'] ?? [] as $c) { |
| 158 | if (!is_array($c)) { |
| 159 | continue; |
| 160 | } |
| 161 | $colors[] = [ |
| 162 | 'id' => (int) ($c['id'] ?? 0), |
| 163 | 'block' => (string) ($c['block'] ?? $c['name'] ?? ''), |
| 164 | 'name' => (string) ($c['name'] ?? ''), |
| 165 | 'hex' => (string) ($c['hex'] ?? '#888'), |
| 166 | 'shape' => (string) ($c['shape'] ?? ''), |
| 167 | 'image' => (string) ($c['image'] ?? ''), |
| 168 | ]; |
| 169 | } |
| 170 | return [ |
| 171 | 'id' => (string) ($p['id'] ?? ''), |
| 172 | 'name' => (string) ($p['name'] ?? ''), |
| 173 | 'people' => (string) ($p['people'] ?? ''), |
| 174 | 'jumpsToClear' => (float) ($p['jumpsToClear'] ?? 2), |
| 175 | 'fallTime' => (float) ($p['fallTime'] ?? 3), |
| 176 | 'riseTime' => (float) ($p['riseTime'] ?? 1.5), |
| 177 | 'miss_chance' => $d['missChance'], |
| 178 | 'display' => $d, |
| 179 | 'colors' => $colors, |
| 180 | 'custom' => !empty($p['custom']), |
| 181 | 'owner_id' => isset($p['owner_id']) ? (string) $p['owner_id'] : null, |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | private static function clamp(float $value, float $lo, float $hi): float |
| 186 | { |
| 187 | return max($lo, min($hi, $value)); |
| 188 | } |
| 189 | } |
projects-timfalken-com meteors