如何返回获得的所有积分的列表以及添加/删除角色

时间:2019-09-11 17:07:11

标签: c# sql-server discord discord.net

我建立了一个基于反应的投票系统,该系统允许不和谐服务器中的用户投票给某人,并根据他们的投票给他们x积分,​​例如EG 5星评级10分。 >

所有这些数字然后与投票给他们的人一起添加到SQL Server数据库中,并进行DateTime投票。

我现在要弄清楚的是如何为被投票的人添加/删除角色。例如,一旦成员获得100分,他们便被分配了X角色,如果其得分低于100,则该角色将被删除。

我正在努力弄清楚如何编写SQL Function以获取此信息-我认为我有足够的信心编写c#来实现这一点。

我的表格中的字段是

  • Sherpa-被投票的人
  • 投票-投票的人
  • 日期-投票开始的日期
  • 积分-授予的积分数量

如何将数据存储到数据库的示例

var message = (RestUserMessage) await channel.GetMessageAsync(SherpaReact.CatchReaction);
if (reaction.MessageId == SherpaReact.CatchReaction && reaction.UserId == SherpaReact.UserReaction) {
 SqlCommand command;
 command = new SqlCommand($ @ "EXECUTE dbo.AddSherpa @Points = @@Points, @Sherpa = @@Sherpa, @VotedBy = @@VotedBy, @Date = @@Date", StaticObjects._connection);

 if (reaction.Emote.Name == "1\u20e3") {
  await message.DeleteAsync();

  SqlParameter PointsParam = new SqlParameter {
   ParameterName = "@@Points",
    Value = (long) - 8
  };
  command.Parameters.Add(PointsParam);

  SqlParameter SherpaParam = new SqlParameter {
   ParameterName = "@@Sherpa",
    Value = (long) SherpaReact.Sherpa.Id
  };
  command.Parameters.Add(SherpaParam);

  SqlParameter VotedByParam = new SqlParameter {
   ParameterName = "@@VotedBy",
    Value = (long) SherpaReact.UserReaction
  };
  command.Parameters.Add(VotedByParam);

  SqlParameter DateParam = new SqlParameter {
   ParameterName = "@@Date",
    Value = DateTime.Now
  };
  command.Parameters.Add(DateParam);
  command.ExecuteNonQuery();

  var sherpa = SherpaReact.Sherpa.Nickname.Contains("(") ? SherpaReact.Sherpa.Nickname.Substring(0, SherpaReact.Sherpa.Nickname.IndexOf("(") - 1) : SherpaReact.Sherpa.Nickname;
  EmbedBuilder eb = new EmbedBuilder() {
   Title = "Vote Accepted",
    Description = $ "Thanks for voting for {sherpa}",
    Color = new Color(127, 127, 0)
  };
  IUserMessage msg = await channel.SendMessageAsync(embed: eb.Build());
  _ = Task.Run(() => DeleteMessagesAsync(msg));
 }

我已经做了一个简单的功能,只是返回夏尔巴人迄今获得的所有积分的总和,但是如果有人可以指出我的意思,我将无法解决该问题。正确的方向将不胜感激。

CREATE FUNCTION [dbo].[GetSherpaPoints]
(
)
RETURNS @returntable TABLE
(
    Sherpa BIGINT,
    Points BIGINT
)
AS
BEGIN

    INSERT @returntable
    SELECT
        [Sherpa],
        SUM([Points]) AS [PointsSum]
    FROM
        [dbo].[SherpaVotes]
    WHERE
        [Sherpa] = Sherpa
    GROUP BY
        [Sherpa]
    ORDER BY
        [PointsSum] DESC
    RETURN
END

1 个答案:

答案 0 :(得分:1)

您可以为案件做些事情:

SELECT
   Sherpa,
   CASE 
     WHEN Points < 100 THEN 'NoRole'
     WHEN Points >= 100 AND Points < 200 THEN 'Role1'
     WHEN Points >= 200 AND Points < 300 THEN 'Role2' -- And so on...
     END as Role
FROM @Results;

用于尽可能少的角色。 如果您的工作量很大,则应创建一个包含点的最小值和最大值的角色表,并据此进行计算。

编辑: 这应该按照您的评论进行操作

CREATE FUNCTION [dbo].[GetSherpaPoints]
(
)
RETURNS @returntable TABLE
(
    Sherpa BIGINT,
    Role nvarchar(20)
)
AS
BEGIN

    INSERT @returntable
    SELECT
        [Sherpa],
         CASE 
             WHEN SUM(Points) < 100 THEN 'NoRole'
             WHEN SUM(Points) >= 100 AND SUM(Points) < 200 THEN 'Role1'
             WHEN SUM(Points) >= 200 AND SUM(Points) < 300 THEN 'Role2' -- And so on...
        END as Role
    FROM
        [dbo].[SherpaVotes]
    WHERE
        [Sherpa] = Sherpa
    GROUP BY
        [Sherpa]
    ORDER BY
        [PointsSum] DESC
    RETURN
END