BlueGit

blue-star tribegen

Files

tribegen / web / src / System / SaveManager.php

SaveManager.php

initial generation
timfa Tim · pushed by timfa · 9 months ago
0b655138 History
SaveManager.php 4.4 KiB Download Raw
1<?php
2
3namespace TribeGen\System;
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 . '/' . intval($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 getKoboldSpritePath($userId, $koboldId)
50 {
51 $path = $this->basePath . '/' . intval($userId) . '/kobolds/' . intval($koboldId) . '/sprites';
52 if (!is_dir($path)) {
53 mkdir($path, 0755, true);
54 }
55 return $path;
56 }
57
58 /**
59 * Save data to JSON file
60 */
61 public function saveJson($filePath, $data)
62 {
63 $dir = dirname($filePath);
64 if (!is_dir($dir)) {
65 mkdir($dir, 0755, true);
66 }
67
68 $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
69 if ($json === false) {
70 throw new \Exception('Failed to encode JSON: ' . json_last_error_msg());
71 }
72
73 $written = file_put_contents($filePath, $json);
74 if ($written === false) {
75 throw new \Exception("Failed to write file: {$filePath}");
76 }
77
78 return true;
79 }
80
81 /**
82 * Load data from JSON file
83 */
84 public function loadJson($filePath)
85 {
86 if (!file_exists($filePath)) {
87 return null;
88 }
89
90 $contents = file_get_contents($filePath);
91 if ($contents === false) {
92 throw new \Exception("Failed to read file: {$filePath}");
93 }
94
95 $data = json_decode($contents, true);
96 if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
97 throw new \Exception('Failed to decode JSON: ' . json_last_error_msg());
98 }
99
100 return $data;
101 }
102
103 /**
104 * Delete a save and all its data
105 */
106 public function deleteSave($userId, $saveId)
107 {
108 $path = $this->getSavePath($userId, $saveId);
109 return $this->deleteDirectory($path);
110 }
111
112 /**
113 * Delete directory recursively
114 */
115 private function deleteDirectory($path)
116 {
117 if (!is_dir($path)) {
118 return true;
119 }
120
121 $files = scandir($path);
122 foreach ($files as $file) {
123 if ($file === '.' || $file === '..') {
124 continue;
125 }
126
127 $filePath = $path . '/' . $file;
128 if (is_dir($filePath)) {
129 $this->deleteDirectory($filePath);
130 } else {
131 unlink($filePath);
132 }
133 }
134
135 return rmdir($path);
136 }
137
138 /**
139 * List all saves for a user
140 */
141 public function listUserSaves($userId)
142 {
143 $savesPath = $this->getUserSavesPath($userId);
144 $saves = [];
145
146 $dirs = scandir($savesPath);
147 foreach ($dirs as $dir) {
148 if ($dir === '.' || $dir === '..' || $dir === 'kobolds') {
149 continue;
150 }
151
152 $saveDataPath = $savesPath . '/' . $dir . '/save.json';
153 if (file_exists($saveDataPath)) {
154 $data = $this->loadJson($saveDataPath);
155 if ($data) {
156 $data['id'] = $dir;
157 $saves[] = $data;
158 }
159 }
160 }
161
162 // Sort by last_played descending
163 usort($saves, function ($a, $b) {
164 $timeA = strtotime($a['last_played'] ?? '2000-01-01');
165 $timeB = strtotime($b['last_played'] ?? '2000-01-01');
166 return $timeB - $timeA;
167 });
168
169 return $saves;
170 }
171
172 /**
173 * Generate unique save ID
174 */
175 public function generateSaveId($userId)
176 {
177 $savesPath = $this->getUserSavesPath($userId);
178 $id = 1;
179
180 while (is_dir($savesPath . '/' . $id)) {
181 $id++;
182 }
183
184 return $id;
185 }
186}