diff --git a/src/Engine.php b/src/Engine.php index cbe4222..130c6b5 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -25,7 +25,7 @@ function runBrainGame(string $moduleName): void runGame($module); } -function loadTexts($module): array +function loadTexts(array $module): array { $textsFile = __DIR__ . '/texts.json'; if (!file_exists($textsFile)) { @@ -33,11 +33,13 @@ function loadTexts($module): array } try { - $texts = json_decode(file_get_contents($textsFile), true, 512, JSON_THROW_ON_ERROR); + $content = file_get_contents($textsFile); + $texts = $content ? json_decode($content, true, 512, JSON_THROW_ON_ERROR) : ''; } catch (JsonException $e) { } - if (empty($texts)) { + $isEmptyConfig = !isset($texts) || !$texts; + if ($isEmptyConfig) { throw new RuntimeException("Config $textsFile wasn't loaded"); } @@ -45,7 +47,7 @@ function loadTexts($module): array return $module; } -function buildGame($moduleName): array +function buildGame(string $moduleName): array { $module = []; $moduleFile = __DIR__ . '/Games/' . $moduleName . '.php'; @@ -60,7 +62,7 @@ function buildGame($moduleName): array function greetUser(array $module): array { line(getText($module, 'dialogs.welcome')); - $userName = prompt(getText($module, 'prompts.ask_name') . ' ', false, ''); + $userName = prompt(getText($module, 'prompts.ask_name') . ' ', '', ''); $module = setUserName($module, $userName); line(getText($module, 'dialogs.greeting', ['[user_name]' => $userName])); @@ -74,9 +76,8 @@ function runGame(array $module, int $correctAnswerCounter = 0): void [$question, $correctAnswer] = getQuestionAnswerPairHandler($module); line(getText($module, 'prompts.question', ['[question]' => $question])); - $userAnswer = prompt(getText($module, 'prompts.answer')); - $isValidAnswer = (string)$correctAnswer === $userAnswer; + $isValidAnswer = $correctAnswer === $userAnswer; /** Game over */ if (!$isValidAnswer) { diff --git a/src/Games/CalcGame.php b/src/Games/CalcGame.php index 7be91ed..c740bd0 100644 --- a/src/Games/CalcGame.php +++ b/src/Games/CalcGame.php @@ -7,7 +7,7 @@ use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION; use const BrainGames\Utils\ModuleUtils\SETTING_RULES; -function loader($module): array +function loader(array $module): array { $module[SETTING_RULES] = 'What is the result of the expression?'; $module[HANDLER_QUESTION] = static fn() => handler(); diff --git a/src/Games/EvenGame.php b/src/Games/EvenGame.php index b0b4c73..9d9f2b6 100644 --- a/src/Games/EvenGame.php +++ b/src/Games/EvenGame.php @@ -5,7 +5,7 @@ use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION; use const BrainGames\Utils\ModuleUtils\SETTING_RULES; -function loader($module): array +function loader(array $module): array { $module[SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".'; $module[HANDLER_QUESTION] = static fn() => handler(); diff --git a/src/Games/GcdGame.php b/src/Games/GcdGame.php index f6f5a36..44b57e7 100644 --- a/src/Games/GcdGame.php +++ b/src/Games/GcdGame.php @@ -5,7 +5,7 @@ use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION; use const BrainGames\Utils\ModuleUtils\SETTING_RULES; -function loader($module): array +function loader(array $module): array { $module[SETTING_RULES] = 'Find the greatest common divisor of given numbers.'; $module[HANDLER_QUESTION] = static fn() => handler(); @@ -21,7 +21,7 @@ function handler(): array return [$question, $expectedAnswer]; } -function gcd($a, $b): int +function gcd(int $a, int $b): int { return $b === 0 ? $a : gcd($b, $a % $b); } diff --git a/src/Games/PrimeGame.php b/src/Games/PrimeGame.php index f182832..23ea369 100644 --- a/src/Games/PrimeGame.php +++ b/src/Games/PrimeGame.php @@ -5,7 +5,7 @@ use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION; use const BrainGames\Utils\ModuleUtils\SETTING_RULES; -function loader($module): array +function loader(array $module): array { $module[SETTING_RULES] = 'Answer "yes" if given number is prime. Otherwise answer "no".'; $module[HANDLER_QUESTION] = static fn() => handler(); @@ -19,7 +19,7 @@ function handler(): array return [$question, $isPrime ? 'yes' : 'no']; } -function isPrime($number): bool +function isPrime(int $number): bool { if ($number <= 1) { return false; diff --git a/src/Games/ProgressionGame.php b/src/Games/ProgressionGame.php index df2d93f..0afa002 100644 --- a/src/Games/ProgressionGame.php +++ b/src/Games/ProgressionGame.php @@ -5,7 +5,7 @@ use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION; use const BrainGames\Utils\ModuleUtils\SETTING_RULES; -function loader($module): array +function loader(array $module): array { $module[SETTING_RULES] = 'What number is missing in the progression?'; $module[HANDLER_QUESTION] = static fn() => handler(); diff --git a/src/Utils/ModuleUtils.php b/src/Utils/ModuleUtils.php index 977c1ee..c87f085 100644 --- a/src/Utils/ModuleUtils.php +++ b/src/Utils/ModuleUtils.php @@ -19,36 +19,37 @@ * 1: string * } */ -function getQuestionAnswerPairHandler($module): array +function getQuestionAnswerPairHandler(array $module): array { return $module[HANDLER_QUESTION](); } -function getRulesDescription($module): string +function getRulesDescription(array $module): string { return $module[SETTING_RULES]; } -function getText($module, $key, $replace_pairs = []): string +function getText(array $module, string $key, array $replacePairs = []): string { $texts = getTexts($module); $keys = explode('.', $key); + /** @var string $text */ $text = array_reduce($keys, fn($tail, $key) => is_array($tail) ? ($tail[$key] ?? '') : $tail, $texts); - return strtr($text, $replace_pairs); + return strtr($text, $replacePairs); } -function setUserName($module, $userName): array +function setUserName(array $module, string $userName): array { $module[LOCATION_DATA][DATA_USER_NAME] = $userName; return $module; } -function getUserName($module): string +function getUserName(array $module): string { return $module[LOCATION_DATA][DATA_USER_NAME]; } -function getTexts($module): array +function getTexts(array $module): array { return $module[LOCATION_TEXTS]; }