Jump to content

How could i do this ? Give me some ideas


adicrst

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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