BlueGit

projects-timfalken-com meteors

Files

meteors / server / lib / Planets.php

Planets.php

Planets.php 4.4 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 /** 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 /** Miss chance: 0.60 at power 0, 0.05 at power 1. */
79 public static function missChance(array $planet): float
80 {
81 return 0.60 - 0.55 * self::power($planet);
82 }
83
84 public static function missChanceForId(string $id): float
85 {
86 $p = self::byId($id);
87 if ($p === null) {
88 $p = self::default();
89 }
90 return self::missChance($p);
91 }
92
93 /** @return array{gravity:float,density:float,newtons:float,power:int,missChance:float} */
94 public static function displayValues(array $planet): array
95 {
96 $j = self::clamp((float) ($planet['jumpsToClear'] ?? 2), 1, 4);
97 $f = self::clamp((float) ($planet['fallTime'] ?? 3), 1, 5);
98 $r = self::clamp((float) ($planet['riseTime'] ?? 1.5), 0.5, 3);
99 $power = self::power($planet);
100 return [
101 'gravity' => 9.81 * $j / 2,
102 'density' => 0.25 + ((5 - $f) / 4) * 1.60,
103 'newtons' => 40 + (($r - 0.5) / 2.5) * 160,
104 'power' => (int) round($power * 100),
105 'missChance' => 0.60 - 0.55 * $power,
106 ];
107 }
108
109 /** Public subset sent to clients (no need to ship the full catalog again). */
110 public static function present(?string $id): array
111 {
112 $p = $id ? self::byId($id) : null;
113 if ($p === null) {
114 $p = self::default();
115 }
116 $d = self::displayValues($p);
117 $colors = [];
118 foreach ($p['colors'] ?? [] as $c) {
119 if (!is_array($c)) {
120 continue;
121 }
122 $colors[] = [
123 'id' => (int) ($c['id'] ?? 0),
124 'name' => (string) ($c['name'] ?? ''),
125 'hex' => (string) ($c['hex'] ?? '#888'),
126 'shape' => (string) ($c['shape'] ?? ''),
127 'image' => (string) ($c['image'] ?? ''),
128 ];
129 }
130 return [
131 'id' => (string) $p['id'],
132 'name' => (string) $p['name'],
133 'people' => (string) $p['people'],
134 'jumpsToClear' => (float) $p['jumpsToClear'],
135 'fallTime' => (float) $p['fallTime'],
136 'riseTime' => (float) $p['riseTime'],
137 'miss_chance' => $d['missChance'],
138 'display' => $d,
139 'colors' => $colors,
140 ];
141 }
142
143 private static function clamp(float $value, float $lo, float $hi): float
144 {
145 return max($lo, min($hi, $value));
146 }
147}