flixbeat Posted December 27, 2015 Share Posted December 27, 2015 (edited) 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 December 27, 2015 by flixbeat Quote Link to comment Share on other sites More sharing options...
Coly010 Posted December 27, 2015 Share Posted December 27, 2015 (edited) 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 December 27, 2015 by Coly010 1 Quote Link to comment Share on other sites More sharing options...
flixbeat Posted December 27, 2015 Author Share Posted December 27, 2015 thanks coly, you've been helpful to me since my very first post. :) Quote Link to comment Share on other sites More sharing options...
KyleMassacre Posted December 27, 2015 Share Posted December 27, 2015 You can look into this post as well for the timer: http://makewebgames.io/forum/general-development-central/non-mccode-game-support/28821-count-down-timer?p=278701#post278701 That should provide you with a base for it Quote Link to comment Share on other sites More sharing options...
Aventro Posted December 27, 2015 Share Posted December 27, 2015 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. 2 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.