-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwi-mysqlstats.sp
298 lines (247 loc) · 9.74 KB
/
kiwi-mysqlstats.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <cstrike>
#include <sourcemod>
#include "include/kiwi.inc"
#include "kiwi/util.sp"
#include "kiwi/version.sp"
#include "include/logdebug.inc"
Database db = null;
char queryBuffer[1024];
int g_MatchID = -1;
int g_MapNumber = 0;
ConVar g_ForceMatchIDCvar;
bool g_DisableStats = false;
public Plugin myinfo = {
name = "[KIWI] MySQL Stats",
author = "drop",
description = "Records match stats collected by matches to MySQL",
version = PLUGIN_VERSION,
url = "https://kiir.us"
};
public void OnPluginStart() {
InitDebugLog("kiwi_debug", "kiwi_mysql");
g_ForceMatchIDCvar = CreateConVar("kiwi_mysql_force_matchid", "0", "If set to a positive integer, this will force kiwi to use the matchid in this convar");
char error[255];
db = SQL_Connect("kiwi-stats", true, error, sizeof(error));
if (db == null) {
SetFailState("Could not connect to kiwi database: %s", error);
} else {
g_DisableStats = false;
db.SetCharset("utf8");
}
}
public void Get5_OnSeriesInit() {
g_MatchID = -1;
g_MapNumber = 0;
char seriesType[64];
char team1Name[64];
char team2Name[64];
char seriesTypeSz[sizeof(seriesType)*2 + 1];
char team1NameSz[sizeof(team1Name)*2 + 1];
char team2NameSz[sizeof(team2Name)*2 + 1];
KeyValues tmpStats = new KeyValues("Stats");
Get5_GetMatchStats(tmpStats);
tmpStats.GetString(STAT_SERIESTYPE, seriesType, sizeof(seriesType));
db.Escape(seriesType, seriesTypeSz, sizeof(seriesTypeSz));
tmpStats.GetString(STAT_SERIES_TEAM1NAME, team1Name, sizeof(team1Name));
db.Escape(team1Name, team1NameSz, sizeof(team1NameSz));
tmpStats.GetString(STAT_SERIES_TEAM2NAME, team2Name, sizeof(team2Name));
db.Escape(team2Name, team2NameSz, sizeof(team2NameSz));
delete tmpStats;
g_DisableStats = false;
if (g_ForceMatchIDCvar.IntValue > 0) {
SetMatchID(g_ForceMatchIDCvar.IntValue);
g_ForceMatchIDCvar.IntValue = 0;
Format(queryBuffer, sizeof(queryBuffer),
"INSERT INTO `kiwi_stats_matches` \
(matchid, series_type, team1_name, team2_name, start_time) VALUES \
(%d, '%s', '%s', '%s', NOW())",
g_MatchID, seriesTypeSz, team1NameSz, team2NameSz);
LogDebug(queryBuffer);
db.Query(SQLErrorCheckCallback, queryBuffer);
LogMessage("Starting match id: %d", g_MatchID);
} else {
Transaction t = SQL_CreateTransaction();
Format(queryBuffer, sizeof(queryBuffer),
"INSERT INTO `kiwi_stats_matches` \
(series_type, team1_name, team2_name, start_time) VALUES \
('%s', '%s', '%s', NOW())",
seriesTypeSz, team1NameSz, team2NameSz);
LogDebug(queryBuffer);
t.AddQuery(queryBuffer);
Format(queryBuffer, sizeof(queryBuffer),
"SELECT LAST_INSERT_ID()");
LogDebug(queryBuffer);
t.AddQuery(queryBuffer);
db.Execute(t, MatchInitSuccess, MatchInitFailure);
}
}
public void MatchInitSuccess(Database database, any data, int numQueries, Handle[] results, any[] queryData) {
Handle matchidResult = results[1];
if (SQL_FetchRow(matchidResult)) {
SetMatchID(SQL_FetchInt(matchidResult, 0));
LogMessage("Starting match id %d", g_MatchID);
} else {
LogError("Failed to get matchid from match init query");
g_DisableStats = true;
}
}
public void MatchInitFailure(Database database, any data, int numQueries, const char[] error, int failIndex, any[] queryData) {
LogError("Failed match creation query, error = %s", error);
g_DisableStats = true;
}
static void SetMatchID(int matchid) {
g_MatchID = matchid;
char idStr[32];
IntToString(g_MatchID, idStr, sizeof(idStr));
Get5_SetMatchID(idStr);
}
public void Get5_OnGoingLive(int mapNumber) {
if (g_DisableStats)
return;
g_MapNumber = mapNumber;
char mapName[255];
GetCurrentMap(mapName, sizeof(mapName));
char mapNameSz[sizeof(mapName)*2 + 1];
db.Escape(mapName, mapNameSz, sizeof(mapNameSz));
Format(queryBuffer, sizeof(queryBuffer),
"INSERT INTO `kiwi_stats_maps` \
(matchid, mapnumber, mapname, start_time) VALUES \
(%d, %d, '%s', NOW())",
g_MatchID, mapNumber, mapNameSz);
db.Query(SQLErrorCheckCallback, queryBuffer);
}
public void UpdateRoundStats(int mapNumber) {
LogMessage("UpdateRoundStats map %d", mapNumber);
// Update team scores
int t1score = CS_GetTeamScore(Get5_MatchTeamToCSTeam(MatchTeam_Team1));
int t2score = CS_GetTeamScore(Get5_MatchTeamToCSTeam(MatchTeam_Team2));
Format(queryBuffer, sizeof(queryBuffer),
"UPDATE `kiwi_stats_maps` \
SET team1_score = %d, team2_score = %d WHERE matchid = %d and mapnumber = %d",
t1score, t2score, g_MatchID, mapNumber);
LogDebug(queryBuffer);
db.Query(SQLErrorCheckCallback, queryBuffer);
// Update player stats
KeyValues kv = new KeyValues("Stats");
Get5_GetMatchStats(kv);
char mapKey[32];
Format(mapKey, sizeof(mapKey), "map%d", mapNumber);
if (kv.JumpToKey(mapKey)) {
if (kv.JumpToKey("team1")) {
AddPlayerStats(kv, MatchTeam_Team1);
kv.GoBack();
}
if (kv.JumpToKey("team2")) {
AddPlayerStats(kv, MatchTeam_Team2);
kv.GoBack();
}
kv.GoBack();
}
delete kv;
}
public void Get5_OnMapResult(const char[] map, MatchTeam mapWinner,
int team1Score, int team2Score, int mapNumber) {
if (g_DisableStats)
return;
// Update the map winner
char winnerString[64];
GetTeamString(mapWinner, winnerString, sizeof(winnerString));
Format(queryBuffer, sizeof(queryBuffer),
"UPDATE `kiwi_stats_maps` SET winner = '%s', end_time = NOW() \
WHERE matchid = %d and mapnumber = %d",
winnerString, g_MatchID, mapNumber);
LogDebug(queryBuffer);
db.Query(SQLErrorCheckCallback, queryBuffer);
// Update the series scores
int t1_seriesscore, t2_seriesscore, tmp;
Get5_GetTeamScores(MatchTeam_Team1, t1_seriesscore, tmp);
Get5_GetTeamScores(MatchTeam_Team2, t2_seriesscore, tmp);
Format(queryBuffer, sizeof(queryBuffer),
"UPDATE `kiwi_stats_matches` \
SET team1_score = %d, team2_score = %d WHERE matchid = %d",
t1_seriesscore, t2_seriesscore, g_MatchID);
LogDebug(queryBuffer);
db.Query(SQLErrorCheckCallback, queryBuffer);
}
public void AddPlayerStats(KeyValues kv, MatchTeam team) {
char name[MAX_NAME_LENGTH];
char auth[AUTH_LENGTH];
char nameSz[MAX_NAME_LENGTH*2 + 1];
char authSz[AUTH_LENGTH*2 + 1];
if (kv.GotoFirstSubKey()) {
do {
kv.GetSectionName(auth, sizeof(auth));
kv.GetString("name", name, sizeof(name));
db.Escape(auth, authSz, sizeof(authSz));
db.Escape(name, nameSz, sizeof(nameSz));
int kills = kv.GetNum(STAT_KILLS);
int deaths = kv.GetNum(STAT_DEATHS);
int flashbang_assists = kv.GetNum(STAT_FLASHBANG_ASSISTS);
int assists = kv.GetNum(STAT_ASSISTS);
int teamkills = kv.GetNum(STAT_TEAMKILLS);
int damage = kv.GetNum(STAT_DAMAGE);
int headshot_kills = kv.GetNum(STAT_HEADSHOT_KILLS);
int roundsplayed = kv.GetNum(STAT_ROUNDSPLAYED);
int plants = kv.GetNum(STAT_BOMBPLANTS);
int defuses = kv.GetNum(STAT_BOMBDEFUSES);
int v1 = kv.GetNum(STAT_V1);
int v2 = kv.GetNum(STAT_V2);
int v3 = kv.GetNum(STAT_V3);
int v4 = kv.GetNum(STAT_V4);
int v5 = kv.GetNum(STAT_V5);
int k2 = kv.GetNum(STAT_2K);
int k3 = kv.GetNum(STAT_3K);
int k4 = kv.GetNum(STAT_4K);
int k5 = kv.GetNum(STAT_5K);
char teamString[16];
GetTeamString(team, teamString, sizeof(teamString));
Format(queryBuffer, sizeof(queryBuffer),
"REPLACE INTO `kiwi_stats_players` \
(matchid, mapnumber, steamid64, team, \
rounds_played, name, kills, deaths, flashbang_assists, \
assists, teamkills, headshot_kills, damage, \
bomb_plants, bomb_defuses, \
v1, v2, v3, v4, v5, \
2k, 3k, 4k, 5k \
) VALUES \
(%d, %d, '%s', '%s', \
%d, '%s', %d, %d, %d, \
%d, %d, %d, %d, \
%d, %d, \
%d, %d, %d, %d, %d,\
%d, %d, %d, %d)",
g_MatchID, g_MapNumber, authSz, teamString,
roundsplayed, nameSz, kills, deaths, flashbang_assists,
assists, teamkills, headshot_kills, damage,
plants, defuses,
v1, v2, v3, v4, v5,
k2, k3, k4, k5);
db.Query(SQLErrorCheckCallback, queryBuffer);
} while (kv.GotoNextKey());
kv.GoBack();
}
}
public void Get5_OnSeriesResult(MatchTeam seriesWinner,
int team1MapScore, int team2MapScore) {
if (g_DisableStats)
return;
char winnerString[64];
GetTeamString(seriesWinner, winnerString, sizeof(winnerString));
Format(queryBuffer, sizeof(queryBuffer),
"UPDATE `kiwi_stats_matches` \
SET winner = '%s', team1_score = %d, team2_score = %d, end_time = NOW() \
WHERE matchid = %d",
winnerString, team1MapScore, team2MapScore, g_MatchID);
LogDebug(queryBuffer);
db.Query(SQLErrorCheckCallback, queryBuffer);
}
public int SQLErrorCheckCallback(Handle owner, Handle hndl, const char[] error, int data) {
if (!StrEqual("", error)) {
LogError("Last Connect SQL Error: %s", error);
}
}
public void Get5_OnRoundStatsUpdated() {
if (Get5_GetGameState() == GameState_Live && !g_DisableStats) {
UpdateRoundStats(g_MapNumber);
}
}