Skip to content

Commit 8a1a325

Browse files
committed
add query_port_variable to game queries
1 parent 1c12bd9 commit 8a1a325

File tree

8 files changed

+55
-9
lines changed

8 files changed

+55
-9
lines changed

player-counter/database/Seeders/PlayerCounterSeeder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,79 @@ class PlayerCounterSeeder extends Seeder
1515
'names' => 'Squad',
1616
'query_type' => 'source',
1717
'query_port_offset' => 19378,
18+
'query_port_variable' => null,
1819
],
1920
[
2021
'names' => 'Barotrauma',
2122
'query_type' => 'source',
2223
'query_port_offset' => 1,
24+
'query_port_variable' => null,
2325
],
2426
[
2527
'names' => 'Valheim',
2628
'query_type' => 'source',
2729
'query_port_offset' => 1,
30+
'query_port_variable' => null,
2831
],
2932
[
3033
'names' => ['V Rising', 'V-Rising', 'VRising'],
3134
'query_type' => 'source',
3235
'query_port_offset' => 1,
36+
'query_port_variable' => null,
3337
],
3438
[
3539
'names' => ['The Forrest', 'TheForrest'],
3640
'query_type' => 'source',
3741
'query_port_offset' => 1,
42+
'query_port_variable' => null,
3843
],
3944
[
4045
'names' => ['Arma 3', 'Arma3'],
4146
'query_type' => 'source',
4247
'query_port_offset' => 1,
48+
'query_port_variable' => null,
4349
],
4450
[
4551
'names' => ['Arma Reforger', 'ArmaReforger'],
4652
'query_type' => 'source',
4753
'query_port_offset' => 15776,
54+
'query_port_variable' => null,
4855
],
4956
[
5057
'names' => ['ARK: Survival Evolved', 'ARK: SurvivalEvolved', 'ARK Survival Evolved', 'ARK SurvivalEvolved', 'ARKSurvivalEvolved'],
5158
'query_type' => 'source',
5259
'query_port_offset' => 19238,
60+
'query_port_variable' => null,
5361
],
5462
[
5563
'names' => 'Unturned',
5664
'query_type' => 'source',
5765
'query_port_offset' => 1,
66+
'query_port_variable' => null,
5867
],
5968
[
6069
'names' => ['Insurgency: Sandstorm', 'Insurgency Sandstorm', 'InsurgencySandstorm'],
6170
'query_type' => 'source',
6271
'query_port_offset' => 29,
72+
'query_port_variable' => null,
6373
],
6474
[
6575
'tag' => 'bedrock',
6676
'query_type' => 'minecraft_bedrock',
6777
'query_port_offset' => null,
78+
'query_port_variable' => null,
6879
],
6980
[
7081
'tag' => 'minecraft',
7182
'query_type' => 'minecraft_java',
7283
'query_port_offset' => null,
84+
'query_port_variable' => null,
7385
],
7486
[
7587
'tag' => 'source',
7688
'query_type' => 'source',
7789
'query_port_offset' => null,
90+
'query_port_variable' => null,
7891
],
7992
];
8093

@@ -89,6 +102,7 @@ public function run(): void
89102
$query = GameQuery::firstOrCreate([
90103
'query_type' => $mapping['query_type'],
91104
'query_port_offset' => $mapping['query_port_offset'],
105+
'query_port_variable' => $mapping['query_port_variable'],
92106
]);
93107

94108
EggGameQuery::firstOrCreate([

player-counter/database/migrations/001_create_game_queries_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function up(): void
1212
$table->increments('id');
1313
$table->string('query_type');
1414
$table->integer('query_port_offset')->nullable();
15+
$table->string('query_port_variable')->nullable();
1516
$table->timestamps();
1617
});
1718
}

player-counter/lang/en/query.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
'type' => 'Type',
77
'port_offset' => 'Query Port Offset',
88
'no_offset' => 'No Offset',
9+
'port_offset_hint' => 'The offset that will be added to the allocation port. Usually empty/0 or 1.',
10+
'port_variable' => 'Query Port Variable',
11+
'no_variable' => 'No Variable',
12+
'port_variable_hint' => 'The env name of the startup variable that will be used to get the query port, e.g. "QUERY_PORT". When a value is set the query port offset will be ignored! Leave empty to use the allocation port and offset.',
913
'eggs' => 'Eggs',
1014
'no_eggs' => 'No Eggs',
1115
'hostname' => 'Hostname',

player-counter/src/Filament/Admin/Resources/GameQueries/GameQueryResource.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public static function table(Table $table): Table
5454
TextColumn::make('query_port_offset')
5555
->label(trans('player-counter::query.port_offset'))
5656
->placeholder(trans('player-counter::query.no_offset')),
57+
TextColumn::make('query_port_variable')
58+
->label(trans('player-counter::query.port_variable'))
59+
->placeholder(trans('player-counter::query.no_variable'))
60+
->badge(),
5761
TextColumn::make('eggs.name')
5862
->label(trans('player-counter::query.eggs'))
5963
->placeholder(trans('player-counter::query.no_eggs'))
@@ -85,14 +89,23 @@ public static function form(Schema $schema): Schema
8589
->options(fn (QueryTypeService $service) => $service->getMappings())
8690
->selectablePlaceholder(false)
8791
->preload()
88-
->searchable(),
92+
->searchable()
93+
->columnSpanFull(),
8994
TextInput::make('query_port_offset')
9095
->label(trans('player-counter::query.port_offset'))
9196
->placeholder(trans('player-counter::query.no_offset'))
9297
->numeric()
9398
->nullable()
9499
->minValue(1)
95-
->maxValue(65535 - 1024),
100+
->maxValue(65535 - 1024)
101+
->hintIcon('tabler-question-mark')
102+
->hintIconTooltip(trans('player-counter::query.port_offset_hint')),
103+
TextInput::make('query_port_variable')
104+
->label(trans('player-counter::query.port_variable'))
105+
->placeholder(trans('player-counter::query.no_variable'))
106+
->nullable()
107+
->hintIcon('tabler-question-mark')
108+
->hintIconTooltip(trans('player-counter::query.port_variable_hint')),
96109
Select::make('eggs')
97110
->label(trans('admin/mount.eggs'))
98111
->relationship('eggs', 'name')

player-counter/src/Filament/Server/Pages/PlayersPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function table(Table $table): Table
117117
$gameQuery = $server->egg->gameQuery; // @phpstan-ignore property.notFound
118118

119119
if ($gameQuery) {
120-
$data = $gameQuery->runQuery($server->allocation);
120+
$data = $gameQuery->runQuery($server);
121121

122122
if ($data) {
123123
$players = $data['players'] ?? [];

player-counter/src/Filament/Server/Widgets/ServerPlayerWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected function getStats(): array
4545
return [];
4646
}
4747

48-
$data = $gameQuery->runQuery($server->allocation) ?? [];
48+
$data = $gameQuery->runQuery($server) ?? [];
4949

5050
return [
5151
SmallStatBlock::make(trans('player-counter::query.hostname'), $data['hostname'] ?? trans('player-counter::query.unknown')),

player-counter/src/Http/Controllers/Api/Client/Servers/PlayerCounterController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ private function runQuery(Server $server): ?array
6666
abort(Response::HTTP_NOT_ACCEPTABLE, 'Server has no query');
6767
}
6868

69-
return $gameQuery->runQuery($server->allocation);
69+
return $gameQuery->runQuery($server);
7070
}
7171
}

player-counter/src/Models/GameQuery.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Allocation;
66
use App\Models\Egg;
7+
use App\Models\Server;
78
use Boy132\PlayerCounter\Extensions\Query\QueryTypeService;
89
use Illuminate\Database\Eloquent\Collection;
910
use Illuminate\Database\Eloquent\Model;
@@ -13,6 +14,7 @@
1314
* @property int $id
1415
* @property string $query_type
1516
* @property ?int $query_port_offset
17+
* @property ?string $query_port_variable
1618
* @property Collection|Egg[] $eggs
1719
* @property int|null $eggs_count
1820
*/
@@ -21,10 +23,12 @@ class GameQuery extends Model
2123
protected $fillable = [
2224
'query_type',
2325
'query_port_offset',
26+
'query_port_variable',
2427
];
2528

2629
protected $attributes = [
2730
'query_port_offset' => null,
31+
'query_port_variable' => null,
2832
];
2933

3034
public function eggs(): BelongsToMany
@@ -33,19 +37,29 @@ public function eggs(): BelongsToMany
3337
}
3438

3539
/** @return ?array{hostname: string, map: string, current_players: int, max_players: int, players: ?array<array{id: string, name: string}>} */
36-
public function runQuery(Allocation $allocation): ?array
40+
public function runQuery(Server $server): ?array
3741
{
38-
if (!static::canRunQuery($allocation)) {
42+
if (!static::canRunQuery($server->allocation)) {
3943
return null;
4044
}
4145

42-
$ip = config('player-counter.use_alias') && is_ip($allocation->alias) ? $allocation->alias : $allocation->ip;
46+
$ip = config('player-counter.use_alias') && is_ip($server->allocation->alias) ? $server->allocation->alias : $server->allocation->ip;
4347
$ip = is_ipv6($ip) ? '[' . $ip . ']' : $ip;
4448

49+
$port = $server->allocation->port + ($this->query_port_offset ?? 0);
50+
51+
if ($this->query_port_variable) {
52+
$variableValue = $server->variables()->where('env_variable', $this->query_port_variable)->first()?->server_value;
53+
54+
if ($variableValue && is_int($variableValue)) {
55+
$port = $variableValue;
56+
}
57+
}
58+
4559
/** @var QueryTypeService $service */
4660
$service = app(QueryTypeService::class);
4761

48-
return $service->get($this->query_type)?->process($ip, $allocation->port + ($this->query_port_offset ?? 0));
62+
return $service->get($this->query_type)?->process($ip, $port);
4963
}
5064

5165
public static function canRunQuery(?Allocation $allocation): bool

0 commit comments

Comments
 (0)