|
|
@@ -0,0 +1,84 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\GameItems\Commands;
|
|
|
+
|
|
|
+use App\Module\Game\DCache\ItemJsonConfig;
|
|
|
+use App\Module\Game\Services\JsonConfigService;
|
|
|
+use Illuminate\Console\Command;
|
|
|
+use App\Module\GameItems\Models\Item;
|
|
|
+use Illuminate\Support\Facades\File;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+class GenerateChestJsonCommand extends Command
|
|
|
+{
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 命令名称和签名
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $signature = 'gameitems:generate-json';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 命令描述
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $description = 'Generate items.json from Item table';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行命令
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * 生成物品JSON数据
|
|
|
+ */
|
|
|
+ public static function generateJson()
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 查询Item表中的数据
|
|
|
+ $items = Item::query()
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'name',
|
|
|
+ 'description',
|
|
|
+ 'sell_price',
|
|
|
+ 'display_attributes'
|
|
|
+ ])
|
|
|
+ ->get()
|
|
|
+ ->map(function ($item) {
|
|
|
+ return [
|
|
|
+ 'id' => $item->id,
|
|
|
+ 'name' => $item->name,
|
|
|
+ 'description' => $item->description,
|
|
|
+ 'sell_price' => $item->sell_price,
|
|
|
+ 'display_attributes' => $item->display_attributes
|
|
|
+ ];
|
|
|
+ })
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 准备完整数据,包含生成时间
|
|
|
+ $data = [
|
|
|
+ 'generated_at' => now()->toDateTimeString(),
|
|
|
+ 'items' => $items
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('Generate items.json failed: ' . $e->getMessage());
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function handle()
|
|
|
+ {
|
|
|
+ if (ItemJsonConfig::getData([], true)) {
|
|
|
+ $this->info('Successfully generated items.json with timestamp');
|
|
|
+ } else {
|
|
|
+ $this->error('Failed to generate items.json');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|