Author Topic: crons..etc  (Read 4415 times)

Offline Wakish

  • Level 14
  • *
  • Posts: 111
  • Reputation: +0/-1
    • View Profile
    • Wakish Wonderz
crons..etc
« on: June 27, 2007, 05:18:04 PM »
Does all text-based game need crons for stuffs like "bank-interest"..etc ?
Is there any other means to do this in PHP ?

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: crons..etc
« Reply #1 on: June 27, 2007, 08:14:54 PM »
There's always the way of checking the current time versus the time a certain action has been executed the last time. And if the time passed between those two is grater than let's say 24h, you do some stuff :D


For banking:
have a field called last_banked and put in the time an account was first opened. Now everytime the user visits any page (or only the bank page), take the current time and subtract the last_banked time. If this difference is greater than 24*60*60 (one day in seconds), update the interest and so on.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: crons..etc
« Reply #2 on: June 28, 2007, 04:19:07 AM »
Yeah, I think for a feature like bank interest, it's better to only update it when the user visits his bank, otherwise every day the user will get more money even if he doesn't play :P

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: crons..etc
« Reply #3 on: June 28, 2007, 06:28:59 AM »
Well, I sure as hell hope you don't check into the bank each day to make sure they're adding the interest to your balance :D

For this kind of task (bank, etc.) I would suggest the use of crons as it generalises the game time a bit. But of course, if you have a lot of players from different timezones, the above mentioned method might be more suitable to guarantee the same game-experience for every player.

Offline Wakish

  • Level 14
  • *
  • Posts: 111
  • Reputation: +0/-1
    • View Profile
    • Wakish Wonderz
Re: crons..etc
« Reply #4 on: June 28, 2007, 10:31:41 AM »
Coool, thanks for the info guys!

Offline Boozerbear

  • Level 3
  • *
  • Posts: 7
  • Reputation: +0/-0
  • Why should Kirk get all the hot green alien chicks
    • View Profile
    • Secret Society Wars
Re: crons..etc
« Reply #5 on: March 01, 2008, 01:29:42 PM »
Cron jobs are pretty easy to set up, and if you have PHP compiled as both a stand-alone executable and as an apache module, you can use the executable one and cron to run your PHP maintenance scripts from the command line. You definately should *not* have cron fire off a wget or lynx request to a web-accessible maintenance script, unless you like having clever people fire off your maintenance scripts at arbitrary times. :D

Offline greendots

  • Level 3
  • *
  • Posts: 8
  • Reputation: +0/-0
    • View Profile
Re: crons..etc
« Reply #6 on: May 16, 2008, 12:29:22 PM »
You can have a cron or do these things on each page load, but doing an action on each page load adds up heavily.  You have to figure in establishing a DB connection, running a (potential) query, doing the action, updating records and closing the connection.  Running a DB query is very quick, but there are a lot of overheads with the connection.

A cron will keep things regular, create a single DB connection rather than numerous, and it can run private scripts outside of your web directory also (security issues).  Well, your embedded php code can run scripts outside your web directory also, but its a huge security risk.  You also need root access (usually) to set up cron jobs.

Offline Kimmybwoy

  • Level 15
  • *
  • Posts: 124
  • Reputation: +2/-6
  • My Topics Are Random, gets more interest
    • View Profile
Re: crons..etc
« Reply #7 on: July 05, 2008, 09:20:00 AM »
Crons is important, Well If you want a game where brave dont refill , you cant tell how old someone is.. well yeah dont bovver with crons , but if you want the game to be playerble crons help ALOT

most hosts have a "Cron Job" sections

Offline Null

  • Level 1
  • *
  • Posts: 2
  • Reputation: +0/-0
    • View Profile
Re: crons..etc
« Reply #8 on: September 29, 2008, 03:42:25 PM »
Crons are easy to setup and yes they modify the daily or hourly or every minute things. Every game has one :)

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: crons..etc
« Reply #9 on: September 30, 2008, 02:31:16 AM »
You can do everything Cron does other ways ie Timestamps, Mysql 5.3+ Timed queries etc.

I have played several games that don't cron jobs and do pretty nice job.

Using timestamps gets less stressful to the server than huge cron jobs when there is over 5k players to be updated.


I can send you pics of my cocks if you want reference.


Offline Renex

  • Level 10
  • *
  • Posts: 58
  • Reputation: +0/-0
    • View Profile
    • Universal War
Re: crons..etc
« Reply #10 on: October 21, 2008, 04:48:23 AM »
I have a question for the programmers that saw the concept Travian uses. They have a count down (which is done in JavaScript) to tell the user how much time is left until his buildings are done.

My question is: how is this done in the backend because they have some buildings which are done in 3-4 minutes so there is a small probability they use cronjobs . Also think at the number of players they have: over  100k . I believe it's impossible to run crons at 1 minute speed with 100k players. So how do they update the info of the player?
Another thing: the action is not done with the JavaScript because the buildings continue to construct even the user logs out.
Could there be something like raestlyn with Mysql Timed queries and if yes how are those done?
Creator of www.universalwar.org - science fiction BBG with 1 minute and 1 hour ticks.
Creator of BBGUniverse.com - online web gaming directory, blog & forums
www.georgeolah.com - Website development, online marketing, SEO

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: crons..etc
« Reply #11 on: October 21, 2008, 06:58:31 AM »
Yes, they probably have a 'queue' table that details what is to be added and it supplies a timestamp at when it should be added. Then, whenever something is being performed that may be affected by anything that could be finished in the queue, it simply looks for anything with a timestamp less than the current timestamp and adds those items.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #12 on: October 21, 2008, 09:47:55 AM »
It would be nice if someone could create a tutorial or something about this, because I will be needing this if I have made a bid more progress in the stuff I'm coding right now.  ;D

Offline davidjwest

  • Level 15
  • *
  • Posts: 130
  • Reputation: +0/-0
    • View Profile
    • Iron-Empire
Re: crons..etc
« Reply #13 on: November 22, 2008, 02:04:40 PM »
My host has a cron tutorial:

http://support.34sp.com/crontab-manager

If anyone wants a new host, then I highly recommend this company, I've had a lot of hosts and these have been by far the best.  They are not the cheapest, but not expensive either and I have only ever had one 15 minute spell of downtime in 3 years (that I've noticed) and they respond to technical queries helpfully and quickly.

No, I'm not on commission.

 ;D

Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #14 on: November 22, 2008, 03:55:23 PM »
I acctually ment about countdown thing...

Offline Scion

  • Level 27
  • **
  • Posts: 402
  • Reputation: +11/-0
    • View Profile
Re: crons..etc
« Reply #15 on: November 24, 2008, 06:16:15 AM »
given the way they do battles, i dont think they actually do that....I think they have some way of scheduling jobs.....and probably re-use the same 'job scheduler' for calculating battles as they do for adding buildings....(KISS principle)

note this would mean the jobs get run at the scheduled time rather than on lookup....

Its also possible that a game as large as travian only uses the php for generating the game interface pages, and that the battle, building and resource calculation etc is handled differently using c/java/whatever.

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: crons..etc
« Reply #16 on: December 11, 2008, 08:05:47 AM »
It would be nice if someone could create a tutorial or something about this, because I will be needing this if I have made a bid more progress in the stuff I'm coding right now.  ;D
The discussion and
The Tick will give you an idea how to do it. If you want more info I can post here my update class when my laptop comes back from repair.


I can send you pics of my cocks if you want reference.


Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #17 on: December 11, 2008, 11:16:53 AM »
It would be nice if someone could create a tutorial or something about this, because I will be needing this if I have made a bid more progress in the stuff I'm coding right now.  ;D
The discussion and
The Tick will give you an idea how to do it. If you want more info I can post here my update class when my laptop comes back from repair.

Thanks. It would be nice if you could post your update class   :P. You're really the best  :-*.

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: crons..etc
« Reply #18 on: December 12, 2008, 10:11:25 AM »
Code: ( My update class) [Select]
<?php

abstract class update extends db{

function 
city($city){
$timenow time();
$stats=$this->get("city_production""name='$city'"); //I get everything in the database
foreach ($stats as $akey => $aval) { //lets assign them to the class
            
$this -> {$akey} = $aval;
        }
//foreach
if($this->farm>0){ //simple production building
$foodmulti=$timenow $this->time_food//we compare time between now and last update
$mod=($this->farm*2)-($this->population*0.01); //in this case we do a small adjustment to the producting amount, people have to eat after all. The $this->farm*2 shows how much food one farm is producing and $this->population*0.01 how much people are eating in one hour.
if ($mod!=0){ //we can't divide with zero after all
$foodmodi=3600/$mod//calculate how much we are producing in a hour. Time is in seconds so 60*60 seconds is an hour.
$food_amount=floor($foodmulti/$foodmodi); //here we calculate how many food has been produced since last update. 
$food_time=($food_amount*$foodmodi)+$this->time_food//Lets set new last update time. If we haven't produced the even number of foods we need to set it lower than time is now.
$food_amount+=$this->food//needed for my other buildings
} else{ $food_amount=$this->food$food_time=$timenow; } //if the modifier is 0 we are producing just enough food to keep everyone happy, so we don't need to update the amount
}else{ $food_amount=$this->food-($this->population*0.01); // We have no farms, so our food stock is getting smaller
$food_time=$timenow; } //set the time
//Other buildings removed, they are just the same as farm.
//ok, now for the building that uses food to produce horses!
if($this->stable>0){ //check do we have any stables
$horsemulti=$timenow $this->time_horse//same as farm above
$horsemodi=3600/($this->stable*2); //2 horses per hour per stable
$horse_amount=floor($horsemulti/$horsemodi); 
$horse_time=($horse_amount*$horsemodi)+$this->time_horse;
//ok, now begins the different things;
if($food_amount<$horse_amount*.1){ //if we don't have enough food to produce as many horses that the stables could  produce
$horse_amount=$food_amount*.1//we produce as many we can
$food_amount=0//we used all the food
$horse_time=$food_time;  //we set the time to the time when we last produced food
} else { $food_amount=round($food_amount-($horse_amount*.1));} //we have enough food for all the horses, so we need to calculate how much we are using.
$horse_amount+=$this->horse;
}else{
$horse_amount=$this->horse$horse_time=$time_now;}

$set_time$this->set("city_production""time_weapon='$weapon_time', weapon='$weapon_amount', time_food='$food_time', food='$food_amount',  time_horse='$horse_time', horse='$horse_amount', time_wood='$wood_time', wood='$wood_amount', time_lux='$lux_time', luxury='$lux_amount',time_stone='$stone_time',  stone='$stone_amount', time_gem='$gem_time', gem='$gem_amount'""name='$city'"); //update the db.


}//city
?>
There you go. I didn't post the whole class, because I want to keep it to myself but that should clear things up. I heavily commented it, but if you have any questions feel free to ask.
$this->(resource name) is amount of named resource the city has, and $this->time_(resource name) is the time last time it was updated. All times are UNIX timestamps.

Table Structure is simple, I have time_(resource name) (int(20)) for the latest update and (resource name) (int(11)) for the each resource.
$this->get and $this->set are just database queries.


I can send you pics of my cocks if you want reference.


Offline Sunchaser

  • Game Owner
  • Level 23
  • *
  • Posts: 296
  • Reputation: +3/-0
  • Game Owner
    • View Profile
    • Medieval Europe
Re: crons..etc
« Reply #19 on: December 12, 2008, 10:24:52 AM »
Raestlyn, Can you kindly post a link to your game? I would like to see it...

Thanks

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: crons..etc
« Reply #20 on: December 12, 2008, 10:28:49 AM »
The game that is for isn't ready yet. Its not in alpha stage yet. :(


I can send you pics of my cocks if you want reference.


Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #21 on: December 12, 2008, 11:09:31 AM »
Code: ( My update class) [Select]
<?php

abstract class update extends db{

function 
city($city){
$timenow time();
$stats=$this->get("city_production""name='$city'"); //I get everything in the database
foreach ($stats as $akey => $aval) { //lets assign them to the class
            
$this -> {$akey} = $aval;
        }
//foreach
if($this->farm>0){ //simple production building
$foodmulti=$timenow $this->time_food//we compare time between now and last update
$mod=($this->farm*2)-($this->population*0.01); //in this case we do a small adjustment to the producting amount, people have to eat after all. The $this->farm*2 shows how much food one farm is producing and $this->population*0.01 how much people are eating in one hour.
if ($mod!=0){ //we can't divide with zero after all
$foodmodi=3600/$mod//calculate how much we are producing in a hour. Time is in seconds so 60*60 seconds is an hour.
$food_amount=floor($foodmulti/$foodmodi); //here we calculate how many food has been produced since last update. 
$food_time=($food_amount*$foodmodi)+$this->time_food//Lets set new last update time. If we haven't produced the even number of foods we need to set it lower than time is now.
$food_amount+=$this->food//needed for my other buildings
} else{ $food_amount=$this->food$food_time=$timenow; } //if the modifier is 0 we are producing just enough food to keep everyone happy, so we don't need to update the amount
}else{ $food_amount=$this->food-($this->population*0.01); // We have no farms, so our food stock is getting smaller
$food_time=$timenow; } //set the time
//Other buildings removed, they are just the same as farm.
//ok, now for the building that uses food to produce horses!
if($this->stable>0){ //check do we have any stables
$horsemulti=$timenow $this->time_horse//same as farm above
$horsemodi=3600/($this->stable*2); //2 horses per hour per stable
$horse_amount=floor($horsemulti/$horsemodi); 
$horse_time=($horse_amount*$horsemodi)+$this->time_horse;
//ok, now begins the different things;
if($food_amount<$horse_amount*.1){ //if we don't have enough food to produce as many horses that the stables could  produce
$horse_amount=$food_amount*.1//we produce as many we can
$food_amount=0//we used all the food
$horse_time=$food_time;  //we set the time to the time when we last produced food
} else { $food_amount=round($food_amount-($horse_amount*.1));} //we have enough food for all the horses, so we need to calculate how much we are using.
$horse_amount+=$this->horse;
}else{
$horse_amount=$this->horse$horse_time=$time_now;}

$set_time$this->set("city_production""time_weapon='$weapon_time', weapon='$weapon_amount', time_food='$food_time', food='$food_amount',  time_horse='$horse_time', horse='$horse_amount', time_wood='$wood_time', wood='$wood_amount', time_lux='$lux_time', luxury='$lux_amount',time_stone='$stone_time',  stone='$stone_amount', time_gem='$gem_time', gem='$gem_amount'""name='$city'"); //update the db.


}//city
?>
There you go. I didn't post the whole class, because I want to keep it to myself but that should clear things up. I heavily commented it, but if you have any questions feel free to ask.
$this->(resource name) is amount of named resource the city has, and $this->time_(resource name) is the time last time it was updated. All times are UNIX timestamps.

Table Structure is simple, I have time_(resource name) (int(20)) for the latest update and (resource name) (int(11)) for the each resource.
$this->get and $this->set are just database queries.

Thanks !

Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #22 on: January 21, 2009, 10:39:11 AM »
Raestlyn, your example realy helped. I just have one question:

Do you use timestamp in your databse? If yes, how do you store it to the db?

Or are you just using INT or something?

And why would option one be better than option two?

I would be very much appriciated if you could clear that out for me  :), though this may be a rather noobish question.

Offline raestlyn

  • Level 29
  • **
  • Posts: 464
  • Reputation: +9/-5
    • View Profile
Re: crons..etc
« Reply #23 on: January 21, 2009, 02:28:17 PM »
Code: ( My update class) [Select]
<?php

$timenow 
time();

$foodmulti=$timenow $this->time_food//we compare time between now and last update

?>
....
Table Structure is simple, I have time_(resource name) (int(20)) for the latest update ...
As you can see, I store the timestamp last time the resource is updated in the DB. I really don't need the current timestamp there, because in next second its useless.

Did that answer your question?
« Last Edit: January 21, 2009, 02:31:27 PM by raestlyn »


I can send you pics of my cocks if you want reference.


Offline jannesiera

  • Level 35
  • **
  • Posts: 1,026
  • Reputation: +6/-1
    • View Profile
    • BBGameDesign
Re: crons..etc
« Reply #24 on: January 21, 2009, 03:57:02 PM »
Ow, now I see. I didn't see that about your table structure first. Okay, I think that would be alright then, thanks.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal