| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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 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(): bool
- {
- 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();
- // 确保public/json目录存在
- $directory = public_path('json');
- if (!File::exists($directory)) {
- if (!File::makeDirectory($directory, 0755, true)) {
- Log::error('Failed to create directory: ' . $directory);
- return false;
- }
- }
- // 检查目录是否可写
- if (!is_writable($directory)) {
- Log::error('Directory is not writable: ' . $directory);
- return false;
- }
- // 准备完整数据,包含生成时间
- $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');
- }
- }
- }
|