Анонимный класс для регистрации. 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
namespace Battles;
use Exceptions\GameException;
use db;
class User
{
@ -53,7 +56,7 @@ class 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) {
if (isset($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, при условии наличия свободных очков статов.
* @return string
* @throws GameException
@ -80,7 +83,7 @@ class User
return $this->$stat_name;
}
} 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 ($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';
\db::c()->query($query, $stat_name, $stat_name, $this->id);
db::c()->query($query, $stat_name, $stat_name, $this->id);
} else {
throw new \Exceptions\GameException(self::ERROR_STAT_IS_MAXIMUM);
throw new GameException(self::ERROR_STAT_IS_MAXIMUM);
}
} else {
throw new \Exceptions\GameException(self::ERROR_STAT_UNKNOWN);
throw new GameException(self::ERROR_STAT_UNKNOWN);
}
}

View File

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