BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

api.php 7.0 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 $view = $store->viewFor($user['id']);
31 $profile = $store->profilePayload($user['id']);
32 }
33 jsonOut([
34 'ok' => true,
35 'configured' => $configured,
36 'local' => $local,
37 'csrf' => csrfToken(),
38 'me' => $user,
39 'lobby' => $view['lobby'],
40 'incoming' => $view['incoming'],
41 'shot_results' => $view['shot_results'],
42 'shot_flights' => $view['shot_flights'] ?? [],
43 'miss_chance_default' => METEORS_DEFAULT_MISS,
44 'flight_duration' => METEORS_FLIGHT_DURATION,
45 'login_url' => 'auth.php',
46 'logout_url' => 'logout.php',
47 'lifetime_points' => $profile['lifetime_points'],
48 'locale' => $profile['locale'],
49 'last_planet' => $profile['last_planet'],
50 'blocks' => $profile['blocks'],
51 'planets' => $profile['planets'],
52 ]);
53 }
54
55 if ($action === 'test_login') {
56 if (!localDev()) {
57 jsonFail('Niet beschikbaar.', 403);
58 }
59 requireCsrf();
60 $who = (string) ($body['user'] ?? $_GET['user'] ?? '');
61 if (!in_array($who, ['test_user_1', 'test_user_2'], true)) {
62 jsonFail('Onbekende testgebruiker.');
63 }
64 loginUser('local:' . $who, $who);
65 jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]);
66 }
67
68 if (!$user && $action !== 'me') {
69 jsonFail('Log in om verder te gaan.', 401);
70 }
71
72 requireCsrf();
73 $store = Store::open();
74 $userId = $user['id'];
75 $name = $user['name'];
76
77 switch ($action) {
78 case 'lobby_create':
79 $miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;
80 $planet = (string) ($body['planet_id'] ?? '');
81 $lobby = $store->createLobby($userId, $name, $miss, $planet);
82 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
83
84 case 'lobby_join':
85 $code = (string) ($body['code'] ?? '');
86 $planet = (string) ($body['planet_id'] ?? '');
87 $lobby = $store->joinLobby($userId, $name, $code, $planet);
88 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
89
90 case 'lobby_set_planet':
91 $planet = (string) ($body['planet_id'] ?? '');
92 $lobby = $store->setPlanet($userId, $planet);
93 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
94
95 case 'lobby_leave':
96 $store->leaveLobby($userId);
97 jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]);
98
99 case 'lobby_ready':
100 $ready = (bool) ($body['ready'] ?? true);
101 $lobby = $store->setReady($userId, $ready);
102 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
103
104 case 'lobby_start':
105 $lobby = $store->startGame($userId);
106 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
107
108 case 'poll':
109 $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null;
110 $score = isset($body['score']) ? (int) $body['score'] : null;
111 $eliminated = !empty($body['eliminated']);
112 $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : [];
113 $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing);
114 $profile = $store->profilePayload($userId);
115 jsonOut([
116 'ok' => true,
117 'csrf' => csrfToken(),
118 'me' => $user,
119 'lobby' => $view['lobby'],
120 'incoming' => $view['incoming'],
121 'shot_results' => $view['shot_results'],
122 'shot_flights' => $view['shot_flights'] ?? [],
123 'lifetime_points' => $profile['lifetime_points'],
124 'locale' => $profile['locale'],
125 'last_planet' => $profile['last_planet'],
126 'blocks' => $profile['blocks'],
127 'planets' => $profile['planets'],
128 ]);
129
130 case 'play_begin':
131 $mode = (string) ($body['mode'] ?? 'solo');
132 $ses = $store->playBegin($userId, $mode);
133 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'token' => $ses['token']]);
134
135 case 'play_finish':
136 $token = (string) ($body['token'] ?? '');
137 $score = isset($body['score']) ? (int) $body['score'] : 0;
138 $result = $store->playFinish($userId, $token, $score);
139 jsonOut([
140 'ok' => true,
141 'csrf' => csrfToken(),
142 'lifetime_points' => $result['lifetime_points'],
143 'awarded' => $result['awarded'],
144 'already' => $result['already'],
145 'blocks' => Blocks::present($result['lifetime_points']),
146 ]);
147
148 case 'planet_save':
149 $snap = $store->saveCustomPlanet($userId, is_array($body['planet'] ?? null) ? $body['planet'] : $body);
150 jsonOut([
151 'ok' => true,
152 'csrf' => csrfToken(),
153 'planet' => $snap,
154 'planets' => $store->listCustomPlanets($userId),
155 ]);
156
157 case 'planet_delete':
158 $store->deleteCustomPlanet($userId, (string) ($body['id'] ?? $body['planet_id'] ?? ''));
159 jsonOut([
160 'ok' => true,
161 'csrf' => csrfToken(),
162 'planets' => $store->listCustomPlanets($userId),
163 ]);
164
165 case 'set_last_planet':
166 $pid = $store->setLastPlanet($userId, (string) ($body['planet_id'] ?? ''));
167 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'last_planet' => $pid]);
168
169 case 'set_locale':
170 $locale = $store->setLocale($userId, (string) ($body['locale'] ?? ''));
171 jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]);
172
173 default:
174 jsonFail('Onbekende actie.', 404);
175 }
176} catch (RuntimeException $e) {
177 jsonFail($e->getMessage(), 400);
178} catch (Throwable $e) {
179 jsonFail('Interne fout.', 500);
180}