开发者

A good data model for storing score and history for a game

开发者 https://www.devze.com 2023-03-24 07:04 出处:网络
I am developing a little game (I\'m using Play! framework). In this game, the players answer tec开发者_运维技巧hnical questions, and win points when they answer correctly.

I am developing a little game (I'm using Play! framework).

In this game, the players answer tec开发者_运维技巧hnical questions, and win points when they answer correctly. In addition to that, they may win some awards, which give additional points. This is similar to the SO badges, except that the player wins points for that awards.

I am wondering what is the best way to store user score and the history of them, especially since I want to be able to display "Top player" for last week, for last month, etc.? These "tops" will be similar to the "Week reputation ranking in SO" (ex: https://stackoverflow.com/users?tab=reputation&filter=week)

Today, I have:

  • A User entity, where I have a score attribute, which stores the current score of the user;
  • A QuestionHistory entity, where I put every question answered by the user, which has the following properties: user, answeredOn, correctAnswer, etc. (I can add a pointsAwarded, but for the moment, all questions give the same amount of points).

Thanks to QuestionHistory entity, I am able to calculate the score won by one player for a given time period. However, it will not include the score received by the awards.

I was thinking of replacing the QuestionHistory entity by another entity, for example UserActivity, which will store any activity for this user:

  • when he answers a question, with the result;
  • when the player submit a new question;
  • when he wins an award;
  • etc.

The structure of this entity may look like:

@Entity
public class PlayerActivity extends Model {

    public User user;
    public Date activityDate;
    public ActivityType type; // Define the type of activity: "QUESTION_ANSWERED", "AWARD_RECEIVED", "QUESTION_SUBMITTED", etc.
    public int score; // Points won by this activity.

}

With such entity, I would be also able to display an activity history like the one provided on SO site (ex: https://stackoverflow.com/users/22656/jon-skeet?tab=activity)

What do you think of that new proposition, or is there a smater way of doing that?


I think this is definitely a better way to calculate the points, as you are managing events and you are "future proof" in case you want to extend the activities that reward users.

Yes, probably you may find more convoluted designs to do this. But the one you propose is simple and it works, so... go for it :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号