Page 1 of 1

Point system class and db

Posted: Sun Nov 04, 2012 5:58 pm
by pinehead
I'm designgin a point system for my app. Basically whenever an "action" takes place I'd like to call the class that adds points to their db.
such as $achievement->completed_task('task_name')

and have points added based off task_name.

So two questions, how would you suggest handling this in the db? increase a row per action completed with the number of points they received then for their score add all the rows? That way actions/points can be tracked/logged?


For the class i was thinking it might go something like this.. Thouhts?

Code: Select all

class Achievements {

  public function completed_task($task) {
    case($task)
         scored:
             +15pts
         break;
         quit:
           -10pts
         break; 
}  

}

Thoughts?

Thanks,

Re: Point system class and db

Posted: Sun Nov 04, 2012 6:34 pm
by requinix
It's okay to break normalization a bit for things like post counts or user points. If you want a log of activity then keep that, but you can update a field in a table with the new value too. So yes the number of points is actually stored in two places.

But I wouldn't put in code the number of points for a task. Store that in the database too. You can even store a date range for each row, allowing you to change the number of points for a task for some time (eg, a special bonus).

Re: Point system class and db

Posted: Mon Nov 05, 2012 2:42 pm
by Christopher
pinehead wrote:So two questions, how would you suggest handling this in the db? increase a row per action completed with the number of points they received then for their score add all the rows? That way actions/points can be tracked/logged?
The way you have shown it, it looks like the points are kept in the same place. So a single record per user might be fine. Do you need to track the history of scores or just their current score?

Also, unless this class also defines the list of tasks then I would prefer a method per task like:

Code: Select all

class Achievements {
  protected $db;
  protected $userId;

  public function __construct($db, $userId) {
         $this->db = $db;
         $this->userId = $userId;
  }

  public function completedScored() {
         $this->db->query("UPDATE scores SET score=score+15 WHERE id='{$this->userId}'");
  }

  public function completedQuit() {
         $this->db->query("UPDATE scores SET score=score-10 WHERE id='{$this->userId}'");
  }

}