-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmapstats.sp
282 lines (244 loc) · 7.28 KB
/
mapstats.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma semicolon 1
#include <sourcemod>
#define PLUGIN_VERSION "1.1.4"
public Plugin:myinfo =
{
name = "Map Stats",
author = "Stevo.TVR",
description = "Records server population stats for maps",
version = PLUGIN_VERSION,
url = "http://www.theville.org"
}
new Handle:hDatabase = INVALID_HANDLE;
new Handle:hTimer = INVALID_HANDLE;
new Handle:hPlayerTrie = INVALID_HANDLE;
new Handle:hSnapshots = INVALID_HANDLE;
new Handle:sm_mapstats_interval = INVALID_HANDLE;
new String:g_mapName[128];
new g_hostip;
new g_hostport;
new g_serverId;
new g_mapId;
new g_userId[MAXPLAYERS+1];
new g_playerJoins;
new g_playerQuits;
new g_mapStartTime;
new bool:g_waitingForPlayers = true;
new bool:g_mapChanging = false;
public OnPluginStart()
{
CreateConVar("sm_mapstats_version", PLUGIN_VERSION, "Map Stats plugin version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
sm_mapstats_interval = CreateConVar("sm_mapstats_interval", "300.0", "Number of seconds between population snapshots", _, true, 30.0);
HookConVarChange(sm_mapstats_interval, ConVarChanged);
AutoExecConfig(true, "mapstats");
HookEvent("player_disconnect", Event_PlayerDisconnect);
hPlayerTrie = CreateTrie();
hSnapshots = CreateArray();
SQL_TConnect(T_DBConnect, "mapstats");
}
public OnConfigsExecuted()
{
hTimer = CreateTimer(GetConVarFloat(sm_mapstats_interval), Timer_Snapshot, _, TIMER_REPEAT);
}
public ConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
if(hTimer != INVALID_HANDLE)
{
CloseHandle(hTimer);
hTimer = CreateTimer(GetConVarFloat(sm_mapstats_interval), Timer_Snapshot, _, TIMER_REPEAT);
}
}
public OnMapStart()
{
g_mapStartTime = GetTime();
LoadMap();
}
LoadMap()
{
if(hDatabase != INVALID_HANDLE)
{
decl String:query[512], String:mapName[64];
GetCurrentMap(mapName, sizeof(mapName));
SQL_EscapeString(hDatabase, mapName, g_mapName, sizeof(g_mapName));
Format(query, sizeof(query), "SELECT `id` FROM `mapstats_maps` WHERE `name` = '%s';", g_mapName);
SQL_TQuery(hDatabase, T_FetchMapId, query);
}
}
public OnMapEnd()
{
if(!g_waitingForPlayers)
{
SendSummary();
}
g_mapId = 0;
g_mapChanging = true;
CreateTimer(30.0, Timer_Mapchange);
}
SendSummary()
{
if(hDatabase != INVALID_HANDLE && g_serverId > 0 && g_mapId > 0)
{
new avgPop, num = GetArraySize(hSnapshots);
if(num > 0)
{
for(new i = 0; i < num; i++)
{
avgPop += GetArrayCell(hSnapshots, i);
}
avgPop /= num;
decl String:query[512];
Format(query, sizeof(query), "INSERT INTO `mapstats_summary` (`serverid`, `mapid`, `popavg`, `quits`, `joins`, `duration`) VALUES (%d, %d, %d, %d, %d, %d);", g_serverId, g_mapId, avgPop, g_playerQuits, g_playerJoins, GetTime() - g_mapStartTime);
SQL_TQuery(hDatabase, T_FastQuery, query);
}
}
g_playerQuits = 0;
g_playerJoins = 0;
ClearTrie(hPlayerTrie);
ClearArray(hSnapshots);
}
public OnClientAuthorized(client, const String:auth[])
{
if(g_waitingForPlayers)
{
TakeSnapshot(0);
g_mapStartTime = GetTime();
g_waitingForPlayers = false;
}
new uid = GetClientUserId(client);
if(g_userId[client] != uid)
{
g_userId[client] = uid;
new val;
if(GetTrieValue(hPlayerTrie, auth, val))
{
g_playerQuits--;
}
else
{
g_playerJoins++;
}
}
SetTrieValue(hPlayerTrie, auth, true);
}
public Action:Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast)
{
g_playerQuits++;
}
public Action:Timer_Snapshot(Handle:Timer)
{
if(!g_waitingForPlayers && !g_mapChanging)
{
new pop = GetRealClientCount();
if(pop > 0)
{
PushArrayCell(hSnapshots, pop);
TakeSnapshot(pop);
}
else
{
TakeSnapshot(0);
SendSummary();
g_waitingForPlayers = true;
}
}
}
public Action:Timer_Mapchange(Handle:Timer)
{
g_mapChanging = false;
}
TakeSnapshot(pop)
{
if(hDatabase != INVALID_HANDLE && g_serverId > 0 && g_mapId > 0)
{
decl String:query[512];
Format(query, sizeof(query), "INSERT INTO `mapstats_pop` (`serverid`, `mapid`, `pop`) VALUES (%d, %d, %d);", g_serverId, g_mapId, pop);
SQL_TQuery(hDatabase, T_FastQuery, query);
}
}
GetRealClientCount()
{
new clients = 0;
for(new i = 1; i <= MaxClients; i++)
{
if(IsClientInGame(i) && !IsFakeClient(i))
{
clients++;
}
}
return clients;
}
// Threaded DB stuff
public T_DBConnect(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl == INVALID_HANDLE)
{
SetFailState(error);
}
hDatabase = hndl;
SQL_TQuery(hndl, T_FastQuery, "CREATE TABLE IF NOT EXISTS `mapstats_servers` (`id` int NOT NULL AUTO_INCREMENT, `ip` varchar(64) NOT NULL, `name` text NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ip` (`ip`)) ENGINE=InnoDB;");
SQL_TQuery(hndl, T_FastQuery, "CREATE TABLE IF NOT EXISTS `mapstats_maps` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`)) ENGINE=InnoDB;");
SQL_TQuery(hndl, T_FastQuery, "CREATE TABLE IF NOT EXISTS `mapstats_pop` (`serverid` int NOT NULL, `mapid` int NOT NULL, `pop` int NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB;");
SQL_TQuery(hndl, T_FastQuery, "CREATE TABLE IF NOT EXISTS `mapstats_summary` (`serverid` int NOT NULL, `mapid` int NOT NULL, `popavg` int NOT NULL, `quits` int NOT NULL, `joins` int NOT NULL, `duration` int NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB;");
g_hostip = GetConVarInt(FindConVar("hostip"));
g_hostport = GetConVarInt(FindConVar("hostport"));
decl String:query[512];
Format(query, sizeof(query), "SELECT `id` FROM `mapstats_servers` WHERE `ip` = '%d:%d';", g_hostip, g_hostport);
SQL_TQuery(hndl, T_FetchServerId, query);
LoadMap();
}
public T_FetchServerId(Handle:owner, Handle:hndl, const String:error[], any:data)
{
decl String:serverName[256], String:serverNameSafe[512], String:query[1024];
GetConVarString(FindConVar("hostname"), serverName, sizeof(serverName));
SQL_EscapeString(hDatabase, serverName, serverNameSafe, sizeof(serverNameSafe));
if(hndl != INVALID_HANDLE)
{
if(SQL_GetRowCount(hndl) > 0)
{
if(SQL_FetchRow(hndl))
{
g_serverId = SQL_FetchInt(hndl, 0);
Format(query, sizeof(query), "UPDATE `mapstats_servers` SET `name` = '%s' WHERE `id` = %d;", serverNameSafe, g_serverId);
SQL_TQuery(hDatabase, T_FastQuery, query);
return;
}
}
}
Format(query, sizeof(query), "INSERT INTO `mapstats_servers` (`ip`, `name`) VALUES ('%d:%d', '%s');", g_hostip, g_hostport, serverNameSafe);
SQL_TQuery(hDatabase, T_InsertServer, query);
}
public T_InsertServer(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl != INVALID_HANDLE && SQL_GetAffectedRows(owner) > 0)
{
g_serverId = SQL_GetInsertId(owner);
}
}
public T_FetchMapId(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl != INVALID_HANDLE)
{
if(SQL_GetRowCount(hndl) > 0)
{
if(SQL_FetchRow(hndl))
{
g_mapId = SQL_FetchInt(hndl, 0);
return;
}
}
}
decl String:query[512];
Format(query, sizeof(query), "INSERT INTO `mapstats_maps` (`name`) VALUES ('%s');", g_mapName);
SQL_TQuery(hDatabase, T_InsertMap, query);
}
public T_InsertMap(Handle:owner, Handle:hndl, const String:error[], any:data)
{
if(hndl != INVALID_HANDLE && SQL_GetAffectedRows(owner) > 0)
{
g_mapId = SQL_GetInsertId(owner);
}
}
public T_FastQuery(Handle:owner, Handle:hndl, const String:error[], any:data)
{
// Nothing to do
}