Jump to content

Updating Resources (Text Based Game)


Deviants

Recommended Posts

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

Link to comment
Share on other sites

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.

:-) 

 

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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); 
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.