<?php
$soor_type = 1; // Проверка наличия сооружения
include_once('inclfiles/core.php');
$building = mysql_fetch_array(mysql_query("SELECT * FROM `buildings` WHERE `country`= ".$country['id']." AND `type` = 1"));
$building_all = mysql_fetch_array(mysql_query("SELECT * FROM `buildings_all` WHERE `type` = 1 AND `level` = ".$building['level'].""));
$kol_sosedi  = mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` < ".$country['id']." LIMIT ".$country['zapad']."")) + mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` > ".$country['id']." LIMIT ".$country['vostok'].""));
if($id != 0){
	if(mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `id` = ".$id."")) == 0){
		$title = 'Ошибка';
		include_once('inclfiles/head.php');
		t('Данное государство не найдено');
		a('sosedi.php?','Соседние государства','sosedi',$kol_sosedi);
		a('buildings.php?id=1&',$building_all['name'],'building');
		include_once('inclfiles/foot.php');
	}

	$sosed_country = mysql_fetch_array(mysql_query("SELECT * FROM `countries` WHERE `id` = ".$id.""));

    if($kol_sosedi!=0){
	    $kol_zapad  = mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` < ".$country['id']." LIMIT ".$country['zapad'].""));
	    if($kol_zapad != 0){
			$sosedi_zapad = mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` < ".$country['id']." ORDER BY `id` DESC LIMIT ".$country['zapad']."");
			while($sosed_zapad = mysql_fetch_array($sosedi_zapad)) if($sosed_zapad['id'] == $id) $sosed = true;
		}

	       $kol_vostok = mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` > ".$country['id']." LIMIT ".$country['vostok'].""));
		if($kol_vostok != 0){
			$sosedi_vostok = mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` > ".$country['id']." ORDER BY `id` ASC LIMIT ".$country['vostok']."");
			while($sosed_vostok = mysql_fetch_array($sosedi_vostok)) if($sosed_vostok['id'] == $id) $sosed = true;
		}
    }

    if(!$sosed){
		$title = 'Ошибка';
		include_once('inclfiles/head.php');
    	t('Данное государство не является соседом Вашего');
    	a('sosedi.php?','Соседние государства','sosedi',$kol_sosedi);
		a('buildings.php?id=1?',$building_all['name'],'building');
		include_once('inclfiles/foot.php');
    }

    switch($act){
    	case 'resurses':
    	    $title = 'Казна и ресурсы государства '.$sosed_country['name'];
			include_once('inclfiles/head.php');
			if($act2 == 'ukrast'){
				if(time() - $country['time_vorovstvo'] > 5400){
					mysql_query("UPDATE `countries` SET `time_vorovstvo`= ".time()." WHERE `id` = ".$country['id']."");
					$country['time_vorovstvo'] = time();
     				if($country['vorovstvo'] >= $sosed_country['shpionazh']){
	            	    $proc = $country['vorovstvo']+1 - $sosed_country['shpionazh'];
						$ukradeno_money = floor($sosed_country['money']*$proc/100);
		   				$ukradeno_wood = floor($sosed_country['wood']*$proc/100);
		    			$ukradeno_stone = floor($sosed_country['stone']*$proc/100);
		   				$ukradeno_iron = floor($sosed_country['iron']*$proc/100);
		   				$ukradeno_grain = floor($sosed_country['grain']*$proc/100);
						$ukradeno_oil = floor($sosed_country['oil']*$proc/100);

						$mesto = $set['mesto'];
					 	if(mysql_num_rows(mysql_query("SELECT * FROM `buildings` WHERE `type` = 3 AND `country` = ".$country['id']."")) != 0){
					 	    $hranilishe = mysql_fetch_array(mysql_query("SELECT * FROM `buildings` WHERE `country`= ".$country['id']." AND `type` = 3"));
					 		$mesto = $mesto + 4 * $hranilishe['level'] * $hranilishe['level'] * $hranilishe['territory'];
					 	}
					 	$mesto = $mesto - $country['wood'] - $country['stone'] - $country['iron'] - $country['grain'] - $country['oil'];

						$vsego = $ukradeno_wood+$ukradeno_stone+$ukradeno_iron+$ukradeno_grain+$ukradeno_oil;
						if($mesto < $vsego){
                        	$procent = $mesto/$vsego;
			   				$vzjato_wood = floor($ukradeno_wood*$procent);
			    			$vzjato_stone = floor($ukradeno_stone*$procent);
			   				$vzjato_iron = floor($ukradeno_iron*$procent);
			   				$vzjato_grain = floor($ukradeno_grain*$procent);
							$vzjato_oil = floor($ukradeno_oil*$procent);
							$pomestilos = ', но поместилось лишь '.floor($procent*100).'% украденных ресурсов';
						}
						else{
							$vzjato_wood = $ukradeno_wood;
			    			$vzjato_stone = $ukradeno_stone;
			   				$vzjato_iron = $ukradeno_iron;
			   				$vzjato_grain = $ukradeno_grain;
							$vzjato_oil = $ukradeno_oil;
							$pomestilos = '';
						}

						mysql_query("UPDATE `countries` SET `money`= (`money` - ".$ukradeno_money."), `wood`= (`wood` - ".$ukradeno_wood."),`stone`= (`stone` - ".$ukradeno_stone."),`iron`= (`iron` - ".$ukradeno_iron."),`grain`= (`grain` - ".$ukradeno_grain."),`oil`= (`oil` - ".$ukradeno_oil.") WHERE `id` = ".$sosed_country['id']."");
						mysql_query("UPDATE `countries` SET `money`= (`money` + ".$ukradeno_money."), `wood`= (`wood` + ".$vzjato_wood."),`stone`= (`stone` + ".$vzjato_stone."),`iron`= (`iron` + ".$vzjato_iron."),`grain`= (`grain` + ".$vzjato_grain."),`oil`= (`oil` + ".$vzjato_oil.") WHERE `id` = ".$country['id']."");

	                    if($sosed_country['shpionazh'] > 1){
	                    	mysql_query("UPDATE `countries` SET `shpionazh`= (`shpionazh` - 1) WHERE `id` = ".$sosed_country['id']."");
	                  	}
                        if($sosed_country['vorovstvo'] < 50){
                        	mysql_query("UPDATE `countries` SET `vorovstvo`= (`vorovstvo` + 1) WHERE `id` = ".$country['id']."");
                        	$uroven = '. Уровень воровства повышен на 1%';
                        }
                        else $uroven = '';

                        m('Украдено '.chislo($ukradeno_money,'монета','монеты','монет').', '.$ukradeno_wood.' дерева, '.$ukradeno_stone.' камня, '.$ukradeno_iron.' железа, '.$ukradeno_grain.' зерна и '.$ukradeno_oil.' нефти'.$pomestilos.$uroven);
                        mysql_query("INSERT INTO `journal` SET `country` = ".$sosed_country['id'].", `type` = 3, `num1` = ".$country['id'].", `num2` = ".$ukradeno_money.", `num3` = ".$ukradeno_wood.", `num4` = ".$ukradeno_stone.", `num5` = ".$ukradeno_iron.", `num6` = ".$ukradeno_grain.", `num7` = ".$ukradeno_oil.", `time` = ".time()."");

                        $sosed_country = mysql_fetch_array(mysql_query("SELECT * FROM `countries` WHERE `id` = ".$id.""));
                    }
                    else{
                    	if($sosed_country['shpionazh'] < 100){
	                    	mysql_query("UPDATE `countries` SET `shpionazh`= (`shpionazh` + 1) WHERE `id` = ".$sosed_country['id']."");
	                  	}
                        if($sosed_country['vorovstvo'] > 1){
                        	mysql_query("UPDATE `countries` SET `vorovstvo`= (`vorovstvo` - 1) WHERE `id` = ".$country['id']."");
                        	$uroven = '. Уровень воровства понижен на 1%';
                        }
                        else $uroven = '';
                        $err[]='Вашим грабителям не удалось украсть ресурсы соседнего государства'.$uroven;
                        mysql_query("INSERT INTO `journal` SET `country` = ".$sosed_country['id'].", `type` = 4, `num1` = ".$country['id'].", `time` = ".time()."");

                    }
				}
				else $err[]='Ваши грабители еще не готовы для нового задания';
    		}

			err();

			$ot = 0.5 + $country['shpionazh']/200;
			$do = 1.5 - $country['shpionazh']/200;

			$sosed_country['money'] = mt_rand($sosed_country['money']*$ot,$sosed_country['money']*$do);
			$sosed_country['wood'] = mt_rand($sosed_country['wood']*$ot,$sosed_country['wood']*$do);
			$sosed_country['stone'] = mt_rand($sosed_country['stone']*$ot,$sosed_country['stone']*$do);
			$sosed_country['iron'] = mt_rand($sosed_country['iron']*$ot,$sosed_country['iron']*$do);
			$sosed_country['grain'] = mt_rand($sosed_country['grain']*$ot,$sosed_country['grain']*$do);
			$sosed_country['oil'] = mt_rand($sosed_country['oil']*$ot,$sosed_country['oil']*$do);

			t1();
   			echo 'Монеты: '.$sosed_country['money'].'<br/>';
			echo 'Дерево: '.$sosed_country['wood'].'<br/>';
			echo 'Камень: '.$sosed_country['stone'].'<br/>';
			echo 'Железо: '.$sosed_country['iron'].'<br/>';
			echo 'Зерно: '.$sosed_country['grain'].'<br/>';
			echo 'Нефть: '.$sosed_country['oil'].'<br/>';
			t2();

			p('Точность шпионажа '.$country['shpionazh'].'%');
			if(time() - $country['time_vorovstvo'] > 5400) a('sosedi.php?id='.$id.'&act=resurses&act2=ukrast&','Украсть ресурсы','vorovstvo');
			else t('До следующей возможности воровства осталось: '.period(5400 + $country['time_vorovstvo'] - time()));
			a('sosedi.php?id='.$sosed_country['id'].'&',$sosed_country['name'],'flags/'.$sosed_country['flag']);
    	break;
   		default:
   			$title = $sosed_country['name'];
			include_once('inclfiles/head.php');
			t1();
			echo 'Правилель: ';
		    if($sosed_country['user'] != 0) echo user($sosed_country['user']);
		    else echo 'нет правителя';
		    echo '<br/>';
		    echo 'Дата создания: '.data($sosed_country['time_create']).'<br/>';
		    echo 'Возраст: '.vozrast(time() - $sosed_country['time_create']);
			t2();
			a_user($sosed_country['user']);
			a('sosedi.php?id='.$id.'&act=resurses&','Казна и ресурсы государства','resurses');
	}

    a('sosedi.php?','Соседние государства','sosedi',$kol_sosedi);
}
else{
	$title = 'Соседние государства';
	include_once('inclfiles/head.php');
	if($kol_sosedi!=0){
	    $kol_zapad  = mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` < ".$country['id']." LIMIT ".$country['zapad'].""));
	    if($kol_zapad != 0){
			$sosedi_zapad = mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` < ".$country['id']." ORDER BY `id` DESC LIMIT ".$country['zapad']."");
			while($sosed_zapad = mysql_fetch_array($sosedi_zapad)) a('sosedi.php?id='.$sosed_zapad['id'].'&',$sosed_zapad['name'],'flags/'.$sosed_zapad['flag']);
		}

        $kol_vostok = mysql_num_rows(mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` > ".$country['id']." LIMIT ".$country['vostok'].""));
		if($kol_vostok != 0){
			$sosedi_vostok = mysql_query("SELECT * FROM `countries` WHERE `zahvatil` = 0 AND `id` > ".$country['id']." ORDER BY `id` ASC LIMIT ".$country['vostok']."");
			while($sosed_vostok = mysql_fetch_array($sosedi_vostok)) a('sosedi.php?id='.$sosed_vostok['id'].'&',$sosed_vostok['name'],'flags/'.$sosed_vostok['flag']);
		}
	}
	else t('Нет соседних государств');
	a('buildings.php?id=1&',$building_all['name'],'building');
}
include_once('inclfiles/foot.php');
?>