BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

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