forked from notnoahj/Votifier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVotifier.cs
207 lines (181 loc) · 7.77 KB
/
Votifier.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
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned.Player;
using System;
using System.Collections.Generic;
using System.Linq;
using fr34kyn01535.Votifier.Config;
using Rocket.API.DependencyInjection;
using Rocket.API.Eventing;
using Rocket.API.Scheduler;
using Rocket.API.User;
using Rocket.Core.I18N;
using Rocket.Core.Player.Events;
using Rocket.Core.Scheduler;
namespace fr34kyn01535.Votifier
{
public class Votifier : Plugin<VotifierConfiguration>, IEventListener<PlayerConnectedEvent>
{
private readonly IUserManager _proxiedUserManager;
private readonly ITaskScheduler _scheduler;
private readonly Queue<VoteResult> _queue;
private ITask _updateTask;
public Votifier(IDependencyContainer container, IUserManager proxiedUserManager, ITaskScheduler scheduler) : base("Votifier", container)
{
_proxiedUserManager = proxiedUserManager;
_scheduler = scheduler;
_queue = new Queue<VoteResult>();
}
protected override void OnLoad(bool isFromReload)
{
base.OnLoad(isFromReload);
EventManager.AddEventListener(this, this);
_updateTask = _scheduler.ScheduleEveryAsyncFrame(this, UpdateTask, "VotifierHandleTask");
}
protected override void OnUnload()
{
_updateTask?.Cancel();
}
void Votifier_OnPlayerVoted(UnturnedPlayer player, ServiceDefinition definition)
{
int propabilysum = ConfigurationInstance.RewardBundles.Sum(p => p.Probability);
RewardBundle bundle = new RewardBundle();
if (propabilysum != 0)
{
Random r = new Random();
int i = 0, diceRoll = r.Next(0, propabilysum);
foreach (RewardBundle b in ConfigurationInstance.RewardBundles)
{
if (diceRoll > i && diceRoll <= i + b.Probability)
{
bundle = b;
break;
}
i = i + b.Probability;
}
}
else
{
Logger.LogWarning(Translations.Get("no_rewards_found"));
return;
}
foreach (Reward reward in bundle.Rewards)
{
if (!player.GiveItem(reward.ItemId, reward.Amount))
{
Logger.LogError(Translations.Get("vote_give_error_message", player.CharacterName, reward.ItemId, reward.Amount));
}
}
_proxiedUserManager.BroadcastLocalized(Translations, "vote_success_message", player.CharacterName, definition.Name, bundle.Name);
}
public override Dictionary<string, string> DefaultTranslations => new Dictionary<string, string>
{
{"no_apikeys_message","No apikeys supplied."},
{"api_unknown_message", "The API for {0} is unknown"},
{"api_down_message","Can't reach {0}, is it down?!"},
{"not_yet_voted","You have not yet voted for this server on: {0}"},
{"no_rewards_found","Failed finding any rewardbundles"},
{"vote_give_error_message","Failed giving a item to {0} ({1},{2})"},
{"vote_success_message","{0} voted on {1} and received the \"{2}\" bundle"},
{"vote_pending_message","You have an outstanding reward for your vote on {0}"},
{"vote_due_message","You have already voted for this server on {0}, Thanks!"}
};
public void Vote(UnturnedPlayer caller, bool giveItemDirectly = true)
{
try
{
if (ConfigurationInstance.Services.FirstOrDefault(s => !String.IsNullOrEmpty(s.APIKey)) == null)
{
Logger.LogError(Translations.Get("no_apikeys_message")); return;
}
List<Service> services = ConfigurationInstance.Services.Where(s => !String.IsNullOrEmpty(s.APIKey)).ToList();
foreach (Service service in services)
{
ServiceDefinition apiDefinition = ConfigurationInstance.ServiceDefinitions.FirstOrDefault(s => s.Name == service.Name);
if (apiDefinition == null)
{
Logger.LogWarning(Translations.Get("api_unknown_message", service.Name)); return;
}
try
{
VotifierWebclient wc = new VotifierWebclient();
wc.DownloadStringCompleted += (sender, e) => OnDownloadStringCompleted(e, caller, service, apiDefinition, giveItemDirectly);
wc.DownloadStringAsync(new Uri(String.Format(apiDefinition.CheckHasVoted, service.APIKey, caller.ToString())));
}
catch (TimeoutException)
{
Logger.LogError(Translations.Get("api_down_message", service.Name));
caller.User.SendLocalizedMessage(Translations, "api_down_message", service.Name);
}
}
}
catch (Exception ex)
{
Logger.LogError(null, ex);
}
}
private void OnDownloadStringCompleted(System.Net.DownloadStringCompletedEventArgs e, UnturnedPlayer caller, Service service, ServiceDefinition apidefinition, bool giveItemDirectly)
{
VoteResult v = new VoteResult
{
Caller = caller,
Result = e.Result,
ApiDefinition = apidefinition,
Service = service,
GiveItemDirectly = giveItemDirectly
};
lock (_queue)
{
_queue.Enqueue(v);
}
}
private void UpdateTask()
{
lock (_queue)
{
if (_queue.Count > 0)
{
VoteResult v = _queue.Dequeue();
HandleVote(v);
}
}
}
private void HandleVote(VoteResult result)
{
switch (result.Result)
{
case "0":
result.Caller.User.SendLocalizedMessage(Translations, "not_yet_voted", result.Service.Name);
break;
case "1":
if (result.GiveItemDirectly)
{
if (!ConfigurationInstance.EnableRewardBundles)
return;
EventManager.Emit(this, new PlayerVotedEvent(result.Caller, result.ApiDefinition), (e) =>
{
var @event = (PlayerVotedEvent) e;
if (@event.IsCancelled)
return;
Votifier_OnPlayerVoted((UnturnedPlayer)@event.Player, @event.Service);
});
new VotifierWebclient().DownloadStringAsync(new Uri(String.Format(result.ApiDefinition.ReportSuccess, result.Service.APIKey, result.Caller.ToString())));
return;
}
else
{
result.Caller.User.SendLocalizedMessage(Translations, "vote_pending_message", result.Service.Name);
return;
}
case "2":
result.Caller.User.SendLocalizedMessage(Translations, "vote_due_message", result.Service.Name);
break;
}
}
public void HandleEvent(IEventEmitter emitter, PlayerConnectedEvent @event)
{
if (@event.Player is UnturnedPlayer player)
Vote(player, false);
}
}
}