| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?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;
- /**
- * 生成物品配置表JSON数据命令
- *
- * 该命令用于从数据库中的物品表生成物品JSON数据文件,供客户端使用。
- * 生成的JSON文件包含物品的基本信息,如ID、名称、描述、售价和显示属性等。
- * 该命令通常在物品数据更新后运行,以确保客户端获取最新的物品数据。
- */
- class GenerateItemsJsonCommand 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');
- }
- }
- }
|