| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace App\Console\Commands;
- use App\Models\EgameItem;
- use Illuminate\Console\Command;
- class EgameSetNetwork extends Command
- {
- protected $signature = 'egame:set-network
- {network : mainland / hk / kr / jp / none(none 表示不限制)}
- {--plat-type= : 平台代码,如 jdb、pg}
- {--game-type= : 游戏大类,1~7;不传则该平台全部大类}
- {--force : 覆盖已有网络;默认只改空行}
- {--dry-run : 只统计不写库}';
- protected $description = '按平台批量设置支持网络,并同步到该平台下的游戏(用于正式服已有目录,不要逐条改)';
- public function handle(): int
- {
- $network = $this->normalizedNetwork((string) $this->argument('network'));
- if ($network === null) {
- $this->error('network 只允许 mainland、hk、kr、jp、none');
- return self::FAILURE;
- }
- $platType = strtolower(trim((string) $this->option('plat-type')));
- $gameTypeOption = $this->option('game-type');
- $gameType = $gameTypeOption === null || $gameTypeOption === '' ? null : (int) $gameTypeOption;
- $onlyEmpty = !$this->option('force');
- $dryRun = (bool) $this->option('dry-run');
- if ($platType === '') {
- $this->error('必须指定 --plat-type');
- return self::FAILURE;
- }
- if ($gameType !== null && ($gameType < 1 || $gameType > 7)) {
- $this->error('--game-type 必须是 1~7');
- return self::FAILURE;
- }
- $platformQuery = EgameItem::query()
- ->where('item_type', EgameItem::TYPE_PLATFORM)
- ->where('plat_type', $platType)
- ->where('game_type', '>', 0);
- if ($gameType !== null) {
- $platformQuery->where('game_type', $gameType);
- }
- if ($onlyEmpty) {
- $platformQuery->where(function ($query) {
- $query->whereNull('network')->orWhere('network', '');
- });
- }
- $gameQuery = EgameItem::query()
- ->where('item_type', EgameItem::TYPE_GAME)
- ->where('plat_type', $platType);
- if ($gameType !== null) {
- $gameQuery->where('game_type', $gameType);
- }
- if ($onlyEmpty) {
- $gameQuery->where(function ($query) {
- $query->whereNull('network')->orWhere('network', '');
- });
- }
- $platformCount = (clone $platformQuery)->count();
- $gameCount = (clone $gameQuery)->count();
- $label = $network === '' ? '不限制' : EgameItem::networkName($network);
- $this->line(sprintf(
- 'plat_type=%s game_type=%s network=%s(%s) only-empty=%s',
- $platType,
- $gameType === null ? '全部' : (string) $gameType,
- $network === '' ? 'none' : $network,
- $label,
- $onlyEmpty ? 'yes' : 'no'
- ));
- $this->line(sprintf('将更新平台行 %d 条、游戏 %d 条', $platformCount, $gameCount));
- if ($dryRun) {
- $this->info('dry-run,未写库');
- return self::SUCCESS;
- }
- $now = now();
- $platformUpdated = $platformQuery->update(['network' => $network, 'updated_at' => $now]);
- $gameUpdated = $gameQuery->update(['network' => $network, 'updated_at' => $now]);
- $this->info(sprintf('已更新平台行 %d、游戏 %d', $platformUpdated, $gameUpdated));
- return self::SUCCESS;
- }
- private function normalizedNetwork(string $value): ?string
- {
- $value = strtolower(trim($value));
- if (in_array($value, ['none', 'unrestricted', 'clear', '-'], true)) {
- return '';
- }
- if (in_array($value, EgameItem::networkValues(), true)) {
- return $value;
- }
- return null;
- }
- }
|