GameConfigController.php 9.7 KB

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