dnyce
dnyce

Reputation: 415

How should I store user levels for a site wide point system? in the DB? or on the fly?

I am in the process of building a user experience/level system on my site. The effect I am trying to achieve is similar to that of the popular game site, Kongregate, whereby when a user completes particular actions on the site they are awarded points. Once the points reach a certain amount the user will level up. This has the effect of giving the site a game like feel, while also grabbing the user's attention.

The problem I'm having is figuring out if it's best to... 1. Store each level in a database table and then match the user up to that level via an ID? 2. OR store the user's experience for the actions completed and then summing that number up everytime I need to call on the level?

It should also be noted that the level will be used to figure out other aspects of this system. i.e. A user at level 10 will gain additional schwag for their profile, added functionality on the site, etc... A LA Stack O.

Any pointers on the proper direction to take?

Upvotes: 0

Views: 249

Answers (2)

Steve Nguyen
Steve Nguyen

Reputation: 5974

I think it's best to determine the "level" everytime the user gets more points. So when he / she does something to get more points, do the calculation then and there. If the point warrants a level increase, then change the level_id within the user's profile. That way you minimize calculations on the server side.

Upvotes: 0

rockerest
rockerest

Reputation: 10508

  • Store the levels and their associated "swag"/"privileges" in the database.
  • Store the user's current level on their profile in the database.
  • Store the user's actions that result in points in the database

When they add points from your site (by commenting, or playing a game or whatever), calculate it on the fly, but once it's definitely done adding up, store it in the database (in the actions table) and update their total points (in the user table or whatever) so it's available to other parts of the site.

This makes everything easier:

  • when you want to see what privileges they have it's just a matter of comparing their total to the next lowest (or equal) "swag" level
  • when you want to get their total it's a single call instead of a bunch of queries and math
  • when you want to show a list of what they've done you have it right in the database
  • You won't duplicate code by adding up their total on every different type of page that needs to show the total.
  • ...probably tons of other reasons. data is in the name of the tool for a reason.

Upvotes: 3

Related Questions