<?php

include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';

$int=intval($_GET['id']);
$set['title']='Ферма!';
error_reporting (0);
include_once '../sys/inc/thead.php';
title();
err();
aut();
//header("Content-type: application/vnd.wap.xhtml+xml");
header("Content-type: text/html");
echo "<link rel='stylesheet' href='style.css' type='text/css' /><center>";
echo "<div class='ferm1'><img src='fermer.gif'><br/>";
$ank=mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id`= $int LIMIT 1")); 
if($_GET['id']==$user['id'])header('Location: my.php');
include 'inc/fermer_level.php';
include_once 'inc/popularity.php';
if($karma==0){echo '<a href="original_appearance.php"><img src="blog.png"></a> : 0<br />';}else{echo '<a href="original_appearance.php"><img src="blog.png"></a> : '.$karma.' %<br />';}
include_once 'inc/popul.php';

if($socstatus>=0 && $socstatus<=499){echo '<a href="area.php"><img src="chart.png"></a> : Колхозник ('.$socstatus.' %)<br />';}
elseif($socstatus>=500 && $socstatus<=999){echo '<a href="area.php"><img src="chart.png"></a> : Продвинутый ('.$socstatus.' %)<br />';}
elseif($socstatus>=1000 && $socstatus<=1499){echo '<a href="area.php"><img src="chart.png"></a> : Первый на деревне ('.$socstatus.' %)<br />';}
echo '</div><div class="ferm4">.</div>';
echo'<div class="ferm2">';

$doggy=$ank['fermer_doggy'];

if($doggy==1){echo '<img src="dog3.png"><img src="imghouse/house.gif"><img src="sklad.png"><br />';}else{echo '<img src="dog1.png"><img src="imghouse/house.gif"><img src="sklad.png"><br />';}

if(isset($_GET['vor'])){
$gr = mysql_fetch_array(mysql_query("select * from `fermer_gr` WHERE `id` = $_GET[vor] ")); 
$semen = mysql_fetch_array(mysql_query("select * from `fermer_name` WHERE `id` = '$gr[semen]' "));
$qi = mysql_fetch_assoc(mysql_query("SELECT * FROM `fermer_vor` WHERE `id_user` = '$user[id]'  AND `gr` = $_GET[vor] LIMIT 1"));
if($qi['time']<$time){
$rand1=floor($semen['rand1']/5);
$rand2=floor($semen['rand2']/5);
$rand=rand($rand1,$rand2);
$dog=mysql_fetch_array(mysql_query("select * from `fermer_dog` WHERE `id_user` = '$ank[id]' "));


if($dog['time']<$time){
$t=$time+86400;
mysql_query("INSERT INTO `fermer_vor` (`id_user` , `gr`, `time`) VALUES  ('".$user['id']."', '".$_GET['vor']."', '".$t."') ");
mysql_query("UPDATE `user` SET `fermer_doggy` ='0',`fermer_oput` = `fermer_oput`-'500' WHERE `id` = $user[id] LIMIT 1");
if($gr['kol']>$rand){
mysql_query("UPDATE `fermer_gr` SET `kol` = `kol`- $rand WHERE `id` = $_GET[vor] LIMIT 1");
mysql_query("INSERT INTO `fermer_sclad` (`kol` , `semen`, `id_user`) VALUES  ('".$rand."', '".$gr['semen']."', '".$user['id']."') ");
}else{
$rand=mysql_result(mysql_query("SELECT `kol` FROM `fermer_gr` WHERE `id` = $_GET[vor] "),0);
mysql_query("UPDATE `fermer_gr` SET `semen` = '0' WHERE `id` = $_GET[vor] LIMIT 1");
mysql_query("UPDATE `fermer_gr` SET `time` = NULL WHERE `id` = $_GET[vor] LIMIT 1");
mysql_query("UPDATE `fermer_gr` SET `kol` = NULL WHERE `id` = $_GET[vor] LIMIT 1");
mysql_query("INSERT INTO `fermer_sclad` (`kol` , `semen`, `id_user`) VALUES  ('".$rand."', '".$gr['semen']."', '".$user['id']."') ");
}
msg('Украдено '.$rand.'!!!');

}else{

mysql_query("UPDATE `user` SET `fermer_money` = `fermer_money` - $rand WHERE `id` = $user[id] LIMIT 1");
mysql_query("UPDATE `user` SET `fermer_money` = `fermer_money` + $rand WHERE `id` = $ank[id] LIMIT 1");
$rand=rand(1,100);
echo '<div class="err">На вас напала собака фермера! При побеге вы потеряли '.$rand.' !</div>';
}
}else{echo "<div class='err'>С этой грядки больше воровать нельзя!!!</div>";}
}
$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `fermer_gr` WHERE `id_user` = '".$int."'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];

$res = mysql_query("select * from `fermer_gr` WHERE `id_user` =  '".$int."' LIMIT $start, $set[p_str];"); 

while ($post = mysql_fetch_array($res)){

$semen=mysql_fetch_array(mysql_query("select * from `fermer_name` WHERE  `id` = '$post[semen]'  LIMIT 1")); 
if($post['semen']==0){$name_gr='Пустая грядка';}else{$name_gr=$semen['name'];}

echo "<img src='img/$post[semen].gif' <b></b>";
if($post['semen']!=0 && $post['time']<$time){
echo " (".$post['kol'].") ";
$qi = mysql_fetch_array(mysql_query("SELECT * FROM `fermer_vor` WHERE `id_user` = '$user[id]'  AND `gr` = '$post[id]' LIMIT 1"));
if($qi['time']<$time)
echo "<a href='?id=".$int."&amp;vor=".$post['id']."'>[воровать]</a>";


}else{if($post['semen']!=0)echo " (созревает)";}
echo "";

}



if ($k_page>1)str('?',$k_page,$page); // Вывод страниц

echo "<div class='foot'>";
echo "&laquo; <a href='my.php'>Моя ферма</a><br/>";
echo "&laquo; <a href='fermers.php'>Назад</a><br/>";
echo "</div>";

include_once '../sys/inc/tfoot.php';

?>