Skip to content

Commit

Permalink
refactor: reorganize structure
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Aug 27, 2023
1 parent 97c4555 commit f63606a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function BrainGames\ModuleUtils\getRulesDescription;
use function BrainGames\ModuleUtils\getText;
use function BrainGames\ModuleUtils\getUserName;
use function BrainGames\ModuleUtils\setTexts;
use function BrainGames\ModuleUtils\setUserName;
use function cli\line;
use function cli\prompt;
Expand All @@ -31,13 +32,14 @@ function loadGame($moduleName): array
}
$module = loadModule($moduleFile);

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

$config = loadConfigurations($configFile);
return array_replace_recursive($config, $module);
$texts = loadConfigurations($textsFile);
$module = setTexts($module, $texts);
return $module;
}

function greetUser(array $module): array
Expand Down
13 changes: 12 additions & 1 deletion src/Utils/ModuleUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getRulesDescription($module): string

function getText($module, $key, $replace_pairs = []): string
{
$texts = $module[LOCATION_TEXTS];
$texts = getTexts($module);
$keys = explode('.', $key);
$text = array_reduce($keys, fn($tail, $key) => is_array($tail) ? ($tail[$key] ?? '') : $tail, $texts);
return strtr($text, $replace_pairs);
Expand All @@ -61,3 +61,14 @@ function getUserName($module): string
{
return $module[LOCATION_DATA][DATA_USER_NAME];
}

function setTexts($module, $texts): array
{
$module[LOCATION_TEXTS] = $texts;
return $module;
}

function getTexts($module): array
{
return $module[LOCATION_TEXTS];
}
16 changes: 0 additions & 16 deletions src/config.json

This file was deleted.

14 changes: 14 additions & 0 deletions src/texts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"dialogs": {
"welcome": "Welcome to the Brain Game!",
"congratulations": "Congratulations, [user_name]!",
"greeting": "Hello, [user_name]!",
"correct_answer": "Correct!",
"incorrect_answer": "'[user_answer]' is wrong answer ;(. Correct answer was '[correct_answer]'.\nLet's try again, [user_name]!"
},
"prompts": {
"answer": "Your answer",
"ask_name": "May I have your name?",
"question": "Question: [question]"
}
}

0 comments on commit f63606a

Please sign in to comment.