BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

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