<?php
ob_start();
$off=0;
$date = date('H:i:s');
echo '<title>Цивилизация</title>';
if($off == 1 && $_SERVER['REMOTE_ADDR'] != "81.13.164.224")
{
echo ' <?php ob_start("ob_gzhandler", 9); ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <link rel="stylesheet" type="text/css" href="/1/style.css">
<meta name=description content="Культовая онлайн стратегия для мобильных - Цивилизация! Стань правителем, докажи что ты лучший!">
<meta name=keywords content="цива civilz civa civ civilization цивил civilz.net net civil civils civilz,net">
</head>
<body>
<div class="head">
<title>Цивилизация</title>
';
echo '
Уважаемы игроки, на данный момент игра недоступна, в связи с переездом на новый сервер. Пожалуйста, зайдите в игру позже';
exit();
}
$c_host = 'localhost';
$c_login = 'civa';
$c_pass = 'gsd12a';
$c_db = 'civa';
$link = mysql_connect($c_host, $c_login, $c_pass)
  or
die ("Не возможно присоединитса к Базе Данных. Сервер возможно перезагружаетса. Обновите страницу через пару секунд");

mysql_select_db ($c_db)
  or die ("Could not select database");
  mysql_query("SET NAMES 'windows-1251'", $link);
  mysql_set_charset("utf8");

header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: private");
header("Cache-Control: max-stale=0");
header("Content-type: text/html; charset=utf8");

  error_reporting(0);
$time = $_SERVER['REQUEST_TIME'];
$date = date('H:i:s',$time);
$dt=date("Y-m-d H:i:s");
$self=$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
$id=$_COOKIE['id'];
$acode=$_COOKIE['acode'];
$img_s = '<img src="./images/warrior.png" alt=*>';
$img_h = '<img src="./images/horse.png" alt=*>';
$img_a = '<img src="./images/bow.png" alt=*>';
$img_village = '<img src="./images/village.png" alt=*>';
$img_cons = '<img src="./images/cons.png" alt=*>';
$img_tachka = '<img src="./images/tachka.png" alt=*>';
$img_hall = '<img src="./images/hall.png" alt=*>';
$img_univ = '<img src="./images/univ.png" alt=*>';
$img_wall = '<img src="./images/wall.png" alt=*>';
$img_castle = '<img src="./images/castle.png" alt=*>';
$img_house = '<img src="./images/house.png" alt=*>';
$img_swords = '<img src="./images/swords.png" alt=*>';
$img_man = '<img src="./images/gen.png" alt=*>';
$img_men = '<img src="./images/men.png" alt=*>';
$img_money = '<img src="./images/money.png" alt=*>';
$img_massage = '<img src="./images/massage.png" alt=*>';
$img_port = '<img src="./images/ship.png" alt=*>';

$iexit = '<img src="./images/exit.png" alt=*>';
$ichat = '<img src="./images/chat.png" alt=*>';
$ipeople = '<img src="./images/people.png" alt=*>';
$iland = '<img src="./images/land.png" alt=*>';
$istat2 = '<img src="./images/stat2.png" alt=*>';
$ires = '<img src="./images/res.png" alt=*>';
$iland = '<img src="./images/land.png" alt=*>';
$iprofile = '<img src="./images/profile.png" alt=*>';
$iworld = '<img src="./images/world.png" alt=*>';
$iback = '<img src="./images/back2.png" alt=*>';
$ionline = '<img src="./images/online.png" alt=*>';
$ipen = '<img src="./images/pen.png" alt=*>';
$irefresh = '<img src="./images/refresh.png" alt=*>';
$ibuild = '<img src="./images/build.png" alt=*>';
$ireligion = '<img src="./images/religion.png" alt=*>';
$ibuild2 = '<img src="./images/build2.png" alt=*>';
$iband = '<img src="./images/Housing_capacity.png" alt=*>';
$igolds = '<img src="./images/icon_gold.png" alt=*>';

$istat = '<img src="./images/stat.png" alt=* align="left">';
$style = '
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name=description content="Культовая онлайн стратегия для мобильных - Цивилизация! Стань правителем, докажи что ты лучший!">
<meta name=keywords content="цива civilz civa civ civilization цивил civilz.net net civil civils civilz,net">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>Цивилизация</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <link rel="stylesheet" type="text/css" href="/1/style.css">
</head>
<body>

';
$user=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$id' AND acode='$acode'"));
function time_end($secs_end)
{
$time= $_SERVER['REQUEST_TIME'];
$difference = $secs_end-$time;
$vivod='';
$days = floor($difference / 86400) % 30;
if($days > 0)
{
$vivod.='<b>'.$days.'</b> дня ';
}
$hours = floor($difference / 3600) % 24;
if($hours > 0)
{
$vivod.='<b>'.$hours.'</b> часов ';
}
$minutes = floor($difference / 60) % 60;
if($minutes > 0)
{
$vivod.='<b>'.$minutes.'</b> минут ';
}
$seconds = $difference -$days*24*60*60- $minutes*60 - $hours*3600;
if($seconds > 0)
{
$seconds=floor($seconds);
$vivod.='<b>'.$seconds.'</b> секунд ';
}
echo $vivod;
}
function time_leave($secs_end)
{
$time= $_SERVER['REQUEST_TIME'];
$difference = $time - $secs_end;
$vivod='';
$days = floor($difference / 86400) % 30;
if($days > 0)
{
$vivod.='<b>'.$days.'</b> дня ';
}
$hours = floor($difference / 3600) % 24;
if($hours > 0)
{
$vivod.='<b>'.$hours.'</b> часов ';
}
$minutes = floor($difference / 60) % 60;
if($minutes > 0)
{
$vivod.='<b>'.$minutes.'</b> минут ';
}
$seconds = ($difference - ($days*24*60*60 - $minutes*60 - $hours*360)) % 60;
if($seconds > 0)
{

$vivod.='<b>'.$seconds.'</b> секунд ';
}
echo $vivod;
}
function send_mess($l_to,$what,$a,$b)
{
$whom=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE login='$l_to'"));
	if($whom['login'] != "")
	{
$to_whom_id=$whom[id];
$when=date('G:i:s');
$login_send= $a;
$id_send=$b;
  $as=$time+60;
mysql_query("UPDATE users SET msg_send='$as' WHERE id='$id'");
mysql_query("
INSERT INTO `msg` (
`id` ,
`by_whom` ,
`by_whom_id` ,
`to_whom` ,
`to_whom_id` ,
`when` ,
`what` ,
`read`,
`input`,
`output`
)
VALUES (
NULL , '$login_send', '$id_send', '$l_to', '$to_whom_id', '$when', '$what', '0','1','1'
)
");
	}
}

$php_self=$_SERVER['PHP_SELF'];
if($user['login'] == "")
{
	if($php_self != "/index.php" && $php_self != "/reg.php" && $php_self != "/auth.php" && $php_self != "/faq.php" && $php_self != "/rules.php")
	{
	echo $style;
	echo '<link rel="stylesheet" type="text/css" href="style.css"/><div class="head"></div>

	<div class="info"><b>Ошибка авторизации!</b></div><div class="info">
	';
	echo 'Попробуйте авторизоваться еще раз
	</div><a class="razd" href="exit.php?a=1">На главную</a>
	';
	exit();
	}
}
else
{
	//подсчётные переменные
	$freeplace=$user['freeplace']-$user['lumber']-$user['stone']-$user['grain']-$user['iron'];
	$tsolders=$user['solders'];
	$thorses=$user['horses'];
	$tarchers=$user['archers'];
	$twars=mysql_query("SELECT * FROM wars WHERE id_attacker='$id'");
	while($twar=mysql_fetch_array($twars))
	{
	$tsolders+=$twar['solders'];
	$thorses+=$twar['hores'];
	$tarchers+=$twar['archers'];
	}
	$online_time=$time+2*60*60;
	mysql_query("UPDATE users SET authtime='$online_time' WHERE id='$id'");
	//Проверка построенных зданий
	$b_sql=mysql_query("SELECT * FROM ubuildings WHERE owner_id='$id' AND time_end<'$time'");
	while($b_array=mysql_fetch_array($b_sql))
	{
		if($b_array['end'] == 0)
		{
		mysql_query("UPDATE users SET workers=workers+'$b_array[workers]' WHERE id='$id'");
		mysql_query("UPDATE `ubuildings` SET `end`='1' WHERE `id`='$b_array[id]'");
		$text='Постройка здания <u>'.$b_array['name'].'</u> завершена';
		send_mess($user['login'],$text,"Система",'');
		}
	}
	//проверка прироста ресурсов
	$r_sql=mysql_query("SELECT * FROM res_get WHERE id_user='$id' AND time_end<'$time'");
	while($r_array=mysql_fetch_array($r_sql))
	{

		$res=$r_array['res'];
		if ($freeplace >= $r_array['how'])
			{
		mysql_query("UPDATE users SET workers=workers+'$r_array[workers]', $res=$res+'$r_array[how]' WHERE id='$id'");
		mysql_query("DELETE FROM res_get WHERE `id`='$r_array[id]'");
		$text='Ваши рабочие на <u>'.$r_array['name'].'</u> добыли <b>'.$r_array['how'].'</b> ресурса';
		send_mess($user['login'],$text,"Система",'');
			}
			else {
				 $text='Ваши рабочие на <u>'.$r_array['name'].'</u> добыли <b>'.$r_array['how'].'</b> ресурса. Но из за недостатка места на складе, помещено '.$freeplace.' ресурса';
					send_mess($user['login'],$text,"Система",'');
	mysql_query("UPDATE users SET workers=workers+'$r_array[workers]', $res=$res+'$freeplace' WHERE id='$id'");
	mysql_query("DELETE FROM res_get WHERE `id`='$r_array[id]'");
				 }

	}
	//обучение
	$t_sql=mysql_query("SELECT * FROM teach WHERE id_user='$id' AND time_end<'$time'");
	while($t_array=mysql_fetch_array($t_sql))
	{

		$type=$t_array['type'];
		mysql_query("UPDATE users SET
		$type=$type+'$t_array[amount]',
		scientists=scientists+'$t_array[sc]'
		WHERE
		id='$id'
		");
		mysql_query("DELETE FROM teach WHERE `id`='$t_array[id]'");
		$text='Вы подготовили <b>'.$t_array['amount'].'</b> '.$t_array['text'].'';
		send_mess($user['login'],$text,"Система",'');

	}
	//иследования
	$re_sql=mysql_query("SELECT * FROM sc WHERE id_user='$id' AND time_end<'$time'");
	while($re_array=mysql_fetch_array($re_sql))
	{

		$what=$re_array['what'];
		$add=floor($re_array['money']/198);
		$lvls=floor($re_array['money']/1000);
		$re_b=mysql_fetch_array(mysql_query("SELECT * FROM ubuildings WHERE sname='Univercity' AND owner_id='$id'"));
		if($re_b['lvl'] > 1)
		{
			mysql_query("UPDATE ubuildings SET lvl=lvl+'$lvls' WHERE id='$re_b[id]'");
		}
		mysql_query("UPDATE users SET
		$what=$what+'$add',
		scientists=scientists+'$re_array[sc]'
		WHERE
		id='$id'
		");
		mysql_query("DELETE FROM sc WHERE `id`='$re_array[id]'");
        $text='Ваши ученые закончили исследование. '.$re_b['text'].' <b>+'.$add.'%</b>';
		send_mess($user['login'],$text,"Система",'');
	}
	if ($user['defeat'] == '1') header("Location: defeat.php");
	if ($user['bun_end'] > $time) header("Location: bun.php");
}


function add_n($id_1,$id_2)
{

	mysql_query("INSERT INTO seen SET id_user='$id_1', id_neib='$id_2'");
	mysql_query("INSERT INTO seen SET id_user='$id_2', id_neib='$id_1'");
    $c1=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$id_1'"));
    $c2=mysql_fetch_array(mysql_query("SELECT * FROM users WHERE id='$id_2'"));
    $text1='Появилось новое государство <u>'.$c2['country'].'</u>';
	send_mess($c1['login'],$text1,"Система",'');
	$text2='Появилось новое государство <u>'.$c1['country'].'</u>';
	send_mess($c2['login'],$text2,"Система",'');
}

function check_n($id_1,$id_2)
{
	$check=0;
	$check_sql=mysql_num_rows(mysql_query("SELECT * FROM seen WHERE id_user='$id_1' AND id_neib='$id_2'"));
	if($check_sql == 1)
	{
		$check=1;
	}
	return $check;
}
function add_new($c,$uid,$add)
{
	mysql_query("INSERT INTO map SET country='$c', user_id='$uid'");
	$last_id=mysql_insert_id();
	$west=mysql_query("SELECT * FROM map WHERE id<'$last_id' AND die='0' ORDER by id DESC LIMIT $add");
	while($w=mysql_fetch_array($west))
	{
		$nid=$w['user_id'];
		add_n($uid,$nid);
	}
}
?>