diff --git a/src/Civ13/HttpServiceManager.php b/src/Civ13/HttpServiceManager.php index 4fe1342f..4ee09fb4 100644 --- a/src/Civ13/HttpServiceManager.php +++ b/src/Civ13/HttpServiceManager.php @@ -461,6 +461,24 @@ public static function shutdownNoticeDuration(int $seconds): string return $rest ? $text.' '.$rest.' second'.($rest === 1 ? '' : 's') : $text; } + /** + * Whether a staff-channel line from the game should ping @Admin. + * false: never (staff wrote it, or the game reports staff online); + * true: ping (the game reports no staff online); + * null: the game sent no presence fields, so guess from Discord presence. + */ + public static function staffWebhookNeedsPing(array $data): ?bool + { + $truthy = static fn ($value): bool => $value === true || $value === 1 || $value === '1'; + if ($truthy($data['from_staff'] ?? null)) { return false; } + if (($data['kind'] ?? null) === 'asay') { return false; } // Only staff can asay. + // Games before the presence fields still mark asay lines in the text. + if (! isset($data['kind']) && is_string($data['message'] ?? null) && preg_match('/^__\*\*\[[^\]]*\] \([^)]*\) ASAY:\*\*__/', $data['message'])) { return false; } + if (isset($data['staff_online']) && is_numeric($data['staff_online'])) { return (int) $data['staff_online'] <= 0; } + + return null; + } + /** Staff messages preserve display text without inferring account authority. */ public function handleStaffWebhook(ServerRequestInterface $request, GameServer $gameserver, string $kind = 'asay'): HttpResponse { @@ -496,20 +514,24 @@ public function handleStaffWebhook(ServerRequestInterface $request, GameServer $ if ($kind === 'urgent') { return new HttpResponse(HttpResponse::STATUS_ACCEPTED); } + // Staff wrote it, or the game says staff are online: never ping. + $presence = self::staffWebhookNeedsPing($data); + if ($presence === false) { return new HttpResponse(HttpResponse::STATUS_ACCEPTED); } + // Notify staff based on verified online Discord members only. if (isset($this->civ13->role_ids['Admin']) && $guild = $this->discord->guilds->get('id', $this->civ13->civ13_guild_id)) { $urgent = true; - $admin = false; + $admin = $presence === true; // The game reported no online staff; skip the Discord guess. if (isset($this->civ13->verifier)) { if (! $admin) { - if ($playerlist = $gameserver->players) { + if ($playerlist = array_map('strtolower', $gameserver->players)) { if ($admins = $guild->members->filter(function (Member $member) { return $member->roles->has($this->civ13->role_ids['Admin']); })) { try { foreach ($admins as $member) { if ($item = $this->civ13->verifier->get('discord', $member->id)) { - if (in_array($item['ss13'], $playerlist)) { + if (in_array(strtolower((string) $item['ss13']), $playerlist, true)) { $urgent = false; break; } diff --git a/tests/local/ooc-webhook.php b/tests/local/ooc-webhook.php index b0f665ed..62404cb6 100644 --- a/tests/local/ooc-webhook.php +++ b/tests/local/ooc-webhook.php @@ -208,6 +208,20 @@ public function scrutinizeCkey(string $ckey):void {$this->actors[]=$ckey;} $relayed=count($server->calls); if($handler->handle($asay)->getStatusCode()!==202||count($server->calls)!==$relayed+1){throw new RuntimeException('Verifier outage failed staff webhook');} if(count($civ->staffMessages)!==2||!str_contains($civ->staffMessages[1][1],'<@&admin-role>')){throw new RuntimeException('Verifier outage suppressed staff notification');} +// Admin's own asay never pings, even when Discord presence cannot see them (game flags, then legacy text marker). +$civ->verifier->failure=null;$pinged=count($civ->staffMessages);$members->list=[]; +foreach([['kind'=>'asay','from_staff'=>1,'staff_online'=>0,'message'=>'x'],['kind'=>'adminpm','from_staff'=>1,'staff_online'=>0,'message'=>'x'],['message'=>'__**[22:50:01] (gaia) ASAY:**__ **Val** hi']] as $own){ + if($handler->handle($asay->withQueryParams(['key'=>'synthetic-ooc-key','method'=>'asaymessage','data'=>json_encode(['ckey'=>'Val']+$own)]))->getStatusCode()!==202){throw new RuntimeException('Own staff line not relayed');} +} +if(count($civ->staffMessages)!==$pinged){throw new RuntimeException('Admin was pinged for their own staff line');} +$help=fn($online)=>$asay->withQueryParams(['key'=>'synthetic-ooc-key','method'=>'asaymessage','data'=>json_encode(['ckey'=>'player','kind'=>'ahelp','from_staff'=>0,'staff_online'=>$online,'message'=>'help'])]); +$handler->handle($help(2)); +if(count($civ->staffMessages)!==$pinged){throw new RuntimeException('Ahelp pinged while the game reported staff online');} +$members->list=[(new ReflectionClass(OocMember::class))->newInstanceWithoutConstructor()];$server->players=['onlineadmin']; +$handler->handle($help(0)); +if(count($civ->staffMessages)!==$pinged+1){throw new RuntimeException('Ahelp did not ping when the game reported no staff online');} +array_pop($civ->staffMessages);$civ->verifier->failure=new \Civ13\Exceptions\VerifierException('Hosted verification unavailable'); +if(\Civ13\HttpServiceManager::staffWebhookNeedsPing(['message'=>'**AHELP** text'])!==null){throw new RuntimeException('Legacy ahelp lost Discord presence fallback');} $long=$asay->withQueryParams(['key'=>'synthetic-ooc-key','method'=>'asaymessage','data'=>json_encode(['message'=>str_repeat('é',4000)])]); if($handler->handle($long)->getStatusCode()!==202||!isset($civ->staffMessages[2][1])||strlen($civ->staffMessages[2][1])>2000||!str_contains($civ->staffMessages[2][1],'<@&admin-role>')||!preg_match('//u',$civ->staffMessages[2][1])){throw new RuntimeException('Long asay notification exceeds plain-content limit');} $civ->verifier->failure=null;array_pop($civ->staffMessages);array_pop($civ->staffMessages);$civ->verifier->lookups=[['discord','staff-member']];