-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinService.cs
More file actions
325 lines (262 loc) · 9.53 KB
/
Copy pathCoinService.cs
File metadata and controls
325 lines (262 loc) · 9.53 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
using System;
using System.Collections.Generic;
using System.Linq;
using BetterCoinflipsRewritten.Effects;
using BetterCoinflipsRewritten.Effects.Bad;
using BetterCoinflipsRewritten.Effects.Good;
using Exiled.API.Features;
namespace BetterCoinflipsRewritten
{
public sealed class CoinService
{
private readonly Plugin plugin;
private readonly Random random;
private readonly Dictionary<string, CoinState> playerStates;
private readonly List<WeightedEffect> goodEffects;
private readonly List<WeightedEffect> badEffects;
public CoinService(Plugin plugin)
{
this.plugin = plugin;
random = new Random();
playerStates = new Dictionary<string, CoinState>();
goodEffects = new List<WeightedEffect>();
badEffects = new List<WeightedEffect>();
RegisterEffects();
}
private void RegisterEffects()
{
goodEffects.Clear();
badEffects.Clear();
goodEffects.Add(
new WeightedEffect(
new RandomKeycardEffect(plugin.Config),
plugin.Config.KeycardChance));
goodEffects.Add(
new WeightedEffect(
new MedicalKitEffect(),
plugin.Config.MedicalKitChance));
goodEffects.Add(
new WeightedEffect(
new TeleportToEscapeEffect(),
plugin.Config.TpToEscapeChance));
goodEffects.Add(
new WeightedEffect(
new HealEffect(plugin.Config.HealAmount),
plugin.Config.HealChance));
goodEffects.Add(
new WeightedEffect(
new MoreHealthEffect(plugin.Config.MoreHpPercent),
plugin.Config.MoreHpChance));
goodEffects.Add(
new WeightedEffect(
new HatEffect(),
plugin.Config.HatChance));
goodEffects.Add(
new WeightedEffect(
new OneAmmoLogicerEffect(),
plugin.Config.OneAmmoLogicerChance));
goodEffects.Add(
new WeightedEffect(
new LightbulbEffect(),
plugin.Config.LightbulbChance));
goodEffects.Add(
new WeightedEffect(
new PinkCandyEffect(),
plugin.Config.PinkCandyChance));
goodEffects.Add(
new WeightedEffect(
new SizeChangeEffect(),
plugin.Config.SizeChangeChance));
badEffects.Add(
new WeightedEffect(
new HpReductionEffect(
plugin.Config.HpReductionPercent),
plugin.Config.HpReductionChance));
badEffects.Add(
new WeightedEffect(
new TeleportToClassDCellsEffect(),
plugin.Config.TpToClassDCellsChance));
badEffects.Add(
new WeightedEffect(
new LightsOutEffect(
plugin.Config.LightsOutDuration),
plugin.Config.LightsOutChance));
badEffects.Add(
new WeightedEffect(
new TrollFlashEffect(
plugin.Config.TrollFlashFuseTime),
plugin.Config.TrollFlashChance));
badEffects.Add(
new WeightedEffect(
new OneHpLeftEffect(),
plugin.Config.OneHpLeftChance));
badEffects.Add(
new WeightedEffect(
new InventoryResetEffect(),
plugin.Config.InventoryResetChance));
badEffects.Add(
new WeightedEffect(
new InstantExplosionEffect(
plugin.Config.InstantExplosionFuseTime),
plugin.Config.InstantExplosionChance));
badEffects.Add(
new WeightedEffect(
new RandomTeleportEffect(),
plugin.Config.RandomTeleportChance));
Log.Info(
"[BetterCoinflipsRewritten] Registered " +
goodEffects.Count +
" positive effects and " +
badEffects.Count +
" negative effects.");
}
public bool IsOnCooldown(
Player player,
out double secondsRemaining)
{
secondsRemaining = 0;
if (player == null)
return true;
CoinState state;
if (!playerStates.TryGetValue(player.UserId, out state))
return false;
double elapsedSeconds =
(DateTime.UtcNow - state.LastFlipTime).TotalSeconds;
double cooldown = plugin.Config.CoinCooldown;
if (elapsedSeconds >= cooldown)
return false;
secondsRemaining = cooldown - elapsedSeconds;
return true;
}
public void SetCooldown(Player player)
{
if (player == null)
return;
CoinState state;
if (!playerStates.TryGetValue(player.UserId, out state))
{
state = new CoinState();
playerStates.Add(player.UserId, state);
}
state.LastFlipTime = DateTime.UtcNow;
}
public ICoinEffect SelectEffect(
bool isTails,
Player player)
{
List<WeightedEffect> source =
isTails ? badEffects : goodEffects;
List<WeightedEffect> availableEffects =
source.Where(
effect =>
effect != null &&
effect.Effect != null &&
effect.Weight > 0 &&
effect.Effect.CanApply(player))
.ToList();
if (availableEffects.Count == 0)
return null;
int totalWeight =
availableEffects.Sum(effect => effect.Weight);
if (totalWeight <= 0)
return null;
int selectedNumber =
random.Next(1, totalWeight + 1);
foreach (WeightedEffect weightedEffect in availableEffects)
{
selectedNumber -= weightedEffect.Weight;
if (selectedNumber <= 0)
return weightedEffect.Effect;
}
return availableEffects[
availableEffects.Count - 1].Effect;
}
public void ExecuteFlip(
Player player,
bool isTails)
{
if (player == null ||
!player.IsConnected ||
!player.IsAlive)
{
return;
}
ICoinEffect selectedEffect =
SelectEffect(isTails, player);
if (selectedEffect == null)
{
Log.Warn(
"[BetterCoinflipsRewritten] No available effect was " +
"found for " +
player.Nickname +
".");
player.ShowHint(
plugin.Translation.CoinFlipTitle +
"\n" +
plugin.Translation.NoAvailableEffect,
plugin.Config.EffectMessageDuration);
return;
}
try
{
selectedEffect.Execute(player);
string translatedResult =
isTails
? plugin.Translation.TailsName
: plugin.Translation.HeadsName;
string resultLine =
plugin.Translation.ResultFormat.Replace(
"{result}",
translatedResult);
string effectLine =
plugin.Translation.EffectFormat.Replace(
"{effect}",
selectedEffect.Message);
string resultMessage =
plugin.Translation.CoinFlipTitle +
"\n" +
resultLine +
"\n" +
effectLine;
player.ShowHint(
resultMessage,
plugin.Config.EffectMessageDuration);
Log.Info(
"[BetterCoinflipsRewritten] " +
player.Nickname +
" flipped " +
(isTails ? "tails" : "heads") +
". Effect: " +
selectedEffect.Name +
".");
}
catch (Exception exception)
{
Log.Error(
"[BetterCoinflipsRewritten] Error while executing effect " +
selectedEffect.Name +
": " +
exception);
if (player.IsConnected)
{
player.ShowHint(
plugin.Translation.EffectError,
plugin.Config.EffectMessageDuration);
}
}
}
public void RemovePlayer(Player player)
{
if (player == null ||
string.IsNullOrEmpty(player.UserId))
{
return;
}
playerStates.Remove(player.UserId);
}
public void Clear()
{
playerStates.Clear();
}
}
}