BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

api.php 8.6 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 if ($scope === 'friends') {
64 if (!$user) {
65 jsonOut([
66 'ok' => true,
67 'scope' => 'friends',
68 'login_required' => true,
69 'players' => [],
70 'me' => null,
71 ]);
72 }
73 $friends = [];
74 $token = $_SESSION['token'] ?? null;
75 if (is_string($token) && $token !== '') {
76 try {
77 $friends = parseAuthFriends(authRequest('/api/friends', null, $token));
78 } catch (Throwable $e) {
79 jsonFail('De vriendenlijst is nu niet beschikbaar.', 502);
80 }
81 } elseif (localDev()) {
82 $friends = localTestFriends($user['id']);
83 }
84 jsonOut([
85 'ok' => true,
86 'scope' => 'friends',
87 'login_required' => false,
88 'players' => $store->friendsLeaderboard($friends, 10, $viewer),
89 'me' => $viewer,
90 ]);
91 }
92 jsonOut([
93 'ok' => true,
94 'scope' => 'global',
95 'login_required' => false,
96 'players' => $store->topPlayers(10, $viewer),
97 'me' => $viewer,
98 ]);
99 }
100
101 if ($action === 'test_login') {
102 if (!localDev()) {
103 jsonFail('Niet beschikbaar.', 403);
104 }
105 requireCsrf();
106 $who = (string) ($body['user'] ?? $_GET['user'] ?? '');
107 if (!in_array($who, ['test_user_1', 'test_user_2'], true)) {
108 jsonFail('Onbekende testgebruiker.');
109 }
110 loginUser('local:' . $who, $who);
111 jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]);
112 }
113
114 if (!$user && $action !== 'me') {
115 jsonFail('Log in om verder te gaan.', 401);
116 }
117
118 requireCsrf();
119 $store = Store::open();
120 $userId = $user['id'];
121 $name = $user['name'];
122
123 switch ($action) {
124 case 'lobby_create':
125 $miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;
126 $planet = (string) ($body['planet_id'] ?? '');
127 $lobby = $store->createLobby($userId, $name, $miss, $planet);
128 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
129
130 case 'lobby_join':
131 $code = (string) ($body['code'] ?? '');
132 $planet = (string) ($body['planet_id'] ?? '');
133 $lobby = $store->joinLobby($userId, $name, $code, $planet);
134 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
135
136 case 'lobby_set_planet':
137 $planet = (string) ($body['planet_id'] ?? '');
138 $lobby = $store->setPlanet($userId, $planet);
139 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
140
141 case 'lobby_leave':
142 $store->leaveLobby($userId);
143 jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]);
144
145 case 'lobby_ready':
146 $ready = (bool) ($body['ready'] ?? true);
147 $lobby = $store->setReady($userId, $ready);
148 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
149
150 case 'lobby_start':
151 $lobby = $store->startGame($userId);
152 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
153
154 case 'poll':
155 $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null;
156 $score = isset($body['score']) ? (int) $body['score'] : null;
157 $eliminated = !empty($body['eliminated']);
158 $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : [];
159 $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing);
160 $profile = $store->profilePayload($userId);
161 jsonOut([
162 'ok' => true,
163 'csrf' => csrfToken(),
164 'me' => $user,
165 'lobby' => $view['lobby'],
166 'incoming' => $view['incoming'],
167 'shot_results' => $view['shot_results'],
168 'shot_flights' => $view['shot_flights'] ?? [],
169 'lifetime_points' => $profile['lifetime_points'],
170 'locale' => $profile['locale'],
171 'last_planet' => $profile['last_planet'],
172 'blocks' => $profile['blocks'],
173 'planets' => $profile['planets'],
174 ]);
175
176 case 'play_begin':
177 $mode = (string) ($body['mode'] ?? 'solo');
178 $ses = $store->playBegin($userId, $mode);
179 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'token' => $ses['token']]);
180
181 case 'play_finish':
182 $token = (string) ($body['token'] ?? '');
183 $score = isset($body['score']) ? (int) $body['score'] : 0;
184 $result = $store->playFinish($userId, $token, $score);
185 jsonOut([
186 'ok' => true,
187 'csrf' => csrfToken(),
188 'lifetime_points' => $result['lifetime_points'],
189 'awarded' => $result['awarded'],
190 'already' => $result['already'],
191 'blocks' => Blocks::present($result['lifetime_points']),
192 ]);
193
194 case 'planet_save':
195 $snap = $store->saveCustomPlanet($userId, is_array($body['planet'] ?? null) ? $body['planet'] : $body);
196 jsonOut([
197 'ok' => true,
198 'csrf' => csrfToken(),
199 'planet' => $snap,
200 'planets' => $store->listCustomPlanets($userId),
201 ]);
202
203 case 'planet_delete':
204 $store->deleteCustomPlanet($userId, (string) ($body['id'] ?? $body['planet_id'] ?? ''));
205 jsonOut([
206 'ok' => true,
207 'csrf' => csrfToken(),
208 'planets' => $store->listCustomPlanets($userId),
209 ]);
210
211 case 'set_last_planet':
212 $pid = $store->setLastPlanet($userId, (string) ($body['planet_id'] ?? ''));
213 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'last_planet' => $pid]);
214
215 case 'set_locale':
216 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
217 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
218
219 default:
220 jsonFail('Onbekende actie.', 404);
221 }
222} catch (RuntimeException $e) {
223 jsonFail($e->getMessage(), 400);
224} catch (Throwable $e) {
225 jsonFail('Interne fout.', 500);
226}