BlueGit

blue-star tribegen

Files

tribegen / web / src / api / game.php

game.php

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