-
Notifications
You must be signed in to change notification settings - Fork 0
/
FogVoting.cs
315 lines (264 loc) · 9.85 KB
/
FogVoting.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Game.Rust.Cui;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Fog Voting", "Ultra", "1.1.2")]
[Description("Initializes voting to remove fog from the environment")]
class FogVoting : RustPlugin
{
#region Fields
bool isVotingOpen = false;
Timer fogCheckTimer;
Timer votingTimer;
Timer votingPanelRefreshTimer;
DateTime votingStart;
Dictionary<string, bool> currentVoting = new Dictionary<string, bool>();
// CUI panels
static CuiPanel votingPanel;
string votingPanelName = "votingPanelName";
string fogYesPanelName = "fogYesPanelName";
string fogNoPanelName = "fogNoPanelName";
string votingScorePanelName = "votingScorePanelName";
#endregion
#region Oxide Hooks
void OnServerInitialized()
{
InitCUI();
CheckCurrentFog();
}
void Unload()
{
DestroyVotingPanel();
DestroyTimer(fogCheckTimer);
DestroyTimer(votingTimer);
DestroyTimer(votingPanelRefreshTimer);
}
#endregion
#region Chat Commands
[ChatCommand("fogyes")]
void FogYes(BasePlayer player)
{
if (!isVotingOpen) return;
if (currentVoting.ContainsKey(player.UserIDString))
{
currentVoting[player.UserIDString] = true;
}
else
{
currentVoting.Add(player.UserIDString, true);
}
}
[ChatCommand("fogno")]
void FogNo(BasePlayer player)
{
if (!isVotingOpen) return;
if (currentVoting.ContainsKey(player.UserIDString))
{
currentVoting[player.UserIDString] = false;
}
else
{
currentVoting.Add(player.UserIDString, false);
}
}
[ChatCommand("setfog")]
void SetFog(BasePlayer player, string command, string[] args)
{
if (player.IsAdmin)
{
float fogValue = 0;
if (args.Length == 1 && float.TryParse(args[0], out fogValue))
{
if (fogValue >= 0F && fogValue <= 1F)
{
ConsoleSystem.Run(ConsoleSystem.Option.Server, $"weather.fog {fogValue}");
}
}
}
}
[ChatCommand("checkfog")]
void CheckFog(BasePlayer player, string command, string[] args)
{
if (isVotingOpen) return;
if (player.IsAdmin)
{
CheckCurrentFog();
}
}
#endregion
#region Core
void CheckCurrentFog()
{
DestroyTimer(fogCheckTimer);
foreach (var player in BasePlayer.activePlayerList)
{
if (Climate.GetFog(player.transform.position) > configData.FogLimit)
{
OpenVoting();
return;
}
}
fogCheckTimer = timer.Once(configData.FogCheckInterval, () => CheckCurrentFog());
}
void OpenVoting()
{
isVotingOpen = true;
currentVoting = new Dictionary<string, bool>();
votingStart = DateTime.UtcNow;
ShowVotingPanel();
votingTimer = timer.Once(configData.VotingDuration, () => CloseVoting());
}
void CloseVoting()
{
isVotingOpen = false;
DestroyVotingPanel();
if (currentVoting.Count > 0 && currentVoting.Where(w => w.Value).Count() < currentVoting.Where(w => !w.Value).Count())
{
ConsoleSystem.Run(ConsoleSystem.Option.Server, "weather.fog 0");
fogCheckTimer = timer.Once(configData.FogCheckInterval, () => CheckCurrentFog());
}
else
{
// longer interval for another check after unsuccessful voting to remove fog
fogCheckTimer = timer.Once(configData.FogCheckInterval * 10, () => CheckCurrentFog());
}
}
void DestroyTimer(Timer timer)
{
timer?.DestroyToPool();
timer = null;
}
void DestroyVotingPanel()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, votingPanelName);
}
}
#endregion
#region Config
private ConfigData configData;
private class ConfigData
{
[JsonProperty(PropertyName = "Fog value to start voting 0.0 - 1.0")]
public float FogLimit;
[JsonProperty(PropertyName = "Interval to check the current fog (seconds)")]
public float FogCheckInterval;
[JsonProperty(PropertyName = "Voting duration (seconds)")]
public float VotingDuration;
}
protected override void LoadConfig()
{
try
{
base.LoadConfig();
configData = Config.ReadObject<ConfigData>();
if (configData == null)
{
LoadDefaultConfig();
}
}
catch
{
LoadDefaultConfig();
}
if (configData.FogLimit < 0 || configData.FogLimit > 1) configData.FogLimit = 0.1F;
if (configData.FogCheckInterval < 1) configData.FogCheckInterval = 60;
if (configData.VotingDuration < 10) configData.VotingDuration = 30;
SaveConfig();
}
protected override void LoadDefaultConfig()
{
configData = new ConfigData()
{
FogLimit = 0.3F,
FogCheckInterval = 60,
VotingDuration = 30
};
}
protected override void SaveConfig()
{
Config.WriteObject(configData, true);
base.SaveConfig();
}
#endregion
#region CUI
void InitCUI()
{
votingPanel = new CuiPanel
{
CursorEnabled = false,
RectTransform =
{
AnchorMin = $"0.4 0.87",
AnchorMax = $"0.6 0.91"
},
Image =
{ Color = "0 0 0 0.8" }
};
}
void ShowVotingPanel()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, votingPanelName);
if (isVotingOpen)
{
var container = new CuiElementContainer();
container.Add(votingPanel, name: votingPanelName);
// fog yes
container.Add(GetPanel(anchorMin: "0 0", anchorMax: "0.2 1"), votingPanelName, name: fogYesPanelName);
container.Add(GetLabel("/fog<color=#89b38a>yes</color>", align: TextAnchor.MiddleRight), fogYesPanelName);
// voting score
container.Add(GetPanel(anchorMin: "0.2 0", anchorMax: "0.8 1"), votingPanelName, name: votingScorePanelName);
container.Add(GetLabel($"{currentVoting.Where(w => w.Value).Count()} : {currentVoting.Where(w => !w.Value).Count()}", align: TextAnchor.MiddleCenter, size: 18), votingScorePanelName);
// fog no
container.Add(GetPanel(anchorMin: "0.8 0", anchorMax: "1 1"), votingPanelName, name: fogNoPanelName);
container.Add(GetLabel("/fog<color=#89b38a>no</color>", align: TextAnchor.MiddleLeft), fogNoPanelName);
// progress bar
float progress = (float)(DateTime.UtcNow - votingStart).TotalSeconds / configData.VotingDuration;
container.Add(GetPanel(anchorMin: "0 0", anchorMax: $"{progress} 0.1", color: "1 1 1 0.4"), votingPanelName, name: fogNoPanelName);
CuiHelper.AddUi(player, container);
}
}
if(isVotingOpen) votingPanelRefreshTimer = timer.Once(2F, () => ShowVotingPanel());
}
CuiPanel GetPanel(string anchorMin = "0 0", string anchorMax = "1 1", string color = "0.1 0.1 0.1 0", bool cursorEnabled = false)
{
return new CuiPanel
{
CursorEnabled = cursorEnabled,
RectTransform =
{
AnchorMin = anchorMin,
AnchorMax = anchorMax
},
Image = { Color = color }
};
}
CuiLabel GetLabel(string text, int size = 14, string anchorMin = "0.05 0.02", string anchorMax = "0.98 0.9", TextAnchor align = TextAnchor.MiddleCenter, string color = "1 1 1 1", string font = "robotocondensed-regular.ttf")
{
return new CuiLabel
{
Text =
{
Text = text,
FontSize = size,
Align = align,
Color = color,
Font = font
},
RectTransform =
{
AnchorMin = anchorMin,
AnchorMax = anchorMax
},
};
}
#endregion
}
}