2020-07-03 16:23:08 +03:00
< ? php
2021-01-27 17:56:04 +02:00
2020-10-28 22:21:08 +02:00
namespace Battles ;
2021-01-27 17:56:04 +02:00
2021-01-28 23:05:34 +02:00
use Battles\Database\DBPDO ;
2020-10-28 22:21:08 +02:00
use Exceptions\GameException ;
2020-07-03 16:23:08 +03:00
class User
{
2020-07-22 12:29:31 +03:00
public $id = 0 ;
2020-07-21 11:36:21 +03:00
public $login = '<em>Некто</em>' ;
2020-08-30 03:59:29 +03:00
public $pass ;
2020-07-21 11:36:21 +03:00
public $email = '<em>неизвестно</em>' ;
2020-07-03 16:23:08 +03:00
public $realname ;
public $borndate ;
public $info ;
2020-07-21 11:36:21 +03:00
public $level = 0 ;
public $align = 0 ;
public $clan = 0 ;
public $money = 0 ;
public $strength = 0 ;
public $dexterity = 0 ;
public $intuition = 0 ;
public $endurance = 0 ;
public $intelligence = 0 ;
public $wisdom = 0 ;
2020-08-29 21:14:33 +03:00
public $health ;
public $mana ;
2020-07-03 16:23:08 +03:00
public $ip ;
public $session_id ;
2020-07-21 11:36:21 +03:00
public $admin = 0 ;
2020-07-03 16:23:08 +03:00
public $enter_game ;
public $room ;
public $block ;
2020-07-06 13:41:20 +03:00
public $shadow ;
2020-07-20 16:27:32 +03:00
// Удар кулаком всегда 1-2.
public $minDamage = 1 ;
public $maxDamage = 2 ;
2020-07-20 16:44:31 +03:00
//Броня без предметов не существует.
public $headArmor = 0 ;
public $chestArmor = 0 ;
public $legArmor = 0 ;
2020-07-22 17:04:15 +03:00
public $free_stat_points = 0 ;
2020-07-20 12:32:51 +03:00
public const STAT_MAXIMUM_AMOUNT = 40 ;
2020-07-21 11:36:21 +03:00
private const ERROR_STAT_IS_MAXIMUM = 'Ошибка: Параметр достиг своего лимита!' ;
private const ERROR_STAT_UNKNOWN = 'Ошибка: Неизвестный параметр!' ;
2020-07-03 16:49:39 +03:00
// Пока несуществующие, для совместимости.
2020-07-10 11:08:00 +03:00
public $married = 'Someone или нет.' ;
public $experience = 200 ;
2020-08-30 02:21:22 +03:00
public $battle = 0 ;
public $in_tower = 0 ; // Скорее башню похороним чем запустим...
public $zayavka = 0 ;
2020-07-10 11:08:00 +03:00
// Динамически рассчитываемые
2020-08-29 21:14:33 +03:00
public $maxHealth = 5 ;
public $maxMana = 5 ;
2021-01-28 23:05:34 +02:00
protected static $db ;
2020-07-04 13:49:43 +03:00
2021-01-28 23:05:34 +02:00
public function __construct ( int $user )
2020-07-03 16:23:08 +03:00
{
2021-01-28 23:05:34 +02:00
self :: $db = DBPDO :: INIT ();
$user_query = self :: $db -> fetch ( 'SELECT * FROM users WHERE id = ? OR login = ?' , [ $user , $user ]);
2020-07-03 16:23:08 +03:00
foreach ( $this as $key => $value ) {
if ( isset ( $user_query [ $key ])) {
$this -> $key = $user_query [ $key ];
}
}
2020-08-29 21:14:33 +03:00
$this -> maxHealth = round (( $this -> endurance * 3 ) + ( $this -> endurance / 2 ) * ( $this -> level - 1 ) + ( $this -> endurance / 5 ) * (( $this -> level - 1 ) * ( $this -> level - 2 ) / 2 ));
2020-08-29 21:25:22 +03:00
$this -> maxMana = round (( $this -> wisdom * 3 ) + ( $this -> wisdom / 2 ) * ( $this -> level - 1 ) + ( $this -> wisdom / 5 ) * (( $this -> level - 1 ) * ( $this -> level - 2 ) / 2 ));
2020-07-03 16:23:08 +03:00
}
2020-07-03 19:37:24 +03:00
2020-08-30 12:47:24 +03:00
/**
* Отдаёт информацию о базовом(!) стате.
2021-01-27 17:56:04 +02:00
* @param $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'.
2020-08-30 12:47:24 +03:00
* @param int $isMainWindow - переключатель "главного окна". Если включить, дополнительно будет показывать ссылку на повышение стата на 1, при условии наличия свободных очков статов.
* @return string
2020-10-28 22:21:08 +02:00
* @throws GameException
2020-08-30 12:47:24 +03:00
*/
2021-01-28 23:05:34 +02:00
public function getStat ( $stat_name , $isMainWindow = 0 ) : string
2020-07-21 11:36:21 +03:00
{
2020-08-30 12:47:24 +03:00
$allowed = [ 'strength' , 'dexterity' , 'intuition' , 'endurance' , 'intelligence' , 'wisdom' ];
if ( in_array ( $stat_name , $allowed )) {
if ( $this -> free_stat_points && $isMainWindow && $this -> $stat_name < self :: STAT_MAXIMUM_AMOUNT ) {
return sprintf ( '%s <a href="/main.php?edit=%s&ups=%s">[+]</a>' , $this -> $stat_name , mt_rand (), $stat_name );
} else {
return $this -> $stat_name ;
}
2020-07-21 11:36:21 +03:00
} else {
2021-01-27 17:56:04 +02:00
throw new GameException ( self :: ERROR_STAT_UNKNOWN );
2020-07-21 11:36:21 +03:00
}
}
2020-08-30 12:47:24 +03:00
/**
* Повышает один из выбранных статов на 1, но не выше self::STAT_MAXIMUM_AMOUNT при условии наличия свободных очков статов.
2021-01-28 23:05:34 +02:00
* @param string $stat_name - имя стата. Может принимать значения 'strength', 'dexterity', 'intuition', 'endurance', 'intelligence', 'wisdom'.
2020-10-28 22:21:08 +02:00
* @throws GameException
2020-08-30 12:47:24 +03:00
*/
2021-01-28 23:05:34 +02:00
public function addOnePointToStat ( string $stat_name )
2020-07-21 11:36:21 +03:00
{
$allowed = [ 'strength' , 'dexterity' , 'intuition' , 'endurance' , 'intelligence' , 'wisdom' ];
if ( in_array ( $stat_name , $allowed )) {
2020-07-22 17:04:15 +03:00
if ( $this -> free_stat_points > 0 && $this -> $stat_name <= self :: STAT_MAXIMUM_AMOUNT ) {
2021-01-28 23:05:34 +02:00
$query = " UPDATE users SET { $stat_name } = { $stat_name } + 1, free_stat_points = free_stat_points - 1 WHERE id = ? " ;
self :: $db -> execute ( $query , $this -> id );
2020-07-21 11:36:21 +03:00
} else {
2021-01-27 17:56:04 +02:00
throw new GameException ( self :: ERROR_STAT_IS_MAXIMUM );
2020-07-21 11:36:21 +03:00
}
} else {
2021-01-27 17:56:04 +02:00
throw new GameException ( self :: ERROR_STAT_UNKNOWN );
2020-07-21 11:36:21 +03:00
}
}
2021-01-28 23:05:34 +02:00
protected function showStarSign () : ? string
2020-07-06 08:56:25 +03:00
{
/*
* 1 aries
* 2 taurus
* 3 gemini
* 4 cancer
* 5 leo
* 6 virgo
* 7 libra
* 8 scorpio
* 9 sagittarios
* 10 capricorn
* 11 aquarius
* 12 pisches
*/
2021-01-28 23:05:34 +02:00
$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 " ,
];
2020-07-05 20:29:30 +03:00
$dayOfYear = date ( " z " , strtotime ( $this -> borndate ));
$isLeapYear = date ( " L " , strtotime ( $this -> borndate )); //Высокосный?
2020-07-06 08:56:25 +03:00
if ( $isLeapYear && $dayOfYear > 59 ) {
2020-07-05 20:29:30 +03:00
-- $dayOfYear ;
}
foreach ( $zodiac as $day => $sign ) {
if ( $dayOfYear > $day ) {
break ;
}
}
return $sign ? ? null ;
}
2020-07-10 11:08:00 +03:00
2020-08-29 21:18:18 +03:00
public function getHealth () : string
2020-07-10 11:08:00 +03:00
{
2020-08-30 12:47:24 +03:00
return $this -> health . '/' . $this -> maxHealth ;
2020-08-29 21:14:33 +03:00
}
2020-08-29 21:18:18 +03:00
public function getMana () : string
2020-08-29 21:14:33 +03:00
{
2020-08-30 12:47:24 +03:00
return $this -> mana . '/' . $this -> maxMana ;
2020-07-10 11:08:00 +03:00
}
2021-01-28 23:05:34 +02:00
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 ;
}
2020-07-03 16:23:08 +03:00
}