diff --git a/src/Civ13/GameServer.php b/src/Civ13/GameServer.php index c0e28346..7dcac473 100644 --- a/src/Civ13/GameServer.php +++ b/src/Civ13/GameServer.php @@ -2131,7 +2131,7 @@ private function heartbeatEmbed(array $status): Embed 'stale' => 'Stale heartbeat', default => 'Unknown', }) - ->addFieldValues('Observation', $status['reason'].($status['age'] === null ? '' : ' ('.(int) $status['age'].'s since last observed progress/baseline)'), false) + ->addFieldValues('Observation', $status['reason'].($status['age'] === null ? '' : ' ('.ServerHeartbeat::formatAge($status['age']).' since last observed progress/baseline)'), false) ->addFieldValues('Host', $this->displayHost(), true); } diff --git a/src/Civ13/ServerHeartbeat.php b/src/Civ13/ServerHeartbeat.php index e5ee6b91..2bb7b63a 100644 --- a/src/Civ13/ServerHeartbeat.php +++ b/src/Civ13/ServerHeartbeat.php @@ -20,6 +20,22 @@ public function __construct(private string $path, private ?\Closure $clock = nul { } + /** Readable age for staff messages: "45s", "7h 11m", "1d 0h 3m" (seconds only under a minute). */ + public static function formatAge(int|float $seconds): string + { + $seconds = max(0, (int) $seconds); + if ($seconds < 60) { + return $seconds.'s'; + } + $days = intdiv($seconds, 86400); + $hours = intdiv($seconds % 86400, 3600); + $minutes = intdiv($seconds % 3600, 60); + if ($days > 0) { + return "{$days}d {$hours}h {$minutes}m"; + } + return $hours > 0 ? "{$hours}h {$minutes}m" : "{$minutes}m"; + } + /** @return array{state:string, reason:string, age:?float, data:?string} */ public function poll(): array { diff --git a/tests/local/heartbeat.php b/tests/local/heartbeat.php index ee9e14ff..bf40567c 100644 --- a/tests/local/heartbeat.php +++ b/tests/local/heartbeat.php @@ -107,4 +107,7 @@ function record(string $realtime, string $players = '', bool $approved = false): if (is_file($root.'/serverdata.txt')) { unlink($root.'/serverdata.txt'); } rmdir($root); } +foreach ([[0, '0s'], [45.9, '45s'], [60, '1m'], [25901, '7h 11m'], [3600, '1h 0m'], [93780, '1d 2h 3m'], [86400, '1d 0h 0m'], [-5, '0s']] as [$seconds, $expected]) { + check(ServerHeartbeat::formatAge($seconds) === $expected, "Heartbeat age {$seconds}s must read {$expected}"); +} echo "Heartbeat offline contracts passed\n";