Best way to code Achievements system Best way to code Achievements system database database

Best way to code Achievements system


I've implemented a reward system once in what you would call a document oriented database (this was a mud for players). Some highlights from my implementation, translated to PHP and MySQL:

  • Every detail about the badge is stored in the users data. If you use MySQL I would have made sure that this data is in one record per user in the database for performance.

  • Every time the person in question does something, the code triggers the badge code with a given flag, for instance flag('POST_MESSAGE').

  • One event could also trigger a counter, for instance a count of number of posts. increase_count('POST_MESSAGE'). In here you could have a check (either by a hook, or just having a test in this method) that if the POST_MESSAGE count is > 300 then you should have reward a badge, for instance: flag("300_POST").

  • In the flag method, I'd put the code to reward badges. For instance, if the Flag 300_POST is sent, then the badge reward_badge("300_POST") should be called.

  • In the flag method, you should also have the users previous flags present. so you could say when the user has FIRST_COMMENT, FIRST_POST, FIRST_READ you grant badge("NEW USER"), and when you get 100_COMMENT, 100_POST, 300_READ you can grant badge("EXPERIENCED_USER")

  • All of these flags and badges need to be stored somehow. Use some way where you think of the flags as bits. If you want this to be stored really efficiently, you think of them as bits and use the code below: (Or you could just use a bare string "000000001111000" if you don't want this complexity.

$achievments = 0;$bits = sprintf("%032b", $achievements);/* Set bit 10 */$bits[10] = 1;$achievements = bindec($bits);print "Bits: $bits\n";print "Achievements: $achievements\n";/* Reload */$bits = sprintf("%032b", $achievments);/* Set bit 5 */$bits[5] = 1;$achievements = bindec($bits);print "Bits: $bits\n";print "Achievements: $achievements\n";
  • A nice way of storing a document for the user is to use json and store the users data in a single text column. Use json_encode and json_decode to store/retrieve the data.

  • For tracking activity on some of the users data manipulated by some other user, add a data structure on the item and use counters there as well. For instance read count. Use the same technique as described above for awarding badges, but the update should of course go into the owning users post. (For instance article read 1000 times badge).


UserInfuser is an open source gamification platform which implements a badging/points service. You can check out its API here:http://code.google.com/p/userinfuser/wiki/API_Documentation

I implemented it and tried to keep the number of functions minimal. Here is the API for a php client:

class UserInfuser($account, $api_key){    public function get_user_data($user_id);    public function update_user($user_id);    public function award_badge($badge_id, $user_id);    public function remove_badge($badge_id, $user_id);    public function award_points($user_id, $points_awarded);    public function award_badge_points($badge_id, $user_id, $points_awarded, $points_required);    public function get_widget($user_id, $widget_type);}

The end result is to show the data in a meaningful way through the use of widgets. These widgets include: trophy case, leaderboard, milestones, live notifications, rank and points.

The implementation of the API can be found here: http://code.google.com/p/userinfuser/source/browse/trunk/serverside/api/api.py


Achievements can be burdensome and even more so if you have to add them in later, unless you have a well-formed Event class.

This segues into my technique of implementing achievements.

I like to split them first into 'categories' and within those have tiers of accomplishment. i.e. a kills category in a game may have an award at 1 for first kill, 10 ten kills, 1000 thousand kills etc.

Then to the spine of any good application, the class handling your events. Again imagining a game with kills; when a player kills something, stuff happens. The kill is noted, etc and that is best handled in a centralized location, like and Events class that can dispatch info to other places involved.

It falls perfectly into place there, that in the proper method, instantiate your Achievements class and check it the player is due one.

As building the Achievements class it is trivial, just something that checks the database to see if the player has as many kills as are required for the next achievement.

I like to store user's achievements in a BitField using Redis but the same technique can be used in MySQL. That is, you can store the player's achievements as an int and then and that int with the bit you have defined as that achievement to see if they have already gained it. That way it uses only a single int column in the database.

The downside to this is you have to have them organized well and you will likely need to make some comments in your code so you will remember what 2^14 corresponds to later. If your achievements are enumerated in their own table then you can just do 2^pk where pk is the primary key of the achievements table. That makes the check something like

if(((2**$pk) & ($usersAchInt)) > 0){  // fire off the giveAchievement() event } 

This way you can add achievements later and it will dovetail fine, just NEVER change the primary key of the achievements already awarded.