Merge branch 'master' into dev-arena

This commit is contained in:
lopar
2022-02-12 14:41:36 +02:00
84 changed files with 1775 additions and 2134 deletions
+11 -14
View File
@@ -8,7 +8,7 @@
namespace Battles;
use Exceptions\GameException;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use Throwable;
class Bank
@@ -16,7 +16,6 @@ class Bank
public int $user_id = 0;
private int $money = 0;
private $user;
private static DBPDO $db;
const ERROR_NO_MONEY_IN_WALLET = "Ошибка! Нет денег в кошельке!";
const ERROR_NO_BANK_ACCOUNT = "Ошибка! Счёта не существует!";
@@ -33,9 +32,8 @@ class Bank
public function __construct(int $user_id)
{
self::$db = DBPDO::INIT();
$bank_row = self::$db->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $user_id);
$this->user = self::$db->fetch('SELECT money FROM users WHERE id = ?', $user_id);
$bank_row = Db::getInstance()->fetch('SELECT user_id, money FROM bank WHERE user_id = ?', $user_id);
$this->user = Db::getInstance()->fetch('SELECT money FROM users WHERE id = ?', $user_id);
foreach ($this as $key => $value) {
if (isset($bank_row[$key])) {
$this->$key = $bank_row[$key];
@@ -70,9 +68,9 @@ class Bank
*
* @return void
*/
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = 0): void
private function bankLogs(int $receiverId, int $amount, string $operationType, int $senderId = null): void
{
if (!$senderId) {
if (is_null($senderId)) {
$senderId = $this->user_id;
}
$text = self::LOG[$operationType];
@@ -98,7 +96,7 @@ class Bank
*/
public function sendMoney(int $receiver, int $amount): int
{
$receiverWallet = self::$db->fetch('SELECT money FROM bank WHERE user_id = ?', $receiver);
$receiverWallet = Db::getInstance()->ofetch('SELECT money FROM bank WHERE user_id = ?', $receiver);
if ($amount <= 0) {
throw new GameException(self::ERROR_WRONG_AMOUNT);
}
@@ -134,8 +132,8 @@ class Bank
if ($amount <= 0) {
throw new GameException(self::ERROR_WRONG_AMOUNT);
}
$wallet = self::$db->fetch('SELECT money FROM users WHERE id = ?', $this->user_id);
if ($wallet->money < $amount) {
$walletMoney = Db::getInstance()->fetchColumn('SELECT money FROM users WHERE id = ?', $this->user_id);
if ($walletMoney < $amount) {
throw new GameException(self::ERROR_NO_MONEY_IN_WALLET);
}
// Забираем деньги из кошелька получателя
@@ -195,7 +193,7 @@ class Bank
public static function setBankMoney(int $amount, int $user_id, string $operationType = ''): void
{
try {
self::$db->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$amount, $user_id]);
Db::getInstance()->execute('UPDATE bank SET money = ? WHERE user_id = ?', [$amount, $user_id]);
if ($operationType) {
GameLogs::addBankLog(0, 0, $amount, $operationType, self::LOG[$operationType]);
}
@@ -215,9 +213,8 @@ class Bank
*/
public static function setWalletMoney(int $amount, int $user_id): void
{
$u = new User($user_id);
$u->setMoney($amount);
$u->saveMoney();
User::getInstance($user_id)->setMoney($amount);
User::getInstance($user_id)->saveMoney();
}
public function getMoney(): int
+7 -7
View File
@@ -2,16 +2,16 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use DateTime;
use Exception;
class Chat
{
private DateTime $d;
private DBPDO $db;
private Db $db;
public function __construct(DBPDO $db)
public function __construct(Db $db)
{
$this->db = $db;
}
@@ -34,7 +34,7 @@ class Chat
or r.id is null
or s.id = ?
order by chat.id';
$chatrows = $this->db->ofetchALL($query, [User::$current->getId(), User::$current->getId()]);
$chatrows = $this->db->ofetchALL($query, [User::getInstance()->getId(), User::getInstance()->getId()]);
$wrappedMessage = null;
foreach ($chatrows as $row) {
try {
@@ -45,7 +45,7 @@ class Chat
$m = htmlspecialchars($row->msg);
if ($row->type === 'sys' && empty($row->rid)) {
$wrappedMessage .= sprintf('<span class="chatsys">%s %s</span><br>', $this->d->format('H:i'), $m);
} elseif ($row->rid === User::$current->getId()) {
} elseif ($row->rid === User::getInstance()->getId()) {
if ($row->type === 'sys') {
$wrappedMessage .= sprintf('<span class="chatsys">%s [Система] → %s</span><br>', $this->d->format('H:i'), $m);
} elseif ($row->type == 'sms') {
@@ -64,12 +64,12 @@ class Chat
public function addMessage(string $msg)
{
$this->db->execute('insert into chat (user_id, msg) values (?,?)', [User::$current->getId(), $msg]);
$this->db->execute('insert into chat (user_id, msg) values (?,?)', [User::getInstance()->getId(), $msg]);
}
public static function addSYSMessage( string $msg, ?int $receiver_id = null)
{
DBPDO::$db->execute('insert into chat (user_id, msg, receiver_id, type) values (?,?,?,?)', [User::$current->getId(), $msg, $receiver_id, 'sys']);
Db::getInstance()->execute('insert into chat (user_id, msg, receiver_id, type) values (?,?,?,?)', [User::getInstance()->getId(), $msg, $receiver_id, 'sys']);
}
}
+3 -3
View File
@@ -3,19 +3,19 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Check
{
private User $user;
private DBPDO $db;
private Db $db;
/**
* Check constructor.
*
* @param User $user
*/
public function __construct(User $user, DBPDO $db)
public function __construct(User $user, Db $db)
{
$this->user = $user;
$this->db = $db;
+33 -32
View File
@@ -2,104 +2,105 @@
# Date: 23.08.2021 (23:05)
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Clan
{
private DBPDO $db;
private User $user;
private static ?self $_instance = null;
private $clan;
public static Clan $current;
public function __construct()
private function __construct()
{
$this->db = DBPDO::$db;
$this->user = User::$current;
$this->clan = $this->db->ofetch('select * from clans where owner_id = ?', $this->user->getId());
$this->clan = Db::getInstance()->ofetch('select * from clans where owner_id = ?', User::getInstance()->getId());
}
public static function getInstance(): self
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function addMember(string $login): string
{
$target = new User($login);
$error = null;
if (!$this->getProverka($target->getId())) {
if (!$this->getProverka(User::getInstance($login)->getId())) {
$error .= '<br>Нет проверки!';
}
if ($target->getClan()) {
if (User::getInstance($login)->getClan()) {
$error .= '<br>Персонаж уже состоит в клане!';
}
if ($target->getLevel() < 1) {
if (User::getInstance($login)->getLevel() < 1) {
$error .= '<br>Персонаж 0 уровня не может быть принят!';
}
if ($this->user->getMoney() < GameConfigs::CLAN['add_member_cost']) {
if (User::getInstance()->getMoney() < GameConfigs::CLAN['add_member_cost']) {
$error .= '<br>Недостаточно денег!';
}
if ($error) {
return $error;
}
$this->user->setMoney($this->user->getMoney() - GameConfigs::CLAN['add_member_cost']);
$this->user->saveMoney();
$target->setClan($this->user->getClan());
User::getInstance()->setMoney(User::getInstance()->getMoney() - GameConfigs::CLAN['add_member_cost']);
User::getInstance()->saveMoney();
User::getInstance($login)->setClan(User::getInstance()->getClan());
return "Персонаж «{$login}» успешно принят в клан.";
}
public function removeMember(string $login): string
{
$target = new User($login);
$error = null;
if ($this->user->getMoney() < GameConfigs::CLAN['remove_member_cost']) {
if (User::getInstance()->getMoney() < GameConfigs::CLAN['remove_member_cost']) {
$error .= '<br>Недостаточно денег!';
}
if ($target->getId() === $this->user->getId()) {
if (User::getInstance($login)->getId() === User::getInstance()->getId()) {
$error .= '<br>Себя выгонять нельзя!';
}
if ($target->getClan() !== $this->user->getClan()) {
if (User::getInstance($login)->getClan() !== User::getInstance()->getClan()) {
$error .= '<br>Персонаж не состоит в этом клане!';
}
if ($error) {
return $error;
}
$this->user->setMoney($this->user->getMoney() - GameConfigs::CLAN['remove_member_cost']);
$this->user->saveMoney();
$target->setClan(null);
User::getInstance()->setMoney(User::getInstance()->getMoney() - GameConfigs::CLAN['remove_member_cost']);
User::getInstance()->saveMoney();
User::getInstance($login)->setClan(null);
return "Персонаж «{$login}» покинул клан.";
}
public function changeOwner(string $login): string
{
$target = new User($login);
$error = null;
if ($target->getId() === $this->user->getId()) {
if (User::getInstance($login)->getId() === User::getInstance()->getId()) {
$error .= '<br>Самоудовлетворяетесь? ;)';
}
if ($target->getClan() !== $this->user->getClan()) {
if (User::getInstance($login)->getClan() !== User::getInstance()->getClan()) {
$error .= '<br>Персонаж не состоит в этом клане!';
}
if ($error) {
return $error;
}
$this->db->execute('update clans set owner_id = ? where owner_id = ?', [$target->getId(), $this->user->getId()]);
Db::getInstance()->execute('update clans set owner_id = ? where owner_id = ?', [User::getInstance($login)->getId(), User::getInstance()->getId()]);
return 'Вы передали управление кланом персонажу «' . $login . '».';
}
public function setClanInfo(string $text): string
{
$check = $this->db->ofetch('select id from users where clan = (select short_name from clans where owner_id = ?)', $this->user->getId());
if ($check->id !== $this->user->getId()) {
$checkId = Db::getInstance()->fetchColumn('select id from users where clan = (select short_name from clans where owner_id = ?)', User::getInstance()->getId());
if ($checkId !== User::getInstance()->getId()) {
return 'Ошибка доступа!';
}
$this->db->execute('update clans set info = ? where owner_id = ?', [$text, $check->id]);
Db::getInstance()->execute('update clans set info = ? where owner_id = ?', [$text, $checkId]);
return 'Описание клана изменено!';
}
public function getMemberlist(): array
{
return $this->db->ofetchAll('select id, (select 1 from clans where short_name = clan and owner_id = id) as clan_owner, room from users where clan = ? order by clan_owner desc, room, login', $this->user->getClan());
return Db::getInstance()->fetchAll('select id, (select 1 from clans where short_name = clan and owner_id = id) as clan_owner, room from users where clan = ? order by clan_owner desc, room, login', User::getInstance()->getClan());
}
private function getProverka($user_id)
{
return $this->db->fetch('select 1 from users_effects where type = 20 and owner_id = ?', $user_id);
return Db::getInstance()->fetchColumn('select count(*) from users_effects where type = 20 and owner_id = ?', $user_id);
}
public function getClanOwnerId(): ?int
-154
View File
@@ -1,154 +0,0 @@
<?php
namespace Battles\Database;
use Battles\GameConfigs;
use PDO, PDOException;
class DBPDO
{
public $pdo;
private static $_instance = null;
public static DBPDO $db;
function __construct()
{
$this->connect();
}
public static function INIT(): DBPDO
{
if (!self::$_instance) {
self::$_instance = new DBPDO();
}
return self::$_instance;
}
function prep_query($query)
{
return $this->pdo->prepare($query);
}
function connect():bool
{
if (!$this->pdo) {
$dsn = 'mysql:dbname=' . GameConfigs::DATABASE_NAME . ';host=' . GameConfigs::DATABASE_HOST . ';port=' . GameConfigs::DATABASE_PORT . ';charset=utf8;';
$user = GameConfigs::DATABASE_USER;
$password = GameConfigs::DATABASE_PASS;
try {
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
return true;
} catch (PDOException $e) {
die($e->getMessage());
}
} else {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return true;
}
}
function table_exists($table_name)
{
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
$stmt->execute(array($table_name));
return $stmt->rowCount() > 0;
}
function execute($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->prep_query($query);
$stmt->execute($values);
return $stmt;
}
function fetch($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function fetchAll($query, $values = null, $key = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Allows the user to retrieve results using a
// column from the results as a key for the array
if ($key != null && $results[0][$key]) {
$keyed_results = array();
foreach ($results as $result) {
$keyed_results[$result[$key]] = $result;
}
$results = $keyed_results;
}
return $results;
}
function ofetch($query, $values = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_OBJ);
}
function ofetchAll($query, $values = null, $key = null)
{
if ($values == null) {
$values = array();
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
// Allows the user to retrieve results using a
// column from the results as a key for the array
if ($key != null && $results[0][$key]) {
$keyed_results = (object) array();
foreach ($results as $result) {
$keyed_results->$result[$key] = $result;
}
$results = $keyed_results;
}
return $results;
}
function lastInsertId()
{
return $this->pdo->lastInsertId();
}
function fetchColumn($query, $values = null) {
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
return $stmt->fetchColumn();
}
}
+134
View File
@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace Battles\Database;
use Battles\GameConfigs;
use PDO, PDOException;
class Db
{
private PDO $pdo;
private static ?self $_instance = null;
private function __construct()
{
$this->connect();
}
private function connect(): void
{
$dsn = 'mysql:dbname=' . GameConfigs::DATABASE_NAME . ';host=' . GameConfigs::DATABASE_HOST . ';port=' . GameConfigs::DATABASE_PORT . ';charset=utf8;';
$user = GameConfigs::DATABASE_USER;
$password = GameConfigs::DATABASE_PASS;
try {
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance(): self
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function execute($query, $values = null)
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->pdo->prepare($query);
$stmt->execute($values);
return $stmt;
}
public function fetch($query, $values = null)
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
public function fetchAll($query, $values = null, $key = null): array
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Allows the user to retrieve results using a
// column from the results as a key for the array
if (!is_null($key) && $results[0][$key]) {
$keyed_results = array();
foreach ($results as $result) {
$keyed_results[$result[$key]] = $result;
}
$results = $keyed_results;
}
return $results;
}
public function ofetch($query, $values = null)
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_OBJ);
}
public function ofetchAll($query, $values = null, $key = null): object
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
// Allows the user to retrieve results using a
// column from the results as a key for the array
if (!is_null($key) && $results[0][$key]) {
$keyed_results = (object)[];
foreach ($results as $result) {
$keyed_results->$result[$key] = $result;
}
$results = $keyed_results;
}
return $results;
}
public function lastInsertId()
{
return $this->pdo->lastInsertId();
}
public function fetchColumn($query, $values = null)
{
if (is_null($values)) {
$values = [];
} elseif (!is_array($values)) {
$values = [$values];
}
$stmt = $this->execute($query, $values);
return $stmt->fetchColumn();
}
}
+3 -3
View File
@@ -7,7 +7,7 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use stdClass;
class DressedItems
@@ -22,13 +22,13 @@ class DressedItems
*/
public function __construct(int $user_id)
{
self::$db = DBPDO::INIT();
self::$db = Db::getInstance();
$this->USERID = $user_id;
}
public static function getDressedItemBySlot($itemSlot, $ownerId)
{
return self::$db->fetch('SELECT *, COUNT(1) AS count FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
return self::$db->ofetch('SELECT *, COUNT(1) AS count FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
}
public function getItemsInSlots(): stdClass
+1 -1
View File
@@ -81,7 +81,7 @@ class GameLogs
public static function addBattleLog(int $battle_id, string $text)
{
$db = new SQLite3(__DIR__ . '../../Database/battle.logs.db');
$db = new SQLite3(__DIR__ . '../../Database/battle.logs.getInstance');
$row = $db->prepare('insert into newbattles (battle_id, text) values (?,?)');
$row->bindParam(1, $battle_id, SQLITE3_INTEGER);
$row->bindParam(2, $text, SQLITE3_TEXT);
+14 -16
View File
@@ -1,13 +1,12 @@
<?php
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class InventoryItem extends Item
{
private $present;
private $owner_id;
private $db;
private const TOO_MANY_ITEMS_IN_SLOTS = 'Критическая ошибка: Переполнение слота!';
private const UNKNOWN_ITEM_TYPE = 'Неизвестный тип предмета!';
private const REQUIREMENTS_NOT_MET = 'Персонаж не соответствует требованиям!';
@@ -22,7 +21,6 @@ class InventoryItem extends Item
parent::__construct($row);
$this->owner_id = $row->owner_id;
$this->present = $row->present;
$this->db = DBPDO::INIT();
}
public function printInfo()
@@ -37,13 +35,13 @@ class InventoryItem extends Item
{
if (in_array($this->item_type, range(1, 12))) {
echo <<<HTML
<a href=/main.php?edit=1&dress={$this->item_id} title='Надеть'>
<img src="/i/sh/{$this->image}" class="item-wrap-normal" alt="">
<a href=/main.php?edit=1&dress=$this->item_id title='Надеть'>
<img src="/i/sh/$this->image" class="item-wrap-normal" alt="">
</a>
HTML;
} else {
echo <<<IMG
<img src="/i/sh/{$this->image}" class="item-wrap-normal" alt="">
<img src="/i/sh/$this->image" class="item-wrap-normal" alt="">
IMG;
}
}
@@ -79,8 +77,8 @@ IMG;
// считаем сколько ОДЕТЫХ предметов в слоте в который мы хотим одеть предмет. 1=просто вещь 1-3=шашни с кольцами
// Count добавленный в первый запрос возвращает одну строку в любом случае.
// fetch возвращает одну строку в любом случае.
$weared = $this->db->ofetchAll('SELECT dressed_slot FROM inventory WHERE dressed_slot != 0 AND item_type = ? AND owner_id = ?', [$this->item_type, $this->owner_id]);
$wearedCount = $this->db->ofetch('select count(dressed_slot) as c from inventory where dressed_slot !=0 and item_type = ? and owner_id = ?', [$this->item_type, $this->owner_id]);
$weared = Db::getInstance()->ofetchAll('SELECT dressed_slot FROM inventory WHERE dressed_slot != 0 AND item_type = ? AND owner_id = ?', [$this->item_type, $this->owner_id]);
$wearedCount = Db::getInstance()->ofetch('select count(dressed_slot) as c from inventory where dressed_slot !=0 and item_type = ? and owner_id = ?', [$this->item_type, $this->owner_id]);
// Если в слоте есть предмет(ы), забиваем их массив одетых в слот предметов.
if ($wearedCount) {
foreach ($weared as $item) {
@@ -95,11 +93,11 @@ IMG;
//работаем с нормальными слотами
if ($wearedCount->c == 1) {
//если слот занят, снимаем старый предмет и одеваем новый предмет
$this->db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = ? AND owner_id = ?', [$itemInSlot[0], $this->owner_id]);
$this->db->execute('UPDATE inventory SET dressed_slot = item_type WHERE item_id = ? AND owner_id = ?', [$this->item_id, $this->owner_id]);
Db::getInstance()->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = ? AND owner_id = ?', [$itemInSlot[0], $this->owner_id]);
Db::getInstance()->execute('UPDATE inventory SET dressed_slot = item_type WHERE item_id = ? AND owner_id = ?', [$this->item_id, $this->owner_id]);
} elseif (!$wearedCount->c) {
//если слот пуст, одеваем новый предмет
$this->db->execute('UPDATE inventory SET dressed_slot = item_type WHERE item_id = ? AND owner_id = ?', [$this->item_id, $this->owner_id]);
Db::getInstance()->execute('UPDATE inventory SET dressed_slot = item_type WHERE item_id = ? AND owner_id = ?', [$this->item_id, $this->owner_id]);
} else {
/* проверка на переполнение слотов */
$error = self::TOO_MANY_ITEMS_IN_SLOTS;
@@ -113,11 +111,11 @@ IMG;
// Сортируем массив свободных слотов по возрастанию.
sort($emptyRingSlots);
// Одеваем предмет в первый свободный слот.
$this->db->execute('update inventory set dressed_slot = ? where item_id = ?', [$emptyRingSlots[0], $this->item_id]);
Db::getInstance()->execute('update inventory set dressed_slot = ? where item_id = ?', [$emptyRingSlots[0], $this->item_id]);
} elseif ($wearedCount->c == 3) {
// Cнимаем предмет из последнего слота 11 и одеваем новый предмет
$this->db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = 11');
$this->db->execute('UPDATE inventory SET dressed_slot = 11 WHERE item_id = ?', $this->item_id);
Db::getInstance()->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = 11');
Db::getInstance()->execute('UPDATE inventory SET dressed_slot = 11 WHERE item_id = ?', $this->item_id);
} else {
/* проверка на переполнение слотов */
$error = self::TOO_MANY_ITEMS_IN_SLOTS;
@@ -132,7 +130,7 @@ IMG;
public static function destroyItem($itemId)
{
DBPDO::INIT()->execute('delete from inventory where dressed_slot = 0 and owner_id = ? and item_id = ?', [$_SESSION['uid'], $itemId]);
Db::getInstance()->execute('delete from inventory where dressed_slot = 0 and owner_id = ? and item_id = ?', [$_SESSION['uid'], $itemId]);
}
/** Надеюсь, временная заглушка, которая объединяет get_meshok() и другую выдачу одной строкой.
@@ -141,7 +139,7 @@ IMG;
public static function getWeightData(): string
{
$query = 'select sum(weight) as `all`, strength * 4 as max from inventory left join users u on owner_id = id where owner_id = ?';
$weight = DBPDO::$db->ofetch($query, User::$current->getId());
$weight = Db::getInstance()->ofetch($query, User::getInstance()->getId());
$css = $weight->all > $weight->max ? ' style="color:maroon;"' : '';
return "<span$css>$weight->all / $weight->max</span>";
}
+2 -2
View File
@@ -1,7 +1,7 @@
<?php
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Item
{
@@ -193,6 +193,6 @@ class Item
public static function getItemById($item_id): Item
{
return new Item(DBPDO::$db->ofetch('select * from items where id = ?', $item_id));
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $item_id));
}
}
+9 -9
View File
@@ -3,7 +3,7 @@
// Магия лечения травм
namespace Battles\Magic;
use Battles\UserEffects, Battles\Database\DBPDO, Battles\User;
use Battles\UserEffects, Battles\Database\Db, Battles\User;
class CureInjury extends Magic
{
@@ -18,7 +18,7 @@ class CureInjury extends Magic
*/
public function __construct(int $target, int $injuryType)
{
$db = DBPDO::INIT();
$db = Db::getInstance();
$this->target = $target;
if (!$this->isUsable()) {
return $this->status;
@@ -46,13 +46,13 @@ class CureInjury extends Magic
*/
private function isUsable(): bool
{
$caster = new User($_SESSION['uid']);
if ($this->target == $_SESSION['uid']) {
$this->target = $caster;
} else {
$this->target = new User($this->target);
}
$this->target = $this->target == $_SESSION['uid'] ? User::getInstance() : User::getInstance($this->target);
$this->login = $this->target->getLogin();
return ($this->isVisible($caster, $this->target) && $this->isNotDead($caster) && $this->enoughMana($caster) && $this->isNotInBattle($caster));
return ($this->isVisible(User::getInstance(), $this->target) && $this->isNotDead(User::getInstance()) && $this->enoughMana(User::getInstance()) && $this->isNotInBattle(User::getInstance()));
}
public static function cast($target, $type): self
{
return new self($target, $type);
}
}
+4 -9
View File
@@ -2,7 +2,7 @@
// Магия восстановления здоровья
use Battles\Magic\Magic;
use Battles\User;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Healing extends Magic
{
@@ -30,7 +30,7 @@ class Healing extends Magic
if ($healHealthAmount > $this->target->maxHealth) {
$healHealthAmount = $this->target->maxHealth;
}
DBPDO::INIT()->execute('UPDATE users SET health = ? WHERE id = ?', [$healHealthAmount, $this->target->id]);
Db::getInstance()->execute('UPDATE users SET health = ? WHERE id = ?', [$healHealthAmount, $this->target->id]);
$targetName = $this->target->login;
return "Вы восстановили ${healHealthAmount} здоровья персонажу ${targetName}.";
}
@@ -41,12 +41,7 @@ class Healing extends Magic
*/
private function isUsable(): bool
{
$caster = new User($_SESSION['uid']);
if ($this->target == $_SESSION['uid']) {
$this->target = $caster;
} else {
$this->target = new User($this->target);
}
return $this->isVisible($caster, $this->target) && $this->isNotDead($caster) && $this->enoughMana($caster) && $this->isSuccess($caster);
$this->target = $this->target == $_SESSION['uid'] ? User::getInstance() : User::getInstance($this->target);
return $this->isVisible(User::getInstance(), $this->target) && $this->isNotDead(User::getInstance()) && $this->enoughMana(User::getInstance()) && $this->isSuccess(User::getInstance());
}
}
+7 -8
View File
@@ -3,7 +3,7 @@
namespace Battles\Magic;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use Battles\DressedItems;
use Battles\Item;
use Battles\User;
@@ -27,20 +27,19 @@ class Sharpen extends Magic
}
$item = DressedItems::getDressedItemBySlot(Item::ITEM_TYPE_WEAPON, $_SESSION['uid']);
// Проверяем, что в названии предмета нет цифр и плюсов.
if (preg_match('/[\W\S]+\+\[?[\d]]?/', $item['name'])) {
if (preg_match('/[\W\S]+\+\[?[\d]]?/', $item->name)) {
return 'Этот предмет точить нельзя!';
}
$newMinPhysicalDamage = $item['add_min_physical_damage'] + $sharpenStrength;
$newMaxPhysicalDamage = $item['add_max_physical_damage'] + $sharpenStrength;
$newItemName = $item['name'] . " [+$sharpenStrength]";
$newMinPhysicalDamage = $item->add_min_physical_damage + $sharpenStrength;
$newMaxPhysicalDamage = $item->add_max_physical_damage + $sharpenStrength;
$newItemName = $item->name . " [+$sharpenStrength]";
DBPDO::INIT()->execute('UPDATE battles.inventory SET name = ?, add_min_physical_damage = ?, add_max_physical_damage = ? WHERE item_id = ? ', [$newItemName, $newMinPhysicalDamage, $newMaxPhysicalDamage, $item['item_id']]);
Db::getInstance()->execute('UPDATE battles.inventory SET name = ?, add_min_physical_damage = ?, add_max_physical_damage = ? WHERE item_id = ? ', [$newItemName, $newMinPhysicalDamage, $newMaxPhysicalDamage, $item->item_id]);
return "У вас получилось изготовить предмет $newItemName!";
}
private function isUsable(): bool
{
$caster = new User($_SESSION['uid']);
return $this->isNotInBattle($caster) && $this->isSuccess($caster, $this->magicDifficulty);
return $this->isNotInBattle(User::getInstance()) && $this->isSuccess(User::getInstance(), $this->magicDifficulty);
}
}
+2 -2
View File
@@ -7,7 +7,7 @@
namespace Battles\Models;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class EffectsModel
{
@@ -16,7 +16,7 @@ class EffectsModel
public function __construct(int $user_id)
{
$this->DB = DBPDO::INIT()->ofetchAll('SELECT * FROM users_effects WHERE owner_id = ?', $user_id);
$this->DB = Db::getInstance()->ofetchAll('SELECT * FROM users_effects WHERE owner_id = ?', $user_id);
}
/**
+2 -2
View File
@@ -6,7 +6,7 @@
*/
namespace Battles\Models;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class PresentsModel
{
@@ -15,7 +15,7 @@ class PresentsModel
public function __construct(int $user_id)
{
if (!$this->DB) {
$this->DB = DBPDO::INIT()->execute('SELECT sender_id, image FROM `users_presents` WHERE owner_id = ?', $user_id);
$this->DB = Db::getInstance()->execute('SELECT sender_id, image FROM `users_presents` WHERE owner_id = ?', $user_id);
}
}
+5 -5
View File
@@ -3,7 +3,7 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Moderation
{
@@ -54,13 +54,13 @@ class Moderation
public static function blockUser(int $target)
{
self::addEffectStatusToUserLog($target, "Блокировка");
DBPDO::INIT()->execute('UPDATE battles.users SET block = 1 WHERE id = ?', $target);
Db::getInstance()->execute('UPDATE battles.users SET block = 1 WHERE id = ?', $target);
}
public static function unBlockUser(int $target)
{
self::addEffectStatusToUserLog($target, "Блокировка" . self::STATUS_OFF);
DBPDO::INIT()->execute('UPDATE battles.users SET block = 0 WHERE block = 1 AND id = ?', $target);
Db::getInstance()->execute('UPDATE battles.users SET block = 0 WHERE block = 1 AND id = ?', $target);
}
public static function addToUserLog(int $target, string $message, int $senderId)
@@ -70,12 +70,12 @@ class Moderation
public static function setAlign(int $target, int $align)
{
DBPDO::INIT()->execute('UPDATE users SET align = ? WHERE id = ?', [$align, $target]);
Db::getInstance()->execute('UPDATE users SET align = ? WHERE id = ?', [$align, $target]);
}
public static function addChatSysMsg(string $message)
{
DBPDO::INIT()->execute('INSERT INTO chat (user_id,msg,type) VALUES (-1,?,?)', [$message, 'sys']);
Db::getInstance()->execute('INSERT INTO chat (user_id,msg,type) VALUES (-1,?,?)', [$message, 'sys']);
}
public static function addUserCheck(int $target)
+25 -47
View File
@@ -1,40 +1,41 @@
<?php
namespace Battles;
use Battles\Database\Db;
/**
* Разные способы отображения строки с логином персонажа.
*/
const INVIS = '<i>невидимка</i>';
class Nick extends User
class Nick extends UserStats
{
private function getInvisibilityStatus()
private function isInvisible()
{
return self::$db->fetch('SELECT 1 FROM users_effects WHERE type = 1022 AND owner_id = ?', $this->id);
return Db::getInstance()->execute('SELECT count(*) FROM users_effects WHERE type = 1022 AND owner_id = ?', $this->id)->fetchColumn();
}
/**
* Отображение иконки склонности.
* @return string
*/
private function getAlignToNickname():?string
private function getAlignImage(): ?string
{
if ($this->align) {
return sprintf('<img src="i/align_%s.gif">', $this->align);
} else {
return null;
}
return $this->align ? "<img src='i/align_$this->align.gif' alt='Склонность'>" : null;
}
/**
* Отображение иконки клана.
* @return string
*/
private function getClanToNickname():?string
private function getClanImage(): ?string
{
if ($this->clan) {
return sprintf('<img src="i/clan/%s.png">', $this->clan);
} else {
return null;
}
return $this->clan ? "<img src='i/clan/$this->clan.png' alt='Клан'>" : null;
}
private function getInfolinkImage(): string
{
return "<a href='inf.php?$this->login' target='_blank'><img src='i/inf.gif' alt='Ссылка на профиль'></a>";
}
/**
@@ -43,7 +44,7 @@ class Nick extends User
*
* @return Nick
*/
public static function id($playerId): Nick
public static function id($playerId): self
{
return new self($playerId);
}
@@ -55,50 +56,27 @@ class Nick extends User
*
* @return string
*/
public function full($showInvisibility = 0):string
public function full(int $showInvisibility = 0): string
{
if (!$showInvisibility && $this->getInvisibilityStatus()) {
return INVIS;
}
return $this->getAlignToNickname().$this->getClanToNickname().sprintf('<b>%s</b> [%s] <a href="inf.php?%s" target="_blank"><img src="i/inf.gif" style="width:12px;height:11px"></a>', $this->login, $this->level, $this->login);
return !$showInvisibility && $this->isInvisible() ? INVIS : $this->getAlignImage() . $this->getClanImage() . " <b>$this->login</b> [$this->level] " . $this->getInfolinkImage();
}
/**
* Возвращает строку с логином или невидимым статусом.
* @param int $showInvisibility отображать логин даже если персонаж невидимка.
* Возвращает строку с логином.
* Избавиться от этого! Оставлено для совместимости.
* @return string
*/
public function short($showInvisibility = 0):string
public function short(): string
{
if (!$showInvisibility && $this->getInvisibilityStatus()) {
return INVIS;
} else {
return htmlspecialchars($this->login);
}
return $this->login;
}
/**
* Возвращает строку со склонностью, кланом, логином, уровнем, ссылкой на профиль, здоровьем.
* @return string
*/
public function battle():string
public function battle(): string
{
return $this->getAlign().$this->getClan().sprintf('<b>%s</b> [%s] <a href="inf.php?%s" target="_blank"><img src="i/inf.gif" style="width:12px;height:11px"></a> <img src="i/herz.gif" alt="HP"> _hp_/_maxhp_', $this->login, $this->level, $this->login);
}
/**
* Возвращает строку с логином и здоровьем, выделяя строку определённым стилем.
* @param $textstyle - Название стиля отображения логина персонажа (main.css) для цветового разделения команд.
*
* @return string
*/
public function battleShort($textstyle):string
{
if ($this->getInvisibilityStatus()) {
return INVIS;
}
else {
return sprintf('<span style="%s">%s</span> [_hp_/_maxhp_]', $textstyle, $this->login);
}
return $this->full() . "<img src='i/herz.gif' alt='HP'> [$this->health/$this->maxHealth]";
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace Battles;
use Battles\Database\Db;
class Register
{
public static function addUser(string $login, string $password, string $email, string $birthday): int
{
if (Db::getInstance()->execute('select count(*) from users where login = ? or email = ?', [$login, $email])->fetchColumn()) {
return 0;
}
Db::getInstance()->execute('insert into users (login,pass,email,borndate,ip,session_id,shadow) values (?,?,?,?,?,?,?)',
[$login, $password, $email, $birthday, $_SERVER['REMOTE_ADDR'], session_id(), '0.png']);
$userId = Db::getInstance()->lastInsertId();
Db::getInstance()->execute('insert into online (user_id, login_time, room, real_time) values (?,?,1,?)', [$userId, time(), time()]);
Db::getInstance()->execute('insert into bank (user_id) values ?', $userId);
return $userId;
}
}
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace Battles;
use tidy, Battles\Database\Db;
class RememberPassword
{
const OK_MAIL_SENT = 'Письмо отправлено!';
const OK_PASSWORD_CHANGED = 'Пароль изменён!';
const ERROR_MAIL_NOT_SENT = 'Письмо не отправлено!';
const ERROR_WRONG_LOGIN = 'Такого пользователя не существует!';
const ERROR_TOO_MANY_TRIES = 'Вы уже отправляли себе письмо сегодня!';
const ERROR_OLD_HASH = 'Ссылка устарела!';
const ERROR_WRONG_HASH = 'Неверная ссылка!';
private function mailSend(string $to, string $message): bool
{
$from = "=?UTF-8?B?" . base64_encode('Noreply') . "?= <noreply@" . GAMEDOMAIN . ">";
$subject = "=?UTF-8?B?" . base64_encode('Восстановление забытого пароля') . "?=";
$headers = [
'From' => $from,
'MIME-Version' => '1.0',
'Content-type' => 'text/html; charset=UTF-8',
];
if (extension_loaded('tidy')) {
$cleaner = new tidy();
$message = $cleaner->repairString($message, ['show-errors' => 0, 'show-warnings' => false], 'utf8');
}
return mail($to, $subject, $message, $headers);
}
public function sendRecoveryMail(string $to): string
{
$check = Db::getInstance()->ofetch('SELECT email FROM users WHERE login = ?', $to);
if (!empty(Db::getInstance()->fetchColumn('select email from users where login = ?', $to))) {
return self::ERROR_WRONG_LOGIN;
}
if (!empty(Db::getInstance()->ofetch('SELECT 1 FROM users_recovery WHERE login = ?', $to))) {
return self::ERROR_TOO_MANY_TRIES;
}
$hash = uniqid();
$tomorrow = date('d-M-Y', strtotime('+1 days'));
Db::getInstance()->execute('INSERT INTO users_recovery (login, hash, ip, date) VALUES (?,?,?,?)', [$to, $hash, $tomorrow, $_SERVER['REMOTE_ADDR']]);
$message = sprintf('Здравствуйте!<br><br>Кто-то запросил восстановление пароля к вашему персонажу %s <br><br>
Для смены пароля пройдите по <a href="//%s/rememberpassword.php?change=%s">данной ссылке</a>.<br><br>
Ссылка будет действовать до <em>%s</em>',$to, GAMEDOMAIN, $hash, $tomorrow);
return self::mailSend($check->email, $message) ? self::OK_MAIL_SENT : self::ERROR_MAIL_NOT_SENT;
}
public function isAllowed($hash)
{
return Db::getInstance()->execute('SELECT count(*) FROM users_recovery WHERE hash = ? AND date < ?', [$hash, date('d-M-Y')])->fetchColumn() ? true : self::ERROR_OLD_HASH;
}
public function setNewPassword(string $newPassword, string $hash):string
{
$login = Db::getInstance()->execute('select login from users_recovery where hash = ?', $hash)->fetchColumn();
if (empty($login)) {
return self::ERROR_WRONG_HASH;
}
$newPassword = password_hash($newPassword, PASSWORD_DEFAULT);
Db::getInstance()->execute('UPDATE users SET pass = ? WHERE login = ?', [$newPassword, $login]);
Db::getInstance()->execute('DELETE FROM users_recovery WHERE hash = ?', $hash);
return self::OK_PASSWORD_CHANGED;
}
}
+13 -8
View File
@@ -2,29 +2,34 @@
# Date: 29.08.2021 (21:34)
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Shop
{
public const GENERAL_SHOP = 1;
public const BARTER_SHOP = 2;
public const CATEGORY_SALE_ITEMS = -1;
public static Shop $current;
public int $categoryType = 0;
private int $shopId;
public function __construct($shop_id) {
private function __construct($shop_id)
{
$this->shopId = $shop_id;
}
public static function id($shopid): self
{
return new self($shopid);
}
private function showGoods(): string
{
if ($this->categoryType) {
$stmt = DBPDO::$db->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0 and item_type = ?', [$this->shopId, $this->categoryType]);
$stmt2 = DBPDO::$db->ofetchAll('select * from inventory where on_sale != 0 and present is null and item_type = ?', $this->categoryType);
$stmt = Db::getInstance()->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0 and item_type = ?', [$this->shopId, $this->categoryType]);
$stmt2 = Db::getInstance()->ofetchAll('select * from inventory where on_sale != 0 and present is null and item_type = ?', $this->categoryType);
} else {
$stmt = DBPDO::$db->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0', $this->shopId);
$stmt2 = DBPDO::$db->ofetchAll('select * from inventory where on_sale != 0 and present is null');
$stmt = Db::getInstance()->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0', $this->shopId);
$stmt2 = Db::getInstance()->ofetchAll('select * from inventory where on_sale != 0 and present is null');
}
$iteminfo = [];
@@ -39,7 +44,7 @@ class Shop
private function showUserSellItems(): string
{
$stmt = DBPDO::$db->ofetchall('select * from inventory where on_sale = 0 and dressed_slot = 0 and durability > 0 and owner_id = ?', User::$current->getId());
$stmt = Db::getInstance()->ofetchall('select * from inventory where on_sale = 0 and dressed_slot = 0 and durability > 0 and owner_id = ?', User::getInstance()->getId());
$iteminfo = [];
$operationType = 'sellshop';
+17 -18
View File
@@ -2,7 +2,7 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use Battles\Models\PresentsModel;
use Exceptions\GameException;
@@ -137,8 +137,8 @@ SQL;
public static function buyItem($id, User $buyer)
{
$check = DBPDO::$db->ofetch("select * from trade_offers where offer_id = ?", $id);
$item = new Item(DBPDO::$db->fetch('select * from items where id = ?', $check->shop_item_id));
$check = Db::getInstance()->ofetch("select * from trade_offers where offer_id = ?", $id);
$item = new Item(Db::getInstance()->fetch('select * from items where id = ?', $check->shop_item_id));
$price = $item->calculateItemCost();
if (
@@ -149,7 +149,7 @@ SQL;
return;
}
DBPDO::$db->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]);
Db::getInstance()->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]);
$deloText = $buyer->getLogin() . " купил товар «" . $item->name . "» id:(" . $check->shop_item_id . ") в магазине за " . $price . ".";
GameLogs::addUserLog($buyer->getId(), $deloText);
self::$status = "Предмет " . $item->name . " куплен за " . $price . ".";
@@ -162,7 +162,7 @@ SQL;
}
$allowItemRemove = true;
foreach (json_decode($json_list) as $item) {
$row = DBPDO::$db->ofetch('select sum(1) as s from inventory where name = ? and owner_id = ?', [Item::getItemById($item->item_id)->name, $user_id]);
$row = Db::getInstance()->ofetch('select sum(1) as s from inventory where name = ? and owner_id = ?', [Item::getItemById($item->item_id)->name, $user_id]);
if ($row->s < $item->quantity) {
$allowItemRemove = false;
}
@@ -174,20 +174,20 @@ SQL;
foreach (json_decode($json_list) as $item) {
$query = 'delete from inventory where name = ? and owner_id = ? limit ' . (int)$item->quantity;
// У-у-у, сука! https://phpdelusions.net/pdo#limit
DBPDO::$db->execute($query, [Item::getItemById($item->item_id)->name, $user_id]);
Db::getInstance()->execute($query, [Item::getItemById($item->item_id)->name, $user_id]);
}
return true;
}
private static function checkAndPayTheBills(int $price, User $user): bool
{
if ($user->getMoney() > $price) {
$user->setMoney($user->getMoney() - $price);
$user->saveMoney();
if (User::getInstance()->getMoney() > $price) {
User::getInstance()->setMoney(User::getInstance()->getMoney() - $price);
User::getInstance()->saveMoney();
return true;
}
try {
$bank = new Bank($user->getId());
$bank = new Bank(User::getInstance()->getId());
$bank->withdrawMoney($price);
return true;
} catch (GameException $e) {
@@ -205,24 +205,23 @@ SQL;
if ($current_quantity === -1) {
return true;
}
DBPDO::$db->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_quantity != -1 and shop_item_id = ? ", $item_id);
Db::getInstance()->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_quantity != -1 and shop_item_id = ? ", $item_id);
return true;
}
public static function sellItem($id, User $seller, $bankTrade = 0)
{
$db = new DBPDO();
$item = $db->ofetch('select * from inventory where item_id = ?', $id);
$item = Db::getInstance()->ofetch('select * from inventory where item_id = ?', $id);
$sellingItemName = $item->name;
// Продажа за цену от нуля до половины стоимости.
$sellingPrice = $item->price > 1 ? mt_rand(0, $item->price / 2) : mt_rand(0, 1);
$db->execute('delete from inventory where item_id = ?', $id);
Db::getInstance()->execute('delete from inventory where item_id = ?', $id);
if ($bankTrade) {
$bank = new Bank($seller->getId());
$bank->setMoney($bank->getMoney() + $sellingPrice);
Bank::setBankMoney($bank->getMoney(), $seller->getId(), 'sellShop');
} else {
$db->execute('update users set money = money - ? where id = ?', [$sellingPrice, $_SESSION['uid']]);
Db::getInstance()->execute('update users set money = money - ? where id = ?', [$sellingPrice, $_SESSION['uid']]);
}
$deloText = "{$seller->getLogin()} продал товар «{$sellingItemName}» id:($id) в магазине за $sellingPrice кр.";
GameLogs::addUserLog($seller->getId(), $deloText);
@@ -280,12 +279,12 @@ FORM;
*/
public static function giveNewItem(int $item_id, int $to): array
{
$check = DBPDO::$db->ofetch('select 1 from items where id = ?', $item_id);
$check = Db::getInstance()->ofetch('select 1 from items where id = ?', $item_id);
if (!$check) {
return [];
}
DBPDO::$db->execute(self::BUY_QUERY, [$to, $item_id]);
$return = DBPDO::$db->ofetch('select image, name from inventory where item_id = ?', DBPDO::$db->lastInsertId());
Db::getInstance()->execute(self::BUY_QUERY, [$to, $item_id]);
$return = Db::getInstance()->ofetch('select image, name from inventory where item_id = ?', Db::getInstance()->lastInsertId());
return [
'img' => $return->image,
'name' => $return->name,
+75 -9
View File
@@ -2,7 +2,7 @@
# Date: 26.10.2020 (16:08)
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class Travel
{
@@ -55,24 +55,60 @@ class Travel
2702 => 'city.php'
];
private static array $fbattleCheckFiles = [
'c_haos_in.php',
'c_haos.php',
'c_park.php',
'city.php',
'clan_castle.php',
'enter_cave.php',
'library.php',
'atk.php',
'podzem_dialog.php',
'post.php',
'shop.php',
'tournament.php',
'vxod.php',
'bank.php',
'canalizaciya,php',
'forest.php',
'main.php',
'repair.php',
'towerstamp.php',
'hell.php',
'ul_clans.php',
'labirint.php',
'akadem.php',
'towerin.php',
'user_anketa.php',
'zayavka.php',
];
private static array $towerinCheckFiles = [
'main.php',
'city.php',
'tower.php'
];
private static array $roomsCheck = [22, 23, 27, 29, 30, 31, 37, 38, 39, 40, 41, 45, 53, 61, 401, 402, 600, 601, 602, 621, 650, 1051, 1052];
/**
* Перемещение по комнатам.
* @param int $roomId ID куда идём.
*
* @param int $roomId ID куда идём.
* @param int $roomIdCurrent ID откуда идём.
*/
public static function toRoom(int $roomId, int $roomIdCurrent): void
{
$db = DBPDO::INIT();
$itemsWeight = $db->fetch('SELECT SUM(weight) - (select strength * 5 from users where id = ?) AS weight_overflow FROM inventory WHERE owner_id = ? AND on_sale = 0', [$_SESSION['uid'], $_SESSION['uid']]);
$eff = $db->fetch('SELECT type FROM users_effects WHERE owner_id = ? AND (`type` = 10 OR `type` = 13 OR `type` = 14)', $_SESSION['uid']);
UserStats::getInstance()->
$itemsWeightOverflow = Db::getInstance()->fetchColumn('SELECT SUM(weight) - (select strength * 4 from users where id = ?) AS weight_overflow FROM inventory WHERE owner_id = ? AND on_sale = 0', [$_SESSION['uid'], $_SESSION['uid']]);
$eff = Db::getInstance()->fetchColumn('SELECT type FROM users_effects WHERE owner_id = ? AND (`type` = 10 OR `type` = 13 OR `type` = 14)', $_SESSION['uid']);
$errors = [];
if ($itemsWeight['weight_overflow'] > 0) {
if ($itemsWeightOverflow > 0) {
$errors[0] = 'У вас переполнен рюкзак, вы не можете передвигаться...';
}
if ($eff['type'] == 10) {
if ($eff == 10) {
$errors[1] = 'Вы парализованы и не можете передвигаться...';
}
if ($eff['type'] == 13 || $eff['type'] == 14) {
if ($eff == 13 || $eff == 14) {
$errors[2] = 'У вас тяжелая травма, вы не можете передвигаться...';
}
if ($errors) {
@@ -80,7 +116,7 @@ class Travel
echo sprintf('<span class="error">%s</span>', $error);
}
} elseif (in_array($roomId, self::allowedRoomMoves($roomIdCurrent))) {
$db->execute('UPDATE users, online SET users.room = ?, online.room = ? WHERE `online`.`user_id` = `users`.`id` AND `online`.`user_id` = ?', [$roomId, $roomId, $_SESSION['uid']]);
Db::getInstance()->execute('UPDATE users, online SET users.room = ?, online.room = ? WHERE `online`.`user_id` = `users`.`id` AND `online`.`user_id` = ?', [$roomId, $roomId, $_SESSION['uid']]);
header('location: ' . self::$roomFileName[$roomId]);
exit;
}
@@ -88,7 +124,9 @@ class Travel
/**
* Проверка можно ли перейти из комнаты в комнату.
*
* @param int $roomId ID комнаты
*
* @return array|int[]
*/
private static function allowedRoomMoves(int $roomId): array
@@ -141,4 +179,32 @@ class Travel
}
return $room[$roomId];
}
/** Проверки на соответствие скрипта и комнаты, которые были натыканы по всем файлам.
* @param int $inRoom
* @param int $inBattle
* @param int $inTower
*
* @return void
*/
public static function roomRedirects(int $inRoom, int $inBattle, int $inTower)
{
if ($inBattle && in_array(pathinfo(debug_backtrace()[0]['file'])['basename'], self::$fbattleCheckFiles)) {
header('location: fbattle.php');
exit;
}
if ($inTower && in_array(pathinfo(debug_backtrace()[0]['file'])['basename'], self::$towerinCheckFiles)) {
header('location: towerin.php');
exit;
}
// Если я в одной из этих комнат,
// [И] Имя файла который инклюдит файл с проверкой не совпадает с именем файла локации в которой я нахожусь
// [И] Номер комнаты который я пытаюсь открыть есть в списке проверяемых
if (in_array($inRoom, self::$roomsCheck)
&& pathinfo(debug_backtrace()[0]['file'])['basename'] != self::$roomFileName[$inRoom]
&& in_array(array_search(pathinfo(debug_backtrace()[0]['file'])['basename'], self::$roomFileName), self::$roomsCheck)) {
header('location: main.php');
exit;
}
}
}
+23 -19
View File
@@ -2,10 +2,12 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
class User
{
private static ?self $_instance = null;
protected int $id = 0;
protected string $login = '';
protected ?string $pass = null;
@@ -29,26 +31,20 @@ class User
protected int $battle = 0;
protected int $in_tower = 0; // Скорее башню похороним чем запустим...
protected int $zayavka = 0;
protected static DBPDO $db;
public const INFO_CHAR_LIMIT = 1500;
/**
* @var User Переменная инициализируемая при запуске, хранящая объект текущего пользователя.
*/
public static User $current;
/**
* @param int|string $user
*/
public function __construct($user)
protected function __construct($user = null)
{
self::$db = DBPDO::INIT();
if (is_null($user)) {
$user = $_SESSION['uid'];
}
$query = 'select * from users where login = ?';
if (is_numeric($user)) {
$query = 'select * from users where id = ?';
$user = (int)$user;
}
$user_query = self::$db->fetch($query, $user);
$user_query = Db::getInstance()->fetch($query, $user);
foreach ($this as $key => $value) {
if (isset($user_query[$key])) {
$this->$key = $user_query[$key];
@@ -56,6 +52,14 @@ class User
}
}
public static function getInstance($user = null): self
{
if (is_null(self::$_instance)) {
self::$_instance = new self($user);
}
return self::$_instance;
}
/**
* @param int $userId
* @param int $type
@@ -66,13 +70,13 @@ class User
public static function addUserEffect(int $userId, int $type, string $name, int $time, string $json_modifiers_list = null)
{
$mods = json_decode($json_modifiers_list);
self::$db->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time, mod_strength, mod_dexterity, mod_intuition, mod_endurance, mod_intelligence, mod_wisdom) VALUES (?,?,?,?,?,?,?,?,?,?)', [$userId, $type, $name, $time, $mods->str ?? null, $mods->dex ?? null, $mods->int ?? null, $mods->end ?? null, $mods->intel ?? null, $mods->wis ?? null]);
Db::getInstance()->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time, mod_strength, mod_dexterity, mod_intuition, mod_endurance, mod_intelligence, mod_wisdom) VALUES (?,?,?,?,?,?,?,?,?,?)', [$userId, $type, $name, $time, $mods->str ?? null, $mods->dex ?? null, $mods->int ?? null, $mods->end ?? null, $mods->intel ?? null, $mods->wis ?? null]);
}
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]);
if (Db::getInstance()->fetchColumn('SELECT 1 FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type])) {
Db::getInstance()->execute('DELETE FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type]);
}
return false;
}
@@ -157,12 +161,12 @@ class User
public function setMoney(int $money)
{
$this->money = $money < 0 ? 0 : $money;
$this->money = max($money, 0);
}
public function saveMoney()
{
self::$db->execute('update users set money = ? where id = ?', [$this->money, $this->id]);
Db::getInstance()->execute('update users set money = ? where id = ?', [$this->money, $this->id]);
}
public function getAdmin(): int
@@ -229,7 +233,7 @@ class User
public function setOnline()
{
self::$db->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
Db::getInstance()->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
}
public function setInjury(int $type): bool
@@ -285,6 +289,6 @@ class User
$this->admin,
$this->id //where
];
DBPDO::$db->execute($query, $vals);
Db::getInstance()->execute($query, $vals);
}
}
+6 -18
View File
@@ -2,7 +2,7 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use Battles\Models\EffectsModel;
class UserInfo extends UserStats
@@ -10,8 +10,6 @@ class UserInfo extends UserStats
use Rooms;
private int $bankMoney;
//Тот, кто смотрит на информацию.
private User $watcher;
public function __construct($user)
{
@@ -172,7 +170,7 @@ HTML;
while ($userLogRow = $userLogs->fetchArray(SQLITE3_ASSOC)) {
$log .= sprintf('<code>%s</code><br>', date('d.m.Y H:i ', strtotime($userLogRow['date'])) . $userLogRow['text']);
}
$adminData = $this->watcher->getAdmin() ? $this->showAdminOnlyData() : null;
$adminData = User::getInstance()->getAdmin() ? $this->showAdminOnlyData() : null;
return <<<INFO
<div class="secret-info">
E-Mail: $this->email<br>
@@ -216,7 +214,7 @@ INFO;
echo $this->realname ? "Имя: $this->realname" : "";
echo $this->info ? "<br>" . nl2br($this->info) : "";
echo '</div><!-- u-i-c-l -->';
if ($this->watcher->getAdmin() || $this->watcher->getAlign() == 1) {
if (User::getInstance()->getAdmin() || User::getInstance()->getAlign() == 1) {
echo $this->showPrivateData();
}
}
@@ -225,9 +223,9 @@ INFO;
{
$effects = new EffectsModel($this->id);
if ($this->block && (!$this->watcher->getAdmin() || !$this->watcher->getAlign() == 1)) {
if ($this->block && (!User::getInstance()->getAdmin() || !User::getInstance()->getAlign() == 1)) {
echo "<span class='error'>Персонаж $this->login заблокирован!</span>";
} elseif ($effects->getHideUserInfoStatus() && (!$this->watcher->getAdmin() || !$this->watcher->getAlign() == 1)) {
} elseif ($effects->getHideUserInfoStatus() && (!User::getInstance()->getAdmin() || !User::getInstance()->getAlign() == 1)) {
if ($effects->getHideUserInfoStatus() == -1) {
$date = 'навсегда';
} else {
@@ -256,7 +254,7 @@ INFO;
public function showUserEffects(): string
{
$effs = DBPDO::INIT()->ofetchAll('SELECT * FROM users_effects WHERE owner_id = ?', $this->id);
$effs = Db::getInstance()->ofetchAll('SELECT * FROM users_effects WHERE owner_id = ?', $this->id);
$img = UserEffects::$effectImage;
$r = '';
foreach ($effs as $effect) {
@@ -271,14 +269,4 @@ INFO;
}
return $r;
}
/**
* @param mixed $watcher_id
*/
public function setWatcher(int $watcher_id): void
{
$this->watcher = new User($watcher_id);
}
}
+6 -6
View File
@@ -3,7 +3,7 @@
namespace Battles;
use Battles\Database\DBPDO;
use Battles\Database\Db;
use Exceptions\GameException;
class UserStats extends User
@@ -86,7 +86,7 @@ class UserStats extends User
throw new GameException(self::ERROR_STAT_IS_MAXIMUM);
} else {
$query = "UPDATE users SET {$stat_name} = {$stat_name} + 1, free_stat_points = free_stat_points - 1 WHERE id = ?";
self::$db->execute($query, $this->id);
Db::getInstance()->execute($query, $this->id);
}
}
@@ -161,7 +161,7 @@ class UserStats extends User
public function getFullStats(): object
{
$stats = self::$db->ofetch("
$stats = Db::getInstance()->ofetch("
select
strength,
dexterity,
@@ -170,7 +170,7 @@ class UserStats extends User
intelligence,
wisdom
from users where id = $this->id");
$itemBonuses = self::$db->ofetch("
$itemBonuses = Db::getInstance()->ofetch("
select
sum(add_strength) as item_strength,
sum(add_dexterity) as item_dexterity,
@@ -184,7 +184,7 @@ class UserStats extends User
sum(add_min_physical_damage) as item_min_physical_damage,
sum(add_max_physical_damage) as item_max_physical_damage
from inventory where dressed_slot != 0 and owner_id = $this->id");
$effectBonuses = self::$db->ofetch("
$effectBonuses = Db::getInstance()->ofetch("
select
sum(mod_strength) as effect_strength,
sum(mod_dexterity) as effect_dexterity,
@@ -238,6 +238,6 @@ class UserStats extends User
$this->level,
$this->id //where
];
DBPDO::$db->execute($query, $vals);
Db::getInstance()->execute($query, $vals);
}
}
+15 -12
View File
@@ -12,7 +12,7 @@
* маркеры - т.н. "заполнители", а сами данные передаются "позже", в качестве последующих аргументов основного метода,
* выполняющего SQL-запрос - Mysql::query($sql [, $arg, $...]):
*
* $db->query('SELECT * FROM `table` WHERE `name` = "?s" AND `age` = ?i', $_POST['name'], $_POST['age']);
* $getInstance->query('SELECT * FROM `table` WHERE `name` = "?s" AND `age` = ?i', $_POST['name'], $_POST['age']);
*
* Аргументы SQL-запроса, прошедшие через систему placeholders данного класса, экранируются специальными функциями
* экранирования, в зависимости от типа заполнителей. Т.е. вам теперь нет необходимости заключать переменные в функции
@@ -45,8 +45,8 @@
* Например, попытка передать в качестве аргумента значение 55.5 или '55.5' для заполнителя целочисленного типа ?i
* приведет к выбросу исключения:
*
* $db->setTypeMode(Mysql::MODE_STRICT); // устанавливаем строгий режим работы
* $db->query('SELECT ?i', 55.5); // Попытка указать для заполнителя типа int значение типа double в шаблоне запроса SELECT ?i
* $getInstance->setTypeMode(Mysql::MODE_STRICT); // устанавливаем строгий режим работы
* $getInstance->query('SELECT ?i', 55.5); // Попытка указать для заполнителя типа int значение типа double в шаблоне запроса SELECT ?i
*
* Это утверждение не относится к числам (целым и с плавающей точкой), заключенным в строки.
* С точки зрения библиотеки, строка '123' и значение 123 являются типом int.
@@ -140,7 +140,7 @@
* заполнителей скалярного типа, таких как ?i, ?d и ?s. Это сделано по идеологическим соображениям,
* автоподстановка кавычек может стать ограничением для возможностей SQL.
* Например, выражение
* $db->query('SELECT "Total: ?s"', '200');
* $getInstance->query('SELECT "Total: ?s"', '200');
* вернёт строку
* 'Total: 200'
* Если бы кавычки, ограничивающие строковой литерал, ставились бы автоматически,
@@ -151,20 +151,23 @@
* Тем не менее, для перечислений ?as, ?ai, ?ap, ?As, ?Ai и ?Ap ограничивающие кавычки ставятся принудительно, т.к.
* перечисления всегда используются в запросах, где наличие кавчек обязательно или не играет роли (а так ли это?):
*
* $db->query('INSERT INTO `test` SET ?As', array('name' => 'Маша', 'age' => '23', 'adress' => 'Москва'));
* $getInstance->query('INSERT INTO `test` SET ?As', array('name' => 'Маша', 'age' => '23', 'adress' => 'Москва'));
* -> INSERT INTO test SET `name` = "Маша", `age` = "23", `adress` = "Москва"
*
* $db->query('SELECT * FROM table WHERE field IN (?as)', array('55', '12', '132'));
* $getInstance->query('SELECT * FROM table WHERE field IN (?as)', array('55', '12', '132'));
* -> SELECT * FROM table WHERE field IN ("55", "12", "132")
*
* Также исключения составляют заполнители типа ?f, предназначенные для передачи в запрос имен таблиц и полей.
* Аргумент заполнителя ?f всегда обрамляется обратными кавычками (`):
*
* $db->query('SELECT ?f FROM ?f', 'my_field', 'my_table');
* $getInstance->query('SELECT ?f FROM ?f', 'my_field', 'my_table');
* -> SELECT `my_field` FROM `my_table`
*/
namespace Krugozor\Database\Mysql;
use mysqli;
use mysqli_result;
class Mysql
{
/**
@@ -172,7 +175,7 @@ class Mysql
* Если тип заполнителя не совпадает с типом аргумента, то будет выброшено исключение.
* Пример такой ситуации:
*
* $db->query('SELECT * FROM `table` WHERE `id` = ?i', '2+мусор');
* $getInstance->query('SELECT * FROM `table` WHERE `id` = ?i', '2+мусор');
*
* - в данной ситуации тип заполнителя ?i - число или числовая строка,
* а в качестве аргумента передаётся строка '2+мусор' не являющаяся ни числом, ни числовой строкой.
@@ -187,7 +190,7 @@ class Mysql
* к нужному типу - к типу заполнителя.
* Пример такой ситуации:
*
* $db->query('SELECT * FROM `table` WHERE `id` = ?i', '2+мусор');
* $getInstance->query('SELECT * FROM `table` WHERE `id` = ?i', '2+мусор');
*
* - в данной ситуации тип заполнителя ?i - число или числовая строка,
* а в качестве аргумента передаётся строка '2+мусор' не являющаяся ни числом, ни числовой строкой.
@@ -397,7 +400,7 @@ class Mysql
throw new Exception(__METHOD__ . ': ' . $this->mysqli->error . '; SQL: ' . $this->query);
}
if (is_object($result) && $result instanceof \mysqli_result) {
if (is_object($result) && $result instanceof mysqli_result) {
return new Statement($result);
}
@@ -425,7 +428,7 @@ class Mysql
* Применяется для случаев, когда SQL-запрос формируется частями.
*
* Пример:
* $db->prepare('WHERE `name` = "?s" OR `id` IN(?ai)', 'Василий', array(1, 2));
* $getInstance->prepare('WHERE `name` = "?s" OR `id` IN(?ai)', 'Василий', array(1, 2));
* Результат:
* WHERE `name` = "Василий" OR `id` IN(1, 2)
*
@@ -548,7 +551,7 @@ class Mysql
private function connect()
{
if (!is_object($this->mysqli) || !$this->mysqli instanceof mysqli) {
$this->mysqli = @new \mysqli($this->server, $this->user, $this->password, null, $this->port, $this->socket);
$this->mysqli = @new mysqli($this->server, $this->user, $this->password, null, $this->port, $this->socket);
if ($this->mysqli->connect_error) {
throw new Exception(__METHOD__ . ': ' . $this->mysqli->connect_error);
+3 -2
View File
@@ -2,6 +2,7 @@
use Battles\GameLogs;
use Battles\Nick;
use Battles\User;
class Tournament
{
@@ -160,8 +161,8 @@ class Tournament
// кидаем в бой
mysql_query("UPDATE `users` SET `battle` = {$id} WHERE `id` = " . $user1 . " OR `id` = " . $user2);
// создаем лог
$rr = "<b>" . Nick::id($user['id'])->full(1) . "</b> и <b>" . Nick::id($jert['id'])->full(1) . "</b>";
addch("<a href=logs.php?log=" . $id . " target=_blank>Бой</a> между <B><b>" . Nick::id($user['id'])->short() . "</b> и <b>" . Nick::id($jert['id'])->short() . "</b> начался. ", $user->getRoom());
$rr = "<b>" . Nick::id(User::getInstance()->getId())->full(1) . "</b> и <b>" . Nick::id($jert['id'])->full(1) . "</b>";
addch("<a href=logs.php?log=" . $id . " target=_blank>Бой</a> между <B><b>" . Nick::id(User::getInstance()->getId())->short() . "</b> и <b>" . Nick::id($jert['id'])->short() . "</b> начался. ", User::getInstance()->getRoom());
GameLogs::addBattleLog($id, "Часы показывали <span class=date>" . date("Y.m.d H.i") . "</span>, когда " . $rr . " решили выяснить кто из них сильнее. <i>(турнир)</i><BR>");
return $id;
}
+3 -1
View File
@@ -1,5 +1,7 @@
<?php
use Battles\User;
class Quests
{
public $free_x = 28, $data = [], $error = '';
@@ -569,7 +571,7 @@ TASK;
mysql_query('UPDATE `users` SET `money` = "' . $user['money'] . '", `exp` = "' . $user['exp'] . '", `doblest` = "' . $user['doblest'] . '" WHERE `id` = "' . $user['id'] . '" LIMIT 1');
$this->error = 'Вы успешно сдали задание!';
$text = '<font style="color: Red;">Внимание!</font> За успешно выполненное задание Вы получили : ' . $c;
$this->msg($text, '{[]}' . $user['login'] . '{[]}', $user->getRoom());
$this->msg($text, '{[]}' . $user['login'] . '{[]}', User::getInstance()->getRoom());
} else {
$this->error = 'Не все условия задания были выполнены ...';
}