| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Module\GameItems\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\File;
- /**
- * 测试JSON生成和文件输出功能
- */
- class TestJsonGenerationCommand extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'gameitems:test-json-generation';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = 'Test JSON generation and file output functionality';
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('开始测试物品配置表JSON生成和文件输出功能...');
- // 测试物品配置表生成
- $this->info('1. 生成物品配置表...');
- $itemsData = GenerateItemsJsonCommand::generateJson(true);
- if ($itemsData) {
- $this->info(' 物品配置表生成成功!');
- $this->info(' 物品数量: ' . count($itemsData['items']));
- $this->info(' 生成时间: ' . $itemsData['generated_at']);
- } else {
- $this->error(' 物品配置表生成失败!');
- return 1;
- }
- // 测试宝箱配置表生成
- $this->info('2. 生成宝箱配置表...');
- $chestData = GenerateChestJsonCommand::generateJson(true);
- if ($chestData) {
- $this->info(' 宝箱配置表生成成功!');
- $this->info(' 宝箱数量: ' . count($chestData['chest']));
- $this->info(' 生成时间: ' . $chestData['generated_at']);
- } else {
- $this->error(' 宝箱配置表生成失败!');
- return 1;
- }
- // 验证文件是否存在
- $this->info('3. 验证配置文件是否存在...');
- $itemsJsonPath = 'public/json/items.json';
- $chestJsonPath = 'public/json/chest.json';
- if (File::exists($itemsJsonPath)) {
- $this->info(' 物品配置文件存在: ' . $itemsJsonPath);
- $fileSize = File::size($itemsJsonPath);
- $this->info(' 文件大小: ' . $this->formatBytes($fileSize));
- } else {
- $this->error(' 物品配置文件不存在: ' . $itemsJsonPath);
- return 1;
- }
- if (File::exists($chestJsonPath)) {
- $this->info(' 宝箱配置文件存在: ' . $chestJsonPath);
- $fileSize = File::size($chestJsonPath);
- $this->info(' 文件大小: ' . $this->formatBytes($fileSize));
- } else {
- $this->error(' 宝箱配置文件不存在: ' . $chestJsonPath);
- return 1;
- }
- $this->info('测试完成,所有功能正常!');
- return 0;
- }
- /**
- * 格式化字节数为可读格式
- *
- * @param int $bytes 字节数
- * @param int $precision 精度
- * @return string 格式化后的字符串
- */
- protected function formatBytes($bytes, $precision = 2)
- {
- $units = ['B', 'KB', 'MB', 'GB', 'TB'];
- $bytes = max($bytes, 0);
- $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
- $pow = min($pow, count($units) - 1);
- $bytes /= pow(1024, $pow);
- return round($bytes, $precision) . ' ' . $units[$pow];
- }
- }
|