BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

api.php 8.5 KiB Download Raw
1<?php
2declare(strict_types=1);
3require __DIR__ . '/lib/bootstrap.php';
4require __DIR__ . '/lib/Store.php';
5
6$action = $_GET['action'] ?? $_POST['action'] ?? '';
7if ($action === '') {
8 $body = readJsonBody();
9 $action = (string) ($body['action'] ?? '');
10} else {
11 $body = readJsonBody();
12}
13
14try {
15 $configured = authConfigured();
16 $local = localDev();
17 $user = currentUser();
18
19 if ($action === 'me' || $action === '') {
20 $view = ['lobby' => null, 'incoming' => [], 'shot_results' => [], 'shot_flights' => []];
21 $profile = [
22 'lifetime_points' => 0,
23 'locale' => null,
24 'last_planet' => null,
25 'blocks' => [],
26 'planets' => [],
27 ];
28 if ($user) {
29 $store = Store::open();
30 $store->touchProfile($user['id'], $user['name'], $user['avatar'] ?? null);
31 $view = $store->viewFor($user['id']);
32 $profile = $store->profilePayload($user['id']);
33 }
34 jsonOut([
35 'ok' => true,
36 'configured' => $configured,
37 'local' => $local,
38 'csrf' => csrfToken(),
39 'me' => $user,
40 'lobby' => $view['lobby'],
41 'incoming' => $view['incoming'],
42 'shot_results' => $view['shot_results'],
43 'shot_flights' => $view['shot_flights'] ?? [],
44 'miss_chance_default' => METEORS_DEFAULT_MISS,
45 'flight_duration' => METEORS_FLIGHT_DURATION,
46 'login_url' => 'auth.php',
47 'logout_url' => 'logout.php',
48 'lifetime_points' => $profile['lifetime_points'],
49 'locale' => $profile['locale'],
50 'last_planet' => $profile['last_planet'],
51 'blocks' => $profile['blocks'],
52 'planets' => $profile['planets'],
53 ]);
54 }
55
56 if ($action === 'leaderboard') {
57 $scope = (string) ($body['scope'] ?? $_GET['scope'] ?? 'global');
58 if ($scope !== 'friends') {
59 $scope = 'global';
60 }
61 $store = Store::open();
62 $viewer = $user['id'] ?? null;
63 $token = $_SESSION['token'] ?? null;
64 [$known, $friends, $friendsError] = leaderboardKnownIdentities($store, $user, $token);
65 if ($scope === 'friends') {
66 if (!$user) {
67 jsonOut([
68 'ok' => true,
69 'scope' => 'friends',
70 'login_required' => true,
71 'players' => [],
72 'me' => null,
73 ]);
74 }
75 if ($friendsError !== null && $friends === []) {
76 jsonFail('De vriendenlijst is nu niet beschikbaar.', 502);
77 }
78 jsonOut([
79 'ok' => true,
80 'scope' => 'friends',
81 'login_required' => false,
82 'players' => $store->applyIdentities(
83 $store->friendsLeaderboard($friends, 10, $viewer),
84 $known
85 ),
86 'me' => $viewer,
87 ]);
88 }
89 jsonOut([
90 'ok' => true,
91 'scope' => 'global',
92 'login_required' => false,
93 'players' => $store->applyIdentities($store->topPlayers(10, $viewer), $known),
94 'me' => $viewer,
95 ]);
96 }
97
98 if ($action === 'test_login') {
99 if (!localDev()) {
100 jsonFail('Niet beschikbaar.', 403);
101 }
102 requireCsrf();
103 $who = (string) ($body['user'] ?? $_GET['user'] ?? '');
104 if (!in_array($who, ['test_user_1', 'test_user_2'], true)) {
105 jsonFail('Onbekende testgebruiker.');
106 }
107 loginUser('local:' . $who, $who);
108 jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]);
109 }
110
111 if (!$user && $action !== 'me') {
112 jsonFail('Log in om verder te gaan.', 401);
113 }
114
115 requireCsrf();
116 $store = Store::open();
117 $userId = $user['id'];
118 $name = $user['name'];
119
120 switch ($action) {
121 case 'lobby_create':
122 $miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;
123 $planet = (string) ($body['planet_id'] ?? '');
124 $lobby = $store->createLobby($userId, $name, $miss, $planet);
125 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
126
127 case 'lobby_join':
128 $code = (string) ($body['code'] ?? '');
129 $planet = (string) ($body['planet_id'] ?? '');
130 $lobby = $store->joinLobby($userId, $name, $code, $planet);
131 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
132
133 case 'lobby_set_planet':
134 $planet = (string) ($body['planet_id'] ?? '');
135 $lobby = $store->setPlanet($userId, $planet);
136 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
137
138 case 'lobby_leave':
139 $store->leaveLobby($userId);
140 jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]);
141
142 case 'lobby_ready':
143 $ready = (bool) ($body['ready'] ?? true);
144 $lobby = $store->setReady($userId, $ready);
145 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
146
147 case 'lobby_start':
148 $lobby = $store->startGame($userId);
149 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
150
151 case 'poll':
152 $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null;
153 $score = isset($body['score']) ? (int) $body['score'] : null;
154 $eliminated = !empty($body['eliminated']);
155 $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : [];
156 $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing);
157 $profile = $store->profilePayload($userId);
158 jsonOut([
159 'ok' => true,
160 'csrf' => csrfToken(),
161 'me' => $user,
162 'lobby' => $view['lobby'],
163 'incoming' => $view['incoming'],
164 'shot_results' => $view['shot_results'],
165 'shot_flights' => $view['shot_flights'] ?? [],
166 'lifetime_points' => $profile['lifetime_points'],
167 'locale' => $profile['locale'],
168 'last_planet' => $profile['last_planet'],
169 'blocks' => $profile['blocks'],
170 'planets' => $profile['planets'],
171 ]);
172
173 case 'play_begin':
174 $mode = (string) ($body['mode'] ?? 'solo');
175 $ses = $store->playBegin($userId, $mode);
176 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'token' => $ses['token']]);
177
178 case 'play_finish':
179 $token = (string) ($body['token'] ?? '');
180 $score = isset($body['score']) ? (int) $body['score'] : 0;
181 $result = $store->playFinish($userId, $token, $score);
182 jsonOut([
183 'ok' => true,
184 'csrf' => csrfToken(),
185 'lifetime_points' => $result['lifetime_points'],
186 'awarded' => $result['awarded'],
187 'already' => $result['already'],
188 'blocks' => Blocks::present($result['lifetime_points']),
189 ]);
190
191 case 'planet_save':
192 $snap = $store->saveCustomPlanet($userId, is_array($body['planet'] ?? null) ? $body['planet'] : $body);
193 jsonOut([
194 'ok' => true,
195 'csrf' => csrfToken(),
196 'planet' => $snap,
197 'planets' => $store->listCustomPlanets($userId),
198 ]);
199
200 case 'planet_delete':
201 $store->deleteCustomPlanet($userId, (string) ($body['id'] ?? $body['planet_id'] ?? ''));
202 jsonOut([
203 'ok' => true,
204 'csrf' => csrfToken(),
205 'planets' => $store->listCustomPlanets($userId),
206 ]);
207
208 case 'set_last_planet':
209 $pid = $store->setLastPlanet($userId, (string) ($body['planet_id'] ?? ''));
210 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'last_planet' => $pid]);
211
212 case 'set_locale':
213 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
214 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
215
216 default:
217 jsonFail('Onbekende actie.', 404);
218 }
219} catch (RuntimeException $e) {
220 jsonFail($e->getMessage(), 400);
221} catch (Throwable $e) {
222 jsonFail('Interne fout.', 500);
223}