This repository was archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventManager.cs
More file actions
397 lines (362 loc) · 15.1 KB
/
Copy pathEventManager.cs
File metadata and controls
397 lines (362 loc) · 15.1 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using Rocket.API;
using Rocket.API.Collections;
using Rocket.Core;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Enumerations;
using Rocket.Unturned.Events;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
using UnityEngine;
using Logger = Rocket.Core.Logging.Logger;
namespace Game4Freak.EventManager
{
public class EventManager : RocketPlugin<EventManagerConfiguration>
{
public static EventManager Instance;
public const string VERSION = "0.3.1.0";
private string newVersion = null;
private bool notifyUpdate = false;
private int frame = 10;
private int eventIndex;
private List<bool> sendNotifications;
private Event nextEvent;
private bool countdownUIActive;
private ExampleEvent example_item;
private MessageEvent example_message;
public delegate void onEventTriggeredHandler(Event @event);
public static event onEventTriggeredHandler onEventTriggered;
public delegate void onEventNotificationHandler(Event @event, float secondsBeforeEvent);
public static event onEventNotificationHandler onEventNotification;
protected override void Load()
{
Instance = this;
Logger.Log("EventManager v" + VERSION);
WebClient client = new WebClient();
try
{
newVersion = client.DownloadString("http://pastebin.com/raw/GAcbHDDd");
}
catch (WebException e)
{
Logger.Log("No connection to version-check");
}
if (newVersion != null)
{
if (compareVersion(newVersion, VERSION))
{
Logger.Log("A new EventManager version (" + newVersion + ") is available !!!");
notifyUpdate = true;
}
}
if (Configuration.Instance.resetOnReload)
{
Configuration.Instance.lastEventUnixTime = getCurrentTime();
Configuration.Save();
}
eventIndex = 0;
resetSendNotifications();
nextEvent = null;
countdownUIActive = false;
onEventTriggered += onEventTrigger;
U.Events.OnPlayerConnected += onPlayerConnection;
example_item = new ExampleEvent();
example_item.load();
example_message = new MessageEvent();
example_message.load();
}
protected override void Unload()
{
onEventTriggered -= onEventTrigger;
U.Events.OnPlayerConnected -= onPlayerConnection;
example_item.unload();
example_message.unload();
if (Configuration.Instance.useUICountdown)
{
foreach (var sPlayer in Provider.clients)
{
EffectManager.askEffectClearByID(9750, UnturnedPlayer.FromSteamPlayer(sPlayer).CSteamID);
}
}
}
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList()
{
{"notification_about-to-start_minutes", "[EventManager] A event is about to start in {0} minute(s)!" },
{"notification_about-to-start_seconds", "[EventManager] A event is about to start in {0} second(s)!" },
{"notification_start", "[EventManager] The event: {0} is starting!" },
{"next_event_minutes", "[EventManager] The next event starts in {0} minute(s)!" },
{"next_event_seconds", "[EventManager] The next event starts in {0} second(s)!" },
{"insufficient_players", "[EventManager] There are not enough players on the server for a event!" },
{"no_permission", "You dont have permission to do that!" },
{"command_invalid", "Invalid! Try {0} {1}" },
{"command_invalid_number", "{0} is not a number!" },
{"command_invalid_type", "The type: {0} does not exist" },
{"command_invalid_parameters", "Invaid Length! Try {0} {1} {2}" },
{"command_no_events", "[EventManager] The server has no events" },
{"command_forced_next", "[EventManager] The next event has been forced!" },
{"command_reset", "[EventManager] The time to the next event has been reset!" },
{"command_event_exists", "The event: {0} already exists!" },
{"command_event_not_exists", "The event: {0} does not exist!" },
{"command_add", "[EventManager] The event: {0} of type: {1} with priority: {2} was added!" },
{"command_remove", "[EventManager] the event: {0} was removed!" },
{"exampleevent_invalid_ids", "[{0}] There are no item ids to give away!" },
{"exampleevent_give_away", "[{0}] You got a free Item!" },
{"exampleevent_on_event_notification", "[{0}] Get yourself ready for a gift!" },
{"exmapleevent_insufficient_players", "[{0}] There are not enough players on the server for this event!" },
{"countdown_message", "Next Event: {0}" },
{"countdown_start", "Event Started!" }
};
}
}
private void Update()
{
frame++;
if (frame % 10 != 0) return;
if (Configuration.Instance.events.Count == 0)
{
Configuration.Instance.lastEventUnixTime = getCurrentTime();
Configuration.Save();
if (Configuration.Instance.useUICountdown)
{
foreach (var sPlayer in Provider.clients)
{
EffectManager.askEffectClearByID(9750, UnturnedPlayer.FromSteamPlayer(sPlayer).CSteamID);
}
}
return;
}
// change
if (!serverHasMinClients())
{
Configuration.Instance.lastEventUnixTime = getCurrentTime();
Configuration.Save();
if (Configuration.Instance.useUICountdown)
{
foreach (var sPlayer in Provider.clients)
{
EffectManager.askEffectClearByID(9750, UnturnedPlayer.FromSteamPlayer(sPlayer).CSteamID);
}
}
return;
}
if (Configuration.Instance.useUICountdown)
{
if (!countdownUIActive && (int)(EventManager.Instance.Configuration.Instance.minutesBetweenEvents * 60 - (getCurrentTime() - Configuration.Instance.lastEventUnixTime)) <= Configuration.Instance.countdownStartMin * 60)
countdownUIActive = true;
if (countdownUIActive)
{
foreach (var sPlayer in Provider.clients)
{
EffectManager.sendUIEffect(9750, 0, true, formatCountdown());
}
}
}
Int32 currentUnixTimestamp = getCurrentTime();
if (currentUnixTimestamp - Configuration.Instance.lastEventUnixTime >= Configuration.Instance.minutesBetweenEvents * 60)
{
sendNextEvent();
resetSendNotifications();
return;
}
List<float> sortedNotifications = Configuration.Instance.minutesNotificationBefore.OrderBy(i => i).ToList();
for (int i = 0; i < sortedNotifications.Count; i++)
{
if (Configuration.Instance.minutesBetweenEvents * 60 - (currentUnixTimestamp - Configuration.Instance.lastEventUnixTime) <= sortedNotifications[i] * 60)
{
if (sendNotifications[i])
return;
if (i == sendNotifications.Count - 1)
defineNextEvent();
sendNotifications[i] = true;
if (sortedNotifications[i] > 3)
UnturnedChat.Say(Translate("notification_about-to-start_minutes", sortedNotifications[i]), UnturnedChat.GetColorFromName(Configuration.Instance.messageColor, Color.green));
else
UnturnedChat.Say(Translate("notification_about-to-start_seconds", sortedNotifications[i] * 60), UnturnedChat.GetColorFromName(Configuration.Instance.messageColor, Color.green));
onEventNotification(nextEvent, sortedNotifications[i] * 60);
return;
}
}
}
public void onEventTrigger(Event @event)
{
UnturnedChat.Say(Translate("notification_start", @event.name), UnturnedChat.GetColorFromName(Configuration.Instance.messageColor, Color.green));
}
public void sendNextEvent()
{
defineNextEvent();
onEventTriggered(nextEvent);
if (Configuration.Instance.useUICountdown)
{
foreach (var sPlayer in Provider.clients)
{
EffectManager.askEffectClearByID(9750, UnturnedPlayer.FromSteamPlayer(sPlayer).CSteamID);
}
}
countdownUIActive = false;
Configuration.Instance.lastEventUnixTime = getCurrentTime();
Configuration.Save();
nextEvent = null;
}
public void defineNextEvent()
{
if (nextEvent != null)
return;
List<Event> events = Configuration.Instance.events.OrderByDescending(i => i.priority).ToList();
if (!serverHasMinClients())
{
UnturnedChat.Say(Translate("insufficient_players"), UnturnedChat.GetColorFromName(Configuration.Instance.messageColor, Color.green));
return;
}
if (Configuration.Instance.useRandomEvents)
{
bool triggered = false;
while (!triggered)
{
Event triggerEvent = events[UnityEngine.Random.Range(0, events.Count)];
if (Provider.clients.Count >= getEventTypeByID(triggerEvent.type).minPlayers)
{
nextEvent = triggerEvent;
triggered = true;
}
}
}
else
{
bool triggered = false;
int currentEventIndex = eventIndex;
while (!triggered)
{
Event triggerEvent = events[currentEventIndex];
if (Provider.clients.Count >= getEventTypeByID(triggerEvent.type).minPlayers)
{
nextEvent = triggerEvent;
triggered = true;
}
else
{
currentEventIndex++;
if (currentEventIndex > events.Count)
currentEventIndex = 0;
}
}
eventIndex++;
if (eventIndex >= events.Count)
eventIndex = 0;
}
}
public static Int32 getCurrentTime()
{
return (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
}
public bool serverHasMinClients()
{
foreach (var @event in Configuration.Instance.events)
{
if (Provider.clients.Count >= getEventTypeByID(@event.type).minPlayers)
return true;
}
return false;
}
private string formatCountdown()
{
int timeLeft = (int)(EventManager.Instance.Configuration.Instance.minutesBetweenEvents * 60 - (getCurrentTime() - Configuration.Instance.lastEventUnixTime));
int[] times = { 0, 0, 0 };
string countdown = "";
if (timeLeft <= 1)
{
countdown = Translate("countdown_start");
return countdown;
}
while (timeLeft > 0)
{
if (timeLeft >= 3600)
{
times[0]++;
timeLeft = timeLeft - 3600;
}
else if (timeLeft >= 60)
{
times[1]++;
timeLeft = timeLeft - 60;
}
else
{
times[2] = timeLeft;
break;
}
}
for (int x = 0; x < times.Length; x++)
{
if (times[x] < 10)
countdown = countdown + "0" + times[x];
else
countdown = countdown + times[x];
if (x < 2)
countdown = countdown + ":";
}
return Translate("countdown_message", countdown);
}
public EventType getEventTypeByID(string id)
{
foreach (var type in Configuration.Instance.eventTypes)
{
if (type.id.ToLower() == id.ToLower())
return type;
}
return null;
}
public Event getEventByName(string name)
{
foreach (var @event in Configuration.Instance.events)
{
if (@event.name.ToLower() == name.ToLower())
return @event;
}
return null;
}
public void addEventType(string id, int minParameterAmount, string parameters, int minPlayers)
{
if (getEventTypeByID(id) != null)
return;
Configuration.Instance.eventTypes.Add(new EventType(id, minParameterAmount, parameters, minPlayers));
Configuration.Save();
}
public void removeEventType(string id)
{
if (getEventTypeByID(id) == null)
return;
Configuration.Instance.eventTypes.Remove(getEventTypeByID(id));
Configuration.Save();
}
public void resetSendNotifications()
{
sendNotifications = new List<bool>();
foreach (var notification in Configuration.Instance.minutesNotificationBefore)
sendNotifications.Add(false);
}
private bool compareVersion(string version1, string version2)
{
return int.Parse(version1.Replace(".", "")) > int.Parse(version2.Replace(".", ""));
}
private void onPlayerConnection(UnturnedPlayer player)
{
if (player.HasPermission("eventmanager.admin") && notifyUpdate)
{
UnturnedChat.Say(player, "A new EventManager version (" + newVersion + ") is available !!! Yours is: " + VERSION, Color.red);
notifyUpdate = false;
}
}
}
}