forked from CAWCAWCAW/Votekick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVKTimer.cs
153 lines (137 loc) · 6.96 KB
/
VKTimer.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
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
using System.Timers;
namespace VoteKick
{
public class VoteKickTimer
{
public Timer VKTimer;
public Timer VKTimerNotify;
public VoteKickTimer()
{
VKTimer = new Timer(Votekick.config.VoteTime * 1000);
VKTimerNotify = new Timer((Votekick.config.VoteTime / 2) * 1000);
}
public void Start()
{
VKTimer.Enabled = true;
VKTimer.Elapsed += UpdateTimer;
VKTimerNotify.Enabled = true;
VKTimerNotify.Elapsed += NotifyTimer;
}
private void UpdateTimer(object sender, ElapsedEventArgs args)
{
if (Votekick.VoteKickRunning)
{
double active = TShock.Utils.ActivePlayers();
double percentageofactive = ((active) * (Votekick.config.PercentofPlayersVoteYesToKick / 100));
double totalvoters = Votekick.poll.voters.Count;
if (Votekick.poll.votedyes.Count > Votekick.poll.votedno.Count && Votekick.poll.votedyes.Count >= percentageofactive)
{
//TShock.Utils.Kick(Votekick.poll.votedplayer, Votekick.config.KickMessage, true, false);
//Instead of kicking we are going to use our new Connect method. Which for now just sends a new packet type
VKPlayer vkplyr = new VKPlayer(Votekick.poll.votedplayer.Index);
// TODO get this IP from the config.json file
// Load the IP when a player reaches coordinates X and Y
// obviously THIS is not the place to do that, but leaving
// the comment here
vkplyr.Connect("67.186.57.60:7778");
//vkplyr.Connect("Hello World");
Votekick.VoteKickRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
else
{
TSPlayer.All.SendInfoMessage("[VoteKick] The votekick on " + Votekick.poll.votedplayer.Name + " has failed.");
Votekick.VoteKickRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
}
if (Votekick.VoteMuteRunning)
{
double active = TShock.Utils.ActivePlayers();
double percentageofactive = ((active) * (Votekick.config.PercentofPlayersVoteYesToMute / 100));
double totalvoters = Votekick.poll.voters.Count;
if (Votekick.poll.votedyes.Count > Votekick.poll.votedno.Count && Votekick.poll.votedyes.Count >= percentageofactive)
{
TSPlayer.All.SendInfoMessage("[VoteMute] The vote to mute {0} has succeeded.", Votekick.poll.votedplayer.Name);
Votekick.poll.votedplayer.mute = true;
Votekick.VoteMuteRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
else
{
TSPlayer.All.SendInfoMessage("[VoteMute] The vote to mute " + Votekick.poll.votedplayer.Name + " has failed.");
Votekick.VoteMuteRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
}
if (Votekick.VoteBanRunning)
{
double active = TShock.Utils.ActivePlayers();
double percentageofactive = ((active) * (Votekick.config.PercentofPlayersVoteYesToBan / 100));
double totalvoters = Votekick.poll.voters.Count;
if (Votekick.poll.votedyes.Count > Votekick.poll.votedno.Count && Votekick.poll.votedyes.Count >= percentageofactive)
{
TShock.Utils.Kick(Votekick.poll.votedplayer, Votekick.config.BanMessage, true, false);
TShock.Bans.AddBan(Votekick.poll.votedplayer.IP, Votekick.poll.votedplayer.Name, Votekick.poll.votedplayer.UUID, Votekick.config.BanMessage, false, "Server Vote", DateTime.UtcNow.AddDays(Votekick.config.BanTimeInDays).ToString("s"));
Votekick.VoteBanRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
else
{
TSPlayer.All.SendInfoMessage("[VoteBan] The vote to ban " + Votekick.poll.votedplayer.Name + " has failed.");
Votekick.VoteBanRunning = false;
Votekick.poll.voters.Clear();
Votekick.poll.votedno.Clear();
Votekick.poll.votedyes.Clear();
Votekick.poll.votedplayer = null;
VKTimerNotify.Stop();
}
}
}
private void NotifyTimer(object sender, ElapsedEventArgs args)
{
if (Votekick.VoteKickRunning)
{
TSPlayer.All.SendSuccessMessage("The vote is ending in {0} seconds to kick {1}.", (Votekick.config.VoteTime / 2), Votekick.poll.votedplayer.Name);
VKTimerNotify.Stop();
}
if (Votekick.VoteMuteRunning)
{
TSPlayer.All.SendSuccessMessage("The vote is ending in {0} seconds to mute {1}.", (Votekick.config.VoteTime / 2), Votekick.poll.votedplayer.Name);
VKTimerNotify.Stop();
}
if (Votekick.VoteBanRunning)
{
TSPlayer.All.SendSuccessMessage("The vote is ending in {0} seconds to ban {1} for a time length of {2} days.", (Votekick.config.VoteTime / 2), Votekick.poll.votedplayer.Name, Votekick.config.BanTimeInDays);
VKTimerNotify.Stop();
}
}
}
}