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
26 changes: 22 additions & 4 deletions src/Civ13/HttpServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,18 +298,36 @@ public function handleRoundStartWebhook(ServerRequestInterface $request, GameSer
return new HttpResponse(HttpResponse::STATUS_SERVICE_UNAVAILABLE);
}
// A round reloaded from a save keeps its identity; announce it without the ping.
$name = self::noticeServerName($gameserver, $data);
if (in_array($data['resumed'] ?? false, [true, 1, '1'], true)) {
$message = 'Round `'.$data['round'].'` has resumed from a save.';
$message = 'Round `'.$data['round'].'` in **'.$name.'** has resumed from a save.';
} else {
$role = $this->civ13->role_ids['round_start'] ?? null;
$message = ($role ? "<@&$role>, " : '').'New round `'.$data['round'].'` has started!';
$message = ($role ? "<@&$role>, " : '').'New round `'.$data['round'].'` has started in **'.$name.'**!';
}
$this->civ13->sendMessage($channel, $message)->then(null, function (\Throwable $error): void {
$this->logger->error('Queued round notification failed', ['exception' => $error]);
});
return new HttpResponse(HttpResponse::STATUS_ACCEPTED);
}

/**
* The server name for round, save and shutdown notices. Every region of a group posts to
* the same bot server, so the game sends its region's display name (Civ13 webhook_add_region)
* and the notice says "Persistence (North America)". The name is reduced to plain words so
* it can neither ping nor break the Markdown around it; a missing or matching name adds nothing.
*/
public static function noticeServerName(GameServer $gameserver, array $data): string
{
$name = is_string($gameserver->name ?? null) && $gameserver->name !== '' ? $gameserver->name : $gameserver->key;
$region = $data['region_name'] ?? null;
if (!is_string($region)) return $name;
$region = trim(preg_replace('/\s+/u', ' ', preg_replace("/[^\\p{L}\\p{N} .,'()-]+/u", ' ', $region) ?? '') ?? '');
$region = mb_substr($region, 0, 80);
if ($region === '' || strcasecmp($region, $name) === 0) return $name;
return "{$name} ({$region})";
}

/**
* World save notices from a persistent world: "started" when a save begins,
* then "completed" (with its wall time) or "failed". Posted to the server's
Expand All @@ -335,7 +353,7 @@ public function handleWorldSaveWebhook(ServerRequestInterface $request, GameServ
return new HttpResponse(HttpResponse::STATUS_SERVICE_UNAVAILABLE);
}
$shutdown = in_array($data['shutdown'] ?? false, [true, 1, '1'], true);
$name = is_string($gameserver->name ?? null) && $gameserver->name !== '' ? $gameserver->name : $gameserver->key;
$name = self::noticeServerName($gameserver, $data);
$time = '['.date('H:i:s').']';
$message = match ($event) {
'started' => "{$time} **{$name}**: world save started".($shutdown ? ' before a restart' : '').'. The server may pause briefly.',
Expand Down Expand Up @@ -374,7 +392,7 @@ public function handleShutdownNoticeWebhook(ServerRequestInterface $request, Gam
if (!isset($gameserver->discussion) || !$channel = $this->discord->getChannel($gameserver->discussion)) {
return new HttpResponse(HttpResponse::STATUS_SERVICE_UNAVAILABLE);
}
$name = is_string($gameserver->name ?? null) && $gameserver->name !== '' ? $gameserver->name : $gameserver->key;
$name = self::noticeServerName($gameserver, $data);
$time = '['.date('H:i:s').']';
$what = match ($kind) {
'shutdown' => 'save and shut down',
Expand Down
6 changes: 6 additions & 0 deletions tests/local/ooc-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public function scrutinizeCkey(string $ckey):void {$this->actors[]=$ckey;}
if($handler->handle($shutdownRequest(['event'=>'scheduled','kind'=>'reboot','seconds'=>0,'reason'=>'restart by server agent <@&1> @everyone']))->getStatusCode()!==202||!str_contains($now=end($civ->staffMessages)[1],'will restart now. (restart by server agent')||str_contains($now,'@')||str_contains($now,'<')){throw new RuntimeException('Reason pinged or immediate restart not announced: '.$now);}
if($handler->handle($shutdownRequest(['event'=>'cancelled','kind'=>'shutdown']))->getStatusCode()!==202||!str_contains(end($civ->staffMessages)[1],'the planned shutdown was called off.')){throw new RuntimeException('Called-off shutdown not announced');}
if(end($civ->staffMessages)[2]!==true){throw new RuntimeException('Shutdown notice can ping through the server name');}
// Region groups: every region posts to one bot server, so each notice names its region.
if($handler->handle($roundRequest(['round'=>'round-1','resumed'=>1,'region'=>'northamerica','region_name'=>'North America']))->getStatusCode()!==202||!str_contains(end($civ->staffMessages)[1],'(North America)** has resumed from a save.')){throw new RuntimeException('Resumed round did not name its region: '.end($civ->staffMessages)[1]);}
if($handler->handle($saveRequest(['event'=>'started','shutdown'=>1,'region_name'=>'North America']))->getStatusCode()!==202||!str_contains(end($civ->staffMessages)[1],' (North America)**: world save started before a restart')){throw new RuntimeException('World save did not name its region');}
if($handler->handle($saveRequest(['event'=>'completed','seconds'=>337.6,'region_name'=>'North America']))->getStatusCode()!==202||!str_contains(end($civ->staffMessages)[1],' (North America)**: world save completed in 337.6 s.')){throw new RuntimeException('World save completion did not name its region');}
if($handler->handle($shutdownRequest(['event'=>'scheduled','kind'=>'update','seconds'=>0,'region_name'=>'South <@&1> America** @everyone']))->getStatusCode()!==202||!str_contains($regional=end($civ->staffMessages)[1],' (South 1 America everyone)** will move to an updated build now.')||str_contains($regional,'@')||str_contains($regional,'<')){throw new RuntimeException('Region name pinged or was not cleaned: '.$regional);}
if($handler->handle($saveRequest(['event'=>'failed','region_name'=>[]]))->getStatusCode()!==202||str_contains(end($civ->staffMessages)[1],'(')){throw new RuntimeException('Malformed region name was shown');}
$messages=count($civ->staffMessages);$server->denyRound=true;
if($handler->handle($roundRequest(['round'=>'round-2']))->getStatusCode()!==503||count($civ->staffMessages)!==$messages){throw new RuntimeException('Failed round storage falsely notified');}
$server->denyRound=false;$civ->staffMessages=[];
Expand Down
Loading