| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | require __DIR__ . '/lib/bootstrap.php'; |
| 4 | require __DIR__ . '/lib/Store.php'; |
| 5 | |
| 6 | $action = $_GET['action'] ?? $_POST['action'] ?? ''; |
| 7 | if ($action === '') { |
| 8 | $body = readJsonBody(); |
| 9 | $action = (string) ($body['action'] ?? ''); |
| 10 | } else { |
| 11 | $body = readJsonBody(); |
| 12 | } |
| 13 | |
| 14 | try { |
| 15 | $configured = authConfigured(); |
| 16 | $local = localDev(); |
| 17 | $user = currentUser(); |
| 18 | |
| 19 | if ($action === 'me' || $action === '') { |
| 20 | $games = []; |
| 21 | $locale = null; |
| 22 | if ($user) { |
| 23 | $store = Store::open(); |
| 24 | $games = $store->listFor($user['id']); |
| 25 | $locale = $store->locale($user['id']); |
| 26 | } |
| 27 | jsonOut([ |
| 28 | 'ok' => true, |
| 29 | 'configured' => $configured, |
| 30 | 'local' => $local, |
| 31 | 'csrf' => csrfToken(), |
| 32 | 'me' => $user, |
| 33 | 'games' => $games, |
| 34 | 'locale' => $locale, |
| 35 | 'login_url' => 'auth.php', |
| 36 | 'logout_url' => 'logout.php', |
| 37 | 'timezone' => Engine::TZ, |
| 38 | 'defaults' => Engine::defaultRules(), |
| 39 | 'server_now' => Engine::now(), |
| 40 | ]); |
| 41 | } |
| 42 | |
| 43 | if ($action === 'test_login') { |
| 44 | if (!localDev()) { |
| 45 | jsonFail('generic', 403); |
| 46 | } |
| 47 | requireCsrf(); |
| 48 | $who = (string) ($body['user'] ?? $_GET['user'] ?? ''); |
| 49 | if (!preg_match('/^test_user_([1-9]|1[0-6])$/', $who)) { |
| 50 | jsonFail('generic'); |
| 51 | } |
| 52 | loginUser('local:' . $who, $who, 'images/avatars/' . $who . '.svg'); |
| 53 | jsonOut(['ok' => true, 'me' => currentUser(), 'csrf' => csrfToken()]); |
| 54 | } |
| 55 | |
| 56 | if (!$user && $action !== 'me') { |
| 57 | jsonFail('login', 401); |
| 58 | } |
| 59 | |
| 60 | requireCsrf(); |
| 61 | $store = Store::open(); |
| 62 | $userId = $user['id']; |
| 63 | $name = $user['name']; |
| 64 | $avatar = $user['avatar'] ?? null; |
| 65 | |
| 66 | $gameId = (string) ($body['game'] ?? $body['game_id'] ?? $_GET['game'] ?? ''); |
| 67 | |
| 68 | switch ($action) { |
| 69 | case 'set_locale': |
| 70 | $locale = $store->setLocale($userId, (string) ($body['locale'] ?? '')); |
| 71 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'locale' => $locale]); |
| 72 | |
| 73 | case 'lobby_create': |
| 74 | $rules = is_array($body['rules'] ?? null) ? $body['rules'] : $body; |
| 75 | $game = $store->createLobby($userId, $name, $avatar, $rules); |
| 76 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 77 | |
| 78 | case 'lobby_join': |
| 79 | $code = (string) ($body['code'] ?? $_GET['code'] ?? ''); |
| 80 | $game = $store->joinByCode($userId, $name, $avatar, $code); |
| 81 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 82 | |
| 83 | case 'lobby_leave': |
| 84 | if ($gameId === '') { |
| 85 | jsonFail('unknown_code'); |
| 86 | } |
| 87 | $game = $store->leaveLobby($userId, $gameId); |
| 88 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $game ? $store->view($game, $userId) : null]); |
| 89 | |
| 90 | case 'lobby_rules': |
| 91 | if ($gameId === '') { |
| 92 | jsonFail('unknown_code'); |
| 93 | } |
| 94 | $rules = is_array($body['rules'] ?? null) ? $body['rules'] : $body; |
| 95 | $game = $store->setRules($userId, $gameId, $rules); |
| 96 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 97 | |
| 98 | case 'lobby_start': |
| 99 | if ($gameId === '') { |
| 100 | jsonFail('unknown_code'); |
| 101 | } |
| 102 | $game = $store->startGame($userId, $gameId); |
| 103 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 104 | |
| 105 | case 'act': |
| 106 | case 'game_action': |
| 107 | if ($gameId === '') { |
| 108 | jsonFail('unknown_code'); |
| 109 | } |
| 110 | $actionBody = is_array($body['move'] ?? null) ? $body['move'] : $body; |
| 111 | // Don't pass internal keys as the action. |
| 112 | unset($actionBody['action'], $actionBody['game'], $actionBody['game_id'], $actionBody['csrf']); |
| 113 | $game = $store->act($userId, $gameId, $actionBody); |
| 114 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 115 | |
| 116 | case 'note': |
| 117 | if ($gameId === '') { |
| 118 | jsonFail('unknown_code'); |
| 119 | } |
| 120 | $game = $store->setNote( |
| 121 | $userId, |
| 122 | $gameId, |
| 123 | (string) ($body['subject'] ?? $body['target'] ?? ''), |
| 124 | (string) ($body['text'] ?? $body['note'] ?? '') |
| 125 | ); |
| 126 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 127 | |
| 128 | case 'message': |
| 129 | case 'chat': |
| 130 | if ($gameId === '') { |
| 131 | jsonFail('unknown_code'); |
| 132 | } |
| 133 | $game = $store->addMessage( |
| 134 | $userId, |
| 135 | $gameId, |
| 136 | (string) ($body['to'] ?? $body['target'] ?? ''), |
| 137 | (string) ($body['text'] ?? $body['message'] ?? '') |
| 138 | ); |
| 139 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 140 | |
| 141 | case 'sync': |
| 142 | if ($gameId === '') { |
| 143 | jsonFail('unknown_code'); |
| 144 | } |
| 145 | $since = (int) ($body['v'] ?? $body['version'] ?? $_GET['v'] ?? $_GET['version'] ?? -1); |
| 146 | $payload = $store->sync($userId, $gameId, $since); |
| 147 | $payload['csrf'] = csrfToken(); |
| 148 | jsonOut($payload); |
| 149 | |
| 150 | case 'game': |
| 151 | case 'poll': |
| 152 | if ($gameId === '') { |
| 153 | jsonFail('unknown_code'); |
| 154 | } |
| 155 | $view = $store->present($userId, $gameId); |
| 156 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $view]); |
| 157 | |
| 158 | case 'games': |
| 159 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'games' => $store->listFor($userId)]); |
| 160 | |
| 161 | case 'debug_advance': |
| 162 | if (!localDev()) { |
| 163 | jsonFail('generic', 403); |
| 164 | } |
| 165 | if ($gameId === '') { |
| 166 | jsonFail('unknown_code'); |
| 167 | } |
| 168 | $days = isset($body['days']) ? (int) $body['days'] : 1; |
| 169 | $at = isset($body['at']) ? (int) $body['at'] : Engine::now(); |
| 170 | $game = $store->debugAdvance($userId, $gameId, $at, $days); |
| 171 | jsonOut(['ok' => true, 'csrf' => csrfToken(), 'game' => $store->view($game, $userId)]); |
| 172 | |
| 173 | default: |
| 174 | jsonFail('unknown_action'); |
| 175 | } |
| 176 | } catch (RuntimeException $e) { |
| 177 | $code = $e->getMessage(); |
| 178 | jsonFail($code, apiHttpStatus($code)); |
| 179 | } catch (Throwable $e) { |
| 180 | jsonFail('generic', 500); |
| 181 | } |
projects-timfalken-com allianties