Основные классы переехали на обёртку PDO. Плановое №16.

This commit is contained in:
lopar
2021-01-28 23:05:34 +02:00
parent 0099c235a7
commit 8402912098
22 changed files with 284 additions and 1940 deletions
+39 -22
View File
@@ -2,8 +2,8 @@
namespace Battles;
use Battles\Database\DBPDO;
use Exceptions\GameException;
use db;
class User
{
@@ -53,10 +53,12 @@ class User
// Динамически рассчитываемые
public $maxHealth = 5;
public $maxMana = 5;
protected static $db;
public function __construct($user)
public function __construct(int $user)
{
$user_query = db::c()->query('SELECT * FROM users WHERE id = "?s" OR login = "?s"', $user, $user)->fetch_assoc();
self::$db = DBPDO::INIT();
$user_query = self::$db->fetch('SELECT * FROM users WHERE id = ? OR login = ?', [$user, $user]);
foreach ($this as $key => $value) {
if (isset($user_query[$key])) {
$this->$key = $user_query[$key];
@@ -73,7 +75,7 @@ class User
* @return string
* @throws GameException
*/
public function getStat($stat_name, $isMainWindow = 0)
public function getStat($stat_name, $isMainWindow = 0):string
{
$allowed = ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'];
if (in_array($stat_name, $allowed)) {
@@ -89,16 +91,16 @@ class User
/**
* Повышает один из выбранных статов на 1, но не выше self::STAT_MAXIMUM_AMOUNT при условии наличия свободных очков статов.
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'.
* @param string $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'.
* @throws GameException
*/
public function addOnePointToStat($stat_name)
public function addOnePointToStat(string $stat_name)
{
$allowed = ['strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'];
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);
$query = "UPDATE users SET {$stat_name} = {$stat_name} + 1, free_stat_points = free_stat_points - 1 WHERE id = ?";
self::$db->execute($query,$this->id);
} else {
throw new GameException(self::ERROR_STAT_IS_MAXIMUM);
}
@@ -108,7 +110,7 @@ class User
}
protected function showStarSign()
protected function showStarSign(): ?string
{
/*
* 1 aries
@@ -124,19 +126,21 @@ class User
* 11 aquarius
* 12 pisches
*/
$zodiac[356] = "10";
$zodiac[326] = "09";
$zodiac[296] = "08";
$zodiac[266] = "07";
$zodiac[235] = "06";
$zodiac[203] = "05";
$zodiac[172] = "04";
$zodiac[140] = "03";
$zodiac[111] = "02";
$zodiac[78] = "01";
$zodiac[51] = "12";
$zodiac[20] = "11";
$zodiac[0] = "10";
$zodiac = [
356 => "10",
326 => "09",
296 => "08",
266 => "07",
235 => "06",
203 => "05",
172 => "04",
140 => "03",
111 => "02",
78 => "01",
51 => "12",
20 => "11",
0 => "10",
];
$dayOfYear = date("z", strtotime($this->borndate));
$isLeapYear = date("L", strtotime($this->borndate)); //Высокосный?
if ($isLeapYear && $dayOfYear > 59) {
@@ -159,4 +163,17 @@ class User
{
return $this->mana . '/' . $this->maxMana;
}
public static function setUserEffect(int $userId, int $type, string $name, int $time):bool
{
return self::$db->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time) VALUES (?,?,?,?)',[$userId, $type, $name, $time]);
}
public static function removeUserEffect(int $userId, int $type):bool
{
if (self::$db->fetch('SELECT 1 FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type])) {
self::$db->execute('DELETE FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type]);
}
return false;
}
}