GameConfigController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\DCache\ChestJsonConfig;
  4. use App\Module\Game\DCache\FarmHouseJsonConfig;
  5. use App\Module\Game\DCache\ItemJsonConfig;
  6. use App\Module\Game\DCache\PetJsonConfig;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Layout\Row;
  9. use Dcat\Admin\Widgets\Card;
  10. use Dcat\Admin\Widgets\Table;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Artisan;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. use Spatie\RouteAttributes\Attributes\Get;
  16. use Spatie\RouteAttributes\Attributes\Post;
  17. /**
  18. * 游戏配置表管理控制器
  19. *
  20. * 用于显示和管理游戏中的各种配置表
  21. */
  22. class GameConfigController extends AdminController
  23. {
  24. /**
  25. * 页面标题
  26. *
  27. * @var string
  28. */
  29. protected $title = '游戏配置表管理';
  30. /**
  31. * 页面描述
  32. *
  33. * @var string
  34. */
  35. protected $description = '查看和刷新游戏中的各种配置表';
  36. /**
  37. * 配置表首页
  38. *
  39. * @param Content $content
  40. * @return Content
  41. */
  42. public function index(Content $content)
  43. {
  44. return $content
  45. ->title($this->title)
  46. ->description($this->description)
  47. ->body(function (Row $row) {
  48. // 物品配置表卡片
  49. $row->column(6, $this->createConfigCard(
  50. '物品配置表',
  51. 'items.json',
  52. 'gameitems:generate-json',
  53. 'game/configs/refresh-items',
  54. $this->getItemConfigInfo()
  55. ));
  56. // 宝箱配置表卡片
  57. $row->column(6, $this->createConfigCard(
  58. '宝箱配置表',
  59. 'chest.json',
  60. 'gameitems:generate-chest-json',
  61. 'game/configs/refresh-chests',
  62. $this->getChestConfigInfo()
  63. ));
  64. })
  65. ->body(function (Row $row) {
  66. // 宠物配置表卡片
  67. $row->column(6, $this->createConfigCard(
  68. '宠物配置表',
  69. 'pet_config.json, pet_level_config.json, pet_skill_config.json',
  70. 'pet:generate-json',
  71. 'game/configs/refresh-pets',
  72. $this->getPetConfigInfo()
  73. ));
  74. // 农场房屋配置表卡片
  75. $row->column(6, $this->createConfigCard(
  76. '农场房屋配置表',
  77. 'farm_house.json',
  78. 'farm:generate-house-json',
  79. 'game/configs/refresh-farm-house',
  80. $this->getFarmHouseConfigInfo()
  81. ));
  82. });
  83. }
  84. /**
  85. * 创建配置表信息卡片
  86. *
  87. * @param string $title 卡片标题
  88. * @param string $filename 文件名
  89. * @param string $command 生成命令
  90. * @param string $refreshUrl 刷新URL
  91. * @param array $info 配置信息
  92. * @return Card
  93. */
  94. protected function createConfigCard($title, $filename, $command, $refreshUrl, $info)
  95. {
  96. $headers = ['属性', '值'];
  97. $rows = [];
  98. foreach ($info as $key => $value) {
  99. $rows[] = [$key, $value];
  100. }
  101. $card = new Card($title, Table::make($headers, $rows));
  102. $card->tool('<a href="' . admin_url($refreshUrl) . '" class="btn btn-primary btn-sm"><i class="fa fa-refresh"></i> 刷新配置</a>');
  103. $card->footer("<code>文件: {$filename}</code><br><code>命令: php artisan {$command}</code>");
  104. return $card;
  105. }
  106. /**
  107. * 获取物品配置表信息
  108. *
  109. * @return array
  110. */
  111. protected function getItemConfigInfo()
  112. {
  113. $data = ItemJsonConfig::getData();
  114. $info = [
  115. '生成时间' => $data['generated_at'] ?? '未知',
  116. '物品数量' => isset($data['items']) ? count($data['items']) : 0,
  117. '文件路径' => public_path('json/items.json'),
  118. ];
  119. return $info;
  120. }
  121. /**
  122. * 获取宝箱配置表信息
  123. *
  124. * @return array
  125. */
  126. protected function getChestConfigInfo()
  127. {
  128. $data = ChestJsonConfig::getData();
  129. $info = [
  130. '生成时间' => $data['generated_at'] ?? '未知',
  131. '宝箱数量' => isset($data['chest']) ? count($data['chest']) : 0,
  132. '文件路径' => public_path('json/chest.json'),
  133. ];
  134. return $info;
  135. }
  136. /**
  137. * 获取宠物配置表信息
  138. *
  139. * @return array
  140. */
  141. protected function getPetConfigInfo()
  142. {
  143. $data = PetJsonConfig::getData();
  144. $petConfig = $data['pet_config'] ?? [];
  145. $petLevelConfig = $data['pet_level_config'] ?? [];
  146. $petSkillConfig = $data['pet_skill_config'] ?? [];
  147. $info = [
  148. '生成时间' => $petConfig['generated_at'] ?? '未知',
  149. '宠物数量' => isset($petConfig['pets']) ? count($petConfig['pets']) : 0,
  150. '等级配置数量' => isset($petLevelConfig['pet_levels']) ? count($petLevelConfig['pet_levels']) : 0,
  151. '技能配置数量' => isset($petSkillConfig['pet_skills']) ? count($petSkillConfig['pet_skills']) : 0,
  152. ];
  153. return $info;
  154. }
  155. /**
  156. * 获取农场房屋配置表信息
  157. *
  158. * @return array
  159. */
  160. protected function getFarmHouseConfigInfo()
  161. {
  162. $data = FarmHouseJsonConfig::getData();
  163. $info = [
  164. '生成时间' => $data['generated_at'] ?? '未知',
  165. '房屋配置数量' => isset($data['house_configs']) ? count($data['house_configs']) : 0,
  166. '文件路径' => public_path('json/farm_house.json'),
  167. ];
  168. return $info;
  169. }
  170. /**
  171. * 刷新物品配置表
  172. *
  173. * @param Content $content
  174. * @return Content
  175. */
  176. #[Get('game/configs/refresh-items')]
  177. public function refreshItems(Content $content)
  178. {
  179. try {
  180. Artisan::call('gameitems:generate-json');
  181. $output = Artisan::output();
  182. // 强制刷新缓存
  183. ItemJsonConfig::getData([], true);
  184. admin_success('刷新成功', '物品配置表已成功刷新');
  185. return $content
  186. ->title($this->title)
  187. ->description('刷新物品配置表')
  188. ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
  189. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
  190. } catch (\Exception $e) {
  191. admin_error('刷新失败', '物品配置表刷新失败: ' . $e->getMessage());
  192. return $content
  193. ->title($this->title)
  194. ->description('刷新物品配置表')
  195. ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
  196. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
  197. }
  198. }
  199. /**
  200. * 刷新宝箱配置表
  201. *
  202. * @param Content $content
  203. * @return Content
  204. */
  205. #[Get('game/configs/refresh-chests')]
  206. public function refreshChests(Content $content)
  207. {
  208. try {
  209. Artisan::call('gameitems:generate-chest-json');
  210. $output = Artisan::output();
  211. // 强制刷新缓存
  212. ChestJsonConfig::getData([], true);
  213. admin_success('刷新成功', '宝箱配置表已成功刷新');
  214. return $content
  215. ->title($this->title)
  216. ->description('刷新宝箱配置表')
  217. ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
  218. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
  219. } catch (\Exception $e) {
  220. admin_error('刷新失败', '宝箱配置表刷新失败: ' . $e->getMessage());
  221. return $content
  222. ->title($this->title)
  223. ->description('刷新宝箱配置表')
  224. ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
  225. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
  226. }
  227. }
  228. /**
  229. * 刷新宠物配置表
  230. *
  231. * @param Content $content
  232. * @return Content
  233. */
  234. #[Get('game/configs/refresh-pets')]
  235. public function refreshPets(Content $content)
  236. {
  237. try {
  238. Artisan::call('pet:generate-json');
  239. $output = Artisan::output();
  240. // 强制刷新缓存
  241. PetJsonConfig::getData([], true);
  242. admin_success('刷新成功', '宠物配置表已成功刷新');
  243. return $content
  244. ->title($this->title)
  245. ->description('刷新宠物配置表')
  246. ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
  247. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
  248. } catch (\Exception $e) {
  249. admin_error('刷新失败', '宠物配置表刷新失败: ' . $e->getMessage());
  250. return $content
  251. ->title($this->title)
  252. ->description('刷新宠物配置表')
  253. ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
  254. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
  255. }
  256. }
  257. /**
  258. * 刷新农场房屋配置表
  259. *
  260. * @param Content $content
  261. * @return Content
  262. */
  263. #[Get('game/configs/refresh-farm-house')]
  264. public function refreshFarmHouse(Content $content)
  265. {
  266. try {
  267. Artisan::call('farm:generate-house-json');
  268. $output = Artisan::output();
  269. // 强制刷新缓存
  270. FarmHouseJsonConfig::getData([], true);
  271. admin_success('刷新成功', '农场房屋配置表已成功刷新');
  272. return $content
  273. ->title($this->title)
  274. ->description('刷新农场房屋配置表')
  275. ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
  276. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
  277. } catch (\Exception $e) {
  278. admin_error('刷新失败', '农场房屋配置表刷新失败: ' . $e->getMessage());
  279. return $content
  280. ->title($this->title)
  281. ->description('刷新农场房屋配置表')
  282. ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
  283. ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
  284. }
  285. }
  286. }