-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2-store-dueldice.cs
230 lines (184 loc) · 9.04 KB
/
cs2-store-dueldice.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Commands.Targeting;
using StoreApi;
using System.Collections.Concurrent;
using System.Text.Json.Serialization;
namespace Store_DuelDice;
public class Store_DuelDiceConfig : BasePluginConfig
{
[JsonPropertyName("duel_commands")]
public List<string> DuelCommands { get; set; } = ["dueldice", "diceduel"];
[JsonPropertyName("acceptduel_commands")]
public List<string> AcceptDuelCommands { get; set; } = ["acceptdueldice", "acceptdiceduel"];
[JsonPropertyName("refuseduel_commands")]
public List<string> RefuseDuelCommands { get; set; } = ["refusedueldice", "refusediceduel"];
[JsonPropertyName("challenge_cooldown")]
public int ChallengeCooldown { get; set; } = 10;
[JsonPropertyName("accept_timeout")]
public int AcceptTimeout { get; set; } = 60;
[JsonPropertyName("max_dice_value")]
public int MaxDiceValue { get; set; } = 6;
[JsonPropertyName("min_bet")]
public int MinBet { get; set; } = 10;
[JsonPropertyName("max_bet")]
public int MaxBet { get; set; } = 1000;
}
public class DuelRequest
{
public CCSPlayerController Challenger { get; set; }
public CCSPlayerController Opponent { get; set; }
public int BetCredits { get; set; }
public DateTime RequestTime { get; set; }
public DuelRequest(CCSPlayerController challenger, CCSPlayerController opponent, int betCredits)
{
Challenger = challenger;
Opponent = opponent;
BetCredits = betCredits;
RequestTime = DateTime.Now;
}
}
public class Store_DuelDice : BasePlugin, IPluginConfig<Store_DuelDiceConfig>
{
public override string ModuleName => "Store Module [Duel Dice]";
public override string ModuleVersion => "0.0.1";
public override string ModuleAuthor => "Nathy";
private readonly Random random = new();
public IStoreApi? StoreApi { get; set; }
public Store_DuelDiceConfig Config { get; set; } = new();
private readonly ConcurrentDictionary<string, DuelRequest> pendingDuels = new();
private readonly ConcurrentDictionary<string, DateTime> playerLastChallengeTimes = new();
public override void OnAllPluginsLoaded(bool hotReload)
{
StoreApi = IStoreApi.Capability.Get() ?? throw new Exception("StoreApi could not be located.");
CreateCommands();
}
public void OnConfigParsed(Store_DuelDiceConfig config)
{
Config = config;
}
private void CreateCommands()
{
foreach (var cmd in Config.DuelCommands)
{
AddCommand($"css_{cmd}", "Challenge a player to a dice duel", Command_DuelDice);
}
foreach (var cmd in Config.AcceptDuelCommands)
{
AddCommand($"css_{cmd}", "Accept a dice duel challenge", Command_AcceptDuel);
}
foreach (var cmd in Config.RefuseDuelCommands)
{
AddCommand($"css_{cmd}", "Refuse a dice duel challenge", Command_RefuseDuel);
}
}
[CommandHelper(minArgs: 2, usage: "<player> <credits>")]
public void Command_DuelDice(CCSPlayerController? player, CommandInfo info)
{
if (player == null) return;
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
if (playerLastChallengeTimes.TryGetValue(player.SteamID.ToString(), out var lastChallengeTime))
{
var cooldownRemaining = (DateTime.Now - lastChallengeTime).TotalSeconds;
if (cooldownRemaining < Config.ChallengeCooldown)
{
var secondsRemaining = (int)(Config.ChallengeCooldown - cooldownRemaining);
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Challenge cooldown", secondsRemaining]);
return;
}
}
playerLastChallengeTimes[player.SteamID.ToString()] = DateTime.Now;
var targetResult = info.GetArgTargetResult(1);
var opponent = targetResult.Players.FirstOrDefault();
if (opponent == null)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Player not found"]);
return;
}
if (!int.TryParse(info.GetArg(2), out int credits) || credits <= 0)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Invalid amount of credits"]);
return;
}
if (credits < Config.MinBet)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Minimum bet amount", Config.MinBet]);
return;
}
if (credits > Config.MaxBet)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Maximum bet amount", Config.MaxBet]);
return;
}
if (StoreApi.GetPlayerCredits(player) < credits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Not enough credits"]);
return;
}
var duelRequest = new DuelRequest(player, opponent, credits);
pendingDuels[opponent.SteamID.ToString()] = duelRequest;
opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel request", player.PlayerName, credits]);
player.PrintToChat(Localizer["Prefix"] + Localizer["You send a duel", opponent.PlayerName, credits]);
AddTimer(Config.AcceptTimeout, () =>
{
if (pendingDuels.TryRemove(opponent.SteamID.ToString(), out var duel))
{
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel timeout"]);
}
});
}
public void Command_AcceptDuel(CCSPlayerController? player, CommandInfo info)
{
if (player == null) return;
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
if (!pendingDuels.TryGetValue(player.SteamID.ToString(), out var duel))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No pending duel"]);
return;
}
if (StoreApi.GetPlayerCredits(player) < duel.BetCredits)
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["Not enough credits"]);
return;
}
int challengerRoll = random.Next(1, Config.MaxDiceValue + 1);
int opponentRoll = random.Next(1, Config.MaxDiceValue + 1);
var winner = challengerRoll > opponentRoll ? duel.Challenger : (challengerRoll < opponentRoll ? duel.Opponent : null);
var loser = winner == duel.Challenger ? duel.Opponent : (winner == duel.Opponent ? duel.Challenger : null);
if (winner != null)
{
StoreApi.GivePlayerCredits(winner, duel.BetCredits);
StoreApi.GivePlayerCredits(loser, -duel.BetCredits);
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 1", duel.Challenger.PlayerName, challengerRoll]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 1", duel.Challenger.PlayerName, challengerRoll]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 2", duel.Opponent.PlayerName, opponentRoll]);
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 2", duel.Opponent.PlayerName, opponentRoll]);
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel result", winner.PlayerName, duel.BetCredits]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel result", winner.PlayerName, duel.BetCredits]);
}
else
{
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 1", duel.Challenger.PlayerName, challengerRoll]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 1", duel.Challenger.PlayerName, challengerRoll]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 2", duel.Opponent.PlayerName, opponentRoll]);
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel roll 2", duel.Opponent.PlayerName, opponentRoll]);
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel tie"]);
duel.Opponent.PrintToChat(Localizer["Prefix"] + Localizer["Duel tie"]);
}
pendingDuels.TryRemove(player.SteamID.ToString(), out _);
}
public void Command_RefuseDuel(CCSPlayerController? player, CommandInfo info)
{
if (player == null) return;
if (StoreApi == null) throw new Exception("StoreApi could not be located.");
if (!pendingDuels.TryGetValue(player.SteamID.ToString(), out var duel))
{
info.ReplyToCommand(Localizer["Prefix"] + Localizer["No pending duel"]);
return;
}
duel.Challenger.PrintToChat(Localizer["Prefix"] + Localizer["Duel refused", player.PlayerName]);
pendingDuels.TryRemove(player.SteamID.ToString(), out _);
}
}