gpt4 book ai didi

c# - 如何实现徽章?

转载 作者:IT王子 更新时间:2023-10-29 03:55:39 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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

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

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); }
}
}

关于c# - 如何实现徽章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3162446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com