75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LauncherRealmService
|
|
{
|
|
public function __construct(
|
|
private readonly RealmStatusProbe $statusProbe,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, array<string, bool|float|int|string>>
|
|
*/
|
|
public function realms(): Collection
|
|
{
|
|
$cacheSeconds = max(0, (int) config('moonwell.launcher.realm_status_cache', 15));
|
|
|
|
if ($cacheSeconds === 0) {
|
|
return $this->loadRealms();
|
|
}
|
|
|
|
return Cache::remember(
|
|
'launcher.realm-status',
|
|
now()->addSeconds($cacheSeconds),
|
|
fn (): Collection => $this->loadRealms(),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, array<string, bool|float|int|string>>
|
|
*/
|
|
private function loadRealms(): Collection
|
|
{
|
|
return DB::connection(config('moonwell.auth_connection'))
|
|
->table('realmlist')
|
|
->orderBy('id')
|
|
->get([
|
|
'id',
|
|
'name',
|
|
'address',
|
|
'port',
|
|
'icon',
|
|
'flag',
|
|
'timezone',
|
|
'population',
|
|
])
|
|
->map(function (object $realm): array {
|
|
$probeAddress = trim((string) config('moonwell.launcher.realm_status_host'));
|
|
$online = $this->statusProbe->isOnline(
|
|
$probeAddress !== '' ? $probeAddress : (string) $realm->address,
|
|
(int) $realm->port,
|
|
);
|
|
|
|
return [
|
|
'id' => (int) $realm->id,
|
|
'name' => (string) $realm->name,
|
|
'address' => (string) $realm->address,
|
|
'port' => (int) $realm->port,
|
|
'icon' => (int) $realm->icon,
|
|
'flag' => (int) $realm->flag,
|
|
'timezone' => (int) $realm->timezone,
|
|
'population' => (float) $realm->population,
|
|
'online' => $online,
|
|
'status' => $online ? 'online' : 'offline',
|
|
];
|
|
})
|
|
->values();
|
|
}
|
|
}
|