forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventHandlers.cs
351 lines (310 loc) · 14 KB
/
EventHandlers.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
namespace MatchZy;
public partial class MatchZy
{
public HookResult EventPlayerConnectFullHandler(EventPlayerConnectFull @event, GameEventInfo info)
{
try
{
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
Log($"[FULL CONNECT] Player ID: {player!.UserId}, Name: {player.PlayerName} has connected!");
// Handling whitelisted players
if (!player.IsBot || !player.IsHLTV)
{
var steamId = player.SteamID;
bool kicked = HandlePlayerWhitelist(player, steamId.ToString());
if (kicked) return HookResult.Continue;
if (isMatchSetup || matchModeOnly)
{
CsTeam team = GetPlayerTeam(player);
if (team == CsTeam.None)
{
Log($"[EventPlayerConnectFull] KICKING PLAYER STEAMID: {steamId}, Name: {player.PlayerName} (NOT ALLOWED!)");
PrintToAllChat($"Kicking player {player.PlayerName} - Not a player in this game.");
KickPlayer(player);
return HookResult.Continue;
}
}
}
if (player.UserId.HasValue)
{
playerData[player.UserId.Value] = player;
connectedPlayers++;
if (readyAvailable && !matchStarted)
{
playerReadyStatus[player.UserId.Value] = false;
}
else
{
playerReadyStatus[player.UserId.Value] = true;
}
}
// May not be required, but just to be on safe side so that player data is properly updated in dictionaries
// Update: Commenting the below function as it was being called multiple times on map change.
// UpdatePlayersMap();
if (readyAvailable && !matchStarted)
{
// Start Warmup when first player connect and match is not started.
if (GetRealPlayersCount() == 1)
{
Log($"[FULL CONNECT] First player has connected, starting warmup!");
ExecUnpracCommands();
AutoStart();
}
}
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventPlayerConnectFull FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public HookResult EventPlayerDisconnectHandler(EventPlayerDisconnect @event, GameEventInfo info)
{
try
{
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if (!player!.UserId.HasValue) return HookResult.Continue;
int userId = player.UserId.Value;
if (playerReadyStatus.ContainsKey(userId))
{
playerReadyStatus.Remove(userId);
connectedPlayers--;
}
playerData.Remove(userId);
if (matchzyTeam1.coach.Contains(player))
{
matchzyTeam1.coach.Remove(player);
SetPlayerVisible(player);
player.Clan = "";
}
else if (matchzyTeam2.coach.Contains(player))
{
matchzyTeam2.coach.Remove(player);
SetPlayerVisible(player);
player.Clan = "";
}
noFlashList.Remove(userId);
lastGrenadesData.Remove(userId);
nadeSpecificLastGrenadeData.Remove(userId);
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventPlayerDisconnect FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public HookResult EventCsWinPanelRoundHandler(EventCsWinPanelRound @event, GameEventInfo info)
{
// EventCsWinPanelRound has stopped firing after Arms Race update, hence we handle knife round winner in EventRoundEnd.
// Log($"[EventCsWinPanelRound PRE] finalEvent: {@event.FinalEvent}");
// if (isKnifeRound && matchStarted)
// {
// HandleKnifeWinner(@event);
// }
return HookResult.Continue;
}
public HookResult EventCsWinPanelMatchHandler(EventCsWinPanelMatch @event, GameEventInfo info)
{
try
{
HandleMatchEnd();
// ResetMatch();
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventCsWinPanelMatch FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public HookResult EventRoundStartHandler(EventRoundStart @event, GameEventInfo info)
{
try
{
HandlePostRoundStartEvent(@event);
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventRoundStart FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public HookResult EventRoundFreezeEndHandler(EventRoundFreezeEnd @event, GameEventInfo info)
{
try
{
if (!matchStarted) return HookResult.Continue;
HashSet<CCSPlayerController> coaches = GetAllCoaches();
foreach (var coach in coaches)
{
if (!IsPlayerValid(coach)) continue;
// If coaches are still left alive after freezetime ends, this code will force them to spectate their team again.
if (coach.PlayerPawn.Value?.LifeState != (byte)LifeState_t.LIFE_ALIVE) continue;
Position coachPosition = new(coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsOrigin, coach.PlayerPawn.Value!.CBodyComponent!.SceneNode!.AbsRotation);
coach!.PlayerPawn.Value!.Teleport(new Vector(coachPosition.PlayerPosition.X, coachPosition.PlayerPosition.Y, coachPosition.PlayerPosition.Z + 20.0f), coachPosition.PlayerAngle, new Vector(0, 0, 0));
// Dropping the C4 if it was picked up or passed to the coach.
DropWeaponByDesignerName(coach, "weapon_c4");
AddTimer(1.5f, () =>
{
coach!.PlayerPawn.Value!.Teleport(new Vector(coachPosition.PlayerPosition.X, coachPosition.PlayerPosition.Y, coachPosition.PlayerPosition.Z + 20.0f), coachPosition.PlayerAngle, new Vector(0, 0, 0));
DropWeaponByDesignerName(coach, "weapon_c4");
CsTeam oldTeam = GetCoachTeam(coach);
coach.ChangeTeam(CsTeam.Spectator);
AddTimer(0.01f, () => coach.ChangeTeam(oldTeam));
});
}
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventRoundFreezeEnd FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public void OnEntitySpawnedHandler(CEntityInstance entity)
{
try
{
if (!isPractice || entity == null || entity.Entity == null) return;
if (!Constants.ProjectileTypeMap.ContainsKey(entity.Entity.DesignerName)) return;
Server.NextFrame(() => {
CBaseCSGrenadeProjectile projectile = new CBaseCSGrenadeProjectile(entity.Handle);
if (!projectile.IsValid ||
!projectile.Thrower.IsValid ||
projectile.Thrower.Value == null ||
projectile.Thrower.Value.Controller.Value == null ||
projectile.Globalname == "custom"
) return;
CCSPlayerController player = new(projectile.Thrower.Value.Controller.Value.Handle);
if(!player.IsValid || player.PlayerPawn.Value == null || !player.PlayerPawn.IsValid) return;
int client = player.UserId!.Value;
Vector position = new(projectile.AbsOrigin!.X, projectile.AbsOrigin.Y, projectile.AbsOrigin.Z);
QAngle angle = new(projectile.AbsRotation!.X, projectile.AbsRotation.Y, projectile.AbsRotation.Z);
Vector velocity = new(projectile.AbsVelocity.X, projectile.AbsVelocity.Y, projectile.AbsVelocity.Z);
string nadeType = Constants.ProjectileTypeMap[entity.Entity.DesignerName];
if (!lastGrenadesData.ContainsKey(client)) {
lastGrenadesData[client] = new();
}
if (!nadeSpecificLastGrenadeData.ContainsKey(client))
{
nadeSpecificLastGrenadeData[client] = new(){};
}
GrenadeThrownData lastGrenadeThrown = new(
position,
angle,
velocity,
player.PlayerPawn.Value.CBodyComponent!.SceneNode!.AbsOrigin,
player.PlayerPawn.Value.EyeAngles,
nadeType,
DateTime.Now
);
nadeSpecificLastGrenadeData[client][nadeType] = lastGrenadeThrown;
lastGrenadesData[client].Add(lastGrenadeThrown);
if (maxLastGrenadesSavedLimit != 0 && lastGrenadesData[client].Count > maxLastGrenadesSavedLimit)
{
lastGrenadesData[client].RemoveAt(0);
}
lastGrenadeThrownTime[(int)projectile.Index] = DateTime.Now;
if (smokeColorEnabled.Value && nadeType == "smoke")
{
CSmokeGrenadeProjectile smokeProjectile = new(entity.Handle);
smokeProjectile.SmokeColor.X = GetPlayerTeammateColor(player).R;
smokeProjectile.SmokeColor.Y = GetPlayerTeammateColor(player).G;
smokeProjectile.SmokeColor.Z = GetPlayerTeammateColor(player).B;
}
});
}
catch (Exception e)
{
Log($"[OnEntitySpawnedHandler FATAL] An error occurred: {e.Message}");
}
}
public HookResult EventPlayerDeathPreHandler(EventPlayerDeath @event, GameEventInfo info)
{
try
{
// We do not broadcast the suicide of the coach
if (!matchStarted) return HookResult.Continue;
if (@event.Attacker == @event.Userid)
{
if (matchzyTeam1.coach.Contains(@event.Attacker!) || matchzyTeam2.coach.Contains(@event.Attacker!))
{
info.DontBroadcast = true;
}
}
return HookResult.Continue;
}
catch (Exception e)
{
Log($"[EventPlayerDeathPreHandler FATAL] An error occurred: {e.Message}");
return HookResult.Continue;
}
}
public HookResult EventSmokegrenadeDetonateHandler(EventSmokegrenadeDetonate @event, GameEventInfo info)
{
if (!isPractice || isDryRun) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if(lastGrenadeThrownTime.TryGetValue(@event.Entityid, out var thrownTime))
{
PrintToPlayerChat(player!, Localizer["matchzy.pracc.smoke", player!.PlayerName, $"{(DateTime.Now - thrownTime).TotalSeconds:0.00}"]);
lastGrenadeThrownTime.Remove(@event.Entityid);
}
return HookResult.Continue;
}
public HookResult EventFlashbangDetonateHandler(EventFlashbangDetonate @event, GameEventInfo info)
{
if (!isPractice || isDryRun) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if(lastGrenadeThrownTime.TryGetValue(@event.Entityid, out var thrownTime))
{
PrintToPlayerChat(player!, Localizer["matchzy.pracc.flash", player!.PlayerName, $"{(DateTime.Now - thrownTime).TotalSeconds:0.00}"]);
lastGrenadeThrownTime.Remove(@event.Entityid);
}
return HookResult.Continue;
}
public HookResult EventHegrenadeDetonateHandler(EventHegrenadeDetonate @event, GameEventInfo info)
{
if (!isPractice || isDryRun) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if(lastGrenadeThrownTime.TryGetValue(@event.Entityid, out var thrownTime))
{
PrintToPlayerChat(player!, Localizer["matchzy.pracc.grenade", player!.PlayerName, $"{(DateTime.Now - thrownTime).TotalSeconds:0.00}"]);
lastGrenadeThrownTime.Remove(@event.Entityid);
}
return HookResult.Continue;
}
public HookResult EventMolotovDetonateHandler(EventMolotovDetonate @event, GameEventInfo info)
{
if (!isPractice || isDryRun) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if(lastGrenadeThrownTime.TryGetValue(@event.Get<int>("entityid"), out var thrownTime))
{
PrintToPlayerChat(player!, Localizer["matchzy.pracc.molotov", player!.PlayerName, $"{(DateTime.Now - thrownTime).TotalSeconds:0.00}"]);
}
return HookResult.Continue;
}
public HookResult EventDecoyDetonateHandler(EventDecoyDetonate @event, GameEventInfo info)
{
if (!isPractice || isDryRun) return HookResult.Continue;
CCSPlayerController? player = @event.Userid;
if (!IsPlayerValid(player)) return HookResult.Continue;
if(lastGrenadeThrownTime.TryGetValue(@event.Entityid, out var thrownTime))
{
PrintToPlayerChat(player!, Localizer["matchzy.pracc.decoy", player!.PlayerName, $"{(DateTime.Now - thrownTime).TotalSeconds:0.00}"]);
lastGrenadeThrownTime.Remove(@event.Entityid);
}
return HookResult.Continue;
}
}