/    Sign up×
Community /Pin to ProfileBookmark

XP & Rank… Holding both in the DB or calling rank based on XP…

Hello,

I am designing and RPG and as such, each account will have an associated rank based on the amount of XP they have accrued. What I am wondering is do I need to store both the XP and rank in my database and update the rank as and when the XP exceeds the value required for each rank or can I just store the XP and then define the rank based on the XP in a nice easy way…

Thank you for any help you can offer.

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — RPG? XP? You're storing grenades and OS'es?
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Role-playing game and experience points. You get awarded experience points for completing certain tasks and if your XP becomes high enough then you move on to the next rank (level). Does that make sense?
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — Of course it does!

See how easy it was to use proper English to describe your dilemma and get your point across quickly? This is not a game forum - it is a coding forum for those having difficulty with their code.

So - do you have some code that you have developed that you need help with, or do you just want someone to develop your algorithm for you?

As for your one question. Of course you need to store ranks and point values in your database. But just not both in the User table. Each User's record should hold their current points total. Their rank can then be looked up in the separate 'Rank' table as part of any query that needs such info. That way should you re-evaluate the relationship of points to rank you don't have to update every user's record, just the Rank table.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — No, I don't want you to do all my work for me, I was just looking for some tips as to how to approach this. That makes sense, I am not certain about how I wish to structure the ranks yet so being able to change will be useful. How would I determine the rank? I need to query the database along the lines of select rank from rank table such that XP > XP requirement for that rank but XP < XP requirement for the next rank. Is that the way to approach it and what SQL code would be relevant? Thanks again for your help.
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — Excellent approach! Both in your use of this forum and your thoughts about the algorithm. Working on the idea myself right now!
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Thank you on both counts, you have been so very helpful! I need to finish something else I am having problems with before I get to this, I thought I would ask about it in advance so I was prepared when it came to it. I may have to post about my other issue, I can't seem to fix it. Thanks again.
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — Here is the query I finally managed to get working. A bit more difficult than I thought and even my Google results weren't much help. If someone can improve upon this, take theirs!
[code=php]
// The Rank table has two cols - Rank & Points_needed
// where Points_needed is the min value to achieve that Rank.
//
$q = "select a.Rank, b.Points_needed from Ranks a
right join (select max(Points_needed) as Points_needed from Ranks
where Points_needed <= $pts) b
on b.Points_needed = a.Points_needed";
[/code]


Had to use the table twice to properly connect the rank column to the correct selected points_needed row, otherwise I kept getting the table's first row Rank value but the correct row's point value. If you need to retrieve other attributes than Rank after finding the right record based upon the current points value, add them as table a selections.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Wow, thank you so much. That is excellent!
Copy linkTweet thisAlerts:
@NogDogJun 08.2016 — If you expect there to be a solid, unchanging connection between XP and rank, then I think that from a data normalization viewpoint, it makes sense to just track XP and calculate rank on the fly. You might even create a DB view that calculates it for you (e.g. using ginerjm's approach), then you could query against that view instead of the actual table.

But if you see any significant probability that XP and rank might not always be in lock-step, then I might rethink all that.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — What do you mean by if XP and rank may not be in lock-step? Also, if either of you feel like troubleshooting HTML problems, I've posted in that sub-forum too - you guys both seem so knowledgeable.
Copy linkTweet thisAlerts:
@NogDogJun 08.2016 — Just speculating whether there could ever be a situation where two players with the same XP would have different ranks: maybe some game effect lowers (or raises) their rank, but you don't want it to lower their XP. If you don't foresee any such cases, then don't worry about it. Even if you do, it might still be handled by some other modifier stored in some table -- I just wanted to make sure you think it through before you lock yourself into something that you soon find is "wrong". Of course, requirement changes and resulting re-factoring are essentially unavoidable, but a little up-front thinking time can prevent some of it. ?
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Yes, I couldn't agree with your last sentiment more. I have been planning my new project out painstakingly on paper. Deciding what I wish to include, sketching out layouts etc. I am hoping that minimises how much I will have to go back and change. I didn't realise what you had meant but yes, I see what you're saying - fortunately there is no plans for any such system. Each rank will be at a specific XP level and above.
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — So long as you keep the code for the 'rank' calculation/determination in one place (function/method), any future changes should be easily implemented.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Would you mind helping me out with a problem with my HTML? If you have the time and wouldn't mind, could you email me at [email][email protected][/email] so I can tell you my problem without publishing the site publicly (I don't want 10 copies before I'm even finished). Thanks a lot.
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — Sorry but no can do. I see your post re: frames and I have never used them. In fact, I thought they were kind of frowned upon nowadays. Good luck.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — Any idea what the alternative is so I can look that up see if I can make it work?
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — I have to ask what your goal is that made you choose this method? Normally one designs a page (nowadays) with div tags and you can create the entire page using different blocks of code (included) that create the html and use passed in php vars for the dynamic content for each portion of your final page.
Copy linkTweet thisAlerts:
@NewbieDevauthorJun 08.2016 — It is the way I know to have one frame which you load different pages into that looks good. Doing it with DIVs seems to take very careful designing with the CSS and then copious amounts of PHP (at least by comparison, it seems there is a lot more code to achieve the same thing). According to: http://stackoverflow.com/questions/18259232/alternative-for-frames-in-html5-using-iframes (the first answer), I pretty much have to use iframes for what I want to do. Another answer says I could use frameset if I have my index in XHTML, I'm not sure if this is wise or if iframes are also quite easy to use.
Copy linkTweet thisAlerts:
@ginerjmJun 08.2016 — I dont' see how some absolute css wouldn't achieve the same layout as your frames method. Not the wisest but it does it. Of course relative layouts with percentage values should achieve the same thing as frames too. And I don't know at all what you could mean about copious amounts of PHP to create the divs that are replacing the html you are placing in the frames. Somethings wrong with your understanding.

Good luck again.
×

Success!

Help @NewbieDev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...