Consolidate rights-granting through a single function

grantRights() is now the only place a player's RightsForPlayer{N}
setting ever gets written. All three previous call sites (verify+cat
after chat tracking, cd+decode after /verify, and the all-players
grant from decoding) now delegate to it instead of duplicating the
same read-modify-persist logic.

The main point: since every grant now flows through one place, it can
notify the specific player over Mercure the moment they receive new
rights, instead of them only finding out by trying a command that
used to be "Unknown command".

Also includes an already-present (uncommitted) tweak allowing bare
chat/verify/decode console input without the leading slash - unrelated
to this change but sitting in the same file, so it's coming along.
This commit is contained in:
Frank
2026-08-23 21:07:36 +02:00
parent 7f389fbbc2
commit 1c0ab4780b
+52 -73
View File
@@ -294,6 +294,10 @@ class GameResponseService
$result[] = 'These files are protected by the AI virus. Use sudo rm {file} to remove them.'; $result[] = 'These files are protected by the AI virus. Use sudo rm {file} to remove them.';
return ['result' => $result]; return ['result' => $result];
case 'chat':
case 'verify':
case 'decode':
return $this->checkGameCommando('/'.$message, $player);
default: default:
return ['result' => ['Unknown command']]; return ['result' => ['Unknown command']];
} }
@@ -539,33 +543,7 @@ class GameResponseService
} }
// Grant verify right // Grant verify right
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $screen); $this->grantRights($player, ['verify', 'cat']);
if (!$rightsSettingName) {
return;
}
$setting = $this->sessionSettingRepository->getSetting($player->getSession(), $rightsSettingName, $player);
if (!$setting) {
return; // Should have been initialized
}
$rights = json_decode($setting->getValue() ?? '[]', true) ?? [];
$newRights = ['verify', 'cat'];
$updated = false;
foreach ($newRights as $newRight) {
if (!in_array($newRight, $rights)) {
$rights[] = $newRight;
$updated = true;
}
}
if ($updated) {
$setting->setValue(json_encode($rights));
$this->entityManager->persist($setting);
$this->entityManager->flush();
}
} }
private function handleDecodeMessage(string $message, Player $player): string private function handleDecodeMessage(string $message, Player $player): string
@@ -608,31 +586,59 @@ class GameResponseService
private function grantRightToAllPlayers(Session $session, string $right): void private function grantRightToAllPlayers(Session $session, string $right): void
{ {
$updated = false;
foreach ($session->getPlayers() as $sessionPlayer) { foreach ($session->getPlayers() as $sessionPlayer) {
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $sessionPlayer->getScreen()); $this->grantRights($sessionPlayer, [$right]);
if (!$rightsSettingName) { }
continue; }
}
$setting = $this->sessionSettingRepository->getSetting($session, $rightsSettingName, $sessionPlayer); /**
if (!$setting) { * The single place a player's rights ever get granted. Persists any rights the
continue; * player doesn't already have and, if anything actually changed, notifies that
} * player (and only that player) over Mercure that they've gained access.
*
* @param string[] $newRights
* @return bool Whether any right was actually newly granted.
*/
private function grantRights(Player $player, array $newRights): bool
{
$screen = $player->getScreen();
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $screen);
$rights = json_decode($setting->getValue() ?? '[]', true) ?? []; if (!$rightsSettingName) {
if (!in_array($right, $rights)) { return false;
$rights[] = $right; }
$setting->setValue(json_encode($rights));
$this->entityManager->persist($setting); $setting = $this->sessionSettingRepository->getSetting($player->getSession(), $rightsSettingName, $player);
$updated = true; if (!$setting) {
return false; // Should have been initialized
}
$rights = json_decode($setting->getValue() ?? '[]', true) ?? [];
$granted = [];
foreach ($newRights as $newRight) {
if (!in_array($newRight, $rights)) {
$rights[] = $newRight;
$granted[] = $newRight;
} }
} }
if ($updated) { if (empty($granted)) {
$this->entityManager->flush(); return false;
} }
$setting->setValue(json_encode($rights));
$this->entityManager->persist($setting);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new PushMercureMessageEvent(
$player->getSession(),
[$screen, 'MAINFRAME: You have received new access: ' . implode(', ', $granted) . '.', 'mainframe'],
'rights_granted',
$screen,
));
return true;
} }
/** /**
@@ -892,34 +898,7 @@ class GameResponseService
private function grantVerificationRights(Player $player): void private function grantVerificationRights(Player $player): void
{ {
$screen = $player->getScreen(); if ($this->grantRights($player, ['cd', 'decode'])) {
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $screen);
if (!$rightsSettingName) {
return;
}
$setting = $this->sessionSettingRepository->getSetting($player->getSession(), $rightsSettingName, $player);
if (!$setting) {
return;
}
$rights = json_decode($setting->getValue() ?? '[]', true) ?? [];
$newRights = ['cd', 'decode'];
$updated = false;
foreach ($newRights as $newRight) {
if (!in_array($newRight, $rights)) {
$rights[] = $newRight;
$updated = true;
}
}
if ($updated) {
$setting->setValue(json_encode($rights));
$this->entityManager->persist($setting);
$this->entityManager->flush();
$this->checkIfAllPlayersVerified($player); $this->checkIfAllPlayersVerified($player);
} }
} }