Skip to content

Commit 9c05266

Browse files
author
makepeaceJ
committed
Added Palworld schema. Adjusted to handle looking at servers env settings for admin password.
1 parent 1eb679c commit 9c05266

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Boy132\PlayerCounter\Extensions\Query\Schemas;
4+
5+
use App\Models\Server;
6+
use Boy132\PlayerCounter\Extensions\Query\ServerAwareQueryTypeSchemaInterface;
7+
use Illuminate\Support\Facades\Http;
8+
9+
class PalworldQueryTypeSchema implements ServerAwareQueryTypeSchemaInterface
10+
{
11+
public function getId(): string
12+
{
13+
return 'palworld';
14+
}
15+
16+
public function getName(): string
17+
{
18+
return 'Palworld';
19+
}
20+
21+
// Fallback: no auth context available — cannot query REST API
22+
public function process(string $ip, int $port): ?array
23+
{
24+
return null;
25+
}
26+
27+
public function processWithServer(Server $server, string $ip, int $port): ?array
28+
{
29+
$adminPassword = $server->variables()
30+
->where('env_variable', 'ADMIN_PASSWORD')
31+
->first()?->server_value;
32+
33+
if (!$adminPassword) {
34+
return null;
35+
}
36+
37+
try {
38+
$response = Http::timeout(5)
39+
->withBasicAuth('admin', $adminPassword)
40+
->get("http://{$ip}:{$port}/v1/api/players");
41+
42+
if (!$response->ok()) {
43+
return null;
44+
}
45+
46+
$data = $response->json();
47+
$players = array_map(fn ($p) => [
48+
'id' => $p['playeruid'] ?? $p['steamid'] ?? '',
49+
'name' => $p['name'] ?? '',
50+
], $data['players'] ?? []);
51+
52+
// Fetch metrics for servername and max_players
53+
$metrics = Http::timeout(5)
54+
->withBasicAuth('admin', $adminPassword)
55+
->get("http://{$ip}:{$port}/v1/api/metrics")
56+
->json();
57+
58+
return [
59+
'hostname' => $metrics['servername'] ?? $server->name,
60+
'map' => 'Palpagos Islands',
61+
'current_players' => count($players),
62+
'max_players' => $metrics['maxplayers'] ?? 32,
63+
'players' => $players,
64+
];
65+
} catch (\Throwable $e) {
66+
report($e);
67+
return null;
68+
}
69+
}
70+
}
71+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Boy132\PlayerCounter\Extensions\Query;
4+
5+
use App\Models\Server;
6+
7+
interface ServerAwareQueryTypeSchemaInterface extends QueryTypeSchemaInterface
8+
{
9+
/** @return ?array{hostname: string, map: string, current_players: int, max_players: int, players: ?array<array{id: string, name: string}>} */
10+
public function processWithServer(Server $server, string $ip, int $port): ?array;
11+
}

player-counter/src/Models/GameQuery.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\Egg;
77
use App\Models\Server;
88
use Boy132\PlayerCounter\Extensions\Query\QueryTypeService;
9+
use Boy132\PlayerCounter\Extensions\Query\ServerAwareQueryTypeSchemaInterface;
910
use Illuminate\Database\Eloquent\Collection;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -59,7 +60,12 @@ public function runQuery(Server $server): ?array
5960
/** @var QueryTypeService $service */
6061
$service = app(QueryTypeService::class);
6162

62-
return $service->get($this->query_type)?->process($ip, $port);
63+
$schema = $service->get($this->query_type);
64+
if ($schema instanceof ServerAwareQueryTypeSchemaInterface) {
65+
return $schema->processWithServer($server, $ip, $port);
66+
}
67+
68+
return $schema?->process($ip, $port);
6369
}
6470

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

player-counter/src/Providers/PlayerCounterPluginProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Boy132\PlayerCounter\Extensions\Query\Schemas\GoldSourceQueryTypeSchema;
1212
use Boy132\PlayerCounter\Extensions\Query\Schemas\MinecraftBedrockQueryTypeSchema;
1313
use Boy132\PlayerCounter\Extensions\Query\Schemas\MinecraftJavaQueryTypeSchema;
14+
use Boy132\PlayerCounter\Extensions\Query\Schemas\PalworldQueryTypeSchema;
1415
use Boy132\PlayerCounter\Extensions\Query\Schemas\SourceQueryTypeSchema;
1516
use Boy132\PlayerCounter\Filament\Server\Widgets\ServerPlayerWidget;
1617
use Boy132\PlayerCounter\Models\EggGameQuery;
@@ -35,6 +36,7 @@ public function register(): void
3536
$service->register(new MinecraftJavaQueryTypeSchema());
3637
$service->register(new MinecraftBedrockQueryTypeSchema());
3738
$service->register(new CitizenFXQueryTypeSchema());
39+
$service->register(new PalworldQueryTypeSchema());
3840

3941
return $service;
4042
});

0 commit comments

Comments
 (0)