-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayersBet.cs
216 lines (179 loc) · 5.16 KB
/
PlayersBet.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;
namespace PlayersBet;
public class PlayersBet: BasePlugin
{
public override string ModuleName => "PlayersBet";
public override string ModuleVersion => "1.1.2";
public override string ModuleAuthor => "Dliix66";
public override string ModuleDescription => "Allow players to bet money on a team for the round.";
public string prefix = $"[{ChatColors.Green}PlayersBet{ChatColors.Default}]";
private Dictionary<ulong, BetData> _currentBets = new Dictionary<ulong, BetData>();
private bool _isInRound = false;
public override void Load(bool hotReload)
{
base.Load(hotReload);
AddCommand("bet", "bet", CommandBet);
Server.NextFrame(() => { Server.PrintToChatAll($"{prefix} Plugin {(hotReload ? "hot-re" : "")}loaded!"); });
}
[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart evnt, GameEventInfo info)
{
_isInRound = true;
_currentBets.Clear();
return HookResult.Continue;
}
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd evnt, GameEventInfo info)
{
CsTeam winningTeam = (CsTeam)evnt.Winner;
if (winningTeam == CsTeam.Spectator || winningTeam == CsTeam.None)
return HookResult.Continue;
foreach (KeyValuePair<ulong, BetData> currentBet in _currentBets)
{
CCSPlayerController player = currentBet.Value.player;
if (player.IsValid == false)
continue;
if (currentBet.Value.team == winningTeam)
{
int total = currentBet.Value.earnings + currentBet.Value.amountBet;
player.AddMoney(total);
player.PrintToChat($"{prefix} You won ${currentBet.Value.earnings} for your ${currentBet.Value.amountBet} bet!");
}
else
{
player.PrintToChat($"{prefix} You lost your bet...");
}
}
_isInRound = false;
return HookResult.Continue;
}
private void CommandBet(CCSPlayerController player, CommandInfo commandInfo)
{
if (player.IsValid == false || player.InGameMoneyServices == null)
return;
string usage = $"{prefix} Usage: {commandInfo.GetArg(0)} <t/ct> <amount/half/all>";
if (!IsCommandValid(player, commandInfo, 2, usage))
return;
string teamArg = commandInfo.GetArg(1).ToLower();
CsTeam team;
if (teamArg == "t")
{
team = CsTeam.Terrorist;
}
else if (teamArg == "ct")
{
team = CsTeam.CounterTerrorist;
}
else
{
player.PrintToChat(usage);
return;
}
string amountArg = commandInfo.GetArg(2).ToLower();
int amount;
if (amountArg == "all")
{
amount = player.InGameMoneyServices.Account;
}
else if (amountArg == "half")
{
amount = player.InGameMoneyServices.Account / 2;
}
else if (Int32.TryParse(amountArg, out amount) == false)
{
player.PrintToChat(usage);
return;
}
if (player.TeamNum == (byte)CsTeam.Spectator || player.TeamNum == (byte)CsTeam.None)
{
player.PrintToChat($"{prefix} Can not bet while spectating...");
return;
}
if (player.PawnIsAlive)
{
player.PrintToChat($"{prefix} Can not bet while alive...");
return;
}
if (_isInRound == false)
{
player.PrintToChat($"{prefix} Can not bet out of a round...");
return;
}
if (amount > player.InGameMoneyServices.Account)
{
player.PrintToChat($"{prefix} Can not bet this much...");
return;
}
ulong steamId = player.SteamID;
if (_currentBets.ContainsKey(steamId))
{
player.PrintToChat($"{prefix} You already placed a bet for this round.");
return;
}
int earnings = CalculateBet(amount, team);
if (earnings <= 0)
{
player.PrintToChat($"{prefix} Can not bet now.");
return;
}
player.AddMoney(-amount);
_currentBets.Add(steamId, new BetData()
{
amountBet = amount,
earnings = earnings,
player = player,
team = team
});
player.PrintToChat($"{prefix} You would earn ${earnings} if you win your ${amount} bet.");
}
private int CalculateBet(int amount, CsTeam teamBet)
{
List<CCSPlayerController> allPlayers = Utilities.GetPlayers().Where(p => p.IsValid && p.Connected == PlayerConnectedState.PlayerConnected).ToList();
int tAlive = 0;
int ctAlive = 0;
foreach (CCSPlayerController player in allPlayers)
{
// not valid or not playing
if (player.IsValid == false || player.TeamNum == (byte)CsTeam.Spectator || player.TeamNum == (byte)CsTeam.None)
continue;
// dead
if (player.PlayerPawn.IsValid == false || player.PlayerPawn.Value.Health <= 0)
continue;
if (player.TeamNum == (byte)CsTeam.Terrorist)
tAlive++;
else if (player.TeamNum == (byte)CsTeam.CounterTerrorist)
ctAlive++;
}
if (tAlive == 0 || ctAlive == 0)
return 0;
float multiplier;
if (teamBet == CsTeam.Terrorist)
{
multiplier = (float)ctAlive / (float)tAlive;
}
else
{
multiplier = (float)tAlive / (float)ctAlive;
}
return (int)(amount * multiplier);
}
private bool IsCommandValid(CCSPlayerController player, CommandInfo command, int argCount, string usage)
{
if (player == null)
{
return false;
}
if (player.IsValid == false)
return false;
if (command.ArgCount != argCount + 1)
{
player.PrintToConsole(usage);
return false;
}
return true;
}
}