BlueGit

blue-star tribegen

Files

tribegen / web / src / System / SaveManager.php

SaveManager.php

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