adicrst Posted November 15, 2008 Share Posted November 15, 2008 Something bugs me for some time and i want to know how could i create PHP script that does this: Every game (i'm talking about browser based games) have HP (health points). Let's say that the max HP is 10. From some reasons, the user's HP drops to 5, so he has only 5 more left. Now, my question is how could i make a script that every minute that passes, his HP to increase by 1 point. So if 2 minutes pass, his HP increases from 5 to 7, but it doesn't go beyond 10 (that is maxHP). I have the fallowing idea: using CronJob, make a script like this to execute itself every 1 minute: * extract all user's HP that is < then maxHP * add 1 to the previous value * update the database with the new HP value Doesn't this technique stress the database, considering that i make every minute hundred of queries if i have hundred of members in the game ? How could i find another solution ? Something based on the user, like everytime he refreshes his profile to know what was his last activity time and what is the current time and to make a difference and see how many minutes have passed and add to his HP the amount of points, but not to go over the maxHP. But how do i save his last activity time and the time he makes a refresh (refresh means he made another activity) and see how much points to add ? Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/ Share on other sites More sharing options...
ratcateme Posted November 15, 2008 Share Posted November 15, 2008 you could put this in a cron job UPDATE `table_name` SET `HP` = `HP` + 1 WHERE `HP` < 10 that will update every row in one query. i don't know about your game but if you did it every time someone views there profile. i am guess there is some kind of screen that list all users and there HP so you would need to do it here and there are properly lots of other places where this would be the case a cron job looks like th best way to me. Scott. Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-690614 Share on other sites More sharing options...
n3ightjay Posted November 15, 2008 Share Posted November 15, 2008 I'm thinking more along the lines of javascript-> settimeout() to an Ajax call that would update maybe a session variable and or write to the database ??? Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-690626 Share on other sites More sharing options...
adicrst Posted November 15, 2008 Author Share Posted November 15, 2008 well, i thought at something like this: to have a field in the user's table and keep the last time his HP have been decreased. This can happen only in some small and precise circumstencies like battle. So a code in the pages responsible for this to update that time. Another code in some pages like profile, battle, etc that extract that time and compares it with the server's current time and makes a difference. Let's say the HP decreasing was at 10:00 and now the time is 10:05. A code that does 10:05-10:00=5 => curent HP + 5 and updates the new value PS: i don't need the code, just ideas Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-690629 Share on other sites More sharing options...
haku Posted November 15, 2008 Share Posted November 15, 2008 You can force the user's browser to re-load a script every minute using this: header('Refresh: 60; url=index.html'); and replace index.html with the script. Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-690686 Share on other sites More sharing options...
jordanwb Posted November 15, 2008 Share Posted November 15, 2008 UPDATE `table_name` SET `HP` = `HP` + 1 WHERE `HP` < 10 Scott. You can do that? Sweet. Thanks Scott. Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-691022 Share on other sites More sharing options...
.josh Posted November 16, 2008 Share Posted November 16, 2008 UPDATE `table_name` SET `HP` = `HP` + 1 WHERE `HP` < 10 Scott. You can do that? Sweet. Thanks Scott. That's fine and dandy if all the players have 10hp and can never have more. I would have a separate column called "regen" or something and have the cronjob inc it for everybody every 1m. Then when an individual user's hp is queried (user logs in, fights, etc..whenever the script calls for it to be displayed/used), update hp to include regen amount (hp+regen but not more than maxhp), and reset the individual user's regen back to 0. Link to comment https://forums.phpfreaks.com/topic/132798-how-could-i-do-this-give-me-some-ideas/#findComment-691057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.