如何构建Web应用程序的成就系统基于数据库的规则集?
我打算一个ASP.NET MVC网站上的成就系统。我想实现的规则被存储在数据库中 - 更容易增加新成就和管理现有规则的中心位置。用户没有访问改变规则。
I'm planning an achievement system for an ASP.NET MVC site. I want the achievement "rules" be stored in a database - to be easier to add new achievements and a central place to manage existing rules. Users will have no access to change the rules.
当用户执行可能赚取成就一个动作时,DB规则将被查询,如果有比赛,给他们的成就(存储在一个查找表(用户ID,achievementId,dateAwarded)。
When a user performs an action that could potentially earn an achievement, the db rules will be queried and if there are matches, give them the achievements (stored in a lookup table, (userId, achievementId, dateAwarded).
目前,我正打算把在控制器中的某些操作的触发器,但code,做的工作将在模型中。
At the moment I'm planning to put the "triggers" on certain actions in the controller, but the code that does the work will be in the model.
是否有完成此成就系统的标准DB模式?如果没有必要没有必要推倒重来。如果没有,什么样的问题,你觉得会弹出,看出来的是什么?
Is there standard DB schema for an achievement system that accomplishes this? No need to reinvent the wheel if not necessary. If not, what sorts of issues do you think would pop up, what to look out for?
您可能会发现的这个答案 帮助。
You may find this answer helpful.
从经验上讲,建立基于数据库的规则引擎对用户动作是一个非常耗时且容易出错的,痛苦的锻炼。
Speaking from experience, building a database-based rules engine for reacting to user actions is a very time-consuming, error-prone, painful exercise.
相反,你可以写任何数量的单个类,每一个负责知道如何奖励一个特定的成就。使用一个基类或接口给他们所有一个共同的合同:
Instead, you can write any number of individual classes, each one responsible for knowing how to award one particular achievement. Use a base class or interface to give them all a common contract:
public abstract class AchievementAwarder
{
//override to provide specific badge logic
public abstract void Award();
}
您可以的foreach
在每个 AchievementAwarder
和呼叫奖()
重复性计划。例如,你可以有一个100访问成果:
You can foreach
over each AchievementAwarder
and call Award()
on a recurring schedule. For example, you could have a "100 visits" achievement:
public class 100VisitsAwarder : AchievementAwarder
{
public override void Award()
{
//get a visit count from the db for all users
//award achievements
}
}
这解决了两个问题:
-
这是数量级的简单,但更灵活,比基于数据库的规则引擎。正如你所看到的,各个单位都非常小,容易改变,而不会影响更大的系统。
It's orders of magnitude simpler, but much more flexible, than a database-based rules engine. As you can see, the individual units are extremely small and easy to change without affecting the larger system.
它可以异步运行,所以需要一些繁重的成绩,以确定他们是否应该被授予,用户的正常活动不受成就引擎的影响。
It can run asynchronously, so for achievements that require some heavy lifting to determine if they should be awarded, the user's normal activities are not impacted by the achievements engine.