forked from Plugins4Unturned/VoteRewards
-
Notifications
You must be signed in to change notification settings - Fork 3
/
VoteRewards.cs
310 lines (269 loc) · 11.5 KB
/
VoteRewards.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
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
{
public static string GetVote(UnturnedPlayer player, Plugin.VoteRewardsConfig.Service service, string url)
{
WebClient wc = new WebClient();
string result = null;
if (service.APIKey == null || service.APIKey.Length == 0)
{
Logger.LogError("\nVoteRewards >> API key(s) not found\n");
return null;
}
try
{
result = wc.DownloadString(string.Format(url, service.APIKey, player.CSteamID.m_SteamID));
}
catch (WebException)
{
Logger.LogError(string.Format("\nVoteRewards >> Could not connect to {0}'s API\n", service.Name));
return null;
}
if (result.Length != 1)
{
if (result == "Error: invalid server key")
{
Logger.LogError("\nVoteRewards >> API key is invalid\n");
}
else if (result == "Error: no server key")
{
Logger.LogError("\nVoteRewards >> API key not found\n");
}
else
{
Logger.LogError(string.Format("\nVoteRewards >> {0}'s API cannot be used with this plugin\n", service.Name));
}
return null;
}
return result;
}
public static bool SetVote(UnturnedPlayer player, Plugin.VoteRewardsConfig.Service service)
{
WebClient wc = new WebClient();
string result = null;
string url = null;
if (service.Name == "unturned-servers")
{
url = "http://unturned-servers.net/api/?action=post&object=votes&element=claim&key={0}&steamid={1}";
}
else if (service.Name == "unturnedsl")
{
url = "http://unturnedsl.com/api/dedicated/post/{0}/{1}";
}
else if (service.Name == "obs.erve.me" || service.Name == "observatory")
{
url = "http://api.observatory.rocketmod.net/?server={0}&steamid={1}&claim";
}
if (service.APIKey == null || service.APIKey.Length == 0 || url == null)
{
return false;
}
try
{
result = wc.DownloadString(string.Format(url, service.APIKey, player.CSteamID.m_SteamID));
}
catch (WebException)
{
Logger.LogError(string.Format("\nVoteRewards >> Could not connect to {0}'s API\n", service.Name));
return false;
}
if (result.Length != 1)
{
Logger.LogError(string.Format("\nVoteRewards >> {0}'s API cannot be used with this plugin\n", service.Name));
return false;
}
if (result == "0") // Not claimed
{
return false;
}
else if (result == "1") // Claimed
{
return true;
}
return false;
}
public static void HandleVote(UnturnedPlayer player, bool giveReward)
{
string voteResult = null;
string serviceName = null;
var s = new Plugin.VoteRewardsConfig.Service("","");
foreach (var service in Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.Services)
{
if (service.Name == "unturned-servers")
{
if (service.APIKey == null || service.APIKey.Length == 0)
{
continue;
}
s = new Plugin.VoteRewardsConfig.Service(service.Name, service.APIKey);
voteResult = GetVote(player, s, "http://unturned-servers.net/api/?object=votes&element=claim&key={0}&steamid={1}");
serviceName = service.Name;
if (voteResult == "2")
{
continue;
}
break;
}
else if (service.Name == "unturnedsl")
{
if (service.APIKey == null || service.APIKey.Length == 0)
{
continue;
}
s = new Plugin.VoteRewardsConfig.Service(service.Name, service.APIKey);
voteResult = GetVote(player, s, "http://unturnedsl.com/api/dedicated/{0}/{1}");
serviceName = service.Name;
if (voteResult == "2")
{
continue;
}
break;
}
else if (service.Name == "obs.erve.me" || service.Name == "observatory")
{
if (service.APIKey == null || service.APIKey.Length == 0)
{
continue;
}
s = new Plugin.VoteRewardsConfig.Service(service.Name, service.APIKey);
voteResult = GetVote(player, s, "http://api.observatory.rocketmod.net/?server={0}&steamid={1}");
serviceName = service.Name;
if (voteResult == "2")
{
continue;
}
break;
}
}
if (voteResult == null && giveReward == true)
{
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("failed_to_connect"), Color.red);
}
else
{
if (voteResult == "0") // Has not voted
{
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("not_yet_voted", serviceName), Color.red);
}
else if (voteResult == "1") // Has voted & not claimed
{
if (giveReward)
{
if (SetVote(player, s))
{
GiveReward(player, serviceName);
}
else
{
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("failed_to_connect"), Color.red);
}
}
else
{
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("pending_reward"));
}
}
else if (voteResult == "2") // Has voted & claimed
{
if (giveReward)
{
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("already_voted"), Color.red);
}
}
}
}
public static void GiveReward(UnturnedPlayer player, string serviceName)
{
int sum = Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.Rewards.Sum(p => p.Chance);
string selectedElement = null;
string value = null;
System.Random r = new System.Random();
int i = 0, diceRoll = r.Next(0, sum);
foreach (var reward in Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.Rewards)
{
if (diceRoll > i && diceRoll <= i + reward.Chance)
{
selectedElement = reward.Type;
value = reward.Value;
break;
}
i = i + reward.Chance;
}
if (selectedElement == null || value == null)
{
UnturnedChat.Say(player, "The admin hasn't setup rewards yet.", Color.red);
return;
}
// Rewards
if (selectedElement == "item" || selectedElement == "i")
{
List<string> items = value.Split(',').ToList();
foreach (string item in items)
{
ushort itemID = ushort.Parse(item);
player.Inventory.tryAddItem(new Item(itemID, true), true);
}
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", "some items"));
}
else if (selectedElement == "xp" || selectedElement == "exp")
{
player.Experience += uint.Parse(value);
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", value + " xp"));
}
else if (selectedElement == "group" || selectedElement == "permission")
{
R.Permissions.AddPlayerToGroup(value, player);
R.Permissions.Reload();
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", value + " Permission Group"));
}
else if (selectedElement == "uconomy" || selectedElement == "money")
{
if (Plugin.VoteRewardsPlugin.Uconomy)
{
RocketPlugin.ExecuteDependencyCode("Uconomy", (IRocketPlugin plugin) =>
{
Uconomy.Instance.Database.IncreaseBalance(player.CSteamID.ToString(), decimal.Parse(value));
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", value + " Uconomy " + Uconomy.Instance.Configuration.Instance.MoneyName + "s"));
});
}
}
else if (selectedElement == "slot" || selectedElement.Contains("customkit"))
{
if (Plugin.VoteRewardsPlugin.CustomKits)
{
RocketPlugin.ExecuteDependencyCode("CustomKits", (IRocketPlugin plugin) =>
{
SlotManager.AddSlot(player, 1, int.Parse(value));
UnturnedChat.Say(player, Plugin.VoteRewardsPlugin.Instance.Translate("reward", "a CustomKits slot with item limit of " + value));
});
}
}
// Optional global announcement
if (Plugin.VoteRewardsPlugin.Instance.Configuration.Instance.GlobalAnnouncement)
{
foreach (SteamPlayer sP in Provider.clients)
{
var p = sP.playerID.steamID;
if (p != player.CSteamID)
{
ChatManager.say(p, Plugin.VoteRewardsPlugin.Instance.Translate("reward_announcement", player.CharacterName, serviceName), Color.green, EChatMode.GLOBAL);
}
}
}
}
}
}