Compare commits
No commits in common. "93177f70b407e59e06406a9d04545930164fb46e" and "e3915d2495587e4465fec9dfc7cbf1cfb3a969ec" have entirely different histories.
93177f70b4
...
e3915d2495
@ -26,7 +26,6 @@ spl_autoload_register(function (string $classname) {
|
|||||||
$classMap = [
|
$classMap = [
|
||||||
'NewCombats' => __DIR__ . '/class/',
|
'NewCombats' => __DIR__ . '/class/',
|
||||||
'Insallah' => __DIR__ . '/class/Insallah/',
|
'Insallah' => __DIR__ . '/class/Insallah/',
|
||||||
'DarksLight2' => __DIR__ . '/class/DarksLight2/',
|
|
||||||
];
|
];
|
||||||
$parts = explode('\\', $classname);
|
$parts = explode('\\', $classname);
|
||||||
$namespace = array_shift($parts);
|
$namespace = array_shift($parts);
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DarksLight2\Training;
|
|
||||||
|
|
||||||
abstract class StepFactory
|
|
||||||
{
|
|
||||||
|
|
||||||
abstract public function getTitle(): string;
|
|
||||||
abstract public function getMessage(): string;
|
|
||||||
abstract public function getShortName(): string;
|
|
||||||
abstract public function getRewards(): array;
|
|
||||||
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DarksLight2\Training\Steps;
|
|
||||||
|
|
||||||
use DarksLight2\Training\StepFactory;
|
|
||||||
|
|
||||||
class FirstStep extends StepFactory
|
|
||||||
{
|
|
||||||
|
|
||||||
public function getTitle(): string
|
|
||||||
{
|
|
||||||
return 'Первое знакомство';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMessage(): string
|
|
||||||
{
|
|
||||||
return 'test';
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRewards(): array
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getShortName(): string
|
|
||||||
{
|
|
||||||
return 'first_step';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DarksLight2\Training;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
|
|
||||||
class TrainingException extends Exception
|
|
||||||
{
|
|
||||||
public static function alreadyRegistered(): self
|
|
||||||
{
|
|
||||||
return new self("The steps have been registered before");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function noRenderingFile(): self
|
|
||||||
{
|
|
||||||
return new self("Rendering file missing");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,140 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DarksLight2\Training;
|
|
||||||
|
|
||||||
use Core\Db;
|
|
||||||
use DarksLight2\Traits\Singleton;
|
|
||||||
use PDO;
|
|
||||||
use stdClass;
|
|
||||||
|
|
||||||
class TrainingManager
|
|
||||||
{
|
|
||||||
|
|
||||||
use Singleton;
|
|
||||||
|
|
||||||
private $database_records = false;
|
|
||||||
private array $steps_data;
|
|
||||||
private int $user_id;
|
|
||||||
private array $steps;
|
|
||||||
|
|
||||||
public function __construct(int $user_id)
|
|
||||||
{
|
|
||||||
$this->user_id = $user_id;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param StepFactory[] $steps
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
* @throws \DarksLight2\Training\TrainingException
|
|
||||||
*/
|
|
||||||
public function register(array $steps)
|
|
||||||
{
|
|
||||||
if (!empty($this->steps)) {
|
|
||||||
throw TrainingException::alreadyRegistered();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($steps as $step) {
|
|
||||||
if($step instanceof StepFactory) {
|
|
||||||
$this->steps[$step->getShortName()] = new class (
|
|
||||||
$step->getMessage(),
|
|
||||||
$step->getTitle(),
|
|
||||||
$step->getRewards(),
|
|
||||||
$this->stepData($step->getShortName())->complete,
|
|
||||||
$step->getShortName(),
|
|
||||||
$this->stepData($step->getShortName())->progress
|
|
||||||
) {
|
|
||||||
public string $message;
|
|
||||||
public string $title;
|
|
||||||
public string $short_name;
|
|
||||||
public array $rewards;
|
|
||||||
public bool $isComplete;
|
|
||||||
|
|
||||||
public stdClass $progress;
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
string $message,
|
|
||||||
string $title,
|
|
||||||
array $rewards,
|
|
||||||
bool $isComplete,
|
|
||||||
string $short_name,
|
|
||||||
stdClass $progress
|
|
||||||
) {
|
|
||||||
$this->rewards = $rewards;
|
|
||||||
$this->title = $title;
|
|
||||||
$this->isComplete = $isComplete;
|
|
||||||
$this->message = $message;
|
|
||||||
$this->short_name = $short_name;
|
|
||||||
$this->progress = $progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function render()
|
|
||||||
{
|
|
||||||
$path = $_SERVER['DOCUMENT_ROOT'] . '/modules_data/steps/' . $this->short_name . '.php';
|
|
||||||
|
|
||||||
if(file_exists($path)) {
|
|
||||||
require $path;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw TrainingException::noRenderingFile();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function stepData(string $short_name): object
|
|
||||||
{
|
|
||||||
return json_decode($this->getDatabaseRecords()->data)->$short_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getDatabaseRecords()
|
|
||||||
{
|
|
||||||
if(!$this->database_records) {
|
|
||||||
$this->database_records = Db::run('SELECT * FROM user_training WHERE user_id = ?', [$this->user_id])
|
|
||||||
->fetch(PDO::FETCH_OBJ);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->database_records;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function createDatabaseRecord()
|
|
||||||
{
|
|
||||||
if(!$this->getDatabaseRecords()) {
|
|
||||||
Db::run('INSERT INTO user_training (user_id, data) VALUES (?, ?)', [
|
|
||||||
$this->user_id,
|
|
||||||
json_encode($this->firstRecordData())
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function firstRecordData(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'first_step' => [
|
|
||||||
'complete' => 0,
|
|
||||||
'progress' => [
|
|
||||||
'current' => 0,
|
|
||||||
'need' => 0,
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __get(string $name)
|
|
||||||
{
|
|
||||||
return $this->steps[$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addPoint(string $short_name)
|
|
||||||
{
|
|
||||||
$this->$short_name->progress++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DarksLight2\Traits;
|
|
||||||
|
|
||||||
trait Singleton
|
|
||||||
{
|
|
||||||
private static self $instance;
|
|
||||||
|
|
||||||
public static function getInstance(...$args): self
|
|
||||||
{
|
|
||||||
if(!isset(self::$instance)) {
|
|
||||||
self::$instance = new self(...$args);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$instance;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use DarksLight2\Training\TrainingManager;
|
|
||||||
|
|
||||||
$short_name = 'first_step';
|
|
||||||
|
|
||||||
$manager = TrainingManager::getInstance();
|
|
||||||
|
|
||||||
var_dump($manager->$short_name->progress);
|
|
||||||
|
|
||||||
echo 'it\'s work';
|
|
@ -10,7 +10,6 @@ if (!defined('GAME_VERSION')) {
|
|||||||
require_once '_incl_data/autoload.php';
|
require_once '_incl_data/autoload.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
use Core\{Config, Database, Db};
|
use Core\{Config, Database, Db};
|
||||||
use User\Confirmation;
|
use User\Confirmation;
|
||||||
|
|
||||||
|
33
test.php
33
test.php
@ -1,33 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use DarksLight2\Training\Steps\FirstStep;
|
|
||||||
use DarksLight2\Training\TrainingException;
|
|
||||||
use DarksLight2\Training\TrainingManager;
|
|
||||||
|
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
|
||||||
session_start();
|
|
||||||
}
|
|
||||||
if (isset($_GET['unset'])) {
|
|
||||||
session_unset();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!defined('GAME_VERSION')) {
|
|
||||||
require_once '_incl_data/autoload.php';
|
|
||||||
}
|
|
||||||
|
|
||||||
$user = User::start();
|
|
||||||
|
|
||||||
$manager = TrainingManager::getInstance($user->info['id']);
|
|
||||||
|
|
||||||
$manager->createDatabaseRecord();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$manager->register([
|
|
||||||
new FirstStep(),
|
|
||||||
]);
|
|
||||||
} catch (TrainingException $e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$manager->first_step->complete) {
|
|
||||||
$manager->first_step->render();
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user