forked from ProdigySim/L4D2-Logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger-plugin.sp
266 lines (227 loc) · 7.66 KB
/
logger-plugin.sp
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
#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <l4d2_direct>
#include <socket>
#include <left4downtown>
#undef REQUIRE_PLUGIN
#include "damage_tracking"
#include "pause"
#define ENDCHECKDELAY 2.0
#define BUFFERSIZE 512
#define VERSION_INT 6
#define VERSION_STR "6"
public Plugin:myinfo =
{
name = "L4D2 Logger",
author = "CanadaRox",
description = "A plugin that logs the number of survivors that survive to a central server. Collects map name, config name, and number of survivors that survived.",
version = VERSION_STR,
url = "https://github.com/CanadaRox/L4D2-Logger"
}
new String:mapName[64];
new Handle:gSocket;
new bossFlow[2] = { -2, ... };
new Float:roundTime;
new Float:pauseTime;
new roundDamage;
new bool:damageBonusAvailable;
/* cvars */
new Handle:hVsBossBuffer;
new Handle:hPainPillDecayRate;
new Handle:hReadyEnabled;
new Handle:hCfgName;
new Handle:hCheats;
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
MarkNativeAsOptional("DamageTracking_GetRoundDamage");
return APLRes_Success;
}
public OnPluginStart()
{
HookEvent("round_start", RoundStart_Event);
HookEvent("round_end", RoundEnd_Event);
hVsBossBuffer = FindConVar("versus_boss_buffer");
hPainPillDecayRate = FindConVar("pain_pills_decay_rate");
hReadyEnabled = FindConVar("l4d_ready_enabled");
hCfgName = FindConVar("l4d_ready_cfg_name");
hCheats = FindConVar("sv_cheats");
gSocket = SocketCreate(SOCKET_UDP, OnSocketError);
SocketConnect(gSocket, OnSocketConnect, OnSocketRecv, OnSocketDisconnect, "logger.l4dpromod.com", 55555);
}
public OnAllPluginsLoaded() damageBonusAvailable = LibraryExists("damage_tracking");
public OnLibraryRemoved(const String:name[]) if (StrEqual(name, "damage_tracking")) damageBonusAvailable = false;
public OnLibraryAdded(const String:name[]) if (StrEqual(name, "damage_tracking")) damageBonusAvailable = true;
public OnMapStart()
{
GetCurrentMap(mapName, sizeof(mapName));
}
public OnMapEnd()
{
bossFlow[0] = -2;
bossFlow[1] = -2;
}
public OnSocketError(Handle:socket, const errorType, const errorNum, any:arg)
{
LogError("[L4D2 Logger] errorType: %d, errorNum: %d", errorType, errorNum);
}
public OnSocketConnect(Handle:socket, any:arg) { }
public OnSocketRecv(Handle:socket, const String:recvData[], const dataSize, any:arg) { }
public OnSocketDisconnect(Handle:socket, any:arg) { }
public Action:L4D_OnFirstSurvivorLeftSafeArea(client)
{
roundTime = GetTickedTime();
}
public OnPause()
{
pauseTime = GetTickedTime();
}
public OnUnpause()
{
roundTime -= (GetTickedTime() - pauseTime);
}
public RoundStart_Event(Handle:event, const String:name[], bool:dontBroadcas)
{
CreateTimer(15.0, RoundStart_Delay);
}
public Action:RoundStart_Delay(Handle:timer)
{
if (bossFlow[0] == -2)
{
bossFlow[0] = L4D2Direct_GetVSTankToSpawnThisRound(0) ? RoundToNearest(100*(L4D2Direct_GetVSTankFlowPercent(0) - (Float:GetConVarInt(hVsBossBuffer) / L4D2Direct_GetMapMaxFlowDistance()))) : -1;
bossFlow[1] = L4D2Direct_GetVSWitchToSpawnThisRound(0) ? RoundToNearest(100*(L4D2Direct_GetVSWitchFlowPercent(0) - (Float:GetConVarInt(hVsBossBuffer) / L4D2Direct_GetMapMaxFlowDistance()))) : -1;
}
}
public RoundEnd_Event(Handle:event, const String:name[], bool:dontBroadcast)
{
if (GetEventInt(event, "reason") == 5
&& GetConVarBool(hReadyEnabled)
&& !GetConVarBool(hCheats))
{
decl String:message[BUFFERSIZE];
new length = PrepMessage(message);
SocketSend(gSocket, message, length);
}
}
PrepMessage(String:message[BUFFERSIZE])
{
decl String:configName[50];
GetConVarString(hCfgName, configName, sizeof(configName));
new aliveSurvs = GameRules_GetProp("m_iVersusSurvivalMultiplier", _, GameRules_GetProp("m_bAreTeamsFlipped"));
new survCompletion[4] = { -1, ... };
GetPerSurvFlows(survCompletion);
new survHealth[4] = { -1, ... };
GetPerSurvHealth(survHealth);
new itemCount[3] = { 0, ... };
GetItemCount(itemCount);
roundTime = GetTickedTime() - roundTime;
roundDamage = damageBonusAvailable ? DamageTracking_GetRoundDamage(GameRules_GetProp("m_bAreTeamsFlipped")) : -1;
new offset;
offset += WriteToStringBuffer(message[offset], VERSION_INT); // 1 integer
offset += 1 + strcopy(message[offset], sizeof(message) - offset, mapName); // string
offset += 1 + strcopy(message[offset], sizeof(message) - offset, configName); // string
offset += WriteToStringBuffer(message[offset], aliveSurvs); // 1 integer
offset += WriteToStringBuffer(message[offset], L4D_GetVersusMaxCompletionScore()); // 1 integer
offset += WriteArrayToStringBuffer(message[offset], survCompletion, sizeof(survCompletion)); // 4 integers
offset += WriteArrayToStringBuffer(message[offset], survHealth, sizeof(survHealth)); // 4 integers
offset += WriteArrayToStringBuffer(message[offset], itemCount, sizeof(itemCount)); // 3 integers
offset += WriteArrayToStringBuffer(message[offset], bossFlow, sizeof(bossFlow)); // 2 integers
offset += WriteToStringBuffer(message[offset], RoundToNearest(roundTime)); // 1 integer
offset += WriteToStringBuffer(message[offset], roundDamage); // 1 integer
return offset;
}
stock GetPerSurvFlows(survFlows[4])
{
new survCount = 0;
new curTeam = GameRules_GetProp("m_bAreTeamsFlipped");
for (new client = 1; client <= MaxClients && survCount < 4; client++)
{
if(IsClientInGame(client) && IsSurvivor(client))
{
survFlows[survCount] = GameRules_GetProp("m_iVersusDistancePerSurvivor", _, survCount + 4 * curTeam);
survFlows[survCount] = survFlows[survCount] & 0xff; /* bug work around y'all! */
survCount++;
}
}
}
stock GetItemCount(itemCount[3])
{
decl tmp;
decl String:stmp[32];
for (new client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client) && IsSurvivor(client))
{
tmp = GetPlayerWeaponSlot(client, 3);
if (tmp > -1)
{
GetEdictClassname(tmp, stmp, sizeof(stmp));
if (StrEqual(stmp, "weapon_first_aid_kit"))
itemCount[0]++;
}
tmp = GetPlayerWeaponSlot(client, 4);
if (tmp > -1)
{
GetEdictClassname(tmp, stmp, sizeof(stmp));
if (StrEqual(stmp, "weapon_pain_pills"))
itemCount[1]++;
else if (StrEqual(stmp, "weapon_adrenaline"))
itemCount[2]++;
}
}
}
}
stock GetStandingSurvivorCount()
{
new survCount = 0;
for (new client = 1; client <= MaxClients; client++)
{
if (IsClientInGame(client) && IsSurvivor(client) && IsPlayerAlive(client) && !IsIncapacitated(client)) survCount++;
}
return survCount;
}
stock GetPerSurvHealth(survHealth[4])
{
new survCount = 0;
for (new client = 1; client <= MaxClients && survCount < 4; client++)
{
if (IsClientInGame(client) && IsSurvivor(client))
{
if (IsPlayerAlive(client) && !IsIncapacitated(client))
{
survHealth[survCount] = GetSurvivorPermanentHealth(client) + GetSurvivorTempHealth(client);
}
else
{
survHealth[survCount] = 0;
}
survCount++;
}
}
}
stock WriteToStringBuffer(String:str[], any:num)
{
str[0] = (_:num >> 0) & 0xff;
str[1] = (_:num >> 8) & 0xff;
str[2] = (_:num >> 16) & 0xff;
str[3] = (_:num >> 24) & 0xff;
return 4;
}
stock WriteArrayToStringBuffer(String:buf[], any:array[], length)
{
new bytesWritten;
for (new i; i < length; i++)
{
WriteToStringBuffer(buf[bytesWritten], array[i]);
bytesWritten += 4;
}
return bytesWritten;
}
stock bool:IsSurvivor(client) return GetClientTeam(client) == 2;
stock bool:IsIncapacitated(client) return bool:GetEntProp(client, Prop_Send, "m_isIncapacitated");
stock GetSurvivorPermanentHealth(client) return GetEntProp(client, Prop_Send, "m_iHealth");
stock GetSurvivorTempHealth(client)
{
new temphp = RoundToCeil(GetEntPropFloat(client, Prop_Send, "m_healthBuffer") - ((GetGameTime() - GetEntPropFloat(client, Prop_Send, "m_healthBufferTime")) * GetConVarFloat(hPainPillDecayRate))) - 1;
return (temphp > 0 ? temphp : 0);
}