diff --git a/composer.json b/composer.json index c1ab110..d5d16d1 100755 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "svoboda/time": "^1.0", "badfarm/zanzara": "^0.9.1", "nyholm/psr7": "^1.8", - "react/filesystem": "^0.1.2" + "react/filesystem": "^0.1.2", + "mirzaev/record": "^2.0" }, "autoload": { "psr-4": { diff --git a/kodorvan/brainrot/system/localizations/english.php b/kodorvan/brainrot/system/localizations/english.php index a3ef545..5847ff5 100644 --- a/kodorvan/brainrot/system/localizations/english.php +++ b/kodorvan/brainrot/system/localizations/english.php @@ -13,6 +13,7 @@ return [ // Main menu 'menu_title' => 'Main menu', 'menu_accounts' => 'Accounts', + 'menu_accounts_experience' => 'Experience', // Account 'account_title' => 'Account', diff --git a/kodorvan/brainrot/system/localizations/russian.php b/kodorvan/brainrot/system/localizations/russian.php index b15b1c7..02def17 100644 --- a/kodorvan/brainrot/system/localizations/russian.php +++ b/kodorvan/brainrot/system/localizations/russian.php @@ -13,6 +13,7 @@ return [ // Main menu 'menu_title' => 'Главное меню', 'menu_accounts' => 'Аккаунты', + 'menu_accounts_experience' => 'Опыт', // Аккаунт 'account_title' => 'Аккаунт', diff --git a/kodorvan/brainrot/system/models/account.php b/kodorvan/brainrot/system/models/account.php index bf259f7..cd50ed3 100644 --- a/kodorvan/brainrot/system/models/account.php +++ b/kodorvan/brainrot/system/models/account.php @@ -5,7 +5,12 @@ declare(strict_types=1); namespace kodorvan\brainrot\models; // Files of the project -use kodorvan\brainrot\models\core; +use kodorvan\brainrot\models\core, + kodorvan\brainrot\models\authorizations; + +// Active Record pattern +use mirzaev\record\interfaces\record as record_interface, + mirzaev\record\traits\record as record_trait; // Svoboda time use svoboda\time\statement as svoboda; @@ -32,8 +37,9 @@ use Exception as exception, * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @author Arsen Mirzaev Tatyano-Muradovich */ -final class account extends core +final class account extends core implements record_interface { + use record_trait; /** * File * @@ -51,9 +57,11 @@ final class account extends core /** * Constructor * + * @method record|null $record The record + * * @return void */ - public function __construct() + public function __construct(?record $record = null) { // Initializing the database $this->database = new database() @@ -66,14 +74,15 @@ final class account extends core new column('name_second', type::string, ['length' => 64]), new column('language', type::string, ['length' => 2]), new column('robot', type::char), - new column('authorized_system', type::char), - new column('authorized_settings', type::char), - new column('authorized_system_accounts', type::char), - new column('authorized_system_settings', type::char), + new column('experience', type::long_long_unsigned), + new column('slots', type::integer_unsigned), new column('updated', type::integer_unsigned), new column('created', type::integer_unsigned) ) ->connect($this->file); + + // Initializing the record + $record instanceof record and $this->record = $record; } /** @@ -98,16 +107,16 @@ final class account extends core // Found the account record if ( - $account->name_first !== $telegram->getFirstName() || - $account->name_second !== $telegram->getLastName() || - $account->domain !== $telegram->getUsername() + $account->name_first !== $telegram->getFirstName() || + $account->name_second !== $telegram->getLastName() || + $account->domain !== $telegram->getUsername() ) { // The telegram account was updated - + // Updating the account in the database $updated = $this->database->read( filter: fn(record $record) => $record->identifier_telegram === $telegram->getId(), - update: function (record &$record) use ($telegram){ + update: function (record &$record) use ($telegram) { // Writing new values into the record $record->name_first = $telegram->getFirstName(); $record->name_second = $telegram->getLastName(); @@ -167,11 +176,16 @@ final class account extends core * Creates the account record in the database * * @param telegram $telegram The telegram account + * @param int $experience The player experience + * @param int $slots The player avaiable slots * * @return int|false The record identifier, if created */ - public function registrate(telegram $telegram): int|false - { + public function registrate( + telegram $telegram, + int $experience = 0, + int $slots = 1 + ): int|false { // Initializing the identifier $identifier = $this->database->count() + 1; @@ -184,10 +198,8 @@ final class account extends core (string) $telegram->getUsername(), (string) $telegram->getLanguageCode(), (int) $telegram->isBot(), - 1, - 1, - 0, - 0, + $experience, + $slots, svoboda::timestamp(), svoboda::timestamp() ); @@ -198,4 +210,33 @@ final class account extends core // Exit (success) return $created ? $identifier : false; } + + /** + * Authorizations + * + * Search for the account authorizations + * + * @return authorizations|null The account authorizations + */ + public function authorizations(): ?authorizations + { + // Initializing the authorizations model + $authorizations = new authorizations(); + + // Search for the account authorizations + $record = $authorizations->database->read(filter: fn(record $record) => $record->account === $this->identifier, amount: 1)[0] ?? null; + + if ($record instanceof record) { + // Found the account authorizations + + // Writing the found authorizations record into the record object + $authorizations->record = $record; + + // Exit (success) + return $authorizations; + } + + // Exit (fail) + return null; + } } diff --git a/kodorvan/brainrot/system/models/authorizations.php b/kodorvan/brainrot/system/models/authorizations.php new file mode 100644 index 0000000..c099910 --- /dev/null +++ b/kodorvan/brainrot/system/models/authorizations.php @@ -0,0 +1,133 @@ + + */ +final class authorizations extends core implements record_interface +{ + use record_trait; + + /** + * File + * + * @var string $database Path to the database file + */ + protected string $file = DATABASES . DIRECTORY_SEPARATOR . 'authorizations.baza'; + + /** + * Database + * + * @var database $database The database + */ + public protected(set) database $database; + + /** + * Constructor + * + * @method record|null $record The record + * + * @return void + */ + public function __construct(?record $record = null) + { + // Initializing the database + $this->database = new database() + ->encoding(encoding::utf8) + ->columns( + new column('identifier', type::long_long_unsigned), + new column('account', type::long_long_unsigned), + new column('system', type::char), + new column('settings', type::char), + new column('chat', type::char), + new column('generations', type::char), + new column('arena', type::char), + new column('system_accounts', type::char), + new column('system_settings', type::char), + new column('updated', type::integer_unsigned), + new column('created', type::integer_unsigned) + ) + ->connect($this->file); + + // Initializing the record + $record instanceof record and $this->record = $record; + } + + /** + * Write + * + * @param int $account The account identifier + * @param int $system + * @param int $settings + * @param int $chat + * @param int $generations + * @param int $arena + * @param int $system_accounts + * @param int $system_settings + * + * @return int|false The record identifier, if created + */ + public function write( + int $account, + int $system = 1, + int $settings = 1, + int $chat = 1, + int $generations = 1, + int $arena = 0, + int $system_accounts = 0, + int $system_settings = 0, + ): int|false + { + $record = $this->database->record( + $this->database->count() + 1, + $account, + $system, + $settings, + $chat, + $generations, + $arena, + $system_accounts, + $system_settings, + svoboda::timestamp(), + svoboda::timestamp() + ); + + // Writing the record into the database + $created = $this->database->write($record); + + // Exit (success) + return $created ? $record->identifier : false; + } +} diff --git a/kodorvan/brainrot/system/models/telegram/commands.php b/kodorvan/brainrot/system/models/telegram/commands.php index 9502e9f..5d4378a 100644 --- a/kodorvan/brainrot/system/models/telegram/commands.php +++ b/kodorvan/brainrot/system/models/telegram/commands.php @@ -32,10 +32,25 @@ use Zanzara\Context as context, */ final class commands extends core { + /** + * Start + * + * Responce for the commands: '/start' + * + * @param context $context Request data from Telegram + * + * @return void + */ + public static function start(context $context): void + { + // Processing the menu method and exit (success) + static::menu(context: $context); + } + /** * Menu * - * Responce for the commands: "/start", '/menu' + * Responce for the commands: '/menu' * * @param context $context Request data from Telegram * @@ -55,32 +70,83 @@ final class commands extends core if ($localization) { // Initialized localization - // Initializing the title + // Initializing the title message text $title = '📋 *' . $localization['menu_title'] . '*'; - // Initializing accounts - $accounts = '*' . $localization['menu_accounts'] . ':* ' . ((new account)->database->count() ?? 0); + // Initializing the accounts message text + /* $accounts = '*' . $localization['menu_accounts'] . ':* ' . ((new account)->database->count() ?? 0); */ + // Initializing the experience message text + $experience = '*' . $localization['menu_accounts_experience'] . ':*' . $account->experience; + + // Initializing the player avaiable slots + $slots = $account->slots; + + // Declaring the message keyboard + $keyboard = []; + + // Initializing the registry of buttons amount in a row + $row_amount = 2; + + // Initializing the row iterator + $row = 0; + + foreach ([] as $key => $brainrot) { + // Iterating over the player brainrots + + // Initializing the row + $keyboard[$row] ??= []; + + // Initializing the keyboard brainrot button + $keybord[$row][] = [ + 'text' => 'sperma', + 'callback_data' => 'brainrot_profile_dolboeb' + ]; + + if ($keyboard[$row] >= $row_amount) { + // Reached the row buttons limit + + // POHUY + ++$row; + } + + // POHUY + --$slots; + } + + for (; $slots > 0; --$slots) { + // Iterating over remain slots + + // Initializing the row + $keyboard[$row] ??= []; + + // Initializing the keyboard brainrot button + $keybord[$row][] = [ + 'text' => $localization['empty'], + 'callback_data' => 'brainrot_create' + ]; + + if ($keyboard[$row] >= $row_amount) { + // Reached the row buttons limit + + // POHUY + ++$row; + } + + // POHUY + --$slots; + } // Sending the message $context->sendMessage( << [ - 'inline_keyboard' => [ - [ - [ - 'text' => '🐣 ' . $localization['kodorvan'], - 'web_app' => [ - 'url' => 'https://brainrot.svoboda.works' - ] - ] - ] - ], + 'inline_keyboard' => $keyboard, 'disable_notification' => true, 'remove_keyboard' => true ],