BlueGit

projects-timfalken-com meteors

Files

meteors / server / lib / Planets.php

Planets.php

Planets.php 6.9 KiB Download Raw
1<?php
2declare(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 */
8final 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 /** t1199: gravity (jumpsToClear) range; lower values clamp up to 2. */
67 public const GRAVITY_MIN = 2.0;
68 public const GRAVITY_MAX = 4.0;
69 /** t1199: g shown at 100 % (jumpsToClear 2); display g = 9.81·(v − 1). */
70 public const EARTH_G = 9.81;
71
72 public static function clampGravity(float $v): float
73 {
74 return self::clamp($v, self::GRAVITY_MIN, self::GRAVITY_MAX);
75 }
76
77 /** Difficulty in 0..1. Higher is harder. */
78 public static function power(array $planet): float
79 {
80 $j = self::clamp((float) ($planet['jumpsToClear'] ?? 2), self::GRAVITY_MIN, self::GRAVITY_MAX);
81 $f = self::clamp((float) ($planet['fallTime'] ?? 3), 1, 5);
82 $r = self::clamp((float) ($planet['riseTime'] ?? 1.5), 0.5, 3);
83 // t1199: kept on the old 1..4 axis so unchanged planets keep their Power.
84 $gravity = ($j - 1) / 3;
85 $air = (5 - $f) / 4;
86 $mass = (3 - $r) / 2.5;
87 return ($gravity + $air + $mass) / 3;
88 }
89
90 /** Maximum miss chance; displayed Power is 1 − miss / MAX_MISS_CHANCE. */
91 public const MAX_MISS_CHANCE = 0.60;
92 public const MIN_MISS_CHANCE = 0.05;
93
94 /** Miss chance: 0.60 at power 0, 0.05 at power 1. Unchanged gameplay formula. */
95 public static function missChance(array $planet): float
96 {
97 return self::MAX_MISS_CHANCE - (self::MAX_MISS_CHANCE - self::MIN_MISS_CHANCE) * self::power($planet);
98 }
99
100 /** Displayed Power 0–100: 0% miss → 100% Power, 60% miss → 0% Power. */
101 public static function displayPower(array $planet): int
102 {
103 return (int) round((1 - self::missChance($planet) / self::MAX_MISS_CHANCE) * 100);
104 }
105
106 public static function missChanceForId(string $id): float
107 {
108 $p = self::byId($id);
109 if ($p === null) {
110 $p = self::default();
111 }
112 return self::missChance($p);
113 }
114
115 /** @return array{gravity:float,density:float,newtons:float,power:int,missChance:float} */
116 public static function displayValues(array $planet): array
117 {
118 $j = self::clamp((float) ($planet['jumpsToClear'] ?? 2), self::GRAVITY_MIN, self::GRAVITY_MAX);
119 $f = self::clamp((float) ($planet['fallTime'] ?? 3), 1, 5);
120 $r = self::clamp((float) ($planet['riseTime'] ?? 1.5), 0.5, 3);
121 return [
122 'gravity' => self::EARTH_G * ($j - 1),
123 'gravityPct' => (int) round(($j - 1) * 100),
124 'density' => 0.25 + ((5 - $f) / 4) * 1.60,
125 'newtons' => 40 + (($r - 0.5) / 2.5) * 160,
126 'power' => self::displayPower($planet),
127 'missChance' => self::missChance($planet),
128 ];
129 }
130
131 /** Public subset sent to clients (no need to ship the full catalog again). */
132 public static function present(?string $id): array
133 {
134 $p = $id ? self::byId($id) : null;
135 if ($p === null) {
136 $p = self::default();
137 }
138 $d = self::displayValues($p);
139 $colors = [];
140 foreach ($p['colors'] ?? [] as $c) {
141 if (!is_array($c)) {
142 continue;
143 }
144 $colors[] = [
145 'id' => (int) ($c['id'] ?? 0),
146 'name' => (string) ($c['name'] ?? ''),
147 'hex' => (string) ($c['hex'] ?? '#888'),
148 'shape' => (string) ($c['shape'] ?? ''),
149 'image' => (string) ($c['image'] ?? ''),
150 ];
151 }
152 return [
153 'id' => (string) $p['id'],
154 'name' => (string) $p['name'],
155 'people' => (string) $p['people'],
156 'jumpsToClear' => self::clampGravity((float) $p['jumpsToClear']),
157 'fallTime' => (float) $p['fallTime'],
158 'riseTime' => (float) $p['riseTime'],
159 'miss_chance' => $d['missChance'],
160 'display' => $d,
161 'colors' => $colors,
162 ];
163 }
164
165 /** Present a raw planet array (catalog or a validated custom snapshot). */
166 public static function presentData(array $p): array
167 {
168 $d = self::displayValues($p);
169 $colors = [];
170 foreach ($p['colors'] ?? [] as $c) {
171 if (!is_array($c)) {
172 continue;
173 }
174 $colors[] = [
175 'id' => (int) ($c['id'] ?? 0),
176 'block' => (string) ($c['block'] ?? $c['name'] ?? ''),
177 'name' => (string) ($c['name'] ?? ''),
178 'hex' => (string) ($c['hex'] ?? '#888'),
179 'shape' => (string) ($c['shape'] ?? ''),
180 'image' => (string) ($c['image'] ?? ''),
181 ];
182 }
183 return [
184 'id' => (string) ($p['id'] ?? ''),
185 'name' => (string) ($p['name'] ?? ''),
186 'people' => (string) ($p['people'] ?? ''),
187 'jumpsToClear' => self::clamp((float) ($p['jumpsToClear'] ?? 2), self::GRAVITY_MIN, self::GRAVITY_MAX),
188 'fallTime' => (float) ($p['fallTime'] ?? 3),
189 'riseTime' => (float) ($p['riseTime'] ?? 1.5),
190 'miss_chance' => $d['missChance'],
191 'display' => $d,
192 'colors' => $colors,
193 'custom' => !empty($p['custom']),
194 'owner_id' => isset($p['owner_id']) ? (string) $p['owner_id'] : null,
195 ];
196 }
197
198 private static function clamp(float $value, float $lo, float $hi): float
199 {
200 return max($lo, min($hi, $value));
201 }
202}