Deviants Posted November 21, 2008 Share Posted November 21, 2008 Alright, well I want to create a way to update the resources of a player... lets say Money every 1 minute. How would I go about doing this? I've never delt with a time stamp before. I have a column of a table for Money which is set to 0 as default... so how would I make it add lets say.... 1,000 to it every 1 minute Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Every one minute would be a lot of load on the server. I'd probably do something hourly at most. What you'd want to do is run a cron job every hour that just gives everyone x amount of gold. Quote Link to comment Share on other sites More sharing options...
Deviants Posted November 21, 2008 Author Share Posted November 21, 2008 Okay, it was just a test lol at 1 minute... It will probably be around 10 minutes... Anyways, each player should gain a different amount of money each 10 minutes, is there a way to make a cron job for that? Quote Link to comment Share on other sites More sharing options...
Deviants Posted November 21, 2008 Author Share Posted November 21, 2008 I found this on a different forum and I thought this might work, but could someone elaborate more on this? I see these people coding 'turn based games' all the time, no idea how they work, but if a turn is every 30 minutes why not just look at the value returned by time(), then subtract the timestamp of when the first turn took place and then divide by the amount of seconds in a half hour, then floor() it, make this a function and call it to see what turn it's on. This way you only store the timestamp of each users first turn in the DB, no cron jobs, and mySql can focus on more important things, you also don't have to worry about users not getting their 'turns' incremented right away (if your update script takes 5 minutes to run the last users will experience delayed update times) I guess if your database isn't that large your way isn't an issue, but if your site gets too big you will need a more efficient way. :-) Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Let's say you wanted to give everyone +1% more money every hour (every 10 minutes is still quite a heavy load on your server; you'd need multiple databases to stop serious lag if your db gets big, imo. To give everyone +1%: <?php $query = "UPDATE players SET money = (money) + (money * .01)"; $res = mysql_query($query) or die("SQL Error: " . mysql_error() . "<br />Query: $query"); ?> Quote Link to comment Share on other sites More sharing options...
Deviants Posted November 21, 2008 Author Share Posted November 21, 2008 But they would have to be updated differently depending on how much they gain per 10 minutes. So instead of being multiplyed by .01 it would have to be multiplied by a column in the database for each user. How could I do this? Quote Link to comment Share on other sites More sharing options...
xangelo Posted November 21, 2008 Share Posted November 21, 2008 You would have to first run a select statement to get the increase amount from the system. After that you would then run the query again. Since I don't really know your schema, here's just some pseudo-code $query = mysql_query('SELECT playerid,updateAmnt from players'); if($query) { while($r = mysql_fetch_array($query)) { $update = 'UPDATE players SET money = (money)+'.$r['updateAmnt'].' WHERE playerid = '.r['playerid']; mysql_query($update); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.