forked from RocketModPlugins/VoteRewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoteRewards.cs
More file actions
351 lines (317 loc) · 14.4 KB
/
Copy pathVoteRewards.cs
File metadata and controls
351 lines (317 loc) · 14.4 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
using System.Net;
using System.Linq;
using System.Collections.Generic;
using Rocket.Unturned.Player;
using Rocket.Unturned.Chat;
using Rocket.Core.Plugins;
using Rocket.Core;
using Rocket.API;
using SDG.Unturned;
using UnityEngine;
using fr34kyn01535.Uconomy;
using Teyhota.CustomKits;
using Logger = Rocket.Core.Logging.Logger;
namespace Teyhota.VoteRewards
{
public class VoteRewards
{
// Reuse a single WebClient per thread — avoids repeated allocation/disposal
// while remaining thread-safe (each thread gets its own instance).
[System.ThreadStatic]
private static WebClient _wc;
private static WebClient Wc => _wc ?? (_wc = new WebClient());
[System.ThreadStatic]
private static System.Random _rng;
private static System.Random Rng => _rng ?? (_rng = new System.Random());
// ── API helpers ──────────────────────────────────────────────────────────
public static string GetVote(UnturnedPlayer player,
Plugin.VoteRewardsConfig.Service service,
string url)
{
if (string.IsNullOrEmpty(service.APIKey))
{
Logger.LogError("\nVoteRewards >> API key(s) not found\n");
return null;
}
string result;
try
{
result = Wc.DownloadString(
string.Format(url, service.APIKey, player.CSteamID.m_SteamID));
}
catch (WebException)
{
Logger.LogError(
$"\nVoteRewards >> Could not connect to {service.Name}'s API\n");
return null;
}
if (result.Length != 1)
{
switch (result)
{
case "Error: invalid server key":
Logger.LogError("\nVoteRewards >> API key is invalid\n");
break;
case "Error: no server key":
Logger.LogError("\nVoteRewards >> API key not found\n");
break;
default:
Logger.LogError(
$"\nVoteRewards >> {service.Name}'s API cannot be used with this plugin\n");
break;
}
return null;
}
return result;
}
public static bool SetVote(UnturnedPlayer player,
Plugin.VoteRewardsConfig.Service service)
{
string url;
switch (service.Name)
{
case "unturned-servers":
url = "http://unturned-servers.net/api/?action=post&object=votes&element=claim&key={0}&steamid={1}";
break;
case "unturnedsl":
url = "http://unturnedsl.com/api/dedicated/post/{0}/{1}";
break;
case "obs.erve.me":
case "observatory":
url = "http://api.observatory.rocketmod.net/?server={0}&steamid={1}&claim";
break;
default:
return false;
}
if (string.IsNullOrEmpty(service.APIKey))
return false;
string result;
try
{
result = Wc.DownloadString(
string.Format(url, service.APIKey, player.CSteamID.m_SteamID));
}
catch (WebException)
{
Logger.LogError(
$"\nVoteRewards >> Could not connect to {service.Name}'s API\n");
return false;
}
// API must return a single character; anything else is unexpected.
return result.Length == 1 && result == "1";
}
// ── Vote flow ────────────────────────────────────────────────────────────
public static void HandleVote(UnturnedPlayer player, bool giveReward)
{
string voteResult = null;
string serviceName = null;
string lastClaimedService = null;
Plugin.VoteRewardsConfig.Service matched = null;
var services = Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.Services;
for (int idx = 0; idx < services.Count; idx++)
{
var service = services[idx];
if (string.IsNullOrEmpty(service.APIKey))
continue;
string pollUrl;
switch (service.Name)
{
case "unturned-servers":
pollUrl = "http://unturned-servers.net/api/?object=votes&element=claim&key={0}&steamid={1}";
break;
case "unturnedsl":
pollUrl = "http://unturnedsl.com/api/dedicated/{0}/{1}";
break;
case "obs.erve.me":
case "observatory":
pollUrl = "http://api.observatory.rocketmod.net/?server={0}&steamid={1}";
break;
default:
continue;
}
string result = GetVote(player, service, pollUrl);
if (result == "2") // already claimed on this service — try next
{
lastClaimedService = service.Name; // remember it in case all services return "2"
continue;
}
voteResult = result;
serviceName = service.Name;
matched = service;
break;
}
// If all services returned "2", surface that instead of "failed_to_connect"
if (voteResult == null)
{
if (lastClaimedService != null)
{
voteResult = "2";
serviceName = lastClaimedService;
}
else
{
if (giveReward)
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate("failed_to_connect"), Color.red);
return;
}
}
switch (voteResult)
{
case "0": // Has not voted
QueueSay(player,
Plugin.VoteRewardsPlugin.Instance.Translate("not_yet_voted", serviceName),
Color.red);
break;
case "1": // Has voted, reward not yet claimed
if (giveReward)
{
if (SetVote(player, matched))
GiveReward(player, serviceName);
else
QueueSay(player,
Plugin.VoteRewardsPlugin.Instance.Translate("failed_to_connect"),
Color.red);
}
else
{
QueueSay(player,
Plugin.VoteRewardsPlugin.Instance.Translate("pending_reward"));
}
break;
case "2": // Has voted and already claimed
if (giveReward) // May want to drop this if-check.
QueueSay(player,
Plugin.VoteRewardsPlugin.Instance.Translate("already_voted"),
Color.red);
break;
}
}
// ── Reward dispatch ──────────────────────────────────────────────────────
public static void GiveReward(UnturnedPlayer player, string serviceName)
{
var rewards = Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.Rewards;
if (rewards == null || rewards.Count == 0)
{
QueueSay(player, "The admin hasn't setup rewards yet.", Color.red);
return;
}
// Weighted random pick — fix: use >= 0 lower bound so diceRoll == 0
// correctly falls into the first bucket (was strict > causing misses).
int sum = rewards.Sum(r => r.Chance);
int diceRoll = Rng.Next(0, sum); // [0, sum)
int cursor = 0;
Plugin.VoteRewardsConfig.Reward selected = null;
foreach (var reward in rewards)
{
cursor += reward.Chance;
if (diceRoll < cursor) // fixed: >= lower bound, < upper bound
{
selected = reward;
break;
}
}
if (selected == null)
{
// Mathematically unreachable after the fix, but kept as a safety net.
QueueSay(player, "The admin hasn't setup rewards yet.", Color.red);
return;
}
string type = selected.Type;
string value = selected.Value;
if (type == "item" || type == "i")
{
// Parse item IDs once, off-thread — avoids per-item parsing on main thread.
string[] parts = value.Split(',');
ushort[] itemIDs = new ushort[parts.Length];
for (int i = 0; i < parts.Length; i++)
itemIDs[i] = ushort.Parse(parts[i].Trim());
Rocket.Core.Utils.TaskDispatcher.QueueOnMainThread(() =>
{
for (int i = 0; i < itemIDs.Length; i++)
player.Inventory.tryAddItem(new Item(itemIDs[i], true), true);
});
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", "some items"));
}
else if (type == "xp" || type == "exp")
{
uint xp = uint.Parse(value);
// Experience write must be on main thread (engine field).
Rocket.Core.Utils.TaskDispatcher.QueueOnMainThread(() => player.Experience += xp);
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", value + " xp"));
}
else if (type == "group" || type == "permission")
{
// Permission mutations are thread-safe in RocketMod's XML provider.
R.Permissions.AddPlayerToGroup(value, player);
// Not needed: R.Permissions.Reload();
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", value + " Permission Group"));
}
else if (type == "uconomy" || type == "money")
{
if (Plugin.VoteRewardsPlugin.Uconomy)
{
decimal amount = decimal.Parse(value);
RocketPlugin.ExecuteDependencyCode("Uconomy", (IRocketPlugin plugin) =>
{
Uconomy.Instance.Database.IncreaseBalance(
player.CSteamID.ToString(), amount);
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate(
"reward",
value + " Uconomy " + Uconomy.Instance.Configuration.Instance.MoneyName + "s"));
});
}
}
else if (type == "slot" || type.Contains("customkit"))
{
if (Plugin.VoteRewardsPlugin.CustomKits)
{
int slotValue = int.Parse(value);
RocketPlugin.ExecuteDependencyCode("CustomKits", (IRocketPlugin plugin) =>
{
SlotManager.AddSlot(player, 1, slotValue);
QueueSay(player, Plugin.VoteRewardsPlugin.Instance.Translate(
"reward",
"a CustomKits slot with item limit of " + value));
});
}
}
// Optional global announcement — batch snapshot to avoid repeated Provider.clients access.
if (Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.GlobalAnnouncement)
{
string charName = player.CharacterName;
Rocket.Core.Utils.TaskDispatcher.QueueOnMainThread(() =>
{
// Snapshot inside main thread where Provider.clients is safe to read.
var clients = Provider.clients;
for (int i = 0; i < clients.Count; i++)
{
var sP = clients[i];
if (sP.playerID.steamID != player.CSteamID)
{
UnturnedChat.Say(
UnturnedPlayer.FromSteamPlayer(sP),
Plugin.VoteRewardsPlugin.Instance.Translate(
"reward_announcement", charName, serviceName),
Color.green);
}
}
});
}
}
// ── Helpers ──────────────────────────────────────────────────────────────
private static void QueueSay(UnturnedPlayer player, string message, Color? color = null)
{
if (color.HasValue)
{
Color c = color.Value;
Rocket.Core.Utils.TaskDispatcher.QueueOnMainThread(
() => UnturnedChat.Say(player, message, c));
}
else
{
Rocket.Core.Utils.TaskDispatcher.QueueOnMainThread(
() => UnturnedChat.Say(player, message));
}
}
}
}