Файл: monst/core/in/fnct.php
Строк: 293
<?
function r() {
return uniqid(microtime(true), true);
}
function Cook_set($var, $value) {
setcookie($var, $value, time() + 60 * 60 * 24 * 365, '/', $_SERVER['HTTP_HOST'], 0, 1);
}
function Cook_del($var) {
setcookie($var, '', time() - 1, '/', $_SERVER['HTTP_HOST'], 0, 1);
}
function g($url) {
header('Location: '.$url);
exit;
}
function null_html($string) {
return htmlspecialchars(trim($string), ENT_QUOTES, 'UTF-8');
}
function User_get() {
global $HOME, $_users;
if ( empty($_COOKIE['id']) OR empty($_COOKIE['pass']) ) {
return 0;
}
$user = $_users -> findOne(
[
'id' => (int)$_COOKIE['id'],
'pass' => null_html($_COOKIE['pass']),
]
);
if ( $user['id'] ) {
$user_agent = null_html($_SERVER['HTTP_USER_AGENT']);
if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) { # miniopera real ip
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ( isset($_SERVER['HTTP_CF_CONNECTING_IP'] ) AND !empty($_SERVER['HTTP_CF_CONNECTING_IP']) ) { # cloudflare
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if ( $user['user_agent'] != $user_agent OR $user['ip'] != $ip ) {
$upd = $_users -> update(
[
'id' => $user['id']
],
[
'$set' =>
[
'user_agent' => $user_agent,
'ip' => $ip
]
]
);
}
$upd = 0;
if ( !isset($_SESSION['last_upd_time']) ) {
$upd = 1;
} else {
if ( time() - $_SESSION['last_upd_time'] >= 20 ) {
$upd = 1;
}
}
if ( $upd ) {
$update = $_users -> update(
[
'id' => $user['id']
],
[
'$set' =>
[
'l_time' => time(),
]
]
);
$_SESSION['last_upd_time'] = time();
}
return $user;
} else {
return 0;
}
}
function floor_dec($value, $decimals = 2) {
return floor($value * pow(10, $decimals) ) / pow(10, $decimals);
}
function nav($on_page = 20, $aggregate = 0) {
global $pat, $count, $find, $total_page;
if ( $aggregate ) {
global $page, $t;
}
if ( !$aggregate ) {
$page = ( isset($_GET['page']) ? (int)$_GET['page'] : 1);
if ( $page < 1 ) $page = 1;
$total_page = ceil($count / $on_page);
if ( $total_page AND $page > $total_page ) $page = $total_page;
$skip = ( $page * $on_page ) - $on_page;
// $find -> limit($on_page);
if ( $skip > 0 ) {
$find -> skip($skip);
}
}
$string = '<div style="text-align: center;">';
if ( $page > 1 ) {
$string .= ' <a href="' . $pat . '?page=' . ( $page - 1 ) . '"><span style="font-size: 30px;">←</span></a> ';
}
$string .= ' ' . $page . ' ';
if ( $page < $total_page ) {
$string .= ' <a href="' . $pat . '?page=' . ( $page + 1 ) . '"><span style="font-size: 30px;">→</span></a> ';
}
$string .= '</div>';
return $string;
}
function user_exist($id) {
global $_users;
$find = $_users -> findOne(
[
'id' => $id
]
);
if ( isset($find['id']) ) {
return 1;
} else {
return 0;
}
}
function Name($id, $upd = 0) {
global $mem, $_users;
$key = md5('monst__id'.$id);
$data = $mem -> get($key);
if ( !$data OR $upd ) {
$user = $_users -> findOne(
[
'id' => $id
]
);
if ( !$user ) {
return 0;
}
$color = ( isset($user['name_color']) ? $user['name_color'] : 0 );
if ( empty($color) ) {
$color = '#999';
}
$arr = [
'name' => $user['name'],
'name_color' => $color,
];
$mem -> set($key, $arr, 0, 60 * 60 * 24 * 2);
return '<span style="color: '.$color.';">'.$user['name'].'</span>';
} else {
return '<span style="color: '.$data['name_color'].';">'.$data['name'].'</span>';
}
}
function num($num) {
global $lang;
// return number_format($num);
if ( $num < 1000 ) return $num;
if ( $num >= 1000000 ) return round($num / 1000000, 2) . $lang['game']['million'];
if ( $num >= 1000 ) return round($num / 1000, 2) . $lang['game']['k'];
}
function hm($time, $displayed_d = 1, $displayed_h = 1, $displayed_min = 1, $displayed_sec = 1) {
global $user;
$d = floor($time / 60 / 60 / 24);
$h = floor(($time - ($d * 60 * 60 * 24) ) / 60 / 60);
$min = floor( ($time - ($h * 60 * 60) - ($d * 60 * 60 * 24) ) / 60 );
$sec = floor( ($time - ($min * 60) - ($h * 60 * 60) - ($d * 60 * 60 * 24) ) );
$string = '';
if ( $user['lang'] == 'ru' ) {
if ( $displayed_d ) $string .= $d . ' дн. ';
if ( $displayed_h ) $string .= $h . ' ч. ';
if ( $displayed_min ) $string .= $min . ' мин. ';
if ( $displayed_sec ) $string .= $sec . ' сек. ';
} else {
if ( $displayed_d ) $string .= $d . ' d. ';
if ( $displayed_h ) $string .= $h . ' h. ';
if ( $displayed_min ) $string .= $min . ' min. ';
if ( $displayed_sec ) $string .= $sec . ' sec. ';
}
return $string;
}
function lang_replace($string, $array) {
global $lang;
foreach ( $array as $var => $value ) {
$string = str_replace($var, $value, $string);
}
return $string;
}
function av($id) {
global $HOME;
if ( file_exists($HOME . '/content/av/'.$id.'.png') ) {
return '/content/av/'.$id.'.png?' . filemtime($HOME . '/content/av/'.$id.'.png');
} else {
return '/core/i/user/no_av.jpg';
}
}
function clanicon($clan_id) {
global $HOME;
if ( file_exists($HOME . '/content/clan/'.$clan_id.'.png') ) {
return '/content/clan/'.$clan_id.'.png?' . filemtime($HOME . '/content/clan/'.$clan_id.'.png');
} else {
return 0;
}
}
function use_item($item_id) {
global $user, $_users, $_item;
$item = $_item -> findOne(
[
'id' => $item_id
]
);
if ( !$item ) {
return 0;
}
$now_item = $_item -> findOne(
[
'user_id' => $user['id'],
'type' => $item['type'],
'use' => 1
]
);
if ( !$now_item ) {
$_users -> update(
[
'id' => $user['id']
],
[
'$set' => [
'st' => $user['st'] + $item['st'],
'hp' => $user['hp'] + $item['hp'],
'hpf' => $user['hpf'] + $item['hp'],
'armor' => $user['armor'] + $item['armor'],
]
]
);
$_item -> update(
[
'id' => $item['id']
],
[
'$set' => [
'use' => 1
]
]
);
} else {
if ( $item['id'] == $now_item['id'] ) {
$_users -> update(
[
'id' => $user['id']
],
[
'$set' => [
'st' => $user['st'] - $item['st'],
'hp' => $user['hp'] - $item['hp'],
'hpf' => $user['hpf'] - $item['hp'],
'armor' => $user['armor'] - $item['armor'],
]
]
);
$_item -> update(
[
'id' => $item['id']
],
[
'$set' => [
'use' => 0
]
]
);
} else {
$_users -> update(
[
'id' => $user['id']
],
[
'$set' => [
'st' => $user['st'] - $now_item['st'],
'hp' => $user['hp'] - $now_item['hp'],
'hpf' => $user['hpf'] - $now_item['hp'],
'armor' => $user['armor'] - $now_item['armor'],
]
]
);
$_item -> update(
[
'id' => $now_item['id']
],
[
'$set' => [
'use' => 0
]
]
);
$user2 = $_users -> findOne(
[
'id' => $user['id']
]
);
if ( !$user2 ) {
return 0;
}
$_users -> update(
[
'id' => $user['id']
],
[
'$set' => [
'st' => $user2['st'] + $item['st'],
'hp' => $user2['hp'] + $item['hp'],
'hpf' => $user2['hpf'] + $item['hp'],
'armor' => $user2['armor'] + $item['armor'],
]
]
);
$_item -> update(
[
'id' => $item['id']
],
[
'$set' => [
'use' => 1
]
]
);
}
}
}
//function use_item($item_id) {
// global $user, $_users, $_item;
//
// // ищем шмотку
// $item = $_item -> findOne(
// [
// 'id' => $item_id
// ]
// );
//
// // если нет щмотки то офаемся (юзер не выбрал)
// if ( !$item ) {
// return 0;
// }
//
// // если данная шмотки не одета
// if ( !$item['use'] ) {
//
// // ищем одетую шмотку такого же типа
// $find = $_item -> findOne(
// [
// 'user_id' => $user['id'],
// 'type' => $item['type'],
// 'use' => 1
// ]
// );
//
// // одетая шмотка того же типа найдена
// if ( $find ) {
// // снимаем шмотку >>>>>>
// $_users -> update(
// [
// 'id' => $user['id']
// ],
//
// [
// '$set' => [
// 'st' => $user['st'] - $find['st'],
// 'hp' => $user['hp'] - $find['hp'],
// 'hpf' => $user['hpf'] - $find['hp'],
// 'armor' => $user['armor'] - $find['armor'],
// ]
// ]
// );
//
// $_item -> update(
// [
// 'id' => $find['id']
// ],
//
// [
// '$set' => [
// 'use' => 0
// ]
// ]
// );
//
// }
//
// //
//
// // одеваем новую шмотку >>
// $_users -> update(
// [
// 'id' => $user['id']
// ],
//
// [
// '$set' => [
// 'st' => $user['st'] + $item['st'],
// 'hp' => $user['hp'] + $item['hp'],
// 'hpf' => $user['hpf'] + $item['hp'],
// 'armor' => $user['armor'] + $item['armor'],
// ]
// ]
// );
//
// $_item -> update(
// [
// 'id' => $item['id']
// ],
//
// [
// '$set' => [
// 'use' => 1
// ]
// ]
// );
//
// return 1;
// } else {
// // одетых шмоток такого же типа нет, одеваем >>
// $_users -> update(
// [
// 'id' => $user['id']
// ],
//
// [
// '$set' => [
// 'st' => $user['st'] - $item['st'],
// 'hp' => $user['hp'] - $item['hp'],
// 'hpf' => $user['hpf'] - $item['hp'],
// 'armor' => $user['armor'] - $item['armor'],
// ]
// ]
// );
//
// $_item -> update(
// [
// 'id' => $item['id']
// ],
//
// [
// '$set' => [
// 'use' => 0
// ]
// ]
// );
//
// return 2;
// }
//}
function new_id($collection){
global $$collection;
$retval = $$collection -> findAndModify(
array('_id' => 'uniq_id'),
array('$inc' => array('count' => 1)),
null,
array(
'new' => true,
'upsert' => true,
)
);
return $retval['count'];
}
function replace_time($time) {
global $lang, $user;
if ( date('d.m.Y', $time) == date('d.m.Y') ) {
$t = time() - $time;
if ( $t < 60 ) {
if ( $t < 5 ) {
return $lang['game']['now'];
} else {
return $t.$lang['game']['sec'];
}
} elseif ( $t >= 60 AND $t < 60 * 60) {
return floor($t / 60). $lang['game']['min'];
} elseif ( $t >= 60 * 60 ) {
return floor($t / (60 * 60)).$lang['game']['hour_ago'];
}
} elseif ( date('d.m.Y', $time) == date('d.m.Y', time() - 60 * 60 * 24) ) {
return $lang['game']['yesterday_at'].date('H:i', $time);
} else {
$days = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$days_ru = array('Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс');
if ( $user['lang'] == 'ru' ) {
$day = str_replace($days, $days_ru, date('D', $time));
} else {
$day = date('D', $time);
}
$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$months_ru = array('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек');
if ( $user['lang'] == 'ru' ) {
$month = str_replace($months, $months_ru, date('M', $time));
} else {
$month = date('M', $time);
}
$d = date('d', $time);
if ( $d{0} == '0') {
$d = $d{1};
}
if ( date('Y', $time) != date('Y') ) {
return $day . ', ' . $d . ' ' . $month . ', ' . date('Y', $time) . '. ' . date('H:i', $time);
} else {
return $day . ', ' . $d . ' ' . $month . '. ' . date('H:i', $time);
}
}
}
function upd_loc($string) {
global $_users, $user;
$_users -> update(
[
'id' => $user['id']
],
[
'$set' => [
'loc' => $string
]
]
);
}
function Online($l_time) {
$time = ( time() - $l_time ) / 60;
$status_col = '#fff';
if ( $time <= 2 ) $status_col = 'yellowgreen';
if ( $time > 2 AND $time <= 10 ) $status_col = 'yellow';
if ( $time > 10 AND $time <= 20 ) $status_col = 'orange';
if ( $time > 20 AND $time <= 30 ) $status_col = 'orangered';
if ( $time > 30 ) $status_col = '#777';
$online_status = '<span style="color: '.$status_col.';">•</span>';
return $online_status;
}
function month($time) {
global $user;
$m = date('M', $time);
if ( $user['lang'] == 'ru' ) {
$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$months_ru = array('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек');
$m = str_replace($months, $months_ru, $m);
return $m;
} else {
return $m;
}
}