Friday, September 14, 2007

Basic Strategy game - automated hourly money gain

Today I will try to explain how to create automated money gain (each hour)
Last time we created really basic system, so now it is time to upgrade it.

Main idea is that user gains some amount of money every hour (starting from his registration time). So theory is like this:

  • registration time (Unix timestamp - in seconds from 1970)
  • check in table when user money was last updated (row: lastupdate)
  • if this time is greater then 1 hour, then we update money
lets open our config.php file (which we will include_once in all our other documents). At bottom add:
//money amount to add for each round
$add_money = 1000;
//time now, and round time in seconds (1h=60*60seconds=3600)
$time_now = time();
$round = 3600;

//time of last update+time needed must be smaller then time now to update
if (($user[lastupdate]+$round) <= $time_now) {
//see how many rounds (hours) were there from last update
$nr_rounds = floor(($time_now-$user[lastupdate])/$round);
//calculate how much money user gained
$all_money = $nr_rounds * $add_money;
//calculate how many rounds in seconds (how many hours in seconds)
$add_time = $nr_rounds * $round;

//lets update users table
mysql_query("UPDATE users SET lastupdate=lastupdate+'$add_time', money=money+'$all_money' WHERE id = '$user[id]'") or die(mysql_error());

//here you can refresh page (so you can see progress) or select user again using login

You would say....oh my god it is so simple to make games! Well actually you need really good plan, then coding part is easy, although it needs time.

So in this lesson we learned how to add money to user every time he login. Script just checks for last update, and if there should be new update it does it. It adds money and it changes time of last update.
In next post I will explain how to allow users to spend their money to build and level-up economical structure (Bank), each level of Bank will add bonus of money gained each turn.

Feel free to comment and ask anything :)


boss said...

Nice work:) I'm trying to make sports manager game, but it's damn hard:) So maybe you'll try to do sports manager tutorial:) Thanks

kingout said...

really good work
you help me so much
thanks man

loopByte said...

it doesn't work