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