Jump to content
MakeWebGames

How can I use unix timestamp for daily cronjobs


Recommended Posts

My host have cronjobs but the they wont work,

I'm using unix timestamp for the 1min & 5min.

But my daily one is broken (cronjob)

so I'm going to use unix_timestamp instead, but the code is a bit more complicated..

So here's my 1min unix timestamp code:

<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$result = mysql_query("SELECT * FROM `updates` WHERE `name` = '1min'");
$result = mysql_fetch_assoc($result);
$hla = time() - $result['last'];

if($hla > (60))
{
$hphp = $ir['maxhp'] / 6.6;
$n = floor($hla / 60);
$hospital = (1 * $n);
$jail = (1 * $n);
$hp = ($hphp * $n);
$val = (1 * $n);

$pt = (1 * $n);
$cht = (1 * $n);

$query_hos = sprintf("UPDATE users SET hospital = hospital - %u WHERE hospital <> 0", $hospital);
$query_jail = sprintf("UPDATE users SET jail = jail - %u WHERE jail <> 0", $jail);
$query_hp = sprintf("UPDATE users SET hp = hp + %u WHERE hp <> maxhp", $hp);
$query_val = sprintf("UPDATE users SET validation = validation - %u WHERE validation <> 0", $val); 
$query_prot = sprintf("UPDATE users SET protection = protection - %u WHERE protection <> 0", $pt);
$query_canh = sprintf("UPDATE users SET canhire = canhire - %u WHERE canhire <> 0", $cht);

mysql_query($query_hos);
mysql_query($query_jail);
mysql_query($query_hp);
mysql_query($query_val);
mysql_query($query_prot);
mysql_query($query_canh);

mysql_query("UPDATE `users` SET `hospital` = 0 WHERE `hospital` < 0");
mysql_query("UPDATE `users` SET `jail` = 0 WHERE `jail` < 0");
mysql_query("UPDATE `users` SET `hp` = `maxhp` WHERE `hp` > `maxhp`");
mysql_query("UPDATE `users` SET `validation` = 0 WHERE `validation` < 0");
mysql_query("UPDATE `users` SET `protection` = 0 WHERE `protection` < 0");
mysql_query("UPDATE `users` SET `canhire` = 0 WHERE `canhire` < 0");

$time = time();
mysql_query("UPDATE `updates` SET `last` = ".$time." WHERE `name` = '1min'");
$floor = $time - (floor($time / 60) * 60);


if($floor > 0)
{
$newUpdate = time() - $floor;
mysql_query("UPDATE `updates` SET `last` = ".$newUpdate." WHERE `name` = '1min'");
}
}
?>

and here's my cron_day.php

<?php
/*
MCCodes FREE
crons/cron_day.php Rev 1.1.0
Copyright (C) 2005-2012 Dabomstew

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

require_once "mysql2.php";
require_once "global_func.php";

$cron_code = 'xxxxxxxxxxxxxxxxxx';
if ($argc == 2)
{
   if ($argv[1] != $cron_code)
   {
       exit;
   }
}
else if (!isset($_GET['code']) || $_GET['code'] !== $cron_code)
{
   exit;
}
mysql_query("UPDATE `fedjail` SET `fed_days` = `fed_days` - 1", $c);
mysql_query("UPDATE `users` SET `turns` = 100", $c);
$q = mysql_query("SELECT * FROM `fedjail` WHERE `fed_days` <= 0", $c);
$ids = array();
while ($r = mysql_fetch_assoc($q))
{
   $ids[] = $r['fed_userid'];
}
mysql_free_result($q);
if (count($ids) > 0)
{
   mysql_query(
           "UPDATE `users` SET `fedjail` = 0 WHERE `userid` IN("
                   . implode(",", $ids) . ")", $c);
}
mysql_query("DELETE FROM `fedjail` WHERE `fed_days` <= 0", $c);
mysql_query("UPDATE `users` SET `dreward` = 1", $c);
mysql_query("UPDATE `users` SET `datotal` = 0", $c);

mysql_query("UPDATE users SET canrate = 1 WHERE donatordays = 0", $c);
mysql_query("UPDATE users SET canrate = 2 WHERE donatordays > 1", $c);

mysql_query("UPDATE users SET noobprotection=noobprotection-1 WHERE noobprotection > 0", $c);

mysql_query("UPDATE users SET sbban=sbban-1 WHERE sbban > 0",$c);
mysql_query("UPDATE users SET daysingang=daysingang+1 WHERE gang > 0");
$user_update_query =
       "UPDATE `users` SET 
        `daysold` = `daysold` + 1,
        `mailban` = `mailban` - IF(`mailban` > 0, 1, 0),
        `donatordays` = `donatordays` - IF(`donatordays` > 0, 1, 0),
        `cdays` = `cdays` - IF(`course` > 0, 1, 0),
        `bankmoney` = `bankmoney` + IF(`bankmoney` > 0, `bankmoney` / 50, 0),
        `cybermoney` = `cybermoney` + IF(`cybermoney` > 0, `cybermoney` / 100 * 7, 0)";
mysql_query($user_update_query, $c);
$q =
       mysql_query(
               "SELECT `userid`, `course` FROM `users` WHERE `cdays` <= 0 AND `course` > 0",
               $c);
$course_cache = array();
while ($r = mysql_fetch_assoc($q))
{
   if (!array_key_exists($r['course'], $course_cache))
   {
       $cd =
               mysql_query(
                       "SELECT `crSTR`, `crGUARD`, `crLABOUR`, `crAGIL`, `crIQ`, `crNAME`
    				     FROM `courses`
                        WHERE `crID` = {$r['course']}", $c);
       $coud = mysql_fetch_assoc($cd);
       mysql_free_result($cd);
       $course_cache[$r['course']] = $coud;
   }
   else
   {
       $coud = $course_cache[$r['course']];
   }
   $userid = $r['userid'];
   mysql_query(
           "INSERT INTO `coursesdone` VALUES({$r['userid']}, {$r['course']})",
           $c);
   $upd = "";
   $ev = "";
   if ($coud['crSTR'] > 0)
   {
       $upd .= ", us.strength = us.strength + {$coud['crSTR']}";
       $ev .= ", {$coud['crSTR']} strength";
   }
   if ($coud['crGUARD'] > 0)
   {
       $upd .= ", us.guard = us.guard + {$coud['crGUARD']}";
       $ev .= ", {$coud['crGUARD']} guard";
   }
   if ($coud['crLABOUR'] > 0)
   {
       $upd .= ", us.labour = us.labour + {$coud['crLABOUR']}";
       $ev .= ", {$coud['crLABOUR']} labour";
   }
   if ($coud['crAGIL'] > 0)
   {
       $upd .= ", us.agility = us.agility + {$coud['crAGIL']}";
       $ev .= ", {$coud['crAGIL']} agility";
   }
   if ($coud['crIQ'] > 0)
   {
       $upd .= ", us.IQ = us.IQ + {$coud['crIQ']}";
       $ev .= ", {$coud['crIQ']} IQ";
   }
   $ev = substr($ev, 1);
   mysql_query(
           "UPDATE `users` AS `u`
               INNER JOIN `userstats` AS `us` ON `u`.`userid` = `us`.`userid`
               SET `u`.`course` = 0{$upd}
               WHERE `u`.`userid` = {$userid}", $c);
   event_add($userid,
           "Congratulations, you have completed the {$coud['crNAME']} course and gained {$ev}!",
           $c);
}
mysql_free_result($q);

I'm not sure how I can make the cron_day work properly, if anyone could help me or start me off I would really appreciate it..

There's atleast one person playing every day, so I was wondering if this could be simple-ised (lol) Instead of making it * $n it could just only execute once a day?

How can I make the code only execute once per day using timestamps? thanks.

Link to comment
Share on other sites

You could alter this version, however;

  • Set up a database table to store unix timestamps and filenames
  • Include the cron files into a global file (that is run with every request)
  • In the cron file, pull the unix timestamp from the database table you created earlier, and see if it's time to update (use simple math)
  • If you have run the crons, because it was time to update, update the unix timestamp value in the database table

 

I hope that helps.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...