如何实施徽章? [英] How to implement badges?

查看:16
本文介绍了如何实施徽章?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经考虑过实施徽章(就像 Stack Overflow 上的徽章),并认为没有 Windows 服务会很困难,但如果可能,我想避免这种情况.

I've given some thought to implementing badges (just like the badges here on Stack Overflow) and think it would be difficult without Windows services, but I'd like to avoid that if possible.

我想出了一个实施一些例子的计划:

I came up with a plan to implement some examples:

  • 自传:检查个人资料中的所有字段是否都已填写.
  • 评论员:发表评论时,检查评论数是否等于 10,如果是,则授予徽章.
  • 好的答案:投票时检查投票分数是否为 25 或更高.

如何在数据库中实现?或者其他方式会更好?

How could this be implemented in the database? Or would another way be better?

推荐答案

基于团队每隔一段时间丢弃的一些信息,类似于 Stackoverflow 的实现实际上比您描述的要简单得多.

A similar-to-Stackoverflow implementation is actually a lot simpler than you have described, based on bits of info dropped by the team every once in awhile.

在数据库中,您只需存储一组 BadgeID-UserID 对来跟踪谁拥有什么(以及一个计数或一个 rowID 以允许对某些徽章进行多次奖励).

In the database, you simply store a collection of BadgeID-UserID pairs to track who has what (and a count or a rowID to allow multiple awards for some badges).

在应用程序中,每种徽章类型都有一个工作对象.对象在缓存中,当缓存过期时,worker 运行自己的逻辑来确定谁应该获得徽章并进行更新,然后将自己重新插入缓存:

In the application, there is a worker object for each badge type. The object is in cache, and when the cache expires, the worker runs its own logic for determining who should get the badge and making the updates, and then it re-inserts itself into the cache:

public abstract class BadgeJob
{
    protected BadgeJob()
    {
        //start cycling on initialization
        Insert();
    }

    //override to provide specific badge logic
    protected abstract void AwardBadges();

    //how long to wait between iterations
    protected abstract TimeSpan Interval { get; }

    private void Callback(string key, object value, CacheItemRemovedReason reason)
    {
        if (reason == CacheItemRemovedReason.Expired)
        {
            this.AwardBadges();
            this.Insert();
        }
    }

    private void Insert()
    {
        HttpRuntime.Cache.Add(this.GetType().ToString(),
            this,
            null,
            Cache.NoAbsoluteExpiration,
            this.Interval,
            CacheItemPriority.Normal,
            this.Callback);
    }
}

还有一个具体的实现:

public class CommenterBadge : BadgeJob
{
    public CommenterBadge() : base() { }

    protected override void AwardBadges()
    {
        //select all users who have more than x comments 
        //and dont have the commenter badge
        //add badges
    }

    //run every 10 minutes
    protected override TimeSpan Interval
    {
        get { return new TimeSpan(0,10,0); }
    }
}

这篇关于如何实施徽章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆