BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

api.php 4.2 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' => []];
21 if ($user) {
22 $view = Store::open()->viewFor($user['id']);
23 }
24 jsonOut([
25 'ok' => true,
26 'configured' => $configured,
27 'local' => $local,
28 'csrf' => csrfToken(),
29 'me' => $user,
30 'lobby' => $view['lobby'],
31 'incoming' => $view['incoming'],
32 'shot_results' => $view['shot_results'],
33 'miss_chance_default' => METEORS_DEFAULT_MISS,
34 'flight_duration' => METEORS_FLIGHT_DURATION,
35 'login_url' => 'auth.php',
36 'logout_url' => 'logout.php',
37 ]);
38 }
39
40 if ($action === 'test_login') {
41 if (!localDev()) {
42 jsonFail('Niet beschikbaar.', 403);
43 }
44 requireCsrf();
45 $who = (string) ($body['user'] ?? $_GET['user'] ?? '');
46 if (!in_array($who, ['test_user_1', 'test_user_2'], true)) {
47 jsonFail('Onbekende testgebruiker.');
48 }
49 loginUser('local:' . $who, $who);
50 jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]);
51 }
52
53 if (!$user && $action !== 'me') {
54 jsonFail('Log in om verder te gaan.', 401);
55 }
56
57 requireCsrf();
58 $store = Store::open();
59 $userId = $user['id'];
60 $name = $user['name'];
61
62 switch ($action) {
63 case 'lobby_create':
64 $miss = isset($body['miss_chance']) ? (float) $body['miss_chance'] : METEORS_DEFAULT_MISS;
65 $planet = (string) ($body['planet_id'] ?? '');
66 $lobby = $store->createLobby($userId, $name, $miss, $planet);
67 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
68
69 case 'lobby_join':
70 $code = (string) ($body['code'] ?? '');
71 $planet = (string) ($body['planet_id'] ?? '');
72 $lobby = $store->joinLobby($userId, $name, $code, $planet);
73 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
74
75 case 'lobby_set_planet':
76 $planet = (string) ($body['planet_id'] ?? '');
77 $lobby = $store->setPlanet($userId, $planet);
78 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
79
80 case 'lobby_leave':
81 $store->leaveLobby($userId);
82 jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]);
83
84 case 'lobby_ready':
85 $ready = (bool) ($body['ready'] ?? true);
86 $lobby = $store->setReady($userId, $ready);
87 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
88
89 case 'lobby_start':
90 $lobby = $store->startGame($userId);
91 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
92
93 case 'poll':
94 $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null;
95 $score = isset($body['score']) ? (int) $body['score'] : null;
96 $eliminated = !empty($body['eliminated']);
97 $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : [];
98 $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing);
99 jsonOut([
100 'ok' => true,
101 'csrf' => csrfToken(),
102 'me' => $user,
103 'lobby' => $view['lobby'],
104 'incoming' => $view['incoming'],
105 'shot_results' => $view['shot_results'],
106 ]);
107
108 default:
109 jsonFail('Onbekende actie.', 404);
110 }
111} catch (RuntimeException $e) {
112 jsonFail($e->getMessage(), 400);
113} catch (Throwable $e) {
114 jsonFail('Interne fout.', 500);
115}