From f63606a2d14da1189159c8891328aeb5e6674c2f Mon Sep 17 00:00:00 2001 From: gkdev Date: Mon, 28 Aug 2023 00:54:19 +0300 Subject: [PATCH] refactor: reorganize structure --- src/Engine.php | 12 +++++++----- src/Utils/ModuleUtils.php | 13 ++++++++++++- src/config.json | 16 ---------------- src/texts.json | 14 ++++++++++++++ 4 files changed, 33 insertions(+), 22 deletions(-) delete mode 100644 src/config.json create mode 100644 src/texts.json diff --git a/src/Engine.php b/src/Engine.php index 680b6c6..dcb92d1 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -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; @@ -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 diff --git a/src/Utils/ModuleUtils.php b/src/Utils/ModuleUtils.php index 6c19d0c..bbfe78a 100644 --- a/src/Utils/ModuleUtils.php +++ b/src/Utils/ModuleUtils.php @@ -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); @@ -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]; +} diff --git a/src/config.json b/src/config.json deleted file mode 100644 index 9df95d1..0000000 --- a/src/config.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "texts": { - "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]" - } - } -} \ No newline at end of file diff --git a/src/texts.json b/src/texts.json new file mode 100644 index 0000000..cf151eb --- /dev/null +++ b/src/texts.json @@ -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]" + } +} \ No newline at end of file