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:
@@ -294,6 +294,10 @@ class GameResponseService
|
||||
$result[] = 'These files are protected by the AI virus. Use sudo rm {file} to remove them.';
|
||||
|
||||
return ['result' => $result];
|
||||
case 'chat':
|
||||
case 'verify':
|
||||
case 'decode':
|
||||
return $this->checkGameCommando('/'.$message, $player);
|
||||
default:
|
||||
return ['result' => ['Unknown command']];
|
||||
}
|
||||
@@ -539,33 +543,7 @@ class GameResponseService
|
||||
}
|
||||
|
||||
// Grant verify right
|
||||
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $screen);
|
||||
|
||||
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();
|
||||
}
|
||||
$this->grantRights($player, ['verify', 'cat']);
|
||||
}
|
||||
|
||||
private function handleDecodeMessage(string $message, Player $player): string
|
||||
@@ -608,31 +586,59 @@ class GameResponseService
|
||||
|
||||
private function grantRightToAllPlayers(Session $session, string $right): void
|
||||
{
|
||||
$updated = false;
|
||||
|
||||
foreach ($session->getPlayers() as $sessionPlayer) {
|
||||
$rightsSettingName = SessionSettingType::tryFrom('RightsForPlayer' . $sessionPlayer->getScreen());
|
||||
if (!$rightsSettingName) {
|
||||
continue;
|
||||
$this->grantRights($sessionPlayer, [$right]);
|
||||
}
|
||||
}
|
||||
|
||||
$setting = $this->sessionSettingRepository->getSetting($session, $rightsSettingName, $sessionPlayer);
|
||||
/**
|
||||
* The single place a player's rights ever get granted. Persists any rights the
|
||||
* 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);
|
||||
|
||||
if (!$rightsSettingName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$setting = $this->sessionSettingRepository->getSetting($player->getSession(), $rightsSettingName, $player);
|
||||
if (!$setting) {
|
||||
continue;
|
||||
return false; // Should have been initialized
|
||||
}
|
||||
|
||||
$rights = json_decode($setting->getValue() ?? '[]', true) ?? [];
|
||||
if (!in_array($right, $rights)) {
|
||||
$rights[] = $right;
|
||||
$setting->setValue(json_encode($rights));
|
||||
$this->entityManager->persist($setting);
|
||||
$updated = true;
|
||||
$granted = [];
|
||||
|
||||
foreach ($newRights as $newRight) {
|
||||
if (!in_array($newRight, $rights)) {
|
||||
$rights[] = $newRight;
|
||||
$granted[] = $newRight;
|
||||
}
|
||||
}
|
||||
|
||||
if ($updated) {
|
||||
$this->entityManager->flush();
|
||||
if (empty($granted)) {
|
||||
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
|
||||
{
|
||||
$screen = $player->getScreen();
|
||||
$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();
|
||||
|
||||
if ($this->grantRights($player, ['cd', 'decode'])) {
|
||||
$this->checkIfAllPlayersVerified($player);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user