create automated money

الكاتب: admin ,فى تاريخ: ديسمبر-11-2009 , أضف تعليق
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:

PHP Code:
//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

PHP Code:
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

Code:
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.

أضف تعليق

You must be logged in to post a comment.