Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Civ13/GameServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 16 additions & 0 deletions src/Civ13/ServerHeartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions tests/local/heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Loading