| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Module\GameItems\Commands;
- use App\Module\Game\DCache\DismantleJsonConfig;
- use Illuminate\Console\Command;
- use App\Module\GameItems\Models\ItemDismantleRule;
- use App\Module\GameItems\Models\ItemDismantleResult;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Log;
- /**
- * 生成物品分解配方配置表JSON数据命令
- *
- * 该命令用于从数据库中的物品分解规则表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含分解规则的基本信息,如规则ID、适用物品、分解结果等。
- * 该命令通常在分解规则数据更新后运行,以确保客户端获取最新的分解规则数据。
- */
- class GenerateDismantleJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:generate-dismantle-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate dismantle.json from ItemDismantleRule table';
- /**
- * 生成分解规则JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 查询ItemDismantleRule表中的数据,并预加载关联数据
- $rules = ItemDismantleRule::query()
- ->with(['item', 'category', 'results.resultItem'])
- ->where('is_active', 1)
- ->get()
- ->map(function ($rule) {
- // 处理分解结果数据
- $results = $rule->results->map(function ($result) {
- return [
- 'result_item_id' => $result->result_item_id,
- 'result_item_name' => $result->resultItem->name ?? '未知物品',
- 'min_quantity' => $result->min_quantity,
- 'max_quantity' => $result->max_quantity,
- 'base_chance' => $result->base_chance,
- 'rarity_factor' => $result->rarity_factor,
- 'quality_factor' => $result->quality_factor,
- ];
- })->toArray();
- // 构建规则数据
- $ruleData = [
- 'id' => $rule->id,
- 'priority' => $rule->priority,
- 'min_rarity' => $rule->min_rarity,
- 'max_rarity' => $rule->max_rarity,
- 'results' => $results,
- ];
- // 根据规则类型添加不同的字段
- if ($rule->item_id) {
- $ruleData['rule_type'] = 'item';
- $ruleData['item_id'] = $rule->item_id;
- $ruleData['item_name'] = $rule->item->name ?? '未知物品';
- } else {
- $ruleData['rule_type'] = 'category';
- $ruleData['category_id'] = $rule->category_id;
- $ruleData['category_name'] = $rule->category->name ?? '未知分类';
- }
- return $ruleData;
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_ts' => time(),
- 'dismantle_rules' => $rules
- ];
- // 保存到文件
- self::saveJsonToFile($data);
- return $data;
- } catch (\Exception $e) {
- Log::error('Generate dismantle.json failed: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 将JSON数据保存到文件
- *
- * @param array $data 要保存的数据
- * @return bool 是否保存成功
- */
- protected static function saveJsonToFile(array $data): bool
- {
- try {
- // 确保目录存在
- $directory = 'public/json';
- if (!File::exists($directory)) {
- File::makeDirectory($directory, 0755, true);
- }
- // 将数据保存为JSON文件
- $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- $filePath = $directory . '/dismantle.json';
- File::put($filePath, $jsonContent);
- Log::info('Dismantle JSON file saved to: ' . $filePath);
- return true;
- } catch (\Exception $e) {
- Log::error('Save dismantle.json to file failed: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('Generating dismantle JSON data...');
-
- if (DismantleJsonConfig::getData([], true)) {
- $this->info('Successfully generated dismantle.json with timestamp');
- $this->info('JSON file saved to public/json/dismantle.json');
- } else {
- $this->error('Failed to generate dismantle.json');
- }
- }
- }
|