1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. To chat with the GameOgre community, you need to have at least 15 posts. Once you have the 15 posts, post at Become A New Ogre
    Dismiss Notice

Cron job

Discussion in 'Online Game Development' started by rtkwar, Nov 21, 2011.

Thread Status:
Not open for further replies.
  1. rtkwar

    rtkwar Member

    Messages:
    139
    Likes Received:
    0
    Trophy Points:
    16
    Credit:
    - 54.14
    if iwant turn in my game increase every hour how ican make that but idon't want use cron job because ican't understand it iwant other way?

     
  2. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    You want it every hour like clockwork? then the only way to do it, is a cronjob

    another way to do it, is keep your browser open 24 hours every day and autorefresh that page each hour

    If you have a lot of visitors you could also let the update script run in the background on the login page, then whenever a user logs in, things get updated, but you run the risk that it is not done at regular intervals.

    Lets face it, cronjob is the way to go.

    For some tutorials:

    How to Create a Cron Job (Scheduled Task) for Your Website or Blog (thesitewizard.com)
    http://drupal.org/cron
     
  3. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    but if you only want to update turns, then I really don't see why you need a cronjob.

    Here is how you let the turns update:

    STEP 1
    When a user logs in, have a php script check the current time

    STEP 2
    you check the difference in seconds between the current time and the time that the game started (we call this your homemade timestamp).

    STEP 3
    you fetch from a sql table the time in seconds since timestamp, from the last time a user logged in

    STEP 4
    you check the difference in seconds between the stored time variable and the current time variable

    STEP 5
    you update your sql table with the new time in seconds from your homemade timestamp

    STEP 6
    see step 4,
    if the difference is more than 3600 seconds between the stored and current variable, you update the turns with whatever extra turns a player gets after 1 hour
    if the difference is more than 7200 seconds between the stored and current variable, you update the turns with whatever extra turns a player gets after 2 hours
    etcetera etcetera

    the way to do that is to divide the difference by 3600 and round downwards

    STEP 7
    add as much turns as hours have passed



    why 3600 do you ask?

    well, there are 60 seconds in a minute and 60 minutes in an hour, so that makes 60 * 60 = 3600 seconds = 1 hour

    why round downwards?

    well, you want to update when it is more than 3600 seconds difference, not less, because if it is less then not a full hour has gone by
     
    Last edited by a moderator: Nov 21, 2011
  4. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    a short part of a php script with a similar way to solve your problem

    Code:
    //FETCHING GAME START TIMES
    $staresult = mysql_query ("SELECT * FROM Startdate");
    $starow = mysql_fetch_array($staresult);
    $stadays = $starow['Day'];
    $stamonths = $starow['Month'];
    $stayear = $starow['Year'];
    
    //MAKE HOMEMADE TIMESTAMP
    $gamestart = mktime(0, 0, 0, $stamonths, $stadays, $stayear);
    
    //CURRENT TIMESTAMP
    $today = time();
    
    //seconds passed
    $rawdifference = $today - $gamestart;
    
    //hours passed since homemade timestamp
    $timedifference = floor($rawdifference / 3600);
    
    //fetching the hour when the user last logged in
    $llresult = mysql_query ("SELECT * FROM Lastlogin WHERE username ='$cookiename'");
    $llrow = mysql_fetch_array($llresult);
    $llhours = $llrow['Lastlogininhours'];
    
    //calculate hours passed since last login
    $hourspassed = $timedifference - $llhours;
    
    //if 1 or more hour has indeed passed
    if ($hourspassed >=1){
    
    $addedturns = $hourspassed * $extraturns;
    mysql_query("UPDATE Lastlogin SET Lastlogininhours ='$timedifference' WHERE username ='$cookiename'");
    
    //fetch old turns
    $turnresult = mysql_query ("SELECT * FROM Turns WHERE username ='$cookiename'");
    $turnrow = mysql_fetch_array($turnresult);
    $oldturns = $turnrow['Playerturns'];
    
    //calculate new turns
    $newturns = $oldturns + $addedturns;
    
    mysql_query("UPDATE Turns SET Playerturns ='$newturns' WHERE username ='$cookiename'");
    
    }
    
    
    
     
    Last edited by a moderator: Nov 21, 2011
  5. rtkwar

    rtkwar Member

    Messages:
    139
    Likes Received:
    0
    Trophy Points:
    16
    Credit:
    - 54.14
    thanks for that iwas use this way but isolve problem now thanks
    but your code is large ican use your code if iwant make game like travian but thanks iwas search from code like this




     
  6. rtkwar

    rtkwar Member

    Messages:
    139
    Likes Received:
    0
    Trophy Points:
    16
    Credit:
    - 54.14
    iwant update players rank in datebase every 12 hours but ican't understand cron job
     
  7. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    in that case you'd have to change 3600 with 43200 which corresponds to 12 hours
     
    Last edited by a moderator: Nov 22, 2011
  8. rtkwar

    rtkwar Member

    Messages:
    139
    Likes Received:
    0
    Trophy Points:
    16
    Credit:
    - 54.14
    yes but thats not work because iuse ranks in war
    example: officer can't attack on general and general can't attack on officer so ineed update all ranks in datebase every 12 hour
     
  9. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    well that's no problem,

    when one user logs in, loop through all the users with a while loop and then use an
    if-else construction on the data obtained from the loop to check the rank and then update accordingly

    and if the rank is in a different table, use a join, and then the while loop

    tutorial about loops:
    http://www.w3schools.com/php/php_looping.asp

    the only difference with what you want to do is use a while on the results from the sql query

    like so:

    Code:
    $result = mysql_query("SELECT * FROM Users");
    while ($row = mysql_fetch_array($result))
      {
    
    
      }
    
     
    Last edited by a moderator: Nov 24, 2011
  10. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    oh one more thing I remembered:

    when the user starts the script in the background, because he or she logged in first after the hour, then you may want to switch an sql cell to another boolean with an update, and switch it back to default after the while loop is finished. That is to avoid concurrency errors! Better yet is to temporarily lock tables, but this is more complicated.

    Concurrency errors is what happens when two users start the same part of the script at exactly the same time. For short running scripts on a single row this concurrency issue will generally not be a problem, but with long loops on lots of data it can potentially be a problem (though it depends on chance and how many users you have at certain hours of the day).
     
    Last edited by a moderator: Dec 2, 2011
  11. rtkwar

    rtkwar Member

    Messages:
    139
    Likes Received:
    0
    Trophy Points:
    16
    Credit:
    - 54.14
    ok, iwill try solve this problem but ihave problem now with economy system in my game
     
  12. shugo

    shugo Active Member Ogre Veteran

    Messages:
    2,092
    Likes Received:
    10
    Trophy Points:
    38
    Credit:
    14,250.21
    well I know lots of stuff about economy, so if you're stuck with it, you know where to find me
     
Thread Status:
Not open for further replies.

Share This Page