From 97c4555905c4d2f5099fa3fed37776dfb349b46e Mon Sep 17 00:00:00 2001 From: gkdev Date: Mon, 28 Aug 2023 00:45:30 +0300 Subject: [PATCH] refactor: reorganize structure --- bin/brain-even | 2 +- composer.json | 4 ++-- src/{BrainGame.php => Engine.php} | 15 ++++++++------- src/{Modules => Games}/EvenGame/Module.php | 9 +-------- src/{Modules => Games}/EvenGame/config.php | 6 ------ src/Utils/ModuleUtils.php | 12 ------------ src/config.json | 3 --- 7 files changed, 12 insertions(+), 39 deletions(-) rename src/{BrainGame.php => Engine.php} (84%) rename src/{Modules => Games}/EvenGame/Module.php (68%) rename src/{Modules => Games}/EvenGame/config.php (62%) diff --git a/bin/brain-even b/bin/brain-even index e296cdc..67dc80b 100755 --- a/bin/brain-even +++ b/bin/brain-even @@ -1,7 +1,7 @@ #!/usr/bin/env php $question])); @@ -76,11 +77,11 @@ function runGame(array $module, int $questionCounter = 0, int $correctAnswerCoun line(getText($module, 'dialogs.correct_answer', ['[user_name]' => $userName])); /** User Won! End game */ - $isWin = getWinConditionHandler($module, $correctAnswerCounter, $questionCounter); + $isWin = ATTEMPT_NUMBER <= $correctAnswerCounter; if ($isWin) { line(getText($module, 'dialogs.congratulations', ['[user_name]' => $userName])); return; } - runGame($module, $questionCounter, $correctAnswerCounter); + runGame($module, $correctAnswerCounter); } diff --git a/src/Modules/EvenGame/Module.php b/src/Games/EvenGame/Module.php similarity index 68% rename from src/Modules/EvenGame/Module.php rename to src/Games/EvenGame/Module.php index c718479..28ba766 100644 --- a/src/Modules/EvenGame/Module.php +++ b/src/Games/EvenGame/Module.php @@ -23,11 +23,4 @@ function questionHandler(): array $question = random_int(1, 100); $isEven = $question % 2 === 0; return [$question, $isEven ? 'yes' : 'no']; -} - -function winConditionHandler(int $correctAnswers, int $totalQuestions): bool -{ - return - NUMBER_CORRECT_ANSWERS_FOR_WIN === $correctAnswers and - NUMBER_CORRECT_ANSWERS_FOR_WIN === $totalQuestions; -} +} \ No newline at end of file diff --git a/src/Modules/EvenGame/config.php b/src/Games/EvenGame/config.php similarity index 62% rename from src/Modules/EvenGame/config.php rename to src/Games/EvenGame/config.php index 0e1a081..8860e3e 100644 --- a/src/Modules/EvenGame/config.php +++ b/src/Games/EvenGame/config.php @@ -3,17 +3,11 @@ namespace BrainGames\Modules\EvenGame\config; use function BrainGames\Modules\EvenGame\Module\questionHandler; -use function BrainGames\Modules\EvenGame\Module\winConditionHandler; use function BrainGames\ModuleUtils\setQuestionAnswerPairHandler; use function BrainGames\ModuleUtils\setRulesDescription; -use function BrainGames\ModuleUtils\setWinConditionHandler; return static function ($module) { $module = setRulesDescription($module, 'Answer "yes" if the number is even, otherwise answer "no".'); $module = setQuestionAnswerPairHandler($module, static fn() => questionHandler()); - $module = setWinConditionHandler( - $module, - static fn($correctAnswers, $totalQuestions) => winConditionHandler($correctAnswers, $totalQuestions) - ); return $module; }; diff --git a/src/Utils/ModuleUtils.php b/src/Utils/ModuleUtils.php index 96e23e2..6c19d0c 100644 --- a/src/Utils/ModuleUtils.php +++ b/src/Utils/ModuleUtils.php @@ -4,7 +4,6 @@ const DATA_USER_NAME = 3010; const HANDLER_QUESTION = 100; -const HANDLER_WIN_CONDITION = 101; const LOCATION_DATA = 'data'; const LOCATION_HANDLERS = 'handlers'; const LOCATION_SETTINGS = 'settings'; @@ -33,17 +32,6 @@ function setQuestionAnswerPairHandler($module, callable $handler): array return $module; } -function setWinConditionHandler($module, callable $handler): array -{ - $module[LOCATION_HANDLERS][HANDLER_WIN_CONDITION] = $handler; - return $module; -} - -function getWinConditionHandler(array $module, int $correctAnswers, int $totalQuestions): bool -{ - return $module[LOCATION_HANDLERS][HANDLER_WIN_CONDITION]($correctAnswers, $totalQuestions); -} - function setRulesDescription($module, $rulesDescription): array { $module[LOCATION_SETTINGS][SETTING_RULES_DESCRIPTION] = $rulesDescription; diff --git a/src/config.json b/src/config.json index 4c2e0fe..9df95d1 100644 --- a/src/config.json +++ b/src/config.json @@ -12,8 +12,5 @@ "ask_name": "May I have your name?", "question": "Question: [question]" } - }, - "settings": { - "attempts": 0 } } \ No newline at end of file