This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
forked from Arechii/CallVote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVote.cs
147 lines (112 loc) · 4.37 KB
/
Vote.cs
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
using Arechi.CallVote.Utils;
using Rocket.API;
using Rocket.Core;
using Rocket.Unturned.Player;
using SDG.Unturned;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Arechi.CallVote
{
public class Vote : IVote
{
public IVoteSettings Settings { get; set; }
public List<string> Arguments { get; set; }
public List<ulong> Voters { get; set; } = new List<ulong>();
public VoteStatus Status { get; set; } = VoteStatus.Ready;
public int CooldownTime { get; set; }
private Coroutine _startCoroutine;
public Vote(IVoteSettings settings)
{
Settings = settings;
}
public int GetPercentage()
{
return (int)(decimal.Divide(Voters.Count, Provider.clients.Count == 0 ? 1 : Provider.clients.Count) * 100);
}
public VoteResult GetResult()
{
return GetPercentage() >= Settings.RequiredPercent ? VoteResult.Success : VoteResult.Failure;
}
public void Start(List<string> arguments)
{
if (!CanStart(arguments)) return;
Status = VoteStatus.Ongoing;
Arguments = arguments;
SendMessage("START", Settings.Alias);
_startCoroutine = Plugin.Instance.StartCoroutine(Start());
}
protected IEnumerator Start()
{
var time = Settings.Timer;
while (time != 0)
{
yield return new WaitForSeconds(1f);
time--;
}
Stop();
}
protected bool CanStart(List<string> arguments)
{
if (Status == VoteStatus.CoolingDown)
throw new VoteStartException("COOLING_DOWN", CooldownTime);
if (arguments.Count < Settings.MinimumArguments)
throw new VoteStartException("NOT_ENOUGH_ARGUMENTS", Settings.MinimumArguments);
if (Provider.clients.Count < Settings.MinimumPlayers)
throw new VoteStartException("NOT_ENOUGH_PLAYERS", Settings.MinimumPlayers);
return true;
}
public void AddVote(UnturnedPlayer player)
{
if (Voters.Contains(player.CSteamID.m_SteamID)) return;
Voters.Add(player.CSteamID.m_SteamID);
SendMessage("RESULT", GetPercentage(), Settings.RequiredPercent, Settings.Alias);
if (GetResult() != VoteResult.Success) return;
Plugin.Instance.StopCoroutine(_startCoroutine);
Stop();
}
public void Stop()
{
if (GetResult() == VoteResult.Failure)
{
SendMessage("FAILURE");
Cooldown();
return;
}
var command = new List<string> { Settings.Command };
if (Arguments != null)
command.AddRange(Arguments);
SendMessage("SUCCESS");
R.Commands.Execute(new ConsolePlayer(), string.Join(" ", command));
Cooldown();
}
public void Cooldown()
{
Status = VoteStatus.CoolingDown;
SendMessage("COOLDOWN", Settings.CooldownTime);
Plugin.Instance.StartCoroutine(Cooldown(Settings.CooldownTime));
Plugin.Instance.StopCoroutine(_startCoroutine);
}
protected IEnumerator Cooldown(int cooldown)
{
CooldownTime = cooldown;
while (CooldownTime != 0)
{
yield return new WaitForSeconds(1f);
CooldownTime--;
}
Voters.Clear();
Status = VoteStatus.Ready;
SendMessage("READY");
}
protected void SendMessage(string translationKey, params object[] args)
{
var message = Plugin.Instance.Translate("VOTE_CHAT_FORMAT")
.Replace("{color}", $"<color={Settings.Color}>")
.Replace("{/color}", "</color>")
.Replace("{vote}", $"{Settings.Name}{(Settings.MinimumArguments > 0 ? " " + string.Join(" ", Arguments) : "")}")
.Replace("{text}", Plugin.Instance.Translate(translationKey, args));
ChatUtil.Broadcast(message, Settings.Icon, Color.white);
}
}
}