BlueGit

projects-timfalken-com meteors

Files

meteors / server / api.php

api.php

api.php 3.8 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 $lobby = $store->createLobby($userId, $name, $miss);
66 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
67
68 case 'lobby_join':
69 $code = (string) ($body['code'] ?? '');
70 $lobby = $store->joinLobby($userId, $name, $code);
71 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
72
73 case 'lobby_leave':
74 $store->leaveLobby($userId);
75 jsonOut(['ok' => true, 'lobby' => null, 'csrf' => csrfToken()]);
76
77 case 'lobby_ready':
78 $ready = (bool) ($body['ready'] ?? true);
79 $lobby = $store->setReady($userId, $ready);
80 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
81
82 case 'lobby_start':
83 $lobby = $store->startGame($userId);
84 jsonOut(['ok' => true, 'lobby' => $store->presentLobby($lobby, $userId), 'csrf' => csrfToken()]);
85
86 case 'poll':
87 $snapshot = isset($body['snapshot']) && is_array($body['snapshot']) ? $body['snapshot'] : null;
88 $score = isset($body['score']) ? (int) $body['score'] : null;
89 $eliminated = !empty($body['eliminated']);
90 $outgoing = isset($body['outgoing_shots']) && is_array($body['outgoing_shots']) ? $body['outgoing_shots'] : [];
91 $view = $store->viewFor($userId, $snapshot, $score, $eliminated, $outgoing);
92 jsonOut([
93 'ok' => true,
94 'csrf' => csrfToken(),
95 'me' => $user,
96 'lobby' => $view['lobby'],
97 'incoming' => $view['incoming'],
98 'shot_results' => $view['shot_results'],
99 ]);
100
101 default:
102 jsonFail('Onbekende actie.', 404);
103 }
104} catch (RuntimeException $e) {
105 jsonFail($e->getMessage(), 400);
106} catch (Throwable $e) {
107 jsonFail('Interne fout.', 500);
108}