diff --git a/_incl_data/class/DarksLight2/Training/Steps/SecondStep.php b/_incl_data/class/DarksLight2/Training/Steps/SecondStep.php new file mode 100644 index 00000000..ec5ce3b3 --- /dev/null +++ b/_incl_data/class/DarksLight2/Training/Steps/SecondStep.php @@ -0,0 +1,30 @@ +user_id = $user_id; - + $this->generateToken(); } /** @@ -71,14 +71,16 @@ class TrainingManager public function render() { - $path = $_SERVER['DOCUMENT_ROOT'] . '/modules_data/steps/' . $this->short_name . '.php'; + if(!$this->isComplete) { + $path = $_SERVER['DOCUMENT_ROOT'] . '/modules_data/steps/' . $this->short_name . '.php'; - if(file_exists($path)) { - require $path; - return; + if (file_exists($path)) { + require $path; + return; + } + + throw TrainingException::noRenderingFile(); } - - throw TrainingException::noRenderingFile(); } }; } @@ -87,19 +89,61 @@ class TrainingManager private function stepData(string $short_name): object { - return json_decode($this->getDatabaseRecords()->data)->$short_name; + $data = $this->getDatabaseRecords()->data; + + if(!isset($data->$short_name)) { + $this->updateDatabaseRecord($short_name); + $data = $this->getDatabaseRecords()->data; + } + + return $data->$short_name; } - private function getDatabaseRecords() + private function generateToken($length = 16) + { + $letters = 'abcdefgmikiHGJKLjkGASysj7603456'; + + $token = ''; + + for ($i = 0; $i <= $length; $i++) { + $token .= $letters[rand(0, strlen($letters))]; + } + + Db::run('UPDATE user_training SET api_token = ? WHERE user_id = ?', [ + $token, + $this->user_id + ]); + } + + public function getDatabaseRecords(): stdClass { if(!$this->database_records) { - $this->database_records = Db::run('SELECT * FROM user_training WHERE user_id = ?', [$this->user_id]) + $data = Db::run('SELECT * FROM user_training WHERE user_id = ?', [$this->user_id]) ->fetch(PDO::FETCH_OBJ); + + $this->database_records = new stdClass(); + + $this->database_records->user_id = $data->user_id; + $this->database_records->api_token = $data->api_token; + $this->database_records->data = json_decode($data->data); } return $this->database_records; } + public function updateDatabaseRecord(string $short_name) + { + if($this->getDatabaseRecords()) { + + $this->database_records->data->$short_name = $this->firstRecordData()->$short_name; + + Db::run('UPDATE user_training SET data = ? WHERE user_id = ?', [ + json_encode($this->database_records->data), + $this->user_id + ]); + } + } + public function createDatabaseRecord() { if(!$this->getDatabaseRecords()) { @@ -110,19 +154,22 @@ class TrainingManager } } - private function firstRecordData(): array + private function firstRecordData(): stdClass { - return [ - 'first_step' => [ - 'complete' => 0, - 'progress' => [ - 'current' => 0, - 'need' => 0, - ] - ] + $data = new stdClass(); + $short_names = [ + 'first_step', + 'second_step' ]; - } + foreach ($short_names as $name) { + $data->$name->complete = false; + $data->$name->progress->current = 0; + $data->$name->progress->need = 1; + } + + return $data; + } public function __get(string $name) { return $this->steps[$name]; @@ -130,11 +177,25 @@ class TrainingManager public function addPoint(string $short_name) { - $this->$short_name->progress++; + $this->database_records->data->$short_name->progress->current++; + } + + public function nextStep() + { + foreach ($this->database_records->data as $step) { + if($step->complete === false && $step->progress->current >= $step->progress->need) { + $step->complete = true; + } + } } public function store() { + $this->nextStep(); + Db::run('UPDATE user_training SET data = ? WHERE user_id = ?', [ + json_encode($this->database_records->data), + $this->user_id + ]); } } \ No newline at end of file diff --git a/api/.htaccess b/api/.htaccess new file mode 100644 index 00000000..5b0712ea --- /dev/null +++ b/api/.htaccess @@ -0,0 +1,4 @@ + + RewriteEngine On + RewriteRule ^training/complete/?$ index.php?get=complete [L] + \ No newline at end of file diff --git a/api/index.php b/api/index.php new file mode 100644 index 00000000..07462aa2 --- /dev/null +++ b/api/index.php @@ -0,0 +1,47 @@ +info)) { + + $training = TrainingManager::getInstance($user->info['id']); + + if (!isset($data->token) || !password_verify( + $training->getDatabaseRecords()->api_token . $data->time . $user->info['id'], + $data->token + )) { + http_response_code(401); + die; + } + + if (isset($_GET['get'])) { + switch ($_GET['get']) { + default: + http_response_code(400); + die; + case 'complete': + if ($training->{$data->short_name}->isComplete) { + http_response_code(400); + die; + } + + $training->addPoint($data->short_name); + $training->nextStep(); + $training->store(); + + die(json_encode(['status' => 'ok'])); + } + } +} \ No newline at end of file diff --git a/css/training/modal.css b/css/training/modal.css index 95bfa33d..09fda884 100644 --- a/css/training/modal.css +++ b/css/training/modal.css @@ -4,7 +4,7 @@ right: 0; bottom: 0; left: 0; - background: rgba(0, 0, 0, 0.5); + /*background: rgba(0, 0, 0, 0.5);*/ opacity: 0; z-index: -1; pointer-events: none; diff --git a/js/training/modal.js b/js/training/modal.js index 6abec861..b4be8069 100644 --- a/js/training/modal.js +++ b/js/training/modal.js @@ -1,3 +1,24 @@ +const request = new XMLHttpRequest(); + +const training_handler = () => { + request.open('POST', '/api/training/complete') + request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); + request.onreadystatechange = function() {//Call a function when the state changes. + if(request.readyState === 4 && request.status === 200) { + parent.frames['main'].location.reload(); + } + } + + request.send(JSON.stringify({ + time: training_data().time, + token: training_data().token, + short_name: training_data().short_name + })) + +} + +let active_training = null; + (function () { if (typeof window.CustomEvent === "function") return false; function CustomEvent(event, params) { diff --git a/main.php b/main.php index 7fb492b8..9ab501f5 100644 --- a/main.php +++ b/main.php @@ -29,11 +29,14 @@
top.myexpLineTop27(' . $u->info['exp'] . ',' . $spl . ');' . $tjs . 'top.ctest("' . $u->info['city'] . '");top.sd4key="' . $u->info['nextAct'] . '"; var battle = ' . (0 + $u->info['battle']) . '; top.hic(); '; +$training_manager = TrainingManager::getInstance(); + +if($u->room['file']=='cp1') { + $training_manager->first_step->render(); + $training_manager->second_step->render(); +} ?> diff --git a/modules_data/steps/second_step.php b/modules_data/steps/second_step.php new file mode 100644 index 00000000..920294f3 --- /dev/null +++ b/modules_data/steps/second_step.php @@ -0,0 +1,31 @@ + + + + + \ No newline at end of file diff --git a/modules_data/steps/step.php b/modules_data/steps/step.php new file mode 100644 index 00000000..15b8c9e2 --- /dev/null +++ b/modules_data/steps/step.php @@ -0,0 +1,16 @@ +$short_name; +$button_text = 'Продолжить'; +$time = time(); + +$token = password_hash($manager->getDatabaseRecords()->api_token . $time . $user->info['id'], PASSWORD_DEFAULT);