Time Progressive Increase of number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Time Progressive Increase of number

Post by 8ennett »

Hi guys,

I'm currently designing my first PHP MMORPG to help me practice learning PHP. I have already written a user login with registration which is stored in a MySQL db and uses a gd2 generated captcha image for security.

I'm just wondering though, the player has a current amount of hp and a max amount of hp, so when the current hp is say at 200hp(50%), I want it to add 50hp every five server minutes, just wondered if you had any ideas of the best method of doing this. Also, if I could create say like a status bar displaying the current percentage would anyone have any ideas of how i could do that too?

Thanks guys, just looking for a bit of help!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Time Progressive Increase of number

Post by califdon »

The most useful thing you can learn about PHP is that it is a server language. It preprocesses a script before the file is sent to the browser. Therefore, once the user sees the web page, PHP is no longer present! It cannot possibly do anything that depends on a user's action or the passage of time. That must be done with client language, namely Javascript. The Javascript may call another PHP script on the server. So to achieve what you want, think in terms of how to write the Javascript to measure the elapsed time.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

I can see how javascript would work for the status bar, but i can't see how it would help in incrementing a database integer if the page would be constantly changing, probably me just being an idiot but that's why i'm asking for help!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Time Progressive Increase of number

Post by califdon »

8ennett wrote:I can see how javascript would work for the status bar, but i can't see how it would help in incrementing a database integer if the page would be constantly changing, probably me just being an idiot but that's why i'm asking for help!
If you really need to update a database, the Javascript could use the XMLHttpRequest object and cause a PHP script on the server to do so, but generally that is probably not a workable design. The web and HTTP protocol is a different environment from a workstation. Once a page request has been sent to a requesting browser, there is no persistent connection between the server and the client. The server has no way of knowing whether a particular user is even still looking at the page, or indeed if the user has shut off his or her computer! So you generally have to design applications so that everything depends on the client browser sending another request to the server. "Constantly changing" data updating a database is usually to be avoided. If you have high web traffic, your database server will be very busy! I believe the usual practice is to re-think the application so that you don't need to store rapidly changing data. I'm not experienced at game designing, so that may be a different environment, but as a long-time database designer, I cringe at the thought of "constantly changing" data being stored in a database.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

So basically by having all users hp increase every 5 mins server time being run from the server side, not the client side, would result in my resources being depleted, that much I thought already! I realised it would have to come from a command implemented from the client but was looking for the best php coding method to achieve this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Time Progressive Increase of number

Post by califdon »

Stop and think about it: do you really want to be updating every user's records every 5 minutes, even if they haven't logged into your application in months??!! Unless you need to retain a value for the future (next week, next year), don't use a database at all. Probably you can do everything you want to do in Javascript, which has a timer function that can update a local variable in the browser. In case you have to show the values to other users, you'll have to get a little craftier, but perhaps you could store each user's "hp" in Session variables that could be accessed by a PHP script triggered from Javascript's XMLHttpRequest object at intervals.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

califdon wrote:Stop and think about it: do you really want to be updating every user's records every 5 minutes, even if they haven't logged into your application in months??!!
I did say I already understood this, but thanks for confirming.
califdon wrote:Unless you need to retain a value for the future (next week, next year), don't use a database at all. Probably you can do everything you want to do in Javascript, which has a timer function that can update a local variable in the browser. In case you have to show the values to other users, you'll have to get a little craftier, but perhaps you could store each user's "hp" in Session variables that could be accessed by a PHP script triggered from Javascript's XMLHttpRequest object at intervals.
I don't fully understand, could you show me an example?

If this method was used, what would happen if the user logged off and then logged back on to the system, wouldn't that automatically refill their hp, or not compensate for the time logged off and remain at the same hp until you log back in again and then the hp carries on increasing every 5 minutes without the extra you should have got in the time logged off?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Time Progressive Increase of number

Post by califdon »

8ennett wrote:If this method was used, what would happen if the user logged off and then logged back on to the system, wouldn't that automatically refill their hp, or not compensate for the time logged off and remain at the same hp until you log back in again and then the hp carries on increasing every 5 minutes without the extra you should have got in the time logged off?
The short answer is, what would happen is whatever you programmed to happen. Nothing is automatic. I can't give you an example because I don't know anything about gaming and I don't know what you want to happen, besides which I'm about to pack for a week's vacation. But I would recommend the following references:

Javascript timer ( setTimeout() and setInterval() ): http://www.elated.com/articles/javascri ... tinterval/

PHP Session variables: http://www.tizag.com/phpT/phpsessions.php

Ajax (Javascript + PHP): http://www.w3schools.com/Ajax/ajax_intro.asp
and http://www.xul.fr/en-xml-ajax.html

Good luck.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

Ok thanks, I will read up on this.

And I did attempt to explain it in a way that doesn't require knowledge of games, because basically this is a concept I have thought of myself and haven't yet seen it implecated (although there is no doubt it is in use).

Enjoy your holiday!




A better explanation;
VarA = 100%
VarB = 100%
User interaction causes VarA to become 10%
VarA = 10%
VarB = 100%
The current server time is 9:01

At 9:05 I want VarA to increase by 10% making it 20%. At 9:10 I want it to increase by another 10% and continue to do so every 5 minutes until it has reached the value of VarB.

Keeping in mind that the user may continue to change pages so one same page is not open constantly and may even log off, I still need VarA to be properly calculated when the user next logs on.

So if the user logs off at 9:08 and VarA is at 20%, then logs back in at 9:23. VarA should be at 50%.

I realise it wouldn't be productive to have every single users VarA increment by 10% every time the server passes 5 minutes. But also, the users that have been inactive for days would already have VarA=100% and VarB=100% as it would only take 50 minutes to refill VarA from 0%.

I just need a viable method to achieve the above, that's all. If anyone can help then I would be very grateful!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Time Progressive Increase of number

Post by onion2k »

Work out the number of minutes, divide it by 5, multiply it by 10 and add that number of points on.

Eg.. "So if the user logs off at 9:08 and VarA is at 20%, then logs back in at 9:23. VarA should be at 50%."

Do an SQL query to get the user's "last seen" time:

Code: Select all

SELECT UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`last_seen`) AS unix_last_seen FROM `user` WHERE `user`.`user_id` = 1
That'll put the number of seconds since the user did something into an alias called 'unix_last_seen'.

Code: Select all

 
define("MINUTES_PER_TICK", 5); //One 'tick' is a game event. Yours are 5 minutes apart..
 
$number_of_minutes = floor(($record->unix_last_seen)/60);
$ticks_since_last_seen = floor($number_of_minutes/MINUTES_PER_TICK);
$percent_health_regenerated = $ticks_since_last_seen*10;
 
Then just add it on with an UPDATE query, checking that it won't take the user over 100% of course.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

Seems like a sound method, I was just thinking of something fairly similar myself. Although your code is designed for unix systems, it's easily adaptable for windows. I would have to include additional calculations though as it will be monitoring the seconds also, otherwise it would be possible to just refresh the page that the custom function would be included in (which will be all of them!) and refill VarA repeatedly within the minute. I've got some kind of method based around what you have said slowly being worked out though so thank you!

As for the status bar physically displaying the current % of VarA, I've decided that an image replicated 100 times with IF statements before each one would be the best, so if VarA is at 40% then the first 40 duplicate-url images would be displayed and the other 60 would be a blank image of the same size!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Time Progressive Increase of number

Post by onion2k »

8ennett wrote:Although your code is designed for unix systems, it's easily adaptable for windows.
Err.. no it isn't. It's PHP and SQL. It'll work anyway.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

lol i just saw unix when skimming the code, nm.
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: Time Progressive Increase of number

Post by 8ennett »

I only starting learning PHP about 3 days ago and am already capable of achieving so much, I just want to say thanks to the guys who have helped me in my queries.

I already have a good working knowledge of how to program code, arrays, loops, structures, terminology, and so on, and have created amazingly complex codes with very basic programming languages (such as D-Script (darksigns.com, a very complex game which implemented the first network environment within a game for the later systems and is always adapting and upgrading, current level is astounding with darksigns and darksignsonline), I managed to create an entire casino, with images expanding beyond the usual capability of the normal scripting language which is what programming is all about) but since my son was born 2 years ago (and I've had to take care of him since birth on my own :() I've always thought about learning PHP but never got around to it. So any posts I make and you reply to, thank you very much in helping me to learn and adapt and to any new methods I haven't encountered previously!

Thanks guys! That's why i chose this forum and not the others, you aren't condesending and actually help people!
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Time Progressive Increase of number

Post by Stryks »

It's probably bad advice to give at this point, but my first thought in terms of an incrementing solution was to use a cron job. Just create a script to increment values by whatever where the current value is less than 100% and then set up a job to do it at whatever interval. It's just how I pictured it being done when I have looked at one of those games.

When i think about it now though, onion2k's way seems like a much better solution. The thing that complicates it is the fact that users can (depending on the game) interact on each other while offline. This requires each players stats to be up to date at all times, regardless of whether you are there to see it or not. Of course, if you think about it, the same process is all that is required, just triggered on any interaction, not just a specific users interaction.

Anyhow, for the status bar ... do yourself a favor and forget the 100 images thing. Why not create an image that is a set height and just 1px wide. Then in your image tag you can just work out the width needed to show the correct percentage scale and then set the image width parameter.

So, if the values were stored in the database as an int between 0 and 100 (0 being empty and 100 being full) and your status bar width is say, 200, you could say (($value/100) * 200). That should give you an image that is $value percent of the maximum width. You could use a second image placed after it with the width set to the remainder of the maximum width, or just create a div with a tiled background image representing the unused section of bar, and then insert the image using the width setting over the top aligned to the left.

I've never actually done it, but I'm thinking that a little tinkering would give you a nice clean status bar.

Good luck
Post Reply