From 01d1cbed9c2bd334ccc37fa7e2306e9b8d6aa59c Mon Sep 17 00:00:00 2001 From: ardent Date: Thu, 14 Nov 2024 14:50:40 +0100 Subject: [PATCH] Added attribute injection --- src/Command/CheckCommand.php | 2 - src/Command/UsersCommand.php | 40 ++++++++++++++ src/Service/Factorio.php | 7 ++- src/Service/Rules/PlayTimeRule.php | 47 +++++------------ src/Service/Rules/TimeRule.php | 11 ++-- src/Service/SimpleDataService.php | 38 -------------- .../SimpleDatabase/PlayTimeRepository.php | 47 +++++++++++++++++ .../SimpleDatabase/SimpleDataService.php | 52 +++++++++++++++++++ 8 files changed, 165 insertions(+), 79 deletions(-) create mode 100644 src/Command/UsersCommand.php delete mode 100755 src/Service/SimpleDataService.php create mode 100755 src/Service/SimpleDatabase/PlayTimeRepository.php create mode 100755 src/Service/SimpleDatabase/SimpleDataService.php diff --git a/src/Command/CheckCommand.php b/src/Command/CheckCommand.php index babb11f..6f413c1 100644 --- a/src/Command/CheckCommand.php +++ b/src/Command/CheckCommand.php @@ -6,9 +6,7 @@ use App\Service\Factorio; use App\Service\RuleChecker; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; diff --git a/src/Command/UsersCommand.php b/src/Command/UsersCommand.php new file mode 100644 index 0000000..bf0894f --- /dev/null +++ b/src/Command/UsersCommand.php @@ -0,0 +1,40 @@ +factorio->getOnlinePlayers(); + + $io->table(['Name', 'Time'], array_map(fn($player) => [ + $player->getName(), + $this->playTimeRepository->getPlayTimeOnDay($player), + ], $players)); + + return Command::SUCCESS; + } +} diff --git a/src/Service/Factorio.php b/src/Service/Factorio.php index 397bc06..ec0280c 100755 --- a/src/Service/Factorio.php +++ b/src/Service/Factorio.php @@ -43,7 +43,10 @@ class Factorio $players = []; foreach ($output as $line) { - $players[] = new Player(explode(' ', trim($line))[0]); + $lineSegments = explode(' ', trim($line)); + if (count($lineSegments) >= 2 && $lineSegments[1] === '(online)') { + $players[] = new Player($lineSegments[0]); + } } return $players; @@ -56,7 +59,7 @@ class Factorio public function whisper(Player $player, string $message): void { - $this->runCmd(['/whisper', $player->getName(), $message]); + $this->runCmd(['/whisper', $player->getName(), sprintf('[%s]', date('H:i:s')), $message]); } public function say(string $message): void diff --git a/src/Service/Rules/PlayTimeRule.php b/src/Service/Rules/PlayTimeRule.php index 75d33a4..10ad60a 100755 --- a/src/Service/Rules/PlayTimeRule.php +++ b/src/Service/Rules/PlayTimeRule.php @@ -3,20 +3,21 @@ namespace App\Service\Rules; use App\Dto\Player; -use App\Service\SimpleDataService; +use App\Service\SimpleDatabase\PlayTimeRepository; +use App\Service\SimpleDatabase\SimpleDataService; class PlayTimeRule extends AbstractRule { public function __construct( - private readonly SimpleDataService $storeData, - private readonly int $defaultPlayTime = 60, - private readonly int $timeBetweenChecks = 1, + private readonly PlayTimeRepository $playTimeRepository, + private readonly int $defaultPlayTime = 60, + private readonly int $timeBetweenChecks = 1, ) {} private function timePerPlayer(Player $player) { $players = [ -// "Ardentsword" => "2:00", +// "Ardentsword" => "120", ]; return $players[$player->getName()] ?? $this->defaultPlayTime; @@ -25,24 +26,22 @@ class PlayTimeRule extends AbstractRule public function check(Player $player): RuleResult { $today = new \DateTimeImmutable(); - $playerData = $this->getPlayerData($player); + $playTime = $this->playTimeRepository->getPlayTimeOnDay($player, $today); // If the player has no data or the data is from a different day, we reset the time - if (!$playerData || $playerData['day'] !== $today->format('Y-m-d')) { - $this->storePlayerData($player, $today, $this->timePerPlayer($player)); + if ($playTime === false) { + $this->playTimeRepository->storePlayTime($player, $today, $this->timePerPlayer($player)); return new RuleResult(RuleResultEnum::WARNING, sprintf('Extra time activated, set to %d minutes', $this->timePerPlayer($player))); } // If the player has time left, we decrease it and return denied if nothing left - $timeLeft = $playerData['timeLeft'] - $this->timeBetweenChecks; - $this->storePlayerData($player, $today, $timeLeft); + $playTime -= $this->timeBetweenChecks; + $this->playTimeRepository->storePlayTime($player, $today, $playTime); - if ($timeLeft <= 0) { + if ($playTime <= 0) { return new RuleResult(RuleResultEnum::DENIED); - } elseif ($timeLeft <= 5) { - return new RuleResult(RuleResultEnum::WARNING, sprintf('%d minute(s) left', $timeLeft)); - }elseif($timeLeft === 30) { - return new RuleResult(RuleResultEnum::WARNING, sprintf('30 minutes left')); + } elseif ($playTime <= 5 || $playTime === 30) { + return new RuleResult(RuleResultEnum::WARNING, sprintf('%d minute(s) left', $playTime)); } return new RuleResult(RuleResultEnum::ALLOWED); @@ -52,22 +51,4 @@ class PlayTimeRule extends AbstractRule { return sprintf('PlayTimeRule, limited play time'); } - - private function storePlayerData(Player $player, \DateTimeImmutable $day, int $timeLeft): void - { - $this->storeData->set( - sprintf('playTime-%s', $player->getName()), - [ - 'player' => $player->getName(), - 'timeLeft' => $timeLeft, - 'day' => $day->format('Y-m-d'), - ]); - } - - private function getPlayerData(Player $player): ?array - { - return $this->storeData->get( - sprintf('playTime-%s', $player->getName()) - ); - } } \ No newline at end of file diff --git a/src/Service/Rules/TimeRule.php b/src/Service/Rules/TimeRule.php index 6400800..08abfd8 100755 --- a/src/Service/Rules/TimeRule.php +++ b/src/Service/Rules/TimeRule.php @@ -8,7 +8,7 @@ class TimeRule extends AbstractRule { public function __construct( private readonly string $start = '19:00', - private readonly string $end = '23:00', + private readonly string $end = '20:00', ) {} public function check(Player $player): RuleResult @@ -18,9 +18,12 @@ class TimeRule extends AbstractRule $end = new \DateTimeImmutable($this->end); if ($now >= $start && $now <= $end) { - $fiveMinutesBeforeEnd = $end->sub(new \DateInterval('PT5M')); - if ($now >= $fiveMinutesBeforeEnd) { - return new RuleResult(RuleResultEnum::WARNING, 'Only 5 minutes remaining'); + // Always add 1 because we are a few seconds into the minute + $timeLeft = $end->diff($now); + $minutesLeft = $timeLeft->i + 1; + $hoursLeft = $timeLeft->h; + if ($hoursLeft == 0 && ($minutesLeft <= 5 || $minutesLeft === 30)) { + return new RuleResult(RuleResultEnum::WARNING, sprintf('%d minute(s) left', $minutesLeft)); } return new RuleResult(RuleResultEnum::ALLOWED); } diff --git a/src/Service/SimpleDataService.php b/src/Service/SimpleDataService.php deleted file mode 100755 index 1d8d344..0000000 --- a/src/Service/SimpleDataService.php +++ /dev/null @@ -1,38 +0,0 @@ -file = $kernel->getProjectDir() . '/data.json'; - } - - public function set(string $key, array $data): self - { - $all = $this->getAll(); - $all[$key] = $data; - - file_put_contents($this->file, json_encode($all, JSON_PRETTY_PRINT)); - - return $this; - } - - public function get(string $key): ?array - { - return $this->getAll()[$key] ?? null; - } - - public function getAll(): array - { - if (!file_exists($this->file)) { - return []; - } - return json_decode(file_get_contents($this->file), true); - } -} \ No newline at end of file diff --git a/src/Service/SimpleDatabase/PlayTimeRepository.php b/src/Service/SimpleDatabase/PlayTimeRepository.php new file mode 100755 index 0000000..d45089f --- /dev/null +++ b/src/Service/SimpleDatabase/PlayTimeRepository.php @@ -0,0 +1,47 @@ +getName()); + } + + public function storePlayTime(Player $player, \DateTimeImmutable $day, int $timeLeft): void + { + $this->dataService->set( + $this->getKey($player), + [ + 'player' => $player->getName(), + 'timeLeft' => $timeLeft, + 'day' => $day->format('Y-m-d'), + ] + ); + } + + public function getPlayTimeRaw(Player $player): ?array + { + return $this->dataService->get($this->getKey($player)); + } + + public function getPlayTimeOnDay(Player $player, ?\DateTimeImmutable $day = null): false|int + { + if ($day === null) { + $day = new \DateTimeImmutable(); + } + + $data = $this->getPlayTimeRaw($player); + if (!$data || $data['day'] !== $day->format('Y-m-d')) { + return false; + } + return $data['timeLeft']; + } +} \ No newline at end of file diff --git a/src/Service/SimpleDatabase/SimpleDataService.php b/src/Service/SimpleDatabase/SimpleDataService.php new file mode 100755 index 0000000..a8368a6 --- /dev/null +++ b/src/Service/SimpleDatabase/SimpleDataService.php @@ -0,0 +1,52 @@ +dataCache === null) { + $this->dataCache = $this->getAll(); + } + $this->dataCache[$key] = $data; + + $this->writeCache(); + + return $this; + } + + public function get(string $key): ?array + { + return $this->getAll()[$key] ?? null; + } + + public function getAll(): array + { + if ($this->dataCache !== null) { + return $this->dataCache; + } + + if (!file_exists($this->dataFile)) { + $this->dataCache = []; + } else { + $this->dataCache = json_decode(file_get_contents($this->dataFile), true); + } + + return $this->dataCache; + } + + private function writeCache(): void + { + file_put_contents($this->dataFile, json_encode($this->dataCache, JSON_PRETTY_PRINT)); + } +} \ No newline at end of file