Skip to content

Commit 5c80edc

Browse files
committed
update and fix palworld query type schema
1 parent 9049b4d commit 5c80edc

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

player-counter/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ Show the amount of connected players to game servers with real-time querying cap
44

55
## Setup
66

7-
Make sure your server has an allocation with a public ip. Alternatively, if you use local ips you can put the public ip in the allocation alias and enable "Use allocation alias?" in the plugin settings. (Using a domain as allocation alias or `0.0.0.0`/`::` as allocation ip will not work!)
7+
Make sure your server has an allocation with a public ip. Alternatively, if you use local ips you can put the public ip in the allocation alias and enable "Use allocation alias?" in the plugin settings. Using a domain as allocation alias or `0.0.0.0`/`::` as allocation ip will not work!
8+
9+
### Minecraft
810

911
Minecraft servers will first try the query (which requires you to set `enable-query` to true and `query-port` to your server port in `server.properties`) and will fallback to ping. It is recommended to enable query.
1012

13+
### Palworld
14+
15+
For Palworld servers you need to set `RESTAPIEnabled` to `true` and `RESTAPIPort` to your server port in `PalWorldSettings.ini`. You also need to set an admin password via the `ADMIN_PASSWORD` startup variable.
16+
1117
## Features
1218

1319
- Real-time player count display for game servers

player-counter/src/Extensions/Query/Schemas/PalworldQueryTypeSchema.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,24 @@ public function process(Server $server, string $ip, int $port): ?array
3030
}
3131

3232
try {
33-
$base = "http://{$ip}:{$port}";
34-
[$playersResp, $metricsResp] = Http::pool(fn ($pool) => [
35-
$pool->timeout(5)->withBasicAuth('admin', $adminPassword)->get("{$base}/v1/api/players"),
36-
$pool->timeout(5)->withBasicAuth('admin', $adminPassword)->get("{$base}/v1/api/metrics"),
37-
]);
33+
$http = Http::acceptJson()
34+
->timeout(5)
35+
->withBasicAuth('admin', $adminPassword)
36+
->throw()
37+
->baseUrl("http://{$ip}:{$port}");
3838

39-
if (!$playersResp->ok()) {
40-
return null;
41-
}
39+
$info = $http->get('v1/api/info')->json();
40+
$metrics = $http->get('v1/api/metrics')->json();
41+
$players = $http->get('v1/api/players')->json();
4242

43-
$data = $playersResp->json();
44-
$players = array_map(fn ($p) => [
45-
'id' => $p['playeruid'] ?? $p['steamid'] ?? '',
46-
'name' => $p['name'] ?? '',
47-
], $data['players'] ?? []);
48-
49-
$maxPlayers = $metricsResp->ok() ? ($metricsResp->json()['maxplayernum'] ?? 32) : 32;
43+
$players = $players['players'] ?? [];
5044

5145
return [
52-
'hostname' => $server->name,
46+
'hostname' => $info['servername'],
5347
'map' => 'Palpagos Islands',
54-
'current_players' => count($players),
55-
'max_players' => $maxPlayers,
56-
'players' => $players,
48+
'current_players' => $metrics['currentplayernum'],
49+
'max_players' => $metrics['maxplayernum'],
50+
'players' => array_map(fn ($player) => ['id' => (string) ($player['playerId'] ?? $player['userId']), 'name' => (string) $player['name']], $players),
5751
];
5852

5953
} catch (Exception $exception) {

0 commit comments

Comments
 (0)