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

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

问题描述

我已经给一些想法实施徽章(就像这里对堆栈溢出徽章),并认为这将是困难的没有窗户的服务,但我想避免,如果可能的。

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:


  • Audobiographer:检查配置文件中的所有领域充满了

  • 评注:当进行评论检查,如果评论数等于10,如果是这样的奖励徽章

  • 很好的回答:当投票了检查,看是否投得分为25或更高

这怎么可能在数据库中执行?或将另一种方式更好呢?

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

推荐答案

一个类似的对#1的实施实际上是简单了很多比你所描述的基础上,通过团队每一次在一段时间下跌的信息位。

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 的集合 - 用户名对跟踪谁拥有什么(计数或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).

在该应用程序,存在用于每个徽章类型一个工人对象。对象是在缓存中,当缓存过期,工人运行其自己的逻辑来确定谁应该得到的徽章,使更新,然后将其重新插入自己到缓存:

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天全站免登陆