Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd have a separate table which tracks events and their corresponding XP adjustments per-user:</p> <ul> <li>xp_events <ul> <li>event: The event as a string or foreign keyed ID or whatever.</li> <li>user: The user in question.</li> <li>delta: The corresponding XP change.</li> </ul></li> </ul> <p>Then store both the total XP and the current level with your users. Add triggers to <code>xp_events</code> to update the total XP and current level in the user table whenever xp-events are added. I'd recommend against ever altering or removing entries in <code>xp_events</code>, an audit trail is quite useful and you can always undo one event by adding another event.</p> <p>This approach gives you your summary information pretty much for free so you don't have to do a bunch of extra aggregation to compute a listing page of your users. You also get a nice history in <code>xp_events</code> for auditing or just in case people want to see their progress. As an added bonus, you get an audit trail in <code>xp_events</code> that you can use to regenerate each user's total XP and current level when you have bugs in your triggers.</p> <p>You'd also want to have the xp-to-level information in the database, your triggers will need it and it can't hurt to have all your data in the database anyway. Something like this:</p> <pre><code>min_xp | level -------+------ 0 | 1 100 | 2 500 | 3 1000 | 4 ... </code></pre> <p>Then you can get the level for <code>$xp</code> XP with:</p> <pre><code>select max(level) from xp_levels where $xp - min_xp &gt;= 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload