| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . '/../../vendor/autoload.php'; |
| 4 | require_once __DIR__ . '/../../src/Database/Database.php'; |
| 5 | require_once __DIR__ . '/../../src/Classes/User.php'; |
| 6 | require_once __DIR__ . '/../../src/Classes/Tribe.php'; |
| 7 | require_once __DIR__ . '/../../src/Classes/Kobold.php'; |
| 8 | require_once __DIR__ . '/../../src/System/SaveManager.php'; |
| 9 | |
| 10 | $method = $_SERVER['REQUEST_METHOD']; |
| 11 | |
| 12 | if ($method === 'POST') { |
| 13 | $input = json_decode(file_get_contents('php://input'), true); |
| 14 | |
| 15 | $apiPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 16 | |
| 17 | if (strpos($apiPath, 'generation') !== false) { |
| 18 | // Generate initial kobolds for new game |
| 19 | try { |
| 20 | $userId = $input['userId'] ?? null; |
| 21 | if (!$userId) { |
| 22 | throw new \Exception('User ID required'); |
| 23 | } |
| 24 | |
| 25 | $tribe = Tribe::create($userId, $input['saveName']); |
| 26 | |
| 27 | // Generate 20 random kobolds |
| 28 | $kobolds = []; |
| 29 | for ($i = 0; $i < 20; $i++) { |
| 30 | $kobold = Kobold::generate($userId, $tribe->getSaveId()); |
| 31 | $kobold->setName('Kobold ' . ($i + 1)); |
| 32 | $tribe->addKobold($kobold); |
| 33 | |
| 34 | $kobolds[] = [ |
| 35 | 'id' => $kobold->getId(), |
| 36 | 'name' => $kobold->getName(), |
| 37 | 'age' => $kobold->getAge(), |
| 38 | 'stats' => [ |
| 39 | 'vitality' => $kobold->getStat('vitality'), |
| 40 | 'intelligence' => $kobold->getStat('intelligence'), |
| 41 | 'tinkering' => $kobold->getStat('tinkering'), |
| 42 | 'medicine' => $kobold->getStat('medicine'), |
| 43 | 'crafting' => $kobold->getStat('crafting'), |
| 44 | 'personality' => $kobold->getStat('personality'), |
| 45 | 'will' => $kobold->getStat('will') |
| 46 | ] |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | $tribe->save(); |
| 51 | |
| 52 | echo json_encode([ |
| 53 | 'success' => true, |
| 54 | 'save' => [ |
| 55 | 'id' => $tribe->getSaveId(), |
| 56 | 'name' => $input['saveName'] |
| 57 | ], |
| 58 | 'kobolds' => $kobolds |
| 59 | ]); |
| 60 | } catch (\Exception $e) { |
| 61 | http_response_code(400); |
| 62 | echo json_encode([ |
| 63 | 'success' => false, |
| 64 | 'error' => $e->getMessage() |
| 65 | ]); |
| 66 | } |
| 67 | } elseif (strpos($apiPath, 'save') !== false && strpos($apiPath, 'endMonth') === false) { |
| 68 | // Save tribe setup |
| 69 | try { |
| 70 | $userId = $input['userId'] ?? null; |
| 71 | $saveId = $input['saveId'] ?? null; |
| 72 | |
| 73 | if (!$userId || !$saveId) { |
| 74 | throw new \Exception('User ID and Save ID required'); |
| 75 | } |
| 76 | |
| 77 | $tribe = Tribe::load($userId, $saveId); |
| 78 | |
| 79 | // TODO: Process leader and members selection |
| 80 | // For now just save |
| 81 | $tribe->save(); |
| 82 | |
| 83 | echo json_encode([ |
| 84 | 'success' => true, |
| 85 | 'tribe' => [] |
| 86 | ]); |
| 87 | } catch (\Exception $e) { |
| 88 | http_response_code(400); |
| 89 | echo json_encode([ |
| 90 | 'success' => false, |
| 91 | 'error' => $e->getMessage() |
| 92 | ]); |
| 93 | } |
| 94 | } elseif (strpos($apiPath, 'event') !== false) { |
| 95 | // Execute an event |
| 96 | try { |
| 97 | $userId = $input['userId'] ?? null; |
| 98 | $saveId = $input['saveId'] ?? null; |
| 99 | |
| 100 | if (!$userId || !$saveId) { |
| 101 | throw new \Exception('User ID and Save ID required'); |
| 102 | } |
| 103 | |
| 104 | $tribe = Tribe::load($userId, $saveId); |
| 105 | |
| 106 | // TODO: Execute event and get story |
| 107 | $story = "An event occurred in the tribe..."; |
| 108 | |
| 109 | echo json_encode([ |
| 110 | 'success' => true, |
| 111 | 'story' => $story |
| 112 | ]); |
| 113 | } catch (\Exception $e) { |
| 114 | http_response_code(400); |
| 115 | echo json_encode([ |
| 116 | 'success' => false, |
| 117 | 'error' => $e->getMessage() |
| 118 | ]); |
| 119 | } |
| 120 | } elseif (strpos($apiPath, 'endMonth') !== false) { |
| 121 | // End the current month |
| 122 | try { |
| 123 | $userId = $input['userId'] ?? null; |
| 124 | $saveId = $input['saveId'] ?? null; |
| 125 | |
| 126 | if (!$userId || !$saveId) { |
| 127 | throw new \Exception('User ID and Save ID required'); |
| 128 | } |
| 129 | |
| 130 | $tribe = Tribe::load($userId, $saveId); |
| 131 | $foodStory = $tribe->endMonth(); |
| 132 | |
| 133 | echo json_encode([ |
| 134 | 'success' => true, |
| 135 | 'save' => [ |
| 136 | 'id' => $tribe->getSaveId(), |
| 137 | 'current_month' => $tribe->getCurrentMonth(), |
| 138 | 'current_season' => $tribe->getCurrentSeason(), |
| 139 | 'food_count' => $tribe->getFoodCount() |
| 140 | ], |
| 141 | 'monthStory' => $foodStory |
| 142 | ]); |
| 143 | } catch (\Exception $e) { |
| 144 | http_response_code(400); |
| 145 | echo json_encode([ |
| 146 | 'success' => false, |
| 147 | 'error' => $e->getMessage() |
| 148 | ]); |
| 149 | } |
| 150 | } |
| 151 | } elseif ($method === 'GET') { |
| 152 | $apiPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); |
| 153 | |
| 154 | if (strpos($apiPath, 'sprite') !== false) { |
| 155 | // Serve a kobold sprite image |
| 156 | try { |
| 157 | $userId = $_GET['userId'] ?? null; |
| 158 | $koboldId = $_GET['koboldId'] ?? null; |
| 159 | |
| 160 | if (!$userId || !$koboldId) { |
| 161 | throw new \Exception('User ID and Kobold ID required'); |
| 162 | } |
| 163 | |
| 164 | $saveManager = new SaveManager(); |
| 165 | $spritePath = $saveManager->getKoboldSpritePath($userId, $koboldId); |
| 166 | $ageSprite = $_GET['age'] ?? 'adult'; |
| 167 | $spriteFile = $spritePath . '/' . $ageSprite . '.png'; |
| 168 | |
| 169 | if (!file_exists($spriteFile)) { |
| 170 | throw new \Exception('Sprite not found'); |
| 171 | } |
| 172 | |
| 173 | // Serve the sprite image |
| 174 | header('Content-Type: image/png'); |
| 175 | header('Cache-Control: public, max-age=3600'); |
| 176 | readfile($spriteFile); |
| 177 | exit; |
| 178 | } catch (\Exception $e) { |
| 179 | http_response_code(404); |
| 180 | echo json_encode([ |
| 181 | 'success' => false, |
| 182 | 'error' => $e->getMessage() |
| 183 | ]); |
| 184 | } |
| 185 | } elseif (strpos($apiPath, 'list') !== false) { |
| 186 | // List saves for user |
| 187 | try { |
| 188 | $userId = $_GET['userId'] ?? $_SESSION['user_id'] ?? null; |
| 189 | |
| 190 | if (!$userId) { |
| 191 | throw new \Exception('Not authenticated'); |
| 192 | } |
| 193 | |
| 194 | $user = new User($userId, ''); |
| 195 | $saves = $user->getSaves(); |
| 196 | |
| 197 | echo json_encode([ |
| 198 | 'success' => true, |
| 199 | 'saves' => $saves |
| 200 | ]); |
| 201 | } catch (\Exception $e) { |
| 202 | http_response_code(400); |
| 203 | echo json_encode([ |
| 204 | 'success' => false, |
| 205 | 'error' => $e->getMessage() |
| 206 | ]); |
| 207 | } |
| 208 | } elseif (strpos($apiPath, 'load') !== false) { |
| 209 | // Load a specific game save |
| 210 | try { |
| 211 | $userId = $_GET['userId'] ?? $_SESSION['user_id'] ?? null; |
| 212 | $saveId = $_GET['saveId'] ?? null; |
| 213 | |
| 214 | if (!$userId || !$saveId) { |
| 215 | throw new \Exception('User ID and Save ID required'); |
| 216 | } |
| 217 | |
| 218 | $tribe = Tribe::load($userId, $saveId); |
| 219 | |
| 220 | // Get kobolds |
| 221 | $kobolds = []; |
| 222 | foreach ($tribe->getKobolds() as $kobold) { |
| 223 | $kobolds[] = [ |
| 224 | 'id' => $kobold->getId(), |
| 225 | 'name' => $kobold->getName(), |
| 226 | 'age' => $kobold->getAge(), |
| 227 | 'ageCategory' => $kobold->getAgeCategory() |
| 228 | ]; |
| 229 | } |
| 230 | |
| 231 | echo json_encode([ |
| 232 | 'success' => true, |
| 233 | 'save' => [ |
| 234 | 'id' => $tribe->getSaveId(), |
| 235 | 'name' => $tribe->getName(), |
| 236 | 'current_month' => $tribe->getCurrentMonth(), |
| 237 | 'current_season' => $tribe->getCurrentSeason(), |
| 238 | 'food_count' => $tribe->getFoodCount(), |
| 239 | 'created_at' => $tribe->getCreatedAt(), |
| 240 | 'last_played' => $tribe->getLastPlayed() |
| 241 | ], |
| 242 | 'tribe' => [ |
| 243 | 'kobolds' => $kobolds |
| 244 | ] |
| 245 | ]); |
| 246 | } catch (\Exception $e) { |
| 247 | http_response_code(400); |
| 248 | echo json_encode([ |
| 249 | 'success' => false, |
| 250 | 'error' => $e->getMessage() |
| 251 | ]); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |