• 15
• 15
• 11
• 9
• 10

how to do this ? Give me some ideas

This topic is 3416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 ? (PS: in case someone didn't understand what i meant, i'm talking about a regeneration rate of 1 point/minute)

Share on other sites
update PLAYER set HP = HP + 1 where HP < MAXHP

1 query, not very burdensome. And ideally it wouldn't be a cron job, it'd just be a process (or part of a process) that sat there in a game loop doing updates.

Share on other sites
ya, i know the query, but i want to know if i should do this every minute for the whole game, considering i have hundreds of players => hundreds of queries per minute for the SQL server, OR a beter solution, to upgrade a specific account only when the user is making an action. Now, how do i know when a user is doing something ? (refresh, navigate, fight with other player, etc). How do i record the last activity time and the current time (considering the user is doing something right now)

Share on other sites
Update it passively, i.e.: figure out how much HP was recovered in the time since the last time the program accessed the player's stats. i.e:

int Player::GetHP(){	my_hp = my_hp + floor(now() - last_time); // where now() returns seconds elapsed since program start	if ( my_hp > my_max_hp )		my_hp = my_max_hp;	last_time = now();	return my_hp;}

Something like that, but only in whatever language you're using... :|

Share on other sites
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

[Edited by - adicrst on November 15, 2008 12:58:58 AM]