| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace App\Module\GameItems\Commands;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use Illuminate\Console\Command;
- use App\Module\GameItems\Models\ItemRecipe;
- use App\Module\GameItems\Models\ItemRecipeMaterial;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Log;
- /**
- * 生成物品合成配方配置表JSON数据命令
- *
- * 该命令用于从数据库中的物品合成配方表生成JSON数据文件,供客户端使用。
- * 生成的JSON文件包含合成配方的基本信息,如ID、名称、产出物品、所需材料等。
- * 该命令通常在合成配方数据更新后运行,以确保客户端获取最新的配方数据。
- */
- class GenerateRecipeJsonCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:generate-recipe-json';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Generate recipe.json from ItemRecipe table';
- /**
- * 生成合成配方JSON数据
- *
- * @return array|bool 生成的数据或失败标志
- */
- public static function generateJson()
- {
- try {
- // 查询ItemRecipe表中的数据,并预加载关联数据
- $recipes = ItemRecipe::query()
- ->with(['resultItem', 'materials.item'])
- ->where('is_active', 1)
- ->get()
- ->map(function ($recipe) {
- // 处理材料数据
- $materials = $recipe->materials->map(function ($material) {
- return [
- 'item_id' => $material->item_id,
- 'item_name' => $material->item->name ?? '未知物品',
- 'quantity' => $material->quantity,
- 'is_consumed' => $material->is_consumed,
- ];
- })->toArray();
- // 构建配方数据
- return [
- 'id' => $recipe->id,
- 'name' => $recipe->name,
- 'result_item_id' => $recipe->result_item_id,
- 'result_item_name' => $recipe->resultItem->name ?? '未知物品',
- 'result_min_quantity' => $recipe->result_min_quantity,
- 'result_max_quantity' => $recipe->result_max_quantity,
- 'success_rate' => $recipe->success_rate,
- 'coin_cost' => $recipe->coin_cost,
- 'level_required' => $recipe->level_required,
- 'is_default_unlocked' => $recipe->is_default_unlocked,
- 'unlock_condition' => $recipe->unlock_condition,
- 'cooldown_seconds' => $recipe->cooldown_seconds,
- 'category_id' => $recipe->category_id,
- 'materials' => $materials,
- ];
- })
- ->toArray();
- // 准备完整数据,包含生成时间
- $data = [
- 'generated_ts' => time(),
- 'recipes' => $recipes
- ];
- // 保存到文件
- self::saveJsonToFile($data);
- return $data;
- } catch (\Exception $e) {
- Log::error('Generate recipe.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 . '/recipe.json';
- File::put($filePath, $jsonContent);
- Log::info('Recipe JSON file saved to: ' . $filePath);
- return true;
- } catch (\Exception $e) {
- Log::error('Save recipe.json to file failed: ' . $e->getMessage());
- return false;
- }
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('Generating recipe JSON data...');
-
- if (RecipeJsonConfig::getData([], true)) {
- $this->info('Successfully generated recipe.json with timestamp');
- $this->info('JSON file saved to public/json/recipe.json');
- } else {
- $this->error('Failed to generate recipe.json');
- }
- }
- }
|