EgameSetNetwork.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\EgameItem;
  4. use Illuminate\Console\Command;
  5. class EgameSetNetwork extends Command
  6. {
  7. protected $signature = 'egame:set-network
  8. {network : mainland / hk / kr / jp / none(none 表示不限制)}
  9. {--plat-type= : 平台代码,如 jdb、pg}
  10. {--game-type= : 游戏大类,1~7;不传则该平台全部大类}
  11. {--force : 覆盖已有网络;默认只改空行}
  12. {--dry-run : 只统计不写库}';
  13. protected $description = '按平台批量设置支持网络,并同步到该平台下的游戏(用于正式服已有目录,不要逐条改)';
  14. public function handle(): int
  15. {
  16. $network = $this->normalizedNetwork((string) $this->argument('network'));
  17. if ($network === null) {
  18. $this->error('network 只允许 mainland、hk、kr、jp、none');
  19. return self::FAILURE;
  20. }
  21. $platType = strtolower(trim((string) $this->option('plat-type')));
  22. $gameTypeOption = $this->option('game-type');
  23. $gameType = $gameTypeOption === null || $gameTypeOption === '' ? null : (int) $gameTypeOption;
  24. $onlyEmpty = !$this->option('force');
  25. $dryRun = (bool) $this->option('dry-run');
  26. if ($platType === '') {
  27. $this->error('必须指定 --plat-type');
  28. return self::FAILURE;
  29. }
  30. if ($gameType !== null && ($gameType < 1 || $gameType > 7)) {
  31. $this->error('--game-type 必须是 1~7');
  32. return self::FAILURE;
  33. }
  34. $platformQuery = EgameItem::query()
  35. ->where('item_type', EgameItem::TYPE_PLATFORM)
  36. ->where('plat_type', $platType)
  37. ->where('game_type', '>', 0);
  38. if ($gameType !== null) {
  39. $platformQuery->where('game_type', $gameType);
  40. }
  41. if ($onlyEmpty) {
  42. $platformQuery->where(function ($query) {
  43. $query->whereNull('network')->orWhere('network', '');
  44. });
  45. }
  46. $gameQuery = EgameItem::query()
  47. ->where('item_type', EgameItem::TYPE_GAME)
  48. ->where('plat_type', $platType);
  49. if ($gameType !== null) {
  50. $gameQuery->where('game_type', $gameType);
  51. }
  52. if ($onlyEmpty) {
  53. $gameQuery->where(function ($query) {
  54. $query->whereNull('network')->orWhere('network', '');
  55. });
  56. }
  57. $platformCount = (clone $platformQuery)->count();
  58. $gameCount = (clone $gameQuery)->count();
  59. $label = $network === '' ? '不限制' : EgameItem::networkName($network);
  60. $this->line(sprintf(
  61. 'plat_type=%s game_type=%s network=%s(%s) only-empty=%s',
  62. $platType,
  63. $gameType === null ? '全部' : (string) $gameType,
  64. $network === '' ? 'none' : $network,
  65. $label,
  66. $onlyEmpty ? 'yes' : 'no'
  67. ));
  68. $this->line(sprintf('将更新平台行 %d 条、游戏 %d 条', $platformCount, $gameCount));
  69. if ($dryRun) {
  70. $this->info('dry-run,未写库');
  71. return self::SUCCESS;
  72. }
  73. $now = now();
  74. $platformUpdated = $platformQuery->update(['network' => $network, 'updated_at' => $now]);
  75. $gameUpdated = $gameQuery->update(['network' => $network, 'updated_at' => $now]);
  76. $this->info(sprintf('已更新平台行 %d、游戏 %d', $platformUpdated, $gameUpdated));
  77. return self::SUCCESS;
  78. }
  79. private function normalizedNetwork(string $value): ?string
  80. {
  81. $value = strtolower(trim($value));
  82. if (in_array($value, ['none', 'unrestricted', 'clear', '-'], true)) {
  83. return '';
  84. }
  85. if (in_array($value, EgameItem::networkValues(), true)) {
  86. return $value;
  87. }
  88. return null;
  89. }
  90. }