<?php
/*
* Класс для работы с пользователем;
*
*/
class User
{
#public $userMoney;
/*
* Пользовательськая информация;
*
*/
public function userInfo ($userId)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT * FROM ".USERS." WHERE `id` = ?", [$userId]);
$this->userDefend = $Filter->clearInt($profile['defend']); // Защита;
$this->userHealth = $Filter->clearInt($profile['health']); // Здоровье;
$this->userStrike = $Filter->clearInt($profile['strike']); // Сила;
$this->userParam = $Filter->clearInt($this->userDefend + $this->userHealth + $this->userStrike); // Сумма параметров;
#$this->userClass = $this->userClass($userId); // Класс;
$this->userMoney = $Filter->clearInt($profile['money']); // Деньги;
$this->userLogin = $Filter->output($profile['login']); // Логин;
}
/*
* Обработка классов;
*
*/
public function userClass ($userId)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT `class` FROM ".USERS." WHERE `id` = ?", [$userId]);
if ($profile['class'] == 'warior')
{
$class = 'Воин';
}
else
{
$class = 'Не выбран';
}
return $class;
}
public function getQuery($query, $id)
{
$this->userInfo($id);
return $this->$query;
}
/*
* Обработка должностей;
*/
public function userAccess ($userId, $type = null)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$ank = DB::$dbs->queryFetch("SELECT `access` FROM ".USERS." WHERE `id` = ?", array($userId));
if ($type == NULL)
{
if ($ank['access'] == 0) $access = 'Игрок';
elseif ($ank['access'] == 1) $access = '<font color="darkorange">Модератор</font>';
elseif ($ank['access'] == 2) $access = '<font color="darkorange">Администратор</font>';
}
elseif ($type == 1)
{
if ($ank['access'] == 0) $access = '';
elseif ($ank['access'] == 1) $access = '<font color="darkorange">[m]</font>';
elseif ($ank['access'] == 2) $access = '<font color="darkorange">[a]</font>';
}
return $access;
}
/*
* Проверка почты;
*/
function checkMail ()
{
global $user, $Filter;
$all = DB::$dbs->querySingle("SELECT COUNT(*) FROM ".MAIL." WHERE `given` = ? AND `read` = ? ", [$user['id'], 1]);
if ($all > 0)
{
echo '<a href="'.HOME.'/modules/mail/index.php" class="link-touch">'.ico('mail.png', '[m-ico]', '16', '16').' Почта <span class="count">+ '.$Filter->clearInt($all).'</span></a>';
}
}
/*
* Check user health;
*/
public function checkHealth ($text)
{
global $user, $Filter;
if ($user['health'] == 0)
{
echo $Filter->output($text) . '<br/><a href="'.$_SERVER['REQUEST_URI'].'" class="link-touch"> Обновить</a> <a href="'.HOME.'/modules/fight/rip.php" class="link-touch"> Покинуть бой</a>';
if (!empty($user['party']) && $user['party_access'] == 1)
{
?>
<a href="<?=$_SERVER['SCRIPT_NAME']?>?kickParty" class="link-touch"> Распустить банду</a>
<?php
if (isset($_GET['kickParty']))
{
$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . USERS . " WHERE `party` = ?", [$user['party']]);
if (!empty($all))
{
$sql = DB::$dbs->query("SELECT * FROM " . USERS ." WHERE `party` = ?", [$user['party']]);
DB::$dbs->query("DELETE FROM " . PARTY . " WHERE `id` = ?", [$user['party']]);
while ($ank = $sql -> fetch())
{
DB::$dbs->query("UPDATE " . USERS . " SET `party` = ?, `party_access` = ? WHERE `id` = ?", [0, 0, $ank['id']]);
if (!empty($ank['dungeon_id']))
{
DB::$dbs->query("UPDATE " . USERS . " SET `dungeon_id` = ? WHERE `id` = ?", [0, $ank['id']]);
}
}
}
success('Вы распустили банду!', '/game/index.php');
}
}
else if (!empty($user['party']))
{
?>
<a href="<?=$_SERVER['SCRIPT_NAME']?>?leaveParty" class="link-touch"> Покинуть банду</a>
<?php
if (isset($_GET['leaveParty']))
{
DB::$dbs->query("UPDATE " . USERS . " SET `party` = ?, `party_access` = ? WHERE `id` = ?", [0, 0, $user['id']]);
if (!empty($user['dungeon_id']))
{
DB::$dbs->query("UPDATE " . USERS . " SET `dungeon_id` = ? WHERE `id` = ?", [0, $user['id']]);
}
success('Вы покинули банду!', '/game/index.php');
}
}
include_once ''.inc.'/core/foot.php';
exit();
}
else
{
return FALSE;
}
}
function staticHealthIcon ()
{
global $user;
if ($user['health'] <= ceil($user['max_health'] * 75 / 100) && $user['health'] > ceil($user['max_health'] * 30 / 100))
{
echo '<font color="orange">'.ico('health-50.png', '[health-ico]'). $user['health'] .'</font>';
}
else if ($user['health'] <= ceil($user['max_health'] * 30 / 100))
{
echo '<font color="red">'.ico('health-30.png', '[health-ico]'). $user['health'] .'</font>';
}
else
{
echo '<font color="green">'.ico('health.png', '[health-ico]'). $user['health'] .'</font>';
}
}
/*
* Restore health;
*/
function restoreHealth ()
{
global $user;
if ($user['in_battle'] == 0)
{
if (($user['last_time'] + 2) < time())
{
if ($user['health'] != $user['max_health'])
{
if ($user['health'] > $user['max_health'])
{
DB::$dbs->query("UPDATE " . USERS . " SET `health` = ? WHERE `id` = ?", [$user['max_health'], $user['id']]);
}
else if ($user['health'] < $user['max_health'])
{
$plusHealth = 5 * (time() - $user['last_time']);
if (($user['health'] + $plusHealth) > $user['max_health'])
{
$plusHealth = $user['max_health'] - $user['health'];
}
if ($user['vip'] > time())
{
$plusHealth = $plusHealth * 2;
}
if ($plusHealth != 0)
{
$_SESSION['restoreHealth'] = $plusHealth;
}
DB::$dbs->query("UPDATE " . USERS . " SET `health` = ? WHERE `id` = ?", [($user['health'] + $plusHealth), $user['id']]);
}
}
}
}
}
/*
* Add log;
*/
function addLog ($adminId, $text)
{
global $Filter;
$adminLogin = DB::$dbs->queryFetch("SELECT `login` FROM " . USERS . " WHERE `id` = ?", [$Filter->clearInt($adminId)]);
$text = $adminLogin['login'] . ': ' . $Filter->clearString($text) . ' [' . dataTime(time()) . '].';
DB::$dbs->query("INSERT INTO " . ADMIN_LOG . " (`text`) VALUES (?)", [$text]);
}
/*
* Update lvl;
*/
function updateLvl ()
{
global $user;
$nextLvlExp = DB::$dbs->queryFetch("SELECT `exp` FROM " . USER_EXP . " WHERE `lvl` = ?", [$user['lvl']]);
if ($user['exp'] >= $nextLvlExp['exp'])
{
$lvlNext = $user['lvl'] + 1;
$plusParam = 5;
if ($lvlNext == 10)
{
if (!empty($user['ref']))
{
$ank = DB::$dbs->queryFetch("SELECT * FROM ". USERS ." WHERE `id` = ?", [$user['ref']]);
if (!empty($ank))
{
DB::$dbs->query("UPDATE ". USERS ." SET `money` = ? WHERE `id` = ?", [($user['money'] + 1000), $user['id']]);
DB::$dbs->query("UPDATE ". USERS ." SET `money` = ? WHERE `id` = ?", [($ank['money'] + 2000), $ank['id']]);
$this->addUserJournal('Вы достигли 10 уровня. В награду вам начислено <span style="color: green;">+'. ico('gold.png') .'1000</span>, от реферальной системы.', $user['id']);
$this->addUserJournal(userLink($user['id']) .' достиг 10 уровня. В награду вам начислено <span style="color: green;">+'. ico('gold.png') .'2000</span>, от реферальной системы.', $ank['id']);
}
}
}
?>
<div class="wrap">
<span style="color: #284440;">
Поздравляем!
</span>
<br/>
Вы получили <?=$lvlNext?> уровень.<br/>
+ <span style="color: #284440;"><?=$plusParam?></span> ко всем параметрам.<br/>
+ <span style="color: #284440;">3</span> очков тренировки<br/>
</div>
<?php
/* Update */
DB::$dbs->query("UPDATE " . USERS . " SET `lvl` = ?, `exp` = ?, `strike` = ?, `defend` = ?, `max_health` = ?, `train` = ? WHERE `id` = ?", [$lvlNext, 0, ($user['strike'] + $plusParam), ($user['defend'] + $plusParam), ($user['max_health'] + $plusParam), ($user['train'] + 3), $user['id']]);
}
}
/*
* Exp bar;
*/
function expBar($type = 'head')
{
global $user;
if ($type == 'head')
{
$nextLvlExp = DB::$dbs->queryFetch("SELECT `exp` FROM " . USER_EXP . " WHERE `lvl` = ?", [$user['lvl']]);
$expProgress = round($user['exp'] / $nextLvlExp['exp'] * 100,2);
?>
<div class="expBarHead"><div class="expBarHeadLine" style="width: <?=$expProgress?>%;"></div></div>
<?php
}
}
/*
* Add clan journal;
*/
function addClanJournal($text, $clanId = null)
{
global $Filter, $user;
$text = $Filter->clearString($text);
$clanId = (!empty($clanId) ? $Filter->clearInt($clanId) : NULL);
if (empty($clanId))
{
DB::$dbs->query("INSERT INTO " . CLANS_JOURNAL . " (`user_id`, `clan_id`, `time`, `text`) VALUES (?, ?, ?, ?)", [$user['id'], $user['clan_id'], time(), $text]);
}
else
{
DB::$dbs->query("INSERT INTO " . CLANS_JOURNAL . " (`user_id`, `clan_id`, `time`, `text`) VALUES (?, ?, ?, ?)", [$user['id'], $clanId, time(), $text]);
}
}
# Add user journal;
# Warning! This function not filtration for give administartors use ico() function in text and other;
# Use careful;
function addUserJournal($text, $userId)
{
global $Filter;
$userId = $Filter->clearInt($userId);
DB::$dbs->query("INSERT INTO " . JOURNAL . " (`text`, `user_id`, `time`, `status`) VALUES (?, ?, ?, ?)", [$text, $userId, time(), 1]);
}
/*
* Text color;
*/
function outputTextColor ($text, $userId)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT * FROM " . USERS . " WHERE `id` = ?", [$userId]);
if ($profile['access'] == 2)
{
$span = '<span style="color: darkorange;">' . $text . '</span>';
return $span;
}
else if ($profile['access'] == 1)
{
$span = '<span style="color: #FFE21C;">'. $text .'</span>';
return $span;
}
else
{
return $text;
}
}
function clanAccess ($userId, $type = null)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT `clan_access` FROM " . USERS . " WHERE `id` = ?", [$userId]);
if ($type == null)
{
return $Filter->clearInt($profile['clan_access']);
}
else if ($type == 'name')
{
if ($profile['clan_access'] == 0)
{
$name = 'Рекрут';
}
else if ($profile['clan_access'] == 1)
{
$name = 'Рядовой';
}
else if ($profile['clan_access'] == 2)
{
$name = 'Ветеран';
}
else if ($profile['clan_access'] == 3)
{
$name = 'Офицер';
}
else if ($profile['clan_access'] == 4)
{
$name = 'Маршал';
}
else if ($profile['clan_access'] == 5)
{
$name = 'Создатель клана';
}
return '<span style="color: darkorange;">' . $name . '</span>';
}
}
function sex ($userId, $type = 'name')
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT `sex` FROM " . USERS . " WHERE `id` = ?", [$userId]);
if ($type == 'name')
{
if ($profile['sex'] == 'male')
{
$sex = 'Мужчина';
}
else if ($profile['sex'] == 'female')
{
$sex = 'Женщина';
}
else
{
$sex = 'Неизвестно';
}
return $sex;
}
else if ($type == 'icon')
{
if ($profile['sex'] == 'male')
{
$icon = ico('male.png');
}
else if ($profile['sex'] == 'female')
{
$icon = ico('female.png');
}
else
{
$icon = NULL;
}
return $icon;
}
}
function partyLink()
{
global $user, $Filter;
if (!empty($user['party']))
{
?>
<div class="head"><a href="<?=HOME?>/modules/dungeons/party.php">Банда</a></div>
<?php
$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . USERS . " WHERE `party` = ?", [$user['party']]);
$n = new Navigator($all,5,'');
$sql = DB::$dbs->query("SELECT * FROM " . USERS ." WHERE `party` = ? ORDER BY `id` DESC LIMIT {$n->start()}, 5", [$user['party']]);
while ($profile = $sql -> fetch())
{
?>
<?=userLink($profile['id'], 'link-touch', ico('health.png') . $profile['health']);
}
}
}
function onlineIcon ($userId)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$profile = DB::$dbs->queryFetch("SELECT `side`, `last_time` FROM " . USERS . " WHERE `id` = ?", [$userId]);
if ($profile['last_time'] > (time() - 600))
{
if ($profile['side'] == 'white')
{
$ico = ico('user-white-online.png');
}
else
{
$ico = ico('user-dark-online.png');
}
}
else
{
$ico = ico('user-offline.png');
}
return $ico;
}
function userAnswer ($userId, $answer)
{
global $Filter;
$userId = $Filter->clearInt($userId);
$ank = DB::$dbs->queryFetch("SELECT `$answer` FROM " . USERS . " WHERE `id` = ?", $userId);
if (is_numeric($ank[$answer]))
{
return $Filter->clearInt($ank[$answer]);
}
else
{
return $Filter->output($ank[$answer]);
}
}
function checkSkillTime ()
{
global $user;
$activeSkillTime = 30; // Second active skill;
/*
* Set skill offline & minus user strike;
*/
if ($user['skill_1_time'] <= (time() + 30) && $user['skill_1_active'] == 1)
{
DB::$dbs->query("UPDATE " . USERS . " SET `strike` = ?, `skill_1_active` = ? WHERE `id` = ?", [($user['strike'] - $user['skill_1_plus_param']), 0, $user['id']]);
}
}
function headNotifier()
{
global $user, $Filter, $User;
# Ban check;
$ban = DB::$dbs->queryFetch("SELECT `text`, `time_ban`, `moderator_id` FROM ".BAN." WHERE `user_id` = ? && `time_ban` > ?", array($user['id'], time()));
if ($ban != NULL)
{
head('Вы заблокированы!');
echo DIV_ERROR;
echo 'Вы заблокированы!<br/> Причина: ' . $Filter->output($ban['text']) . ' <br/>Дата окончания: ' . dataTime($Filter->clearInt($ban['time_ban'])) . '<br/> Заблокировал: ' . userLink($ban['moderator_id']);
echo CLOSE_DIV;
require_once inc . 'core/foot.php';
exit();
}
# Start;
if ($user['registration'] != 'end' && $_SERVER['SCRIPT_NAME'] != '/modules/start/index.php')
{
header("Location: " . HOME . "/modules/start/index.php");
}
# Dungeon redirect;
if (!empty($user['dungeon_id']) && $_SERVER['SCRIPT_NAME'] != '/modules/dungeons/'. $user['dungeon_id'] .'/index.php')
{
error('Вы сейчас в бою.', '/modules/dungeons/'. $user['dungeon_id'] .'/index.php');
}
if ($_SERVER['SCRIPT_NAME'] == '/modules/fight/attack.php' && empty($user['in_battle'])
|| $_SERVER['SCRIPT_NAME'] == '/modules/arena/index.php' && empty($user['in_battle'])
|| $_SERVER['SCRIPT_NAME'] == '/modules/event/HorsemanOfTheApocalypse.php' && empty($user['in_battle'])
|| $_SERVER['SCRIPT_NAME'] == '/modules/dungeons/'. $user['dungeon_id'] .'/index.php' && !empty($user['dungeon_id']) && empty($user['in_battle'])
|| $_SERVER['SCRIPT_NAME'] != '/game/index.php' && !empty($user['task_id']) && empty($user['in_battle']))
{
DB::$dbs->query("UPDATE " . USERS . " SET `in_battle` = ? WHERE `id` = ? LIMIT 1", [1, $user['id']]);
}
else if (!empty($user['in_battle']) && $_SERVER['SCRIPT_NAME'] == '/game/index.php')
{
DB::$dbs->query("UPDATE " . USERS . " SET `in_battle` = ? WHERE `id` = ? LIMIT 1", [0, $user['id']]);
}
# Dungeon delete;
if ($user['dungeon_id'] != 0 && $_SERVER['SCRIPT_NAME'] == '/game/index.php' && !empty($user['fight_id']))
{
DB::$dbs->query("UPDATE ". USERS ." SET `dungeon_id` = ?, `fight_id` = ?, `party` = ?, `party_access` = ?, `dungeon_cooldown` = ? WHERE `id` = ?", [0, 0, 0, 0, time() + 3600, $user['id']]);
$journal = 'Вы покинули бой и по этому были изгнаны из банды. Вам засчитано поражение. Снова отправиться в подземелье вы сможете через 1 час.';
$User->addUserJournal($journal, $user['id']);
}
# Clan invite;
if ($user['clan_invite'] == 1 && $user['in_battle'] == 0)
{
if ($user['clan_id'] == 0)
{
$clan = DB::$dbs->queryFetch("SELECT * FROM " . CLANS . " WHERE `id` = ?", [$user['clan_invite_id']]);
if (isset($_GET['clanInvite']) && !empty($clan))
{
DB::$dbs->query("UPDATE " . CLANS . " SET `users` = ? WHERE `id` = ?", [($clan['users'] + 1), $user['clan_invite_id']]);
DB::$dbs->query("UPDATE " . USERS . " SET `clan_invite` = ?, `clan_invite_id` = ?, `clan_id` = ? WHERE `id` = ?", [0, 0, $user['clan_invite_id'], $user['id']]);
$User->addClanJournal(' вступил в клан', $clan['id']);
success('Вы успешно вступили в клан', '/', 'На главную');
}
if (isset($_GET['clanAbort']))
{
DB::$dbs->query("UPDATE ". USERS ." SET `clan_invite` = ?, `clan_invite_id` = ? WHERE `id` = ?", [0, 0, $user['id']]);
success('Вы отклонили предложение', '/', 'На главную');
}
?>
<br/>
<div class="block">
<div class="content">
Вас приглашают в клан <a href="<?=HOME?>/modules/clan/clans.php?do=info&clanId=<?=$Filter->clearInt($clan['id'])?>"><?=$Filter->output($clan['name'])?></a>
<br/>
<a href="?clanInvite" class="link-touch"><?=ico('1.png')?> Принять</a>
<a href="?clanAbort" class="link-touch"><?=ico('0.png')?> Отклонить</a>
</div>
</div>
<?php
}
}
# Party invite;
if ($user['party_invite'] != 0)
{
if ($user['party'] != 0)
{
DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [0, $user['id']]);
}
else
{
$partyMembers = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM " . PARTY . " WHERE `id` = ?", [$user['party_invite']]);
$party = DB::$dbs->queryFetch("SELECT * FROM " . PARTY . " WHERE `id` = ?", [$user['party_invite']]);
if (isset($_GET['enterParty']))
{
if ($partyMembers == $party['max_members'])
{
DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [0, $user['id']]);
errorNoExit('Предложение отклонено. Банда полна.');
}
else if (empty($party))
{
DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [0, $user['id']]);
errorNoExit('Предложение отклонено. Банды не существует.');
}
else
{
DB::$dbs->query("UPDATE " . USERS . " SET `party` = ?, `party_invite` = ? WHERE `id` = ?", [$user['party_invite'], 0, $user['id']]);
successNoExit('Вы успешно вступили в банду');
}
}
if (isset($_GET['abortParty']))
{
DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [0, $user['id']]);
errorNoExit('Предложение отклонено.');
}
$party = DB::$dbs->queryFetch("SELECT * FROM " . PARTY . " WHERE `id` = ?", [$user['party_invite']]);
$dungeonName = DB::$dbs->queryFetch("SELECT `name` FROM " . DUNGEONS . " WHERE `id` = ?", [$party['dungeon_id']]);
if (!empty($party))
{
?>
Вас приглашают в банду для похода в подземелье <?=$Filter->output($dungeonName['name'])?>.<br/>
<a href="?enterParty" class="link-touch"><?=ico('1.png')?> Принять</a>
<a href="?abortParty" class="link-touch"><?=ico('0.png')?> Отклонить</a>
<?php
}
else
{
DB::$dbs->query("UPDATE " . USERS . " SET `party_invite` = ? WHERE `id` = ?", [0, $user['id']]);
header("Location: ?");
}
}
}
}
function notifier ()
{
if (isset($_SESSION['error']))
{
echo DIV_ERROR . $_SESSION['error'] . CLOSE_DIV;
unset ($_SESSION['error']);
}
if (isset($_SESSION['success']))
{
echo DIV_SUCCESS . $_SESSION['success'] . CLOSE_DIV;
unset ($_SESSION['success']);
}
}
function checkUserBonus()
{
$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". USER_BONUS ." WHERE `time` <= ?", [time()]);
if (!empty($all))
{
$sql = DB::$dbs->query("SELECT * FROM ". USER_BONUS ." WHERE `time` <= ?", [time()]);
while ($bonus = $sql -> fetch())
{
$us = DB::$dbs->queryFetch("SELECT * FROM ". USERS ." WHERE `id` = ?", [$bonus['user_id']]);
if ($bonus['type'] == 'param')
{
DB::$dbs->query("UPDATE ". USERS ." SET `strike` = ?, `defend` = ?, `health` = ?, `max_health` = ? WHERE `id` = ?", [($us['strike'] - $bonus['param']), ($us['defend'] - $bonus['param']), ($us['health'] - $bonus['param']), ($us['max_health'] - $bonus['param']), $us['id']]);
}
DB::$dbs->query("DELETE FROM ". USER_BONUS ." WHERE `id` = ?", [$bonus['id']]);
}
}
}
}