<?php

$time=time();
$db_host='localhost'; // хост
$db_password='727272'; // пароль
$db_user_name='ferma'; // имя пользователя
$db_name='ferma'; // имя базы данных

if(!($db=@mysql_connect($db_host, $db_user_name, $db_password)))
{
echo "<center>Bad MySQL host</center>";
exit;
}

if (!@mysql_select_db($db_name,$db))
{
echo "Wrong MySQL db name";
exit;
}
mysql_query('set charset utf8',$db); 
mysql_query('SET names utf8',$db); 
mysql_query('set character_set_client="utf8"',$db); 
mysql_query('set character_set_connection="utf8"',$db); 
mysql_query('set character_set_result="utf8"',$db);

@session_name('SESS');
@session_start();
$_SESSION['id_user']=1;
$sess=mysql_escape_string(session_id());
if (!preg_match('#[A-z0-9]{32}#i',$sess))$sess=md5(rand(09009,999999));
if (isset($_SESSION['id_user']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_user` WHERE `id` = '$_SESSION[id_user]' LIMIT 1"), 0)==1)
{
$ku=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_user` WHERE `id` = '$_SESSION[id_user]'"));
}
$set=array();
$set['p_str']=10;
$dir="kolhoz";

//echo "<title>Удивительный колхоз</title>";
include_once 'inc/functions.php';

include_once 'inc/head.php';
if(!isset($ku))
{
include_once 'inc/no_auth.php';
}
include_once 'inc/start.php';
if($ku['ip']!==$_SERVER['REMOTE_ADDR']){
mysql_query("UPDATE `kolhoz_user` SET `ip` = '".my_esc($_SERVER['REMOTE_ADDR'])."' WHERE `id` = '$ku[id]'") or die (mysql_error());
}
//ТЕХНИКA
include_once 'inc/tehnika.php';
//ПЕРЕХОД НА СЛЕДУЮЩИЙ УРОВЕНЬ
include_once 'inc/next_level.php';
include_once 'inc/uhead.php';
error_reporting(E_ALL^E_NOTICE);
if($ku['level']>=20)
{
if(isset($_GET['get_prize']))
{
echo "<div class='event'><h1>Вы поймали сюрприз!</h1></div>";
echo '<div class="content"><div class="block">';
if(isset($_GET['gen_code']) && $_SESSION['prize']==$_GET['gen_code'] && isset($_SESSION['prize']))
{
$rr=rand(1,7);
$ku['rubies']=plus_rubies($rr);
$rm=rand(500,5000);
$ku['money']=plus_money($rm);
echo "Поздравляем! Вы поймали <img src='images/icons/ruby.png'/> <span class='title'>$rr</span>".($rm!=0?" и <img src='images/icons/money.png'/> <span class='title'>$rm</span>":null).".";
unset($_SESSION['prize']);
}
else
{
echo "Эх, не успели!";
}
echo "</div>";
include_once 'inc/foot.php';
}

$rand=rand(1,100);
if($rand==38 || $rand==67  || $rand==96)
{
$_SESSION['prize']=md5($rand);
echo "<div class='feedback'><center><img src='images/icons/ruby.png'> <a href='?get_prize=1&gen_code=".md5($rand)."'>Лови сюрприз!</a> <img src='images/icons/ruby.png'></center></div>";
}
}
include_once 'inc/shop.php';


if(isset($_GET['gos']))
{
if(isset($mc))
{
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$gos[id_plant]' AND (`level` = '$ku[level]' OR `level` < '$ku[level]')"),0)!=0)
mysql_query("UPDATE `kolhoz_user` SET `last_plant` = '$gos[id_plant]' WHERE `id` = '$ku[id]'");
header("Location:?myfarm");
exit;
}
else err_game("Для начала вступите в колхоз.");
}
include_once 'inc/change_plant.php';
include_once 'inc/change_soil.php';
include_once 'inc/cultivator_all.php';
include_once 'inc/cultivator.php';
include_once 'inc/harvest_all.php';
include_once 'inc/harvest.php';
include_once 'inc/seat_all.php';
include_once 'inc/seat.php';
include_once 'inc/soil_all.php';
include_once 'inc/soil.php';
include_once 'inc/water_all.php';
include_once 'inc/water.php';
include_once 'inc/clean_grounds.php';
include_once 'inc/new_ground.php';

echo "<div class='eventfarm'><h1>Грядки</h1></div></div>";
if(isset($_SESSION['cp']))
{
echo "<div class='feedback'><ul class='feedbackPanel'><li class='feedbackPanelINFO'><span class='feedbackPanelINFO'>Бесплатная очистка грядок будет доступна через ".time_left($ku['time_cp']-$time)."</span></li></ul></div>";
unset($_SESSION['cp']);
}
echo "<div class='content'>";
echo "<div class='block'>";
echo "<div><img width='16' height='16' src='images/icons/refresh.png' alt='o'> <a href='?myfarm&reflesh=".rand(99999,99999999)."'>Обновить</a></div>";
if($ku['hydropinics_time']>$time)echo "<li><img width='16' height='16' src='images/icons/hydropinics.png' alt='o'> <a class='epic' href='?shop=hydropinics'>Гидропоника</a><span class='minor'> $ku[hydropinics_type]% на <span>".time_left($ku['hydropinics_time']-$time)."</span></span></li>";
if($ku['scarecrow_time']>$time)echo "<li><img width='16' height='16' src='images/icons/scarecrow.png' alt='o'> <a class='epic' href='?shop=scarecrow'>Пугало</a><span class='minor'> на <span>".time_left($ku['scarecrow_time']-$time)."</span></span></li>";
if($ku['time_shmel']>$time)echo "<li><img width='16' height='16' alt='o' src='images/icons/heli1n.png'> <a class='epic' href='?shop=helicopter&1'>Шмель</a><span class='minor'> на <span>".time_left($ku['time_shmel']-$time)."</span></span></li>";
if($ku['time_yastreb']>$time)echo "<li><img width='16' height='16' alt='o' src='images/icons/heli2n.png'> <a class='epic' href='?shop=helicopter&2'>Ястреб</a><span class='minor'> на <span>".time_left($ku['time_yastreb']-$time)."</span></span></li>";
if($ku['time_drakon']>$time)echo "<li><img width='16' height='16' alt='o' src='images/icons/heli3n.png'> <a class='epic' href='?shop=helicopter&3'>Дракон</a><span class='minor'> на <span>".time_left($ku['time_drakon']-$time)."</span></span></li>";
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]' AND (`time` < '$time' OR `time` = '$time') AND `id_plant` != '0'"),0)!=0 && $ku['harvester']>0)echo "<div><img width='16' height='16' src='images/icons/harvest.png' alt='o'> <a href='?myfarm&harvest_all'>Собрать все</a></div>";
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]' AND `soil` = '0' AND (`time_soil` = '$time' OR `time_soil` < '$time') AND `id_plant` != '0' AND `time` > '$time'"),0)!=0 && $ku['soiler']>0)echo "<div><img width='16' height='16' src='images/icons/soil.png' alt='o'> <a href='?myfarm&soil_all'>Удобрить все</a></div>";
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]' AND `id_plant` != '0' AND (`time_water` = '$time' OR `time_water` < '$time') AND `time` > '$time' AND (`soil` = '0' AND `time_soil` > '$time' OR `soil` = '1')"),0)!=0 && $ku['irregate']>0)echo "<div><img width='16' height='16' src='images/icons/water.png' alt='o'> <a href='?myfarm&water_all'>Полить все</a></div>";
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]' AND `id_plant` = '0' AND `cultivator` = '1'"),0)!=0 && $ku['seeder']>0)echo "<div><img width='16' height='16' src='images/icons/seat.png' alt='o'> <a href='?myfarm&seat_all'>Засадить все</a></div>";
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]' AND `id_plant` = '0' AND (`time` = '$time' OR `time` < '$time') AND `cultivator` = '0'"),0)!=0 && $ku['cultivator']>0)echo "<div><img width='16' height='16' src='images/icons/dig.png' alt='o'> <a href='?myfarm&cultivator_all'>Вскопать все</a></div>";
echo "<ul>";

////ГРЯДКИ


$q=mysql_query("SELECT * FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]'");
while($post=mysql_fetch_array($q))
{
echo "<li>";
if($post['id_plant']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"),0)!=0)$plant=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$post[id_plant]'"));

echo "<li><img width='48' height='48' src='".(!isset($plant)?"".($post['cultivator']==1?"images/dirt_new.jpg":"images/dirt2_new.jpg")."":"images/plants/$plant[id].png")."' alt='o' class='portrait'>";
echo "<div>";
echo "".(!isset($plant)?"<span class='title'>".($post['cultivator']==1?"Земля, готовая к посеву":"Необработанная земля")."</span>":"<span  class='title'>".hsc($plant['name'])."</span>".($post['time']>$time?"<span class='small minor'> (Урожай через <span> ".time_left($post['time']-$time)."</span>)</span>":null)."")."";
echo "</div>";
echo "<div>";
if(isset($plant))
{
if($post['time']<=$time)
{
echo "<img width='16' height='16' src='images/icons/harvest.png' alt='o'> <a href='?myfarm&harvest=$post[id]'>Собрать урожай</a>";
}
else
{
if($ku['last_soil']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_soil` WHERE `id` = '$ku[last_soil]'"),0)!=0)$ls=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_soil` WHERE `id` = '$ku[last_soil]'"));
echo "<div class='small minor'>".($post['time_soil']<=$time && $post['soil']==0 && $post['time']>$time?"<img width='16' height='16' src='images/icons/soil.png' alt='o'> ".(isset($ls)?"<a href='?myfarm&soil=$post[id]&select=$ls[id]'>Применить
<span>".hsc($ls['name'])."</span></a>, <a href='?myfarm&soil=$post[id]'><span>выбрать
удобрение</span></a>":"<a href='?myfarm&soil=$post[id]'>Удобрить</a>")."":"".($post['soil']==0 && $post['time']>$time?"Удобрение через ".time_left($post['time_soil']-$time)."":null)."")."</div>";
echo "<div class='small minor'>".($post['time_water']>$time && $post['time']>$time?"Полив через ".time_left($post['time_water']-$time)."":"".($post['time']>$time?"".($post['soil']==0 && $post['time_soil']>$time || $post['soil']==1?"<img alt='' width='16' height='16' src='images/icons/water.png'> <a href='?myfarm&water=$post[id]'>Полить</a>":null)."":null)."")."</div>";
}
}
else
{
echo "<span>";
if($ku['last_plant']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$ku[last_plant]'"),0)!=0)$lp=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$ku[last_plant]'"));
echo "".($post['cultivator']==1?"<img width='16' height='16' src='images/icons/seat.png' alt='o'> ".(isset($lp)?"<a href='?myfarm&seat=$post[id]&select=$lp[id]'>Посадить <span>".hsc($lp['name'])."</span></a>, <a href='?myfarm&seat=$post[id]'><span>выбрать семена</span></a>":"<a href='?myfarm&seat=$post[id]'>Посадить</a>")."":"<img width='16' height='16' src='images/icons/dig.png' alt='o'> <a href='?myfarm&cultivator=$post[id]'><span style='title'>Вскопать</span></a>")."";
echo "</span>";
}
if(isset($plant))unset($plant);
echo "</div><div style='clear:both'></div></li>";
}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]'"),0)<20)echo "<li><img width='48' height='48' src='images/dirt_new.jpg' alt='o' class='portrait'><div><span class='title'>Новая земля</span></div><div><img width='16' height='16' src='images/icons/cart.png' alt='o'> <a href='?myfarm&new_ground'>Купить</a> за <span class='title'><img width='16' height='16' src='images/icons/ruby.png' alt='o'><span>".cpg(mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_ground` WHERE `id_user` = '$ku[id]'"),0))."</span> рубинов</span></div><div style='clear:both'></div></li>";
//echo "</ul>";
if($ku['last_plant']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_plant` WHERE `id` = '$ku[last_plant]'"),0)!=0)$lp=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_plant` WHERE `id` = '$ku[last_plant]'"));
if($ku['last_soil']!=0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `kolhoz_soil` WHERE `id` = '$ku[last_soil]'"),0)!=0)$ls=mysql_fetch_array(mysql_query("SELECT * FROM `kolhoz_soil` WHERE `id` = '$ku[last_soil]'"));
echo "<ul class='ptm'><li><img width='16' height='16' src='images/icons/seat.png' alt='o'> <a href='?myfarm&change_plant'>Сменить растение</a>: <span>".(isset($lp)?"".hsc($lp['name'])."":"-")."</span></li>";
echo "<li><img width='16' height='16' src='images/icons/soil.png' alt='o'> <a href='?myfarm&change_soil'>Сменить удобрение</a>: <span>".(isset($ls)?"".hsc($ls['name'])."":"-")."</span></li>";
echo "".($ku['time_cp']>$time?"<li><img width='16' height='16' src='images/icons/cross.png' alt='o'> До бесплатной очистки: <span>".time_left($ku['time_cp']-$time)."</span></li><li><span><img width='16' height='16' src='images/icons/tick.png' alt='o'> <a href='?myfarm&clean_grounds'><span>Очистить грядки</span></a>(<span>Стоимость</span>: <span><img width='16' height='16' src='images/icons/ruby.png' alt='o'> <span
class='title'>".($ku['count_cp']*10)."</span></span>)</span></li>":"<li><img width='16' height='16' src='images/icons/cross.png' alt='o'> <a href='?myfarm&clean_grounds'>Очистить грядки</a></li>")."</ul></div>";
//echo "</div>";
include_once 'inc/foot.php';
?>