Jump to content

What seems to be an easy percentage problem actually seems impossible


UndeadCircus

Recommended Posts

Hey everyone,

 

I'm new to the forums, but not quite new to PHP...Except it feels like I'm a COMPLETE n00b. I have quite an interesting problem here.. I'll try to explain it in as much depth as I can without it being too confusing.

 

I'm developing an online game and I'm trying to get a progress bar that updates based on what a user's experience points are. I have a table called "levels" setup which is setup so that auto-increment "id" column is the actual level the user can obtain. In this table, I have levels_minfans and levels_maxfans. The rows inserted into this table look something like this:

 

levels_id: 1

levels_minfans: 0

levels_maxfans: 74

 

levels_id: 2

levels_minfans: 75

levels_maxfans: 149

 

levels_id: 3

levels_minfans: 150

levels_maxfans: 299

 

Now, what happens is if the user has anywhere between 0 and 74 fans, he is at level 1. When they hit 75 fans up to 149 fans, they are level 2 and so on. Well, I'm trying to get my static progress bar to display the percentage between these two values. Such as if they are level three, I need it so that the progress bar is at zero when they have 150 fans, and increment up to 100 as they hit 299 fans. So, in short, 150 fans really equals zero percent, and 299 fans equals 100 percent.

 

I think that might be in depth enough to understand the problem. I've been dealing with this crap ALL day today and no matter what combination of math formulas I try, I can't seem to get the progress bar to reset at zero everytime a new level is hit and the player has the minimum number of fans for that level and then top out at 100 when they reach the max fans for that level.

 

Does all of this make sense?

 

If so, I'd appreciate any help I can get from you guys. Thank you so much for reading this - hope I can get an answer soon! :)

 

Ricky

Link to comment
Share on other sites

I have a table called "levels" setup which is setup so that auto-increment "id" column is the actual level the user can obtain.

 

Although this may seem like a smart move it also has some drawbacks, for example: if you want to replace an existing levels name (from a back-end) suddenly those that are level 3 are level-less.

 

Now, what happens is if the user has anywhere between 0 and 74 fans, he is at level 1. When they hit 75 fans up to 149 fans, they are level 2 and so on. Well, I'm trying to get my static progress bar to display the percentage between these two values. Such as if they are level three, I need it so that the progress bar is at zero when they have 150 fans, and increment up to 100 as they hit 299 fans. So, in short, 150 fans really equals zero percent, and 299 fans equals 100 percent.

 

(progress - levels_minfans) / levels_maxfans - levels_minfans * 100

 

Edit: damn Daniel0 beat me to it.

Edit2: I forgot to incalculate the player's progress

 

Man.. I was really hoping that would work because it seemed like such an easy answer.. But still, no such luck. :( I appreciate your reply though! Maybe you have another idea?

 

Multiply by 100 to get a percentage.

Link to comment
Share on other sites

Man.. I was really hoping that would work because it seemed like such an easy answer.. But still, no such luck. :( I appreciate your reply though! Maybe you have another idea?

 

How doesn't it work?

 

Take level 2 for instance:

min = 75; max = 149

 

current = 75 => progress = (75-75)/(149-75) = 0.0 = 0%

current = 112 => progress = (112-75)/(149-75) = 0.5 = 50%

current = 149 => progress = (149-75)/(149-75) = 1.0 = 100%

 

Multiply by 100 to get a percentage.

 

0.1 = 10%

0.1 * 100 = 10% * 100 = 1000%

 

:P

Link to comment
Share on other sites

Thanks a MILLION to you guys! I actually ended up going with Daniel0's answer, I just had to add an extra "*100" to the end of the equation and it balanced everything out. Holy crap.. All day I've been working on this and it comes down to a very simply formula. Words can't describe how thankful I am to you guys right now, lmao.

 

@ignace: Yea, about the database levels.. I knew it could cause some issues like that down the road, but it was the most practical way I could think of in dealing with user's levels later down the road when it comes to the way the game is handling leveling up, etc. But I appreciate the feedback nonetheless. I'll definitely keep what you said in mind! :)

 

Thanks again guys.. Major props to you for the help.

Link to comment
Share on other sites

Okay, so let me explain why it works...

 

Basically you're trying to figure out how far the user is from getting to the next level. For each level you have the minimum required fans. This is acting as an offset, so you essentially just need to subtract this from the other parameters.

Link to comment
Share on other sites

Yea, after I plopped the code in and tweaked it a little, I started to study it to find out exactly what's going on.. And it makes me feel kind of senseless knowing that it was so easy. But then again, being exausted and completely braindead from staring at code all day will have impacts on your brain that push out even the most remedial of tasks, haha. But again I say - thanks a ton. I can sleep soundly tonight knowing one of the biggest aspects of this game are now complete haha.

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.