Przeglądaj źródła

feat(game): 新增游戏配置表管理功能

- 新增 GameConfigController 控制器,用于管理游戏配置表
- 添加物品、宝箱、宠物、农场房屋等配置表的刷新功能
- 重构 FarmHouseJsonConfig 类,移至 Game 模块
- 新增 GenerateFarmHouseConfigJson 命令,用于生成农场房屋配置 JSON 文件
- 更新相关控制器和视图,以支持新的游戏配置表管理功能
Your Name 8 miesięcy temu
rodzic
commit
f899c82cdb

+ 1 - 22
UCore/Model/CastsAttributes.php

@@ -72,29 +72,8 @@ abstract class CastsAttributes implements \Illuminate\Contracts\Database\Eloquen
             return json_encode([], JSON_UNESCAPED_UNICODE);
         }
 
-        if (is_array($value)) {
-            // 确保所有值都是数值类型
-            $sanitized = [];
-            foreach ($value as $k => $v) {
-                $sanitized[$k] = is_numeric($v) ? (float) $v : 0;
-            }
-            return json_encode($sanitized, JSON_UNESCAPED_UNICODE);
-        }
-
-        if (is_string($value) && $this->isJson($value)) {
-            // 如果已经是JSON字符串,确保解码后的值都是数值类型
-            $decoded = json_decode($value, true);
-            if (is_array($decoded)) {
-                $sanitized = [];
-                foreach ($decoded as $k => $v) {
-                    $sanitized[$k] = is_numeric($v) ? (float) $v : 0;
-                }
-                return json_encode($sanitized, JSON_UNESCAPED_UNICODE);
-            }
-            return $value;
-        }
 
-        return json_encode([], JSON_UNESCAPED_UNICODE);
+        return json_encode($value, JSON_UNESCAPED_UNICODE);
     }
 
     /**

+ 2 - 3
app/Module/Farm/AdminControllers/FarmHouseConfigController.php

@@ -8,14 +8,13 @@ use App\Module\Farm\AdminControllers\Helper\GridHelper;
 use App\Module\Farm\AdminControllers\Helper\ShowHelper;
 use App\Module\Farm\AdminControllers\Tools\RefreshCheckTool;
 use App\Module\Farm\AdminControllers\Tools\SyncFarmHouseJsonTool;
-use App\Module\Farm\DCache\FarmHouseJsonConfig;
 use App\Module\Farm\Repositories\FarmHouseConfigRepository;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
-use UCore\DcatAdmin\AdminController;
-use Spatie\RouteAttributes\Attributes\Resource;
 use Spatie\RouteAttributes\Attributes\Get;
+use Spatie\RouteAttributes\Attributes\Resource;
+use UCore\DcatAdmin\AdminController;
 
 /**
  * 房屋等级配置管理控制器

+ 4 - 9
app/Module/Farm/AdminControllers/Tools/RefreshCheckTool.php

@@ -2,6 +2,7 @@
 
 namespace App\Module\Farm\AdminControllers\Tools;
 
+use App\Module\Game\DCache\FarmHouseJsonConfig;
 use Dcat\Admin\Grid\Tools\AbstractTool;
 use Illuminate\Support\Facades\Log;
 
@@ -31,18 +32,12 @@ class RefreshCheckTool extends AbstractTool
 
     public static function checkSyncStatus(): array
     {
-        $jsonPath = public_path('json/farm_house.json');
 
-        if (!file_exists($jsonPath)) {
-            return [
-                'is_synced' => false,
-                'message' => '配置文件不存在,请立即生成',
-                'should_display' => true
-            ];
-        }
+
 
         try {
-            $json = json_decode(file_get_contents($jsonPath), true);
+            $json = FarmHouseJsonConfig::getData([],true);
+
             $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
             $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
 

+ 0 - 1
app/Module/Farm/AdminControllers/Tools/SyncFarmHouseJsonTool.php

@@ -2,7 +2,6 @@
 
 namespace App\Module\Farm\AdminControllers\Tools;
 
-use App\Module\Farm\DCache\FarmHouseJsonConfig;
 use Dcat\Admin\Grid\Tools\AbstractTool;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;

+ 106 - 0
app/Module/Farm/Commands/GenerateFarmHouseConfigJson.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace App\Module\Farm\Commands;
+
+use App\Module\Farm\Models\FarmHouseConfig;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+/**
+ * 生成房屋配置表JSON数据命令
+ *
+ * 该命令用于从数据库中的房屋配置表生成JSON数据文件,供客户端使用。
+ * 生成的JSON文件包含房屋等级的基本信息,如等级、产出加成、特殊土地上限等。
+ * 该命令通常在房屋配置数据更新后运行,以确保客户端获取最新的配置数据。
+ */
+class GenerateFarmHouseConfigJson extends Command
+{
+    /**
+     * 命令名称
+     *
+     * @var string
+     */
+    protected $signature = 'farm:generate-house-json';
+
+    /**
+     * 命令描述
+     *
+     * @var string
+     */
+    protected $description = '生成房屋配置JSON文件';
+
+    /**
+     * 生成房屋配置JSON数据
+     *
+     * @param bool $saveToFile 是否保存到文件
+     * @return array|bool 生成的数据或失败标志
+     */
+    public static function generateJson()
+    {
+        try {
+            // 禁用查询日志以避免权限问题
+            DB::disableQueryLog();
+
+            // 获取所有房屋配置
+            $configs = FarmHouseConfig::orderBy('level')->get();
+
+            // 准备JSON数据
+            $jsonData = [
+                'generated_at' => now()->toDateTimeString(),
+                'house_configs' => []
+            ];
+
+            foreach ($configs as $config) {
+                $jsonData['house_configs'][] = [
+                    'level' => $config->level,
+                    'output_bonus' => $config->output_bonus,
+                    'special_land_limit' => $config->special_land_limit,
+                    'upgrade_materials' => $config->upgrade_materials,
+                    'downgrade_days' => $config->downgrade_days,
+                ];
+            }
+
+
+
+
+            return $jsonData;
+        } catch (\Exception $e) {
+            if (php_sapi_name() === 'cli') {
+                echo "Error: Generate farm_house.json failed: {$e->getMessage()}\n";
+            }
+
+            return false;
+        }
+    }
+
+
+
+
+    /**
+     * 执行命令
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->info('开始生成房屋配置JSON文件...');
+
+
+        try {
+            // 直接调用静态方法生成JSON,而不通过缓存类
+            $result = self::generateJson();
+
+            if ($result !== false) {
+                $this->info('房屋配置JSON文件生成成功');
+                $this->info('共生成 ' . count($result['house_configs']) . ' 条配置数据');
+                return Command::SUCCESS;
+            } else {
+                $this->error('生成房屋配置JSON文件失败');
+                return Command::FAILURE;
+            }
+        } catch (\Exception $e) {
+            $this->error('生成房屋配置JSON文件失败: ' . $e->getMessage());
+            return Command::FAILURE;
+        }
+    }
+}

+ 2 - 0
app/Module/Farm/Providers/FarmServiceProvider.php

@@ -49,6 +49,8 @@ class FarmServiceProvider extends ServiceProvider
             Commands\CheckHouseDowngradeCommand::class,
             Commands\CleanExpiredLogsCommand::class,
             Commands\RebuildFarmCacheCommand::class,
+            Commands\GenerateFarmHouseConfigJson::class,
+            Commands\GenerateFarmHouseJson::class,
         ]);
 
         // 注册服务

+ 326 - 0
app/Module/Game/AdminControllers/GameConfigController.php

@@ -0,0 +1,326 @@
+<?php
+
+namespace App\Module\Game\AdminControllers;
+
+use App\Module\Game\DCache\ChestJsonConfig;
+use App\Module\Game\DCache\FarmHouseJsonConfig;
+use App\Module\Game\DCache\ItemJsonConfig;
+use App\Module\Game\DCache\PetJsonConfig;
+use Dcat\Admin\Layout\Content;
+use Dcat\Admin\Layout\Row;
+use Dcat\Admin\Widgets\Card;
+use Dcat\Admin\Widgets\Table;
+use Dcat\Admin\Http\Controllers\AdminController;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Artisan;
+use Spatie\RouteAttributes\Attributes\Resource;
+use Spatie\RouteAttributes\Attributes\Get;
+use Spatie\RouteAttributes\Attributes\Post;
+
+/**
+ * 游戏配置表管理控制器
+ *
+ * 用于显示和管理游戏中的各种配置表
+ */
+class GameConfigController extends AdminController
+{
+    /**
+     * 页面标题
+     *
+     * @var string
+     */
+    protected $title = '游戏配置表管理';
+
+    /**
+     * 页面描述
+     *
+     * @var string
+     */
+    protected $description = '查看和刷新游戏中的各种配置表';
+
+    /**
+     * 配置表首页
+     *
+     * @param Content $content
+     * @return Content
+     */
+    public function index(Content $content)
+    {
+        return $content
+            ->title($this->title)
+            ->description($this->description)
+            ->body(function (Row $row) {
+                // 物品配置表卡片
+                $row->column(6, $this->createConfigCard(
+                    '物品配置表',
+                    'items.json',
+                    'gameitems:generate-json',
+                    'game/configs/refresh-items',
+                    $this->getItemConfigInfo()
+                ));
+
+                // 宝箱配置表卡片
+                $row->column(6, $this->createConfigCard(
+                    '宝箱配置表',
+                    'chest.json',
+                    'gameitems:generate-chest-json',
+                    'game/configs/refresh-chests',
+                    $this->getChestConfigInfo()
+                ));
+            })
+            ->body(function (Row $row) {
+                // 宠物配置表卡片
+                $row->column(6, $this->createConfigCard(
+                    '宠物配置表',
+                    'pet_config.json, pet_level_config.json, pet_skill_config.json',
+                    'pet:generate-json',
+                    'game/configs/refresh-pets',
+                    $this->getPetConfigInfo()
+                ));
+
+                // 农场房屋配置表卡片
+                $row->column(6, $this->createConfigCard(
+                    '农场房屋配置表',
+                    'farm_house.json',
+                    'farm:generate-house-json',
+                    'game/configs/refresh-farm-house',
+                    $this->getFarmHouseConfigInfo()
+                ));
+            });
+    }
+
+    /**
+     * 创建配置表信息卡片
+     *
+     * @param string $title 卡片标题
+     * @param string $filename 文件名
+     * @param string $command 生成命令
+     * @param string $refreshUrl 刷新URL
+     * @param array $info 配置信息
+     * @return Card
+     */
+    protected function createConfigCard($title, $filename, $command, $refreshUrl, $info)
+    {
+        $headers = ['属性', '值'];
+        $rows = [];
+
+        foreach ($info as $key => $value) {
+            $rows[] = [$key, $value];
+        }
+
+        $card = new Card($title, Table::make($headers, $rows));
+        $card->tool('<a href="' . admin_url($refreshUrl) . '" class="btn btn-primary btn-sm"><i class="fa fa-refresh"></i> 刷新配置</a>');
+        $card->footer("<code>文件: {$filename}</code><br><code>命令: php artisan {$command}</code>");
+
+        return $card;
+    }
+
+    /**
+     * 获取物品配置表信息
+     *
+     * @return array
+     */
+    protected function getItemConfigInfo()
+    {
+        $data = ItemJsonConfig::getData();
+        $info = [
+            '生成时间' => $data['generated_at'] ?? '未知',
+            '物品数量' => isset($data['items']) ? count($data['items']) : 0,
+            '文件路径' => public_path('json/items.json'),
+        ];
+
+        return $info;
+    }
+
+    /**
+     * 获取宝箱配置表信息
+     *
+     * @return array
+     */
+    protected function getChestConfigInfo()
+    {
+        $data = ChestJsonConfig::getData();
+        $info = [
+            '生成时间' => $data['generated_at'] ?? '未知',
+            '宝箱数量' => isset($data['chest']) ? count($data['chest']) : 0,
+            '文件路径' => public_path('json/chest.json'),
+        ];
+
+        return $info;
+    }
+
+    /**
+     * 获取宠物配置表信息
+     *
+     * @return array
+     */
+    protected function getPetConfigInfo()
+    {
+        $data = PetJsonConfig::getData();
+        $petConfig = $data['pet_config'] ?? [];
+        $petLevelConfig = $data['pet_level_config'] ?? [];
+        $petSkillConfig = $data['pet_skill_config'] ?? [];
+
+        $info = [
+            '生成时间' => $petConfig['generated_at'] ?? '未知',
+            '宠物数量' => isset($petConfig['pets']) ? count($petConfig['pets']) : 0,
+            '等级配置数量' => isset($petLevelConfig['pet_levels']) ? count($petLevelConfig['pet_levels']) : 0,
+            '技能配置数量' => isset($petSkillConfig['pet_skills']) ? count($petSkillConfig['pet_skills']) : 0,
+        ];
+
+        return $info;
+    }
+
+    /**
+     * 获取农场房屋配置表信息
+     *
+     * @return array
+     */
+    protected function getFarmHouseConfigInfo()
+    {
+        $data = FarmHouseJsonConfig::getData();
+        $info = [
+            '生成时间' => $data['generated_at'] ?? '未知',
+            '房屋配置数量' => isset($data['house_configs']) ? count($data['house_configs']) : 0,
+            '文件路径' => public_path('json/farm_house.json'),
+        ];
+
+        return $info;
+    }
+
+    /**
+     * 刷新物品配置表
+     *
+     * @param Content $content
+     * @return Content
+     */
+    #[Get('game/configs/refresh-items')]
+    public function refreshItems(Content $content)
+    {
+        try {
+            Artisan::call('gameitems:generate-json');
+            $output = Artisan::output();
+
+            // 强制刷新缓存
+            ItemJsonConfig::getData([], true);
+
+            admin_success('刷新成功', '物品配置表已成功刷新');
+
+            return $content
+                ->title($this->title)
+                ->description('刷新物品配置表')
+                ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
+        } catch (\Exception $e) {
+            admin_error('刷新失败', '物品配置表刷新失败: ' . $e->getMessage());
+
+            return $content
+                ->title($this->title)
+                ->description('刷新物品配置表')
+                ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
+        }
+    }
+
+    /**
+     * 刷新宝箱配置表
+     *
+     * @param Content $content
+     * @return Content
+     */
+    #[Get('game/configs/refresh-chests')]
+    public function refreshChests(Content $content)
+    {
+        try {
+            Artisan::call('gameitems:generate-chest-json');
+            $output = Artisan::output();
+
+            // 强制刷新缓存
+            ChestJsonConfig::getData([], true);
+
+            admin_success('刷新成功', '宝箱配置表已成功刷新');
+
+            return $content
+                ->title($this->title)
+                ->description('刷新宝箱配置表')
+                ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
+        } catch (\Exception $e) {
+            admin_error('刷新失败', '宝箱配置表刷新失败: ' . $e->getMessage());
+
+            return $content
+                ->title($this->title)
+                ->description('刷新宝箱配置表')
+                ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
+        }
+    }
+
+    /**
+     * 刷新宠物配置表
+     *
+     * @param Content $content
+     * @return Content
+     */
+    #[Get('game/configs/refresh-pets')]
+    public function refreshPets(Content $content)
+    {
+        try {
+            Artisan::call('pet:generate-json');
+            $output = Artisan::output();
+
+            // 强制刷新缓存
+            PetJsonConfig::getData([], true);
+
+            admin_success('刷新成功', '宠物配置表已成功刷新');
+
+            return $content
+                ->title($this->title)
+                ->description('刷新宠物配置表')
+                ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
+        } catch (\Exception $e) {
+            admin_error('刷新失败', '宠物配置表刷新失败: ' . $e->getMessage());
+
+            return $content
+                ->title($this->title)
+                ->description('刷新宠物配置表')
+                ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
+        }
+    }
+
+    /**
+     * 刷新农场房屋配置表
+     *
+     * @param Content $content
+     * @return Content
+     */
+    #[Get('game/configs/refresh-farm-house')]
+    public function refreshFarmHouse(Content $content)
+    {
+        try {
+            Artisan::call('farm:generate-house-json');
+            $output = Artisan::output();
+
+            // 强制刷新缓存
+            FarmHouseJsonConfig::getData([], true);
+
+            admin_success('刷新成功', '农场房屋配置表已成功刷新');
+
+            return $content
+                ->title($this->title)
+                ->description('刷新农场房屋配置表')
+                ->body(Card::make('命令输出', "<pre>{$output}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 2000);</script>");
+        } catch (\Exception $e) {
+            admin_error('刷新失败', '农场房屋配置表刷新失败: ' . $e->getMessage());
+
+            return $content
+                ->title($this->title)
+                ->description('刷新农场房屋配置表')
+                ->body(Card::make('错误信息', "<pre>{$e->getMessage()}</pre>"))
+                ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/configs') . "'; }, 3000);</script>");
+        }
+    }
+}

+ 3 - 0
app/Module/Game/AdminControllers/GameRewardGroupController.php

@@ -13,10 +13,13 @@ use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Layout\Content;
 use Dcat\Admin\Widgets\Card;
 use Dcat\Admin\Widgets\Table;
+use Spatie\RouteAttributes\Attributes\Resource;
+use Spatie\RouteAttributes\Attributes\Get;
 
 /**
  * 奖励组管理控制器
  */
+#[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
 class GameRewardGroupController extends AdminController
 {
     /**

+ 2 - 0
app/Module/Game/AdminControllers/GameRewardItemController.php

@@ -9,10 +9,12 @@ use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
+use Spatie\RouteAttributes\Attributes\Resource;
 
 /**
  * 奖励项管理控制器
  */
+#[Resource('game-reward-items', names: 'dcat.admin.game-reward-items')]
 class GameRewardItemController extends AdminController
 {
     /**

+ 3 - 1
app/Module/Game/AdminControllers/GameRewardLogController.php

@@ -11,10 +11,12 @@ use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Widgets\Card;
 use Dcat\Admin\Widgets\Table;
+use Spatie\RouteAttributes\Attributes\Resource;
 
 /**
  * 奖励日志管理控制器
  */
+#[Resource('game-reward-logs', names: 'dcat.admin.game-reward-logs')]
 class GameRewardLogController extends AdminController
 {
     /**
@@ -50,7 +52,7 @@ class GameRewardLogController extends AdminController
                 if (empty($items)) {
                     return '无奖励项';
                 }
-                
+
                 $count = count($items);
                 return "<span class=\"badge badge-primary\">{$count}个奖励项</span>";
             });

+ 0 - 18
app/Module/Game/AdminControllers/TestController.php

@@ -1,18 +0,0 @@
-<?php
-
-namespace App\Module\Game\AdminControllers;
-
-use Dcat\Admin\Http\Controllers\AdminController;
-use Illuminate\Support\Facades\Log;
-use Spatie\RouteAttributes\Attributes\Prefix;
-
-#[Prefix('game-test')]
-class TestController extends AdminController
-{
-
-    public function test()
-    {
-        Log::info(123);
-        echo 1;die;
-    }
-}

+ 1 - 1
app/Module/Farm/DCache/FarmHouseJsonConfig.php → app/Module/Game/DCache/FarmHouseJsonConfig.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Module\Farm\DCache;
+namespace App\Module\Game\DCache;
 
 use App\Module\Farm\Commands\GenerateFarmHouseConfigJson;
 use App\Module\LCache\DQueueJob;

+ 3 - 3
resources/views/admin_core/config/value.blade.php

@@ -1,9 +1,9 @@
 <div style="width: 200px" >
-    @if ($model->type === \App\Module\System\Enums\ConfigType::TYPE_BOOL->value())
+    @if ($model->type === \App\Module\System\Enums\CONFIG_TYPE::TYPE_BOOL->value())
         {{ $value ? '开启' : '关闭' }}
-    @elseif ($model->type === \App\Module\System\Enums\ConfigType::TYPE_IS->value())
+    @elseif ($model->type === \App\Module\System\Enums\CONFIG_TYPE::TYPE_IS->value())
         {{ $value ? '是' : '否' }}
-    @elseif ($model->type === \App\Module\System\Enums\ConfigType::TYPE_TIME->value())
+    @elseif ($model->type === \App\Module\System\Enums\CONFIG_TYPE::TYPE_TIME->value())
         {{  \UCore\Helper\Carbon\CarbonInterval::secondsCascadeForHumans($value) }}
     @else
         {{ $value }}