Jump to content
MakeWebGames

Recommended Posts

Posted
<?php

session_start();

$user = $_SESSION['username'];

//open my database
$connect = mysql_connect('_______', '___', '____') or die('Couldn\'t connect!');
mysql_select_db('__________');


$query = mysql_query("SELECT * FROM `castle wars users` WHERE username = '$user'");


$username = mysql_fetch_array($query);


echo 'Logged user has: '.$username['money'].' money';


//money amount to add for each round
$add_money = 1;
//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 (($username['lastupdate']+$round) <= $time_now) {
//see how many rounds (hours) were there from last update
$nr_rounds = floor(($time_now-$username['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 `castle wars users` SET lastupdate=lastupdate+'$add_time', money=money+'$all_money' WHERE id = '$username[id]'") or die(mysql_error());

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


?>

 

OK now as soon as I log in I get so much money! How can I make so I get 1 dollar every 5 seconds.

Posted

You have basically 2 solutions:

1) You store the last time somebody requested a page (or the last time you gave the money), and if it's over the period you want to give, then you will divide the time by the period, and give X times the money, and store the remaining unused time in the time field. (Not sure I'm really clear here :P)

2) Second solution (and honestly not the smartest one), you use a cron which add money every X min to your players. However this will use CPU and DB even for players inactive.

Posted
OK now as soon as I log in I get so much money! How can I make so I get 1 dollar every 5 seconds.

please tell me your joking, if you updated every active users money every 5 seconds they are online you would use your sites bandwidth in no time. People are obsessed with that mafia wars... Think more of a longer time period with a larger amount of money would seem more logical.

(1 * 60) * 15 900 money per 15 minutes seems more logical and less of a strain(i do know he said 1 buck every 5 seconds im just doing a example...)

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