database-designbadgereward-system

How to design a rewards system on my website


I want to try and design a reward system on a new website I am building. For example (for illustrative purposes only):

enter image description here enter image description here enter image description here

Stack overflow's medals system is another example.

My question is, you can store the description and name of each badge easily in a database table.

**tbl_Badges**
ID  |  Badge Name  |  canHaveMultiple   |  hasProgress  |  progressScale
----------------------------------------------------------------------------
1     Copy Editor     Yes                    No                null
2     The Mob         Yes                    No                null
3     Jquery          No                     Yes               100

And then to associate users with these badges:

**tbl_UsersBadges**
ID  |  Badge ID  | User ID
---------------------------
1      1            5
2      1            5
3      2            5
4      2            12

(And perhaps another one to store user progress through scale badges etc etc)

But the difficulty comes in programming the triggers that award these badges.

Are systems like this built with hard coded triggers in the website? This is about the only way I can think of doing it due to the broadness and flexibility of these badges. However if the triggers are hard coded to a badge ID, you are going to have to be extremely careful when changing anything.

So is this how such a system works? It goes against what I have learnt. Or am I approaching the problem wrongly?


Solution

  • First you need to make sure you have a clear list of "rewarded" user actions. Then, you have to create some code which will be triggered when one of those actions happen. There are multiple ways of looking at the solution:

    1. Save each action which might lead to a reward in the database. Then, either actual triggers in the database will grant badges based on those action properties, or a crontab job might do the work. Benefits are that if you change your reward system calculations, you can replay user actions to get back the new reward;
    2. Code some hub method which is automatically called before calling any other methods. Based on the action, you can grant awards then and redirect to the real method afterwards;
    3. Use hard-coded actions as you mentioned but this is tedious and error-prone;