Skip to content

Commit

Permalink
refactor: simplify architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 6, 2023
1 parent c5c612a commit ad996ed
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 150 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"files": [
"src/Engine.php",
"src/Cli.php",
"src/Utils/ConfigUtils.php",
"src/Utils/ModuleUtils.php"
]
},
Expand Down
41 changes: 28 additions & 13 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,61 @@

namespace BrainGames\Engine;

use JsonException;
use RuntimeException;

use function BrainGames\Utils\ConfigUtils\loadConfigurations;
use function BrainGames\Utils\ConfigUtils\loadModule;
use function BrainGames\Utils\ModuleUtils\getQuestionAnswerPairHandler;
use function BrainGames\Utils\ModuleUtils\getRulesDescription;
use function BrainGames\Utils\ModuleUtils\getText;
use function BrainGames\Utils\ModuleUtils\getUserName;
use function BrainGames\Utils\ModuleUtils\setTexts;
use function BrainGames\Utils\ModuleUtils\setUserName;
use function cli\line;
use function cli\prompt;

use const BrainGames\Utils\ModuleUtils\LOCATION_TEXTS;

const ATTEMPT_NUMBER = 3;

function runBrainGame(string $moduleName): void
{
$module = loadGame($moduleName);
$module = buildGame($moduleName);
$module = loadTexts($module);
$module = greetUser($module);
runGame($module);
}

function loadGame($moduleName): array
function loadTexts($module): array
{
$moduleFile = __DIR__ . '/Games/' . $moduleName . '.php';
if (!file_exists($moduleFile)) {
throw new RuntimeException("Module $moduleFile not found");
}
$module = loadModule($moduleFile);

$textsFile = __DIR__ . '/texts.json';
if (!file_exists($textsFile)) {
throw new RuntimeException("Config $textsFile not found");
}

$texts = loadConfigurations($textsFile);
$module = setTexts($module, $texts);
try {
$texts = json_decode(file_get_contents($textsFile), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
}

if (empty($texts)) {
throw new RuntimeException("Config $textsFile wasn't loaded");
}

$module[LOCATION_TEXTS] = $texts;
return $module;
}

function buildGame($moduleName): array
{
$module = [];
$moduleFile = __DIR__ . '/Games/' . $moduleName . '.php';
if (!file_exists($moduleFile)) {
throw new RuntimeException("Module $moduleFile not found");
}

require_once $moduleFile;
return "BrainGames\Games\\$moduleName\loader"($module);
}

function greetUser(array $module): array
{
line(getText($module, 'dialogs.welcome'));
Expand Down
58 changes: 31 additions & 27 deletions src/Games/CalcGame.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
<?php

namespace BrainGames\Games\EvenGame;
namespace BrainGames\Games\CalcGame;

use RuntimeException;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'What is the result of the expression?';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$operands = ['*', '+', '-'];
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);
$operand = $operands[random_int(0, count($operands) - 1)];
switch ($operand) {
case '*':
$expectedAnswer = $firstNum * $secondNum;
break;
case '+':
$expectedAnswer = $firstNum + $secondNum;
break;
case '-':
$expectedAnswer = $firstNum - $secondNum;
break;
}
function loader($module): array
{
$module[SETTING_RULES] = 'What is the result of the expression?';
$module[HANDLER_QUESTION] = static fn() => handler();
return $module;
}

$question = "$firstNum $operand $secondNum";
return [$question, $expectedAnswer];
};
function handler(): array
{
$operators = ['*', '+', '-'];
$firstNum = random_int(1, 10);
$secondNum = random_int(1, 10);
$operator = $operators[random_int(0, count($operators) - 1)];
switch ($operator) {
case '*':
$expectedAnswer = $firstNum * $secondNum;
break;
case '+':
$expectedAnswer = $firstNum + $secondNum;
break;
case '-':
$expectedAnswer = $firstNum - $secondNum;
break;
default:
throw new RuntimeException('Incorrect operator');
}

return $module;
};
$question = "$firstNum $operator $secondNum";
return [$question, $expectedAnswer];
}
24 changes: 12 additions & 12 deletions src/Games/EvenGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
namespace BrainGames\Games\EvenGame;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$question = random_int(1, 100);
$isEven = $question % 2 === 0;
return [$question, $isEven ? 'yes' : 'no'];
};

function loader($module): array
{
$module[SETTING_RULES] = 'Answer "yes" if the number is even, otherwise answer "no".';
$module[HANDLER_QUESTION] = static fn() => handler();
return $module;
};
}

function handler(): array
{
$question = random_int(1, 100);
$isEven = $question % 2 === 0;
return [$question, $isEven ? 'yes' : 'no'];
}
37 changes: 19 additions & 18 deletions src/Games/GcdGame.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<?php

namespace BrainGames\Games\EvenGame;
namespace BrainGames\Games\GcdGame;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Find the greatest common divisor of given numbers.';

$gcd = function ($a, $b) use (&$gcd) {
return $b === 0 ? $a : $gcd($b, $a % $b);
};
function loader($module): array
{
$module[SETTING_RULES] = 'Find the greatest common divisor of given numbers.';
$module[HANDLER_QUESTION] = static fn() => handler();
return $module;
}

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () use ($gcd) {
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);
$expectedAnswer = $gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
};
function handler(): array
{
$firstNum = random_int(1, 10);
$secondNum = random_int(1, 20);
$expectedAnswer = gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
}

return $module;
};
function gcd($a, $b): int
{
return $b === 0 ? $a : gcd($b, $a % $b);
}
56 changes: 28 additions & 28 deletions src/Games/PrimeGame.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<?php

namespace BrainGames\Games\EvenGame;
namespace BrainGames\Games\PrimeGame;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return static function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Answer "yes" if given number is prime. Otherwise answer "no".';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$isPrime = function ($number) {
if ($number <= 1) {
return false;
}

for ($i = 2; $i * $i <= $number; $i++) {
if ($number % $i === 0) {
return false;
}
}

return true;
};


$question = random_int(1, 100);
$isPrime = $isPrime($question);
return [$question, $isPrime ? 'yes' : 'no'];
};

function loader($module): array
{
$module[SETTING_RULES] = 'Answer "yes" if given number is prime. Otherwise answer "no".';
$module[HANDLER_QUESTION] = static fn() => handler();
return $module;
};
}

function handler(): array
{
$question = random_int(1, 15);
$isPrime = isPrime($question);
return [$question, $isPrime ? 'yes' : 'no'];
}

function isPrime($number): bool
{
if ($number <= 1) {
return false;
}

for ($i = 2; $i * $i <= $number; $i++) {
if ($number % $i === 0) {
return false;
}
}

return true;
}
38 changes: 19 additions & 19 deletions src/Games/ProgressionGame.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<?php

namespace BrainGames\Games\EvenGame;
namespace BrainGames\Games\ProgressionGame;

use const BrainGames\Utils\ModuleUtils\HANDLER_QUESTION;
use const BrainGames\Utils\ModuleUtils\LOCATION_HANDLERS;
use const BrainGames\Utils\ModuleUtils\LOCATION_SETTINGS;
use const BrainGames\Utils\ModuleUtils\SETTING_RULES;

return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'What number is missing in the progression?';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$lengthProgression = 10;
$step = random_int(2, 10);
$shift = random_int(1, 10);
$progression = range($shift, $shift + (($lengthProgression - 1) * $step), $step);
$guessIndex = random_int(0, 8);
$expectedAnswer = $progression[$guessIndex];
$progression[$guessIndex] = '..';
$question = implode(' ', $progression);
return [$question, $expectedAnswer];
};

function loader($module): array
{
$module[SETTING_RULES] = 'What number is missing in the progression?';
$module[HANDLER_QUESTION] = static fn() => handler();
return $module;
};
}

function handler(): array
{
$lengthProgression = 10;
$step = random_int(2, 10);
$shift = random_int(1, 10);
$progression = range($shift, $shift + (($lengthProgression - 1) * $step), $step);
$guessIndex = random_int(0, 8);
$expectedAnswer = $progression[$guessIndex];
$progression[$guessIndex] = '..';
$question = implode(' ', $progression);
return [$question, $expectedAnswer];
}
19 changes: 0 additions & 19 deletions src/Utils/ConfigUtils.php

This file was deleted.

Loading

0 comments on commit ad996ed

Please sign in to comment.