Jump to content
MakeWebGames

+1 AP every minute?


flixbeat

Recommended Posts

Good day MWG, so once i again, i needed guidance with the pro's

I was thinking about a good way to increment the player's action points (AP) for every minute, as most of the text-based games has this feature. It shows the time/interval for AP regen, I'm not sure if it's javascript or ajax.

So i was thinking i were to use ajax like for example:

 

setInterval(function(){

$.ajax({
// send a request to database
// to add 1 to AP
});

},60000)

 

A pretty simple approach yet the user can easily change the interval to like 10, and he's AP will regen for every 10ms, and it's no good. Any better idea to properly do this? thanks :)

Edited by flixbeat
Link to comment
Share on other sites

Create a new column in the users table called last_AP . Give it an int(11) data type.

My SQL is rusty, if this is wrong someone correct it

ALTER TABLE users ADD last_AP int(11);

 

Next, at the bottom of your globals file add this:

 

$AP_Interval = 60;
if(time() - $ir['last_AP'] >= $AP_Interval){
   $db->query("UPDATE users SET ap=ap+1, last_AP=".time()." WHERE userid=".$userid);
}

 

This is the logic behind adding the code. It will keep it in tune across multiple pages as an Ajax timeout set at 60 seconds will only work for however long the player stays on the one page.

You can use this in your footer then

$timeLeft = 60 - (time() - $ir['last_AP']);
$timeLeft = $timeLeft > 0 ? $timeLeft : 0;
?>

 

MWG keeps messing up the last part of my code /:

Basically you'll use $timeLeft to set a setInterval(); to send a request to your PHP file. The backend prevents user from updating AP as frequently as they like, the setTimeout will let it happen without waiting for user to refresh page

Edited by Coly010
  • Like 1
Link to comment
Share on other sites

I would advice against updating your database every minute. Instead you should as Coly suggested have a timestamp in your database that you can later use to calculate on how much points the user should be given when it's actually requested (needed). If the player would want to do something that costs AP, first calculate on how much points they have gained since last it was updated, update the points and finally do the requested task. It will save you a lot of redundant database queries. This is how larger games such as Travian handles it. They pass the formula (x resources per second and how many resources you currently have) and then just updates it in the client, and when you make a request they do the server side processing updating your resources since last time it was updated and then your requested task if appliable, this is why sometimes when you update the web browser you can have more resources than what it actually says, but overall it is pretty much accurate.

  • Like 2
Link to comment
Share on other sites

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