<?php



 function TimeSpent($sec) {

   $days = floor($sec / 86400);
   $hours = floor(($sec / 3600) - $days * 24);
   $minuts = floor(($sec - $hours * 3600 - $days * 86400) / 60);
   $seconds = $sec - ($minuts * 60 + $hours * 3600 + $days * 86400);

     if ($days != 0) $days = $days.' дн. ';
       else $days = NULL;
         if ($hours != 0) $hours = $hours.' ч. ';
           else $hours = NULL;
             if ($minuts != 0) $minuts = $minuts.' мин. ';
               else $minuts = NULL;

      if ($seconds == 0){ $seconds = NULL; }else{ $seconds = $seconds.' сек.'; }

      $sectoday = $days.$hours.$minuts.$seconds;
      return $sectoday;

  }


  $user_id=$user['id'];
 function new_user($id){
 return mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id`='".$id."'"));
 }
 

if(mysql_result(mysql_query("SELECT COUNT(*) FROM `fish_u` WHERE `id_u`='".$user_id."'"),0)==0){
mysql_query("INSERT INTO `fish_u`(`id_u`) VALUES('".$user_id."')");
mysql_query("INSERT INTO `fish_i`(`id_u`,`id_i`,`act`,`type`,`kolvo`) VALUES('".$user_id."','1','0','y','0')");
mysql_query("INSERT INTO `fish_i`(`id_u`,`id_i`,`act`,`type`,`kolvo`) VALUES('".$user_id."','8','0','l','20')");
mysql_query("INSERT INTO `fish_i`(`id_u`,`id_i`,`act`,`type`,`kolvo`) VALUES('".$user_id."','17','0','k','0')");
mysql_query("INSERT INTO `fish_i`(`id_u`,`id_i`,`act`,`type`,`kolvo`) VALUES('".$user_id."','27','0','n','10')");
}
function mes($mes){
echo "<div class='msg'><b>".$mes."</b></div>";
}
function ret($mes,$ms){
echo "<div class='foot'><a href='".$ms."'>".$mes."</a></div>";
}
$fish=mysql_fetch_array(mysql_query("SELECT * FROM `fish_u` WHERE `id_u`='".$user_id."'"));
echo "<div class='b'><img src='d.png' />".$fish['balls']." <img src='rubi.png' />".$fish['rub']." <img src='level.png' />".$fish['level']." <img src='exp.png' />".$fish['exp']."/".$fish['max_exp']." <img src='power.png' />".$fish['energy']."/".$fish['max_energy']."</div>";
#восстановление энергии
if($fish['energy']<$fish['max_energy']){
if($fish['last_update']<(time()-60*60) && ($fish['energy']+6)<=$fish['max_energy']){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+6, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 6 енергии');
}
elseif($fish['last_update']<(time()-60*50) && ($fish['energy']+5)<=$fish['max_energy']){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+5, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 5 енергии');
}
elseif($fish['last_update']<(time()-60*40) && ($fish['energy']+4)<=$fish['max_energy']){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+4, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 4 енергии');
}
elseif($fish['last_update']<(time()-60*30) && ($fish['energy']+3)<=$fish['max_energy']){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+3, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 3 енергии');
}
elseif($fish['last_update']<(time()-60*20) && ($fish['energy']+2)<=$fish['max_energy']){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+2, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 2 енергии');
}
elseif($fish['last_update']<(time()-60*10)){
mysql_query("UPDATE `fish_u` SET `energy`=`energy`+1, `last_update`='".time()."' WHERE `id_u`='".$user_id."'");
mes('Восстановлено 1 енергии');
}
}
#опыт
if($fish['exp']>=$fish['max_exp']){
mysql_query("UPDATE `fish_u` SET `max_exp`=`max_exp`*2,`max_energy`=`max_energy`+2,`level`=`level`+1 WHERE `id_u`='".$user_id."'");
mes('Вы достигли следующего уровня!');
}

@$l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='l'"),0);
@$n=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='n'"),0);
@$p=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='p'"),0);
@$nap=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);
@$t=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);

if($l!=0){
$y_l=mysql_result(mysql_query("SELECT `kolvo` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='l'"),0);
if($y_l<=0){
$y_l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='l'"),0);
mysql_query("DELETE FROM `fish_i` WHERE `id`='".$y_l."'");
}
}
if($n!=0){
$y_l=mysql_result(mysql_query("SELECT `kolvo` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='n'"),0);
if($y_l<=0){
$y_l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='n'"),0);
mysql_query("DELETE FROM `fish_i` WHERE `id`='".$y_l."'");
}
}if($p!=0){
$y_l=mysql_result(mysql_query("SELECT `kolvo` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='p'"),0);
if($y_l<=0){
$y_l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='p'"),0);
mysql_query("DELETE FROM `fish_i` WHERE `id`='".$y_l."'");
}
}if($nap!=0){
$y_l=mysql_result(mysql_query("SELECT `kolvo` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);
if($y_l<=0){
$y_l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='nap'"),0);
mysql_query("DELETE FROM `fish_i` WHERE `id`='".$y_l."'");
}
}if($t!=0){
$y_l=mysql_result(mysql_query("SELECT `kolvo` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='t'"),0);
if($y_l<=0){
$y_l=mysql_result(mysql_query("SELECT `id` FROM `fish_i` WHERE `id_u`='".$user_id."' AND `act`='1' AND `type`='t'"),0);
mysql_query("DELETE FROM `fish_i` WHERE `id`='".$y_l."'");
}
}


?>