Анонимный класс для регистрации. Closes #20

This commit is contained in:
Igor Barkov (iwork) 2021-01-27 17:56:04 +02:00
parent a1c052f585
commit 4701e4b542
2 changed files with 33 additions and 23 deletions

View File

@ -1,6 +1,9 @@
<?php <?php
namespace Battles; namespace Battles;
use Exceptions\GameException; use Exceptions\GameException;
use db;
class User class User
{ {
@ -53,7 +56,7 @@ class User
public function __construct($user) public function __construct($user)
{ {
$user_query = \db::c()->query('SELECT * FROM users WHERE id = "?s" OR login = "?s"', $user, $user)->fetch_assoc(); $user_query = db::c()->query('SELECT * FROM users WHERE id = "?s" OR login = "?s"', $user, $user)->fetch_assoc();
foreach ($this as $key => $value) { foreach ($this as $key => $value) {
if (isset($user_query[$key])) { if (isset($user_query[$key])) {
$this->$key = $user_query[$key]; $this->$key = $user_query[$key];
@ -65,7 +68,7 @@ class User
/** /**
* Отдаёт информацию о базовом(!) стате. * Отдаёт информацию о базовом(!) стате.
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'. * @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'.
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку на повышение стата на 1, при условии наличия свободных очков статов. * @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку на повышение стата на 1, при условии наличия свободных очков статов.
* @return string * @return string
* @throws GameException * @throws GameException
@ -80,7 +83,7 @@ class User
return $this->$stat_name; return $this->$stat_name;
} }
} else { } else {
throw new \Exceptions\GameException(self::ERROR_STAT_UNKNOWN); throw new GameException(self::ERROR_STAT_UNKNOWN);
} }
} }
@ -95,12 +98,12 @@ class User
if (in_array($stat_name, $allowed)) { if (in_array($stat_name, $allowed)) {
if ($this->free_stat_points > 0 && $this->$stat_name <= self::STAT_MAXIMUM_AMOUNT) { if ($this->free_stat_points > 0 && $this->$stat_name <= self::STAT_MAXIMUM_AMOUNT) {
$query = 'UPDATE users SET ?f = ?f + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i'; $query = 'UPDATE users SET ?f = ?f + 1, free_stat_points = free_stat_points - 1 WHERE id = ?i';
\db::c()->query($query, $stat_name, $stat_name, $this->id); db::c()->query($query, $stat_name, $stat_name, $this->id);
} else { } else {
throw new \Exceptions\GameException(self::ERROR_STAT_IS_MAXIMUM); throw new GameException(self::ERROR_STAT_IS_MAXIMUM);
} }
} else { } else {
throw new \Exceptions\GameException(self::ERROR_STAT_UNKNOWN); throw new GameException(self::ERROR_STAT_UNKNOWN);
} }
} }

View File

@ -1,4 +1,7 @@
<?php <?php
use Battles\Template;
session_start(); session_start();
require_once "config.php"; require_once "config.php";
if ($_COOKIE[GAMEDOMAIN] ?? null) { if ($_COOKIE[GAMEDOMAIN] ?? null) {
@ -15,25 +18,29 @@ if ($_COOKIE[GAMEDOMAIN] ?? null) {
$ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); $ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP);
if ($login && $password && $email && $birthday && $law && $law2) { if ($login && $password && $email && $birthday && $law && $law2) {
$check = db::c()->query('SELECT 1 FROM `users` WHERE `login` = "?s" OR `email` = "?s"', $login, $email)->getNumRows(); $newUser = new class {
if ($check > 0) { public static function addUser(string $login, string $password, string $email, string $birthday): bool
$error = "В системе уже есть такие данные!"; {
} else { if (db::c()->query('SELECT 1 FROM `users` WHERE `login` = "?s" OR `email` = "?s"', $login, $email)->getNumRows()) {
db::c()->query('INSERT INTO users (login,pass,email,borndate,ip,session_id) return false;
VALUES ("?s", "?s", "?s", "?s", "?s", "?s")', $login, $password, $email, $birthday, $ip, session_id()); }
$nid = db::c()->getLastInsertId(); db::c()->query('INSERT INTO users (login,pass,email,borndate,ip,session_id,shadow)
db::c()->query('INSERT INTO `online` (user_id, date, room, real_time) VALUES (?i, ?i, ?i, ?i)', $nid, time(), 1, time()); VALUES ("?s", "?s", "?s", "?s", "?s", "?s", "?s")', $login, $password, $email, $birthday, $_SERVER['REMOTE_ADDR'], session_id(), '0.gif');
db::c()->query('INSERT INTO `bank` (`user_id`) VALUES (?i)', $nid, 5); db::c()->query('INSERT INTO `online` (user_id, date, room, real_time) VALUES (?i, ?i, ?i, ?i)', db::c()->getLastInsertId(), time(), 1, time());
setcookie(GAMEDOMAIN, $nid, time() + 3600); db::c()->query('INSERT INTO `bank` (user_id) VALUES (?i)', db::c()->getLastInsertId());
setcookie("battle", time()); setcookie(GAMEDOMAIN, db::c()->getLastInsertId(), time() + 3600);
$_SESSION['uid'] = $nid; setcookie("battle", time());
$_SESSION['sid'] = session_id(); $_SESSION['uid'] = db::c()->getLastInsertId();
header('Location: fight.php'); $_SESSION['sid'] = session_id();
exit; return true;
} }
};
$newUser::addUser($login, $password, $email, $birthday);
header('Location: fight.php');
exit;
} }
} }
\Battles\Template::header('Регистрация персонажа'); Template::header('Регистрация персонажа');
?> ?>
<a href="/"> на главную</a> <a href="/"> на главную</a>
<?php if ($error ?? null): ?> <?php if ($error ?? null): ?>