| 1 | <?php |
| 2 | |
| 3 | class SaveManager |
| 4 | { |
| 5 | private $basePath; |
| 6 | |
| 7 | public function __construct() |
| 8 | { |
| 9 | $this->basePath = __DIR__ . '/../../data/saves'; |
| 10 | $this->ensureDirectories(); |
| 11 | } |
| 12 | |
| 13 | private function ensureDirectories() |
| 14 | { |
| 15 | if (!is_dir($this->basePath)) { |
| 16 | mkdir($this->basePath, 0755, true); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Get user saves directory |
| 22 | */ |
| 23 | public function getUserSavesPath($userId) |
| 24 | { |
| 25 | $path = $this->basePath . '/' . intval($userId); |
| 26 | if (!is_dir($path)) { |
| 27 | mkdir($path, 0755, true); |
| 28 | } |
| 29 | return $path; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get specific save directory |
| 34 | */ |
| 35 | public function getSavePath($userId, $saveId) |
| 36 | { |
| 37 | $path = $this->getUserSavesPath($userId) . '/' . $saveId; |
| 38 | if (!is_dir($path)) { |
| 39 | mkdir($path, 0755, true); |
| 40 | } |
| 41 | return $path; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get kobold sprites directory |
| 46 | */ |
| 47 | public function getKoboldSpritePath($userId, $koboldId) |
| 48 | { |
| 49 | $path = $this->basePath . '/' . intval($userId) . '/kobolds/' . intval($koboldId) . '/sprites'; |
| 50 | if (!is_dir($path)) { |
| 51 | mkdir($path, 0755, true); |
| 52 | } |
| 53 | return $path; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Save data to JSON file |
| 58 | */ |
| 59 | public function saveJson($filePath, $data) |
| 60 | { |
| 61 | $dir = dirname($filePath); |
| 62 | if (!is_dir($dir)) { |
| 63 | mkdir($dir, 0755, true); |
| 64 | } |
| 65 | |
| 66 | $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 67 | if ($json === false) { |
| 68 | throw new \Exception('Failed to encode JSON: ' . json_last_error_msg()); |
| 69 | } |
| 70 | |
| 71 | $written = file_put_contents($filePath, $json); |
| 72 | if ($written === false) { |
| 73 | throw new \Exception("Failed to write file: {$filePath}"); |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Load data from JSON file |
| 81 | */ |
| 82 | public function loadJson($filePath) |
| 83 | { |
| 84 | if (!file_exists($filePath)) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | $contents = file_get_contents($filePath); |
| 89 | if ($contents === false) { |
| 90 | throw new \Exception("Failed to read file: {$filePath}"); |
| 91 | } |
| 92 | |
| 93 | $data = json_decode($contents, true); |
| 94 | if ($data === null && json_last_error() !== JSON_ERROR_NONE) { |
| 95 | throw new \Exception('Failed to decode JSON: ' . json_last_error_msg()); |
| 96 | } |
| 97 | |
| 98 | return $data; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Delete a save and all its data |
| 103 | */ |
| 104 | public function deleteSave($userId, $saveId) |
| 105 | { |
| 106 | $path = $this->getSavePath($userId, $saveId); |
| 107 | return $this->deleteDirectory($path); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Delete directory recursively |
| 112 | */ |
| 113 | private function deleteDirectory($path) |
| 114 | { |
| 115 | if (!is_dir($path)) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | $files = scandir($path); |
| 120 | foreach ($files as $file) { |
| 121 | if ($file === '.' || $file === '..') { |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $filePath = $path . '/' . $file; |
| 126 | if (is_dir($filePath)) { |
| 127 | $this->deleteDirectory($filePath); |
| 128 | } else { |
| 129 | unlink($filePath); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return rmdir($path); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * List all saves for a user |
| 138 | */ |
| 139 | public function listUserSaves($userId) |
| 140 | { |
| 141 | $savesPath = $this->getUserSavesPath($userId); |
| 142 | $saves = []; |
| 143 | |
| 144 | $dirs = scandir($savesPath); |
| 145 | foreach ($dirs as $dir) { |
| 146 | if ($dir === '.' || $dir === '..' || $dir === 'kobolds') { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | $saveDataPath = $savesPath . '/' . $dir . '/save.json'; |
| 151 | if (file_exists($saveDataPath)) { |
| 152 | $data = $this->loadJson($saveDataPath); |
| 153 | if ($data) { |
| 154 | $data['id'] = $dir; |
| 155 | $saves[] = $data; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Sort by last_played descending |
| 161 | usort($saves, function ($a, $b) { |
| 162 | $timeA = strtotime($a['last_played'] ?? '2000-01-01'); |
| 163 | $timeB = strtotime($b['last_played'] ?? '2000-01-01'); |
| 164 | return $timeB - $timeA; |
| 165 | }); |
| 166 | |
| 167 | return $saves; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Generate unique save ID |
| 172 | */ |
| 173 | public function generateSaveId($userId) |
| 174 | { |
| 175 | $savesPath = $this->getUserSavesPath($userId); |
| 176 | $id = 1; |
| 177 | |
| 178 | while (is_dir($savesPath . '/' . $id)) { |
| 179 | $id++; |
| 180 | } |
| 181 | |
| 182 | return $id; |
| 183 | } |
| 184 | } |