[英]How to return a list of all points earned and add/remove a role
I've built a reaction based voting system which allows for users in my discord server to vote for someone, and based on the vote they are given they will get x amount of points EG 5 star rating gives 10 points. 我建立了一个基于反应的投票系统,该系统允许不和谐服务器中的用户投票给某人,并根据他们的投票给他们x积分,例如EG 5星评级得到10分。
All these figures are then added to a SQL-Server Database along with the person who voted for them and the DateTime
the vote was cast. 然后将所有这些数字以及投票给他们的人以及投票的DateTime
添加到SQL Server数据库中。
What i am now trying to figure out is how can i add/remove roles for people who are being voted for. 我现在想找出的是如何为被投票的人添加/删除角色。 For example once a member recieves 100 points they are a assigned X role and if they drop below 100 remove the role is removed. 例如,一旦成员获得100分,他们将被分配为X角色,如果其得分低于100,则该角色将被删除。
I'm struggling to figure out how i would write the SQL Function
to get this information - i think i'll be confident enough to write the c# to accompany this. 我正在努力弄清楚我将如何编写SQL Function
来获取此信息-我想我会很有信心编写c#来做到这一点。
Fields in my Table are 我表中的字段是
Example of how the data is being stored to the Database 数据如何存储到数据库的示例
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));
}
I've have made a simple function just to return a SUM of all the points that a Sherpa has received so far, but i just can't get my head around how to do this next bit if anyone can point me in the right direction would be appreciated. 我已经做了一个简单的功能,只是返回夏尔巴人迄今获得的所有分数的总和,但是如果有人能指出正确的方向,我将无法继续下一步工作将不胜感激。
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
You could do something with a case : 你可以做一个案例:
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;
For as small number of role. 为尽可能少的作用。 If you have a lot you should create a Role table with min and max values of point and compute it accordingly. 如果您的工作量很大,则应创建一个具有点的最小值和最大值的角色表,并进行相应的计算。
EDIT: This should work as per your comment 编辑:这应该按照您的评论
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.