<?php

function getIP() {
   if(isset($_SERVER['HTTP_X_REAL_IP'])) return $_SERVER['HTTP_X_REAL_IP'];
   return $_SERVER['REMOTE_ADDR'];
}

if(!isset($_GET['test'])) 
{
	/*if( $_SERVER['HTTP_CF_CONNECTING_IP'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_CF_CONNECTING_IP'] != '127.0.0.1' ) 
	{ 
		die('<center><br><h3>Ѕроди ;)</h3><img src="i/fack.jpg">'); 
	}*/
	if(getIP() != $_SERVER['SERVER_ADDR'] && getIP() != '127.0.0.1' && getIP() != '' && getIP() != '31.131.97.206') 
	{
		die(getIP().'<br>'.$_SERVER['SERVER_ADDR']);
	}	
}	
/*	
	if( $_SERVER['HTTP_CF_CONNECTING_IP'] != $_SERVER['SERVER_ADDR'] && $_SERVER['HTTP_CF_CONNECTING_IP'] != '127.0.0.1' ) { die('<center><br><h3>Ѕроди ;)</h3><img src="i/fack.jpg">'); }
}*/



define('GAME',true);

include('_incl_data/__config.php');
include('_incl_data/class/__db_connect.php');

function e($t) {
	mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("core #'.date('d.m.Y').' %'.date('H:i:s').' ( ритическа¤ ошибка): <b>'.mysql_real_escape_string($t).'</b>","capitalcity","LEL","6","1","-1")');
}

function testMonster( $mon , $type , $mbk ) {
	$r = true;
	$mbk2 = '';
	if(isset($mon['id'])) {
		//
		if($type == 'start') {
			//?ень недели
			if( $mon['start_day'] != -1 ) {
				if( ($mon['start_day'] < 7 && $mon['start_day'] != (int)date('w')) || $mon['start_day'] != 7 ) {
					$r = false;
					$mbk2 .= '(неделя)';
				}
			}
			//?исло
			if( $mon['start_dd'] != -1 ) {
				if( $mon['start_dd'] != date('j') ) {
					$r = false;
					$mbk2 .= '(число)';
				}
			}
			//месІц
			if( $mon['start_mm'] != -1 ) {
				if( $mon['start_mm'] != date('n') ) {
					$r = false;
					$mbk2 .= '(месяц)';
				}
			}
			//час
			if( $mon['start_hh'] != -1 ) {
				if( $mon['start_hh'] != (int)date('H') ) {
					$r = false;
					$mbk2 .= '('.$mon['start_hh'].'|'.((int)date('H')).' час)';
				}
				if( $mon['start_min'] != -1 ) {
					if( $mon['start_min'] < (int)date('i') ) {
						$r = false;
						$mbk2 .= '('.$mon['start_min'].'|'.((int)date('i')).' минуты)';
					}
				}
			}
		}elseif($type == 'back') {
			//?ень недели
			if( $mon['back_day'] != -1 ) {
				if( ($mon['back_day'] < 7 && $mon['back_day'] != date('w')) || $mon['back_day'] != 7 ) {
					$r = false;
					$mbk2 .= '(день недели)';
				}
			}
			//?исло
			if( $mon['back_dd'] != -1 ) {
				if( $mon['back_dd'] != date('j') ) {
					$r = false;
					$mbk2 .= '(число)';
				}
			}
			//месІц
			if( $mon['back_mm'] != -1 ) {
				if( $mon['back_mm'] != date('n') ) {
					$r = false;
					$mbk2 .= '(месяц)';
				}
			}
			//час
			if( $mon['back_hh'] != -1 ) {
				if( $mon['back_hh'] != (int)date('H') ) {
					$r = false;
					$mbk2 .= '(час)';
				}
				if( $mon['back_min'] != -1 ) {
					if( $mon['back_min'] < (int)date('i') ) {
						$r = false;
						$mbk2 .= '(минуты)';
					}
				}
			}
		}else{
			//что-то другое
			$r = false;
			$mbk2 .= '(что-то другое)';
		}
		//
	}
	if( $mbk == true ) {
		return $mbk2;
	}else{
		return $r;
	}
}

$sp = mysql_query('SELECT `u`.*,`st`.* FROM `users` AS `u` LEFT JOIN `stats` AS `st` ON `st`.`id` = `u`.`id` WHERE `u`.`no_ip` = "trupojor" LIMIT 100');
while($pl = mysql_fetch_array($sp)) {
	/*echo "ID:".$pl['id']."<br>";
	echo "online:".$pl['online']."<br>";
	$testtime=time()-60;
	echo "time:".$testtime."<br>";*/
	$act = 0;
	if($pl['online'] < time()-60) {
		$pl['online'] = time();
		mysql_query('UPDATE `users` SET `online` = "'.$pl['online'].'" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
	}
	
	//$testb=mysql_fetch_array(mysql_query('SELECT * FROM `battle` WHERE `type` = 500 AND `team_win` = -1 LIMIT 1'));
	if($pl['res_x'] < time() /*&& !isset($testb['id'])*/) {
		//ёожно действовать!
		$mon = mysql_fetch_array(mysql_query('SELECT * FROM `aaa_monsters` WHERE `uid` = "'.$pl['id'].'" LIMIT 1'));
		if( isset($mon['id']) ) {
			if( testMonster($mon,'start',false) == true && $pl['room'] == 303 ) {
				$pl['room'] = $mon['start_room'];
				mysql_query('UPDATE `users` SET `room` = "'.$pl['room'].'" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				mysql_query('UPDATE `stats` SET `hpNow` = "1000000000000",`mpNow` = "1000000000000" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				if( $mon['start_text'] != '' /*&& $pl['level'] <10*/) {
					mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=red>Внимание!</font> '.mysql_real_escape_string(str_replace('{b}','<b>'.$pl['login'].'</b> ['.$pl['level'].']<a target=_blank href=inf.php?'.$pl['id'].' ><img width=12 height=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>',$mon['start_text'])).'","'.$pl['city'].'","","6","1","'.time().'")');
					//mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=red>Внимание!</font> '.mysql_real_escape_string(str_replace('{b}','<b>'.$pl['login'].'</b> ['.$pl['level'].']<a target=_blank href=inf.php?'.$pl['id'].' ><img width=12 height=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>',$mon['start_text'])).'","'.$pl['city'].'","","1","1","'.time().'")');
				}
				$act = 1;
			}
		}else{
			mysql_query('UPDATE `stats` SET `res_x` = "'.(time()+3600).'" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
		}
	}
	
	//e('['.date('d.m.Y H:i:s').'] TRUPOJOR -> '.$pl['login'].' -> [room: '.$pl['room'].' , test: '.testMonster($mon,'start',true).' , res_x: '.$pl['res_x'].' , battle: '.$pl['battle'].' , act: '.$act.' ]');
	
	if( $act == 0 && $pl['room'] != 303 && $pl['battle'] == 0 ) {
		if(!isset($mon['id'])) {
			$mon = mysql_fetch_array(mysql_query('SELECT * FROM `aaa_monsters` WHERE `uid` = "'.$pl['id'].'" LIMIT 1'));
		}
		if( isset($mon['id']) ) {
			if( testMonster($mon,'back',false) == true ) {
				$pl['room'] = 303;
				mysql_query('UPDATE `users` SET `room` = "'.$pl['room'].'" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				mysql_query('UPDATE `stats` SET `hpNow` = "1000000000000",`mpNow` = "1000000000000" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				if( $mon['back_text'] != '' ) {
					mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=red>Внимание!</font> '.mysql_real_escape_string(str_replace('{b}','<b>'.$pl['login'].'</b> ['.$pl['level'].']<a target=_blank href=inf.php?'.$pl['id'].' ><img width=12 height=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>',$mon['back_text'])).'","'.$pl['city'].'","","6","1","'.time().'")');
					mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=red>Внимание!</font> '.mysql_real_escape_string(str_replace('{b}','<b>'.$pl['login'].'</b> ['.$pl['level'].']<a target=_blank href=inf.php?'.$pl['id'].' ><img width=12 height=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a>',$mon['back_text'])).'","'.$pl['city'].'","","1","1","'.time().'")');
				}
				$act = 2;
			}else{
				echo '[Ожидание: '.$mon['id'].' -> '.$mon['login'].' | '.testMonster($mon,'back',true).']<br>';
			}
		}
	}
	/*if($pl['battle'] > 0) {
		//inuser_go_atack($pl);
	}else{
		if($pl['room'] == 303 && $pl['timeGo'] < time()) {
			if($pl['res_x'] < time()) {
				$pl['room'] = $pl['invBlock'];
				mysql_query('UPDATE `users` SET `room` = "'.$pl['room'].'" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				mysql_query('UPDATE `stats` SET `hpNow` = "1000000000000",`mpNow` = "1000000000000" WHERE `id` = "'.$pl['id'].'" LIMIT 1');
				mysql_query('INSERT INTO `chat` (`text`,`city`,`to`,`type`,`new`,`time`) VALUES ("<font color=red>Рнимание!</font> <b>'.$pl['login'].'</b> ['.$pl['level'].']<a target=_blank href=inf.php?'.$pl['id'].' ><img width=12 height=11 src=https://img.new-combats.com/i/inf_capitalcity.gif ></a> выбралсІ на охоту, будьте осторожны!","'.$pl['city'].'","","6","1","'.time().'")');
			}
		}
	}*/
	usleep(300000);
}
?>