<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/config.php';
$tpl = [
'user_link' => '<a href="/profile.php?id=%d" class="u-link"><img src="/style/user/%s" class="u-icon"> <span class="u-name"%s>%s</span>%s</a>',
'user_deleted' => '<span class="u-del">[Удален]</span>',
'prefix' => '<span class="u-pref" style="color:%s; margin-left:10px; display:inline-block;">[%s]</span>',
'msg' => '<div class="msg-new"><div class="msg-inner">%s<div class="msg-nav"><a href="?">Продолжить</a></div></div></div>'
];
function out($t) {
return htmlspecialchars($t ?? '', ENT_QUOTES, 'UTF-8');
}
function db_query($sql, $params = []) {
global $pdo;
$s = $pdo->prepare($sql);
$s->execute($params);
return $s;
}
function db_rows_affected($stmt) {
return $stmt ? $stmt->rowCount() : 0;
}
function get_exp_needed($lvl) {
return 1000 * pow($lvl, 2);
}
function check_level_up(&$u) {
if ($u['level'] >= 100) return false;
$needed = get_exp_needed($u['level']);
if ($u['exp'] >= $needed) {
$u['level'] += 1;
db_query("UPDATE users SET level = ? WHERE id = ?", [$u['level'], $u['id']]);
set_msg("Вы достигли " . $u['level'] . " уровня!");
return true;
}
return false;
}
function nick($u) {
global $tpl;
static $cache = [];
if (!$u) return $tpl['user_deleted'];
if (!is_array($u)) {
$id = (int)$u;
if (!isset($cache[$id])) {
$f = db_query("SELECT id, login, sex, online, admin, level, vip FROM users WHERE id = ? LIMIT 1", [$id])->fetch();
$cache[$id] = $f ?: 'deleted';
}
$u = $cache[$id];
}
if ($u === 'deleted') return $tpl['user_deleted'];
$now = time();
$is_online = (isset($u['online']) && $u['online'] > ($now - 300));
$gender = ((int)$u['sex'] === 1) ? 'woman' : 'man';
$icon = $is_online ? $gender . '.png' : $gender . '_off.png';
$is_vip = (isset($u['vip']) && (int)$u['vip'] > $now);
$nick_style = $is_vip ? ' style="color:#ffcc00; font-weight:bold; text-shadow: 0 0 5px rgba(255,204,0,0.5);"' : '';
$prefix = '';
if (isset($u['admin'])) {
if ((int)$u['admin'] === 1) $prefix = sprintf($tpl['prefix'], '#08aa00', 'Мд');
if ((int)$u['admin'] === 2) $prefix = sprintf($tpl['prefix'], '#ff4444', 'Адм');
}
return sprintf($tpl['user_link'], (int)$u['id'], $icon, $nick_style, out($u['login']), $prefix);
}
function n_f($i) {
$i = (float)$i;
if ($i >= 1000000) return round($i/1000000, 1) . ' М';
if ($i >= 1000) return round($i/1000, 1) . ' к';
return number_format($i, 0, '', ' ');
}
function auth() {
if (!isset($GLOBALS['user']) || !$GLOBALS['user']) {
header('Location: /login.php');
exit;
}
}
function set_msg($text) {
$_SESSION['swal'] = $text;
}
function show_msg() {
global $tpl;
if (!isset($_SESSION['swal'])) return;
echo sprintf($tpl['msg'], $_SESSION['swal']);
unset($_SESSION['swal']);
}
function vremja($time = null) {
return date('H:i:s', $time ?? time());
}
$user = false;
if (isset($_SESSION['user_id'])) {
$user = db_query("SELECT * FROM users WHERE id = ? LIMIT 1", [(int)$_SESSION['user_id']])->fetch();
if (!$user) {
session_destroy();
} else {
$now = time();
$is_vip = (isset($user['vip']) && (int)$user['vip'] > $now);
$user['t_str'] = $user['str'] + ($is_vip ? 300 : 0);
$user['t_def'] = $user['def'] + ($is_vip ? 300 : 0);
$user['t_hp'] = $user['hp'] + ($is_vip ? 300 : 0);
if ((int)$user['online'] < ($now - 60)) {
$new_coll = (int)$user['coll_liga'];
$new_time_liga = (int)$user['time_liga'];
if ($new_coll < 5) {
$passed = $now - $new_time_liga;
if ($passed >= 600) {
$add = floor($passed / 600);
$new_coll = min(5, $new_coll + $add);
$new_time_liga = $now;
}
} else {
$new_time_liga = $now;
}
db_query(
"UPDATE users SET online = ?, ip = ?, coll_liga = ?, time_liga = ? WHERE id = ?",
[$now, $_SERVER['REMOTE_ADDR'], $new_coll, $new_time_liga, (int)$user['id']]
);
$user['online'] = $now;
$user['coll_liga'] = $new_coll;
$user['time_liga'] = $new_time_liga;
check_level_up($user);
}
}
}
$myID = $user ? (int)$user['id'] : 0;