-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVoting.cs
More file actions
163 lines (127 loc) · 4.83 KB
/
Copy pathVoting.cs
File metadata and controls
163 lines (127 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using BattleBitAPI.Common;
using BBRAPIModules;
using Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BattleBitBaseModules;
[Module("Simple chat voting system", "1.1.1")]
[RequireModule(typeof(CommandHandler))]
public class Voting : BattleBitModule
{
private bool activeVote = false;
private string voteText = "";
private string[] voteOptions = new string[0];
private Dictionary<ulong, int> votes = new();
private DateTime endOfVote = DateTime.MinValue;
public VoteConfiguration Configuration { get; set; } = null!;
[ModuleReference]
public dynamic? RichText { get; set; }
[ModuleReference]
public CommandHandler CommandHandler { get; set; } = null!;
public override void OnModulesLoaded()
{
this.CommandHandler.Register(this);
}
[CommandCallback("vote", Description = "Votes for an option", Permissions = new[] { "Vote.Vote" }, ConsoleCommand = true)]
public string? StartVoteCommand(Context context, string text, string options)
{
if (this.activeVote)
{
return "There is already an active vote.";
}
this.activeVote = true;
this.voteText = text;
this.voteOptions = options.Split('|');
if (this.voteOptions.Length >= 10)
{
this.activeVote = false;
return "You can only have up to 9 options.";
}
this.votes.Clear();
this.endOfVote = DateTime.Now.AddSeconds(this.Configuration.VoteDuration);
this.Server.SayToAllChat($"{this.RichText?.Size(125)}A vote has been started!");
StringBuilder messageText = new($"{this.RichText?.Size(125)}{this.voteText}{this.RichText?.Size(100)}{Environment.NewLine}");
for (int i = 0; i < this.voteOptions.Length; i++)
{
messageText.AppendLine($"Type {i + 1} in chat for {this.RichText?.FromColorName("yellow")}{this.voteOptions[i]}{this.RichText?.Color()}");
}
messageText.AppendLine($"{this.RichText?.Size(125)}You have {this.Configuration.VoteDuration} seconds to vote.");
foreach (RunnerPlayer player in this.Server.AllPlayers)
{
player.Message($"{this.RichText?.Size(125)}{messageText}", this.Configuration.VoteDuration);
}
this.Server.SayToAllChat(messageText.ToString());
Task.Run(voteHandler);
return null;
}
private void voteHandler()
{
while (this.IsLoaded && this.Server?.IsConnected == true && this.activeVote)
{
if (DateTime.Now > this.endOfVote)
{
break;
}
Task.Delay(1000).Wait();
}
if (!this.IsLoaded || this.Server?.IsConnected != true || !this.activeVote)
{
return;
}
this.activeVote = false;
if (this.votes.Count == 0)
{
this.Server.SayToAllChat($"{this.RichText?.Size(125)}The vote has ended!{Environment.NewLine}Nobody voted.");
return;
}
int[] voteCounts = new int[this.voteOptions.Length];
foreach (int vote in this.votes.Values)
{
voteCounts[vote - 1]++;
}
int maxVotes = voteCounts.Max();
int maxVoteIndex = voteCounts.ToList().IndexOf(maxVotes);
this.Server.SayToAllChat($"{this.RichText?.Size(125)}The vote has ended!{Environment.NewLine}{this.RichText?.FromColorName("yellow")}{this.voteOptions[maxVoteIndex]}{this.RichText?.Color()} won with {maxVotes} votes.");
this.Logger.Info($"The vote has ended! {this.voteOptions[maxVoteIndex]} won with {maxVotes} votes.");
}
public override async Task<bool> OnPlayerTypedMessage(RunnerPlayer player, ChatChannel channel, string msg)
{
if (!this.activeVote)
{
return true;
}
msg = new string(msg.Where(c => char.IsDigit(c)).Distinct().ToArray());
if (msg.Length == 0)
{
return true;
}
if (msg.Length > 1)
{
player.SayToChat("Could not find a unique vote option in your message.");
return true;
}
if (!int.TryParse(msg, out int vote))
{
return true;
}
if (vote < 1 || vote > this.voteOptions.Length)
{
return true;
}
if (this.votes.ContainsKey(player.SteamID))
{
this.votes.Remove(player.SteamID);
}
this.votes.Add(player.SteamID, vote);
player.SayToChat($"You voted for {this.RichText?.FromColorName("yellow")}{this.voteOptions[vote - 1]}{this.RichText?.Color()}. You can change your vote any time.");
await Task.CompletedTask;
return true;
}
}
public class VoteConfiguration : ModuleConfiguration
{
public int VoteDuration { get; set; } = 60;
}