Răsfoiți Sursa

refactor(game): 优化游戏配置管理功能

- 移除 GenerateFarmHouseConfigJson 中的冗余代码
- 更新 GameConfigController 中的数据显示逻辑
- 改进 RefreshCheckTool 和 SyncItemsJsonTool 的实现
- 新增 SyncChetsJsonTool 工具类- 优化 CacheItem 和 SCache 类的实现
- 调整 GeneratePetJsonCommand 中的时区设置
- 更新全局时区配置
Your Name 8 luni în urmă
părinte
comite
92ed012085

+ 16 - 0
UCore/Helper/Datetime.php

@@ -8,6 +8,11 @@ class Datetime
 {
 
 
+    /**
+     * 秒转时间
+     * @param $seconds
+     * @return string
+     */
     static  public function ss($seconds)
     {
 
@@ -16,4 +21,15 @@ class Datetime
         $humanReadableString = $carbonInstance->diffForHumans(null, true, true);
         return $humanReadableString;
     }
+
+    /**
+     * 时间戳 转 字符串
+     * @param $ts
+     * @return string
+     */
+    public static function ts2string($ts)
+    {
+        return Carbon::createFromTimestamp($ts,config('app.timezone'))->toDateTimeString();
+
+    }
 }

+ 1 - 2
app/Module/Farm/Commands/GenerateFarmHouseConfigJson.php

@@ -38,8 +38,7 @@ class GenerateFarmHouseConfigJson extends Command
     public static function generateJson()
     {
         try {
-            // 禁用查询日志以避免权限问题
-            DB::disableQueryLog();
+
 
             // 获取所有房屋配置
             $configs = FarmHouseConfig::orderBy('level')->get();

+ 10 - 6
app/Module/Game/AdminControllers/GameConfigController.php

@@ -7,6 +7,7 @@ use App\Module\Game\DCache\FarmHouseJsonConfig;
 use App\Module\Game\DCache\ItemJsonConfig;
 use App\Module\Game\DCache\PetJsonConfig;
 use App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool;
+use App\Module\GameItems\AdminControllers\Tools\SyncChetsJsonTool;
 use App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool;
 use Carbon\Carbon;
 use Dcat\Admin\Layout\Content;
@@ -19,6 +20,7 @@ use Illuminate\Support\Facades\Artisan;
 use Spatie\RouteAttributes\Attributes\Resource;
 use Spatie\RouteAttributes\Attributes\Get;
 use Spatie\RouteAttributes\Attributes\Post;
+use UCore\Helper\Datetime;
 
 /**
  * 游戏配置表管理控制器
@@ -51,6 +53,7 @@ class GameConfigController extends AdminController
      */
     public function index(Content $content)
     {
+
         return $content
             ->title($this->title)
             ->description($this->description)
@@ -69,7 +72,7 @@ class GameConfigController extends AdminController
                     '宝箱配置表',
                     'chest.json',
                     'gameitems:generate-chest-json',
-                    'game-jsonconfigs/refresh-chests',
+                    SyncChetsJsonTool::make(),
                     $this->getChestConfigInfo()
                 ));
             })
@@ -129,8 +132,8 @@ class GameConfigController extends AdminController
     {
         $data = ItemJsonConfig::getData();
         $info = [
-            '生成时间' => Carbon::createFromTimestamp($data['generated_ts'])->toDateTimeString(),
-            '物品数量' => isset($data['items']) ? count($data['items']) : 0,
+            '生成时间'  => Datetime::ts2string($data['generated_ts']),
+            '物品数量'  => isset($data['items']) ? count($data['items']) : 0,
 
         ];
 
@@ -146,7 +149,7 @@ class GameConfigController extends AdminController
     {
         $data = ChestJsonConfig::getData();
         $info = [
-            '生成时间' => Carbon::createFromTimestamp($data['generated_ts'])->toDateTimeString(),
+            '生成时间'  => Datetime::ts2string($data['generated_ts']),
             '宝箱数量' => isset($data['chest']) ? count($data['chest']) : 0,
 
         ];
@@ -166,8 +169,9 @@ class GameConfigController extends AdminController
         $petLevelConfig = $data['pet_level_config'] ?? [];
         $petSkillConfig = $data['pet_skill_config'] ?? [];
 
+
         $info = [
-            '生成时间'     => Carbon::createFromTimestamp($data['generated_ts'])->toDateTimeString(),
+            '生成时间'  => Datetime::ts2string($data['generated_ts']),
             '宠物数量'     => 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,
@@ -185,7 +189,7 @@ class GameConfigController extends AdminController
     {
         $data = FarmHouseJsonConfig::getData();
         $info = [
-            '生成时间'     => Carbon::createFromTimestamp($data['generated_ts'])->toDateTimeString(),
+            '生成时间'  => Datetime::ts2string($data['generated_ts']),
             '房屋配置数量' => isset($data['house_configs']) ? count($data['house_configs']) : 0,
         ];
 

+ 1 - 1
app/Module/GameItems/AdminControllers/Tools/RefreshCheckTool.php

@@ -42,7 +42,7 @@ class RefreshCheckTool extends AbstractTool
 
     public static function checkSyncStatus(): array
     {
-        $json = ItemJsonConfig::getData([],false);
+        $json = ItemJsonConfig::getData();
         $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
 //        dd($json);
         $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);

+ 46 - 0
app/Module/GameItems/AdminControllers/Tools/SyncChetsJsonTool.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Tools;
+
+use App\Module\Game\DCache\ChestJsonConfig;
+use App\Module\Game\DCache\ItemJsonConfig;
+use Dcat\Admin\Grid\Tools\AbstractTool;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
+
+class SyncChetsJsonTool extends AbstractTool
+{
+
+    protected $style = 'btn btn-danger waves-effect';
+
+
+
+    public function title()
+    {
+        return '立即同步';
+    }
+
+    public function confirm()
+    {
+        return '确定要立即同步JSON数据吗?';
+    }
+
+    public function handle(Request $request)
+    {
+        try {
+            $json =ChestJsonConfig::getData([],true);
+
+
+
+            return $this->response()->success('同步成功')->refresh();
+        } catch (\Exception $e) {
+            Log::error('Sync items.json exception: '.$e->getMessage());
+            return $this->response()->error('同步失败:'.$e->getMessage());
+        }
+    }
+
+
+
+
+
+}

+ 2 - 5
app/Module/GameItems/AdminControllers/Tools/SyncItemsJsonTool.php

@@ -30,12 +30,9 @@ class SyncItemsJsonTool extends AbstractTool
     public function handle(Request $request)
     {
         try {
-            $success = \App\Module\GameItems\Commands\GenerateItemsJsonCommand::generateJson();
+            $json =ItemJsonConfig::getData([],true);
+
 
-            if (!$success) {
-                Log::error('Sync items.json failed - see previous logs for details');
-                return $this->response()->error('同步失败:请检查日志获取详细信息');
-            }
 
             return $this->response()->success('同步成功')->refresh();
         } catch (\Exception $e) {

+ 1 - 1
app/Module/LCache/CacheItem.php

@@ -17,7 +17,7 @@ class CacheItem implements CacheItemInterface
 
     public mixed $value;
 
-    public $isHit;
+    public $isHit = true;
 
 
     public function __construct(string $key, mixed $value = null, int $create_ts = 0, int $ttl = 0)

+ 1 - 1
app/Module/LCache/QueueCache.php

@@ -87,7 +87,7 @@ trait QueueCache
 //        var_dump($key);
         // 3ee2d37c91b4abcf418bd24c0c0e1dea
         // 3ee2d37c91b4abcf418bd24c0c0e1dea
-//        dump($key,$parameter);
+
 
         if (!$item->isHit()) {
             $force = true;

+ 5 - 1
app/Module/LCache/SCache.php

@@ -30,7 +30,7 @@ class SCache
         if ($data instanceof CacheItem) {
             $d2 = $data;
         } else {
-            $d2 = new CacheItem($key, $data, 0, $exp);
+            $d2 = new CacheItem($key, $data, time(), $exp);
         }
 
         \Illuminate\Support\Facades\Cache::put($key, $d2, $exp);
@@ -56,10 +56,14 @@ class SCache
         Logger::debug("Cache-get" . $key);
         $res = \Illuminate\Support\Facades\Cache::get($key, $default);
         if ($res instanceof CacheItem) {
+            Logger::debug("Cache-get hit" . $key);
+            $res->isHit();
             return $res;
         }
         $res = new CacheItem($key, $default, 0);
         $res->setHit(false)->expiresAfter(-1);
+        Logger::debug("Cache-get no hit" . $key);
+
         return $res;
     }
 

+ 22 - 53
app/Module/Pet/Commands/GeneratePetJsonCommand.php

@@ -17,6 +17,7 @@ use Illuminate\Support\Facades\Log;
  */
 class GeneratePetJsonCommand extends Command
 {
+
     /**
      * 命令名称和签名
      *
@@ -37,7 +38,7 @@ class GeneratePetJsonCommand extends Command
      * @param bool $saveToFile 是否保存到文件
      * @return array 生成的数据
      */
-    public static function generateJson(bool $saveToFile = true)
+    public static function generateJson()
     {
         try {
             // 生成宠物基础配置JSON
@@ -49,26 +50,21 @@ class GeneratePetJsonCommand extends Command
             // 生成宠物技能配置JSON
             $petSkillConfigData = self::generatePetSkillConfigJson();
 
-            // 如果需要保存到文件
-            if ($saveToFile) {
-                self::saveJsonToFile('pet_config.json', $petConfigData);
-                self::saveJsonToFile('pet_level_config.json', $petLevelConfigData);
-                self::saveJsonToFile('pet_skill_config.json', $petSkillConfigData);
-            }
 
             // 返回所有配置数据
             return [
-                'pet_config' => $petConfigData,
+                'pet_config'       => $petConfigData,
                 'pet_level_config' => $petLevelConfigData,
                 'pet_skill_config' => $petSkillConfigData,
-                'success' => true
+                'success'          => true,
+                'generated_ts'     => time()
             ];
         } catch (\Exception $e) {
             Log::error('Generate pet JSON failed: ' . $e->getMessage());
 
             return [
                 'success' => false,
-                'error' => $e->getMessage()
+                'error'   => $e->getMessage()
             ];
         }
     }
@@ -82,9 +78,9 @@ class GeneratePetJsonCommand extends Command
         $petConfigs = PetConfig::all()
             ->map(function ($config) {
                 return [
-                    'id' => $config->id,
-                    'pet_type' => $config->pet_type,
-                    'grade_probability' => $config->grade_probability,
+                    'id'                 => $config->id,
+                    'pet_type'           => $config->pet_type,
+                    'grade_probability'  => $config->grade_probability,
                     'display_attributes' => $config->display_attributes,
                     'numeric_attributes' => $config->numeric_attributes
                 ];
@@ -94,7 +90,7 @@ class GeneratePetJsonCommand extends Command
         // 准备完整数据,包含生成时间
         return [
             'generated_ts' => time(),
-            'pets' => $petConfigs
+            'pets'         => $petConfigs
         ];
     }
 
@@ -108,9 +104,9 @@ class GeneratePetJsonCommand extends Command
             ->get()
             ->map(function ($config) {
                 return [
-                    'level' => $config->level,
-                    'exp_required' => $config->exp_required,
-                    'skills' => $config->skills,
+                    'level'              => $config->level,
+                    'exp_required'       => $config->exp_required,
+                    'skills'             => $config->skills,
                     'display_attributes' => $config->display_attributes,
                     'numeric_attributes' => $config->numeric_attributes
                 ];
@@ -120,7 +116,7 @@ class GeneratePetJsonCommand extends Command
         // 准备完整数据,包含生成时间
         return [
             'generated_ts' => time(),
-            'pet_levels' => $petLevelConfigs
+            'pet_levels'   => $petLevelConfigs
         ];
     }
 
@@ -133,12 +129,12 @@ class GeneratePetJsonCommand extends Command
         $petSkills = PetSkill::all()
             ->map(function ($skill) {
                 return [
-                    'id' => $skill->id,
-                    'skill_name' => $skill->skill_name,
+                    'id'           => $skill->id,
+                    'skill_name'   => $skill->skill_name,
                     'stamina_cost' => $skill->stamina_cost,
-                    'cool_down' => $skill->cool_down,
-                    'effect_desc' => $skill->effect_desc,
-                    'min_level' => $skill->min_level
+                    'cool_down'    => $skill->cool_down,
+                    'effect_desc'  => $skill->effect_desc,
+                    'min_level'    => $skill->min_level
                 ];
             })
             ->toArray();
@@ -146,38 +142,11 @@ class GeneratePetJsonCommand extends Command
         // 准备完整数据,包含生成时间
         return [
             'generated_ts' => time(),
-            'pet_skills' => $petSkills
+            'pet_skills'   => $petSkills
         ];
     }
 
-    /**
-     * 将JSON数据保存到文件
-     *
-     * @param string $filename 文件名
-     * @param array $data 要保存的数据
-     * @return bool 是否保存成功
-     */
-    protected static function saveJsonToFile(string $filename, array $data): bool
-    {
-        try {
-            // 确保目录存在
-            $directory = 'public/json';
-            if (!file_exists($directory)) {
-                mkdir($directory, 0755, true);
-            }
-
-            // 将数据保存为JSON文件
-            $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
-            $filePath = $directory . '/' . $filename;
-            file_put_contents($filePath, $jsonContent);
-
-            Log::info('Pet JSON file saved to: ' . $filePath);
-            return true;
-        } catch (\Exception $e) {
-            Log::error('Save pet JSON to file failed: ' . $e->getMessage());
-            return false;
-        }
-    }
+
 
     /**
      * 执行命令
@@ -195,9 +164,9 @@ class GeneratePetJsonCommand extends Command
             $this->info('Pet configs: ' . count($result['pet_config']['pets']));
             $this->info('Pet level configs: ' . count($result['pet_level_config']['pet_levels']));
             $this->info('Pet skill configs: ' . count($result['pet_skill_config']['pet_skills']));
-            $this->info('JSON files saved to public/json/ directory');
         } else {
             $this->error('Failed to generate pet configuration JSON files: ' . $result['error']);
         }
     }
+
 }

+ 3 - 0
app/Providers/AppServiceProvider.php

@@ -2,6 +2,7 @@
 
 namespace App\Providers;
 
+use Carbon\Carbon;
 use Illuminate\Support\ServiceProvider;
 
 class AppServiceProvider extends ServiceProvider
@@ -20,5 +21,7 @@ class AppServiceProvider extends ServiceProvider
     public function boot(): void
     {
         //
+//        Carbon::setTi('Asia/Shanghai');
+
     }
 }

+ 1 - 1
config/app.php

@@ -66,7 +66,7 @@ return [
     |
     */
 
-    'timezone' => env('APP_TIMEZONE', 'UTC'),
+    'timezone' => env('APP_TIMEZONE', 'Asia/Shanghai'),
 
     /*
     |--------------------------------------------------------------------------

+ 1 - 0
public/index.php

@@ -4,6 +4,7 @@ use Illuminate\Http\Request;
 
 define('LARAVEL_START', microtime(true));
 define('RUN_UNIQID', uniqid());
+date_default_timezone_set('Asia/Shanghai');
 
 // Determine if the application is in maintenance mode...
 if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {