<?php

/*** Freedom City | Check RPGBUNNY.COM for more Full RPG Sources | www.rpgbunny.com ***/ini_set('memory_limit', '200M');
set_time_limit(300); // 5 minutes

if(!empty($_SERVER['SERVER_SOFTWARE'])) {
   echo 'Cron can be called only from shell!'; exit;
}
$db_host="localhost"; $db_user="username"; $db_pass="password"; $db_name="database";
$db_id = mysql_connect($db_host, $db_user, $db_pass)
or die("Could not connect to DB.");
mysql_select_db($db_name, $db_id)
or die("Database not found.");

$ora=date("H");

if($ora=='00'){
   //gestione bonus
   $query="SELECT * FROM bonus where days='0'";
   $result=mysql_query($query, $db_id);
   $num_row = mysql_num_rows($result);
   if($num_row>0){
      for ($i=0;$i < $num_row; $i++){
         $row=mysql_fetch_assoc($result);
         if($row['type']=='ebp'){
            $query="update users set level=1 where id=".$row["id_user"];
            mysql_query($query, $db_id);
            $query="delete from bonus where id=".$row['id'];
            mysql_query($query, $db_id);
         }else{
            //gestire altri bonus
            $query="delete from bonus where id=".$row['id'];
            mysql_query($query, $db_id);
         }
      }
   }
   $query="update bonus set days=days-1";
   mysql_query($query, $db_id);
}


$query="SELECT * FROM bonus where type!='ebp'";
$result=mysql_query($query, $db_id);
$num_row = mysql_num_rows($result);
if($num_row>0){
   for ($i=0;$i < $num_row; $i++){
      $row=mysql_fetch_assoc($result);
      $query="select resources,limits,production from towns where id=".$row['id_town'];
      $resultt=mysql_query($query, $db_id);
      $rowt=mysql_fetch_row($resultt);

      $pro=split("-", $rowt[2]);
      $res=split("-", $rowt[0]);
      $lim=split("-", $rowt[1]);
      $lim_r=$lim[1];
      $lim_oro=$lim[2];
      if($row['type']=='pl'){
         $res[1]+=$pro[1]*0.1;
         if($res[1]>$lim_r)$res[1]=$lim_r;
      }
      if($row['type']=='pp'){
         $res[2]+=$pro[2]*0.1;
         if($res[2]>$lim_r)$res[2]=$lim_r;
      }
      if($row['type']=='pf'){
         $res[3]+=$pro[3]*0.1;
         if($res[3]>$lim_r)$res[3]=$lim_r;
      }
      if($row['type']=='po'){
         $res[4]+=$pro[4]*0.1;
         if($res[4]>$lim_oro)$res[4]=$lim_oro;
      }
      $resources=$res[0].'-'.$res[1].'-'.$res[2].'-'.$res[3].'-'.$res[4];
      $query="update towns set resources='$resources' where id=".$row['id_town'];
      mysql_query($query, $db_id);
   }
}
?>