GenerateItemsJsonCommand.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Module\GameItems\Commands;
  3. use App\Module\Game\DCache\ItemJsonConfig;
  4. use App\Module\Game\Services\JsonConfigService;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use App\Module\GameItems\Models\Item;
  8. use Illuminate\Support\Facades\File;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Storage;
  11. /**
  12. * 生成物品配置表JSON数据命令
  13. *
  14. * 该命令用于从数据库中的物品表生成物品JSON数据文件,供客户端使用。
  15. * 生成的JSON文件包含物品的基本信息,如ID、名称、描述、售价和显示属性等。
  16. * 该命令通常在物品数据更新后运行,以确保客户端获取最新的物品数据。
  17. */
  18. class GenerateItemsJsonCommand extends Command
  19. {
  20. /**
  21. * 命令名称和签名
  22. *
  23. * @var string
  24. */
  25. protected $signature = 'gameitems:generate-json';
  26. /**
  27. * 命令描述
  28. *
  29. * @var string
  30. */
  31. protected $description = 'Generate items.json from Item table';
  32. /**
  33. * 执行命令
  34. */
  35. /**
  36. * 生成物品JSON数据
  37. *
  38. * @param bool $saveToFile 是否保存到文件
  39. * @return array|bool 生成的数据或失败标志
  40. */
  41. public static function generateJson()
  42. {
  43. try {
  44. // 查询Item表中的数据
  45. $items = Item::query()
  46. ->select([
  47. 'id',
  48. 'name',
  49. 'description',
  50. 'sell_price',
  51. 'display_attributes',
  52. 'numeric_attributes'
  53. ])
  54. ->get()
  55. ->map(function ($item) {
  56. return [
  57. 'id' => $item->id,
  58. 'name' => $item->name,
  59. 'description' => $item->description,
  60. 'sell_price' => $item->sell_price,
  61. 'display_attributes' => $item->display_attributes,
  62. 'numeric_attributes' => $item->numeric_attributes
  63. ];
  64. })
  65. ->toArray();
  66. // 准备完整数据,包含生成时间
  67. $data = [
  68. 'generated_ts' => time(),
  69. 'items' => $items
  70. ];
  71. return $data;
  72. } catch (\Exception $e) {
  73. Log::error('Generate items.json failed: ' . $e->getMessage());
  74. return false;
  75. }
  76. }
  77. /**
  78. * 将JSON数据保存到文件
  79. *
  80. * @param array $data 要保存的数据
  81. * @return bool 是否保存成功
  82. */
  83. protected static function saveJsonToFile(array $data): bool
  84. {
  85. try {
  86. // 确保目录存在
  87. $directory = 'public/json';
  88. if (!File::exists($directory)) {
  89. File::makeDirectory($directory, 0755, true);
  90. }
  91. // 将数据保存为JSON文件
  92. $jsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  93. $filePath = $directory . '/items.json';
  94. File::put($filePath, $jsonContent);
  95. Log::info('Items JSON file saved to: ' . $filePath);
  96. return true;
  97. } catch (\Exception $e) {
  98. Log::error('Save items.json to file failed: ' . $e->getMessage());
  99. return false;
  100. }
  101. }
  102. public function handle()
  103. {
  104. if (ItemJsonConfig::getData([], true)) {
  105. $this->info('Successfully generated items.json with timestamp');
  106. $this->info('JSON file saved to public/json/items.json');
  107. } else {
  108. $this->error('Failed to generate items.json');
  109. }
  110. }
  111. }