Graphically displaying player stats as a %

let's put our heads together here!

Page 1 of 1

14 Replies - 737 Views - Last Post: 06 November 2008 - 10:18 PM Rate Topic: -----

#1 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Graphically displaying player stats as a %

Posted 06 November 2008 - 05:01 PM

Hey guys,

for my new game project which I have posted about here, I would like to graphically display certain statistics as a percent.

I came up with one method. Please critique it and let me know if you come up with something better!

Let's say our character has a CURRENT_HP and MAX_HP.

He is currently sitting at 90/100, meaning 90% of his total HP. The method I came up with seems taxing on the server, especially since the game will be online and will HOPEFULLY have many people playing at one time.

The game will do CURRENT_HP/MAX_HP and store it into a temporary variable.
I could have 100 images, an hp bar showing hp from 0 to 100%. In a massive (100 case) if-statement, display one of the images based on current hp percentage.

Again, this is the only practical, albeit slow, solution I have come up with.

Is This A Good Question/Topic? 0
  • +

Replies To: Graphically displaying player stats as a %

#2 Syntaxide  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 05:16 PM

You have the right idea using percents, but why not do this:
Generate percent
Scale a rectangle based on percentage. This way, its more dynamic and accurate.

Example:
GUI
-------
[__________] <<100%
[_____] <<50%


So you multiply the percentage by the maximum width of the bar.
Was This Post Helpful? 0
  • +
  • -

#3 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 05:27 PM

How about something like this?

<?php

/*******************************
 Suppose I just had 100 health.
 But then you hit me for some
 random amount of damage. 
 This is how I would do it
********************************/

$damage = $_POST['damage']; // Get the damage
$_SESSION['your_health'] -= $damage; // Calculate current HP after damage
for ($i=1; $i<$_SESSION['your_health']; $i++) /* Display appropriate amount of health units on screen */
{
	echo "<img src='health_bar_unit.gif' />";
}


?>

Was This Post Helpful? 0
  • +
  • -

#4 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 05:36 PM

View PostSyntaxide, on 6 Nov, 2008 - 04:16 PM, said:

You have the right idea using percents, but why not do this:
Generate percent
Scale a rectangle based on percentage. This way, its more dynamic and accurate.

Example:
GUI
-------
[__________] <<100%
[_____] <<50%


So you multiply the percentage by the maximum width of the bar.



You bring up a cool alternative. Basically, I don't want to make a run-of-the-mill "oh ive seen this before" type of web game, I wanted to make a cutting-edge game that can be played by people of all ages with fantastic depth without the need for any real downloadable C++ application. Yea, it may be dated technology, but with fantastic graphical maps and features, I could interest people in playing it.

I can do something like what you said, but stretch it a bit more:
[__________] << 100%
[_________] << 90%
[________] << 80% (I didn't wanna worry so much about HEX color here so excuse the weird greens) :D
<< Etc..

I believe this text would also load MUCH faster than graphical images.


Anyone else have anything to contribute?

View PostMoonbat, on 6 Nov, 2008 - 04:27 PM, said:

How about something like this?

<?php

/*******************************
 Suppose I just had 100 health.
 But then you hit me for some
 random amount of damage. 
 This is how I would do it
********************************/

$damage = $_POST['damage']; // Get the damage
$_SESSION['your_health'] -= $damage; // Calculate current HP after damage
for ($i=1; $i<$_SESSION['your_health']; $i++) /* Display appropriate amount of health units on screen */
{
	echo "<img src='health_bar_unit.gif' />";
}


?>


Displaying 100 units of health seems taxing. Wouldn't this hurt the overall server performance if I am doing lots of database calculations at once?

I notice you are using sessions here to work with user variables. What if someone closes their browser?

How are we gonna send the session variables back to the database to "save" unless you do saving calculations ALL THE TIME?

I can really benefit from knowing this, thanks Moonbat.
Was This Post Helpful? 0
  • +
  • -

#5 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 05:48 PM

The user will be logged in, so therefore we use $_SESSION variables. Even if the user closes his browser, you can change how long a session lasts. How do you think forums can implement features like "Log me in automatically"? It's because they play with the session timing and change it so that it doesn't end if you choose that option.

Also, battles are temporary states. You should never use a database to keep track of something like a battle. If you had to constantly check and update your health from a DB you would be making TONS of queries, and that would tax your database heavily, especially if many people are battling. That's why you use sessions for stuff like that.

I doubt it will take very long to echo 100 little .gif images of this size:

[-]

Use a loop on a small image and try it yourself. I guarantee it won't make much of a difference, even if you had hundreds of users doing it. Using a huge switch statement would not only take longer, but it would be a very long piece of code as opposed to my three lines.

You can still show the numeric value next to the health units, to show the total health so people don't have to count it out or anything :P
Was This Post Helpful? 0
  • +
  • -

#6 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 06:20 PM

View PostMoonbat, on 6 Nov, 2008 - 04:48 PM, said:

The user will be logged in, so therefore we use $_SESSION variables. Even if the user closes his browser, you can change how long a session lasts. How do you think forums can implement features like "Log me in automatically"? It's because they play with the session timing and change it so that it doesn't end if you choose that option.

Also, battles are temporary states. You should never use a database to keep track of something like a battle. If you had to constantly check and update your health from a DB you would be making TONS of queries, and that would tax your database heavily, especially if many people are battling. That's why you use sessions for stuff like that.

I doubt it will take very long to echo 100 little .gif images of this size:

[-]

Use a loop on a small image and try it yourself. I guarantee it won't make much of a difference, even if you had hundreds of users doing it. Using a huge switch statement would not only take longer, but it would be a very long piece of code as opposed to my three lines.

You can still show the numeric value next to the health units, to show the total health so people don't have to count it out or anything :P


Yes you are correct Moonbat.

I think now that I weigh my options of little gif images or [_____], I think I prefer [______], nothing against you my little batty friend! :^: :^:

I am still unsure on two things, one of which is off-topic so I'll just add that to the end this reply.
I understand how to use sessions, but I'm still unsure of at what moments I SHOULD save to database.

Some moments to UPDATE query would be:
-Any changes in a shop (selling/buying)
-Moving item from inventory to auction house

Unsure of things like:
-Moving to a new zone (I think I would save the user's new MAP location to database so others can see WHO is also on the map with them at that current moment -- it would check not only who is on that map in database, but also if they are logged in, logged_on = 0 when not on)
-Leaving combat (do we save user's experience to database yet?)
-Signing off of the website or closing browser - How does the game know to save all of the current session variables to database if the browser was closed? I need some real instructing on this! Thanks



-------------------------------------------------
My other question really should be left for another time, so you dont need to take the time to answer.

Lets say we take that example of having an image map to display the game. The image map has 3 links (as buildings). These links lead to

game.com/game/game.php?loc=1
game.com/game/game.php?loc=2
game.com/game/game.php?loc=3

These 3 links reloaded the page and would change our location in the browser and in the game (in database)

Let's say I have 2 maps (image maps) and depending on where you click, you move to the new image map. This can be done by using an if-statement.

If (map == "town1") {
	  //echo in the display of town1 image map ONLY
}


My question is this:
Is there a way for me to move around ONLY to the available maps by clicking the current image map (borders) without having an experience player just TYPE ?map=town1 or ?loc=6, etc..

By this I mean have the page refresh just as normal but without showing the code in the url.
Wasn't there a technique to do this with POST instead of GET or something? Or am I stuck because POST is for forms only?

Was This Post Helpful? 0
  • +
  • -

#7 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 07:06 PM

I don't have enough experience with Javascript imagemaps and stuff, so I'll work on your database question.

Sessions should be used to hold anything that will regularly be changing. Hitpoints, for example, will change very often in battle. It will be pointless to store HP in a database when you will end up regularly changing it through multiple queries. Another thing that you can use with sessions is location on a map.

Databases should hold things that won't change very often, like level, class, etc.

What I would do is something like this. This process is known as Moonbat's Elaborate Database-Session Game Creation Paradigm:

Quote

1. User logs in
2. All info is taken from database and put in session vars
3. Everything is done with sessions
4. When user logs out, all session info is put into the database
5. When user logs in, the info you stored in the DB is put back into sessions and the process repeats itself

This post has been edited by Moonbat: 06 November 2008 - 07:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 07:12 PM

Thanks moonbat, but I said I understood that.

Login, take values from DB and store as session variables, use session vars until logout.

My question was specifically, what if the user doesnt log out? What if the user closes his browser?

Do I really want my players to lose all their data from neglect to logout? What if it isn't neglect, but a browser that stopped responding while he/she was doing something else?
Was This Post Helpful? 0
  • +
  • -

#9 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 07:31 PM

If it was my game, I would make them stay logged in until they manually log out. I guess that's me.

But to answer your question, I think you can use the PHP function connection_aborted() in an if-else statement to see if the user closed his connection or not. If he did, then just run the code to store the session stuff in the database.
Was This Post Helpful? 0
  • +
  • -

#10 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 07:41 PM

Moonbat you are wonderful! I will check that out, but I think you are definitely right on that. If the user doesn't want to properly log out, they shouldn't get to save their data.

In the few cases where this would matter (putting an item in the auction house for example), it will do a manual save there so items can't be duplicated!

As for the location question, I think I came up with an answer (boy was that straining on the brain). Perhaps you could take a look at it since you seem to be at least mildly interested in this project of mine :)

the only way to do this seems to be by using a old_loc and new_loc variable system.

something like.. at the beginning of the code, check to see if the location is valid before displaying the next map.


//set new location to loc from browser
?loc=5.......... $new_loc = $_GET['loc']; ...........old_loc = 5


//check if new_location is legit by using the game's map system (this is where im stuck)
if (($new_loc == 1) && (($old_loc == 2) || ($old_loc == 3) || ($old_loc == 4)) {
//show image map of location 1, because you are allowed to move to Map 1 FROM map 2, //map 3, and map 4
// set old_loc as a session_variable of the current map (1)
}

if ($new_loc == 1) 
{
	if (($old_loc == 2)) || ($old_loc == 3) || ($old_loc == 4)) 
	{
		 //show image map of location 1, because you are allowed to move to Map 1 FROM map 2,		  //map 3, and map 4
			// set old_loc as a session_variable of the current map (1)
	}
}



My problem is this: where can I set old_loc so that it correct retains the previous location when the page refreshes? Im thinking old_loc needs to be set to a session_variable once the if-statement checks out.

Basically, not sure how to take a variable and set it as a session variable, or if this method will work.

This post has been edited by Xioshin: 06 November 2008 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 brandon99337  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 167
  • Joined: 14-February 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 07:42 PM

This is pretty much a big problem of what I need help on too so I'll just join in!

How would you be able to get the session variables into the db if he the connection was closed? Even using connection_aborted() you would only know if the client disconnected, wouldn't the session variables be lost before you could do anything about it?

And being logged in until you manually log out would help lot's, it takes like one click. I just want to know if the above is possible.
Was This Post Helpful? 0
  • +
  • -

#12 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 08:23 PM

View Postbrandon99337, on 6 Nov, 2008 - 06:42 PM, said:

This is pretty much a big problem of what I need help on too so I'll just join in!

How would you be able to get the session variables into the db if he the connection was closed? Even using connection_aborted() you would only know if the client disconnected, wouldn't the session variables be lost before you could do anything about it?

And being logged in until you manually log out would help lot's, it takes like one click. I just want to know if the above is possible.

Oh, I haven't realized that. I feel kinda stupid now :P

Well then, hmm...

You could use a Javascript event to see if the user is closing the browser, but that event will also keep happening if the user navigates to a different page. If you plan to keep the whole game running on one page you could do this :D

Otherwise, you'll have to just manually save the user's info to the database at the end of every page, which defeats the purpose of using the sessions in the first place.

Oh well, I can't help here. All I suggest is that you make sessions last forever unless the user logs off manually. Sorry :(
Was This Post Helpful? 0
  • +
  • -

#13 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 08:28 PM

No your input is very much appreciated here.

I will come up with a way to save properly.

If you get the chance, please scroll up and check out that code I came up with for making sure players can't "warp" once they know the maps. When brandon posted, you must have missed it :P


Thanks my cave-dwelling friend!
Moonbats dont hide in caves, they soak up the moon's rays, so I guess that doesn't work ;_;

This post has been edited by Xioshin: 06 November 2008 - 08:29 PM

Was This Post Helpful? 0
  • +
  • -

#14 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 34
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 09:38 PM

Well, I need to know exactly how your are going to structure your maps so I can think of a solution. I'm not sure what you mean by "warping maps". I assumed that the whole world would be on just one map. Are you going to have like different worlds or something?
Was This Post Helpful? 0
  • +
  • -

#15 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: Graphically displaying player stats as a %

Posted 06 November 2008 - 10:18 PM

Lol no.. there will be multiple maps. but these maps have links in them to bring you to new areas.

Dont worry about maps if this sounds too complicated.

Lets say we had to links right on the page.
(loc = location)
one ends with .php?loc=1 and the other, .php?loc=2

If we are "on the map" or in the game world at loc = 3, we want the user to be able to get to loc=1 and loc=2 (which is why we have the links, or in the real case, links inside the image map)

My proposal was to combat someone on the map in the game world loc = 5 from typing IN THE URL loc=1 "to warp" or get to a map he is NOT supposed to be able to get to.

I think my code that I posted a few replies ago would solve this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1