AMXX-BG.INFO hns_jumpstats_sql.inc Raw include

hns_jumpstats_sql.inc

Original include source with line numbers.

Back Download .inc
1 #include <sqlx>
2
3 #define TASK_CHECK_SQL_PLAYER 13378
4 ///////////////////////////////////////sql code///////////////////
5 public stats_sql()
6 {
7 new host[64], user[64], pass[64], db[64];
8
9 get_pcvar_string(uq_host, host, 63);
10 get_pcvar_string(uq_user, user, 63);
11 get_pcvar_string(uq_pass, pass, 63);
12 get_pcvar_string(uq_db, db, 63);
13
14 DB_TUPLE = SQL_MakeDbTuple(host, user, pass, db);
15
16 new error;
17 SqlConnection = SQL_Connect(DB_TUPLE,error,g_error,511);
18
19 if(!SqlConnection)
20 {
21 server_print("uq_jumpstats: Could not connect to SQL database; Error #%d: %s", error, g_error);
22 log_amx("uq_jumpstats: Could not connect to SQL database; Error #%d: %s", error, g_error);
23 return pause("a");
24 }
25
26 new CreateInto[1001];
27
28 formatex(CreateInto, 1000, "CREATE TABLE IF NOT EXISTS uq_players (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(64) NOT NULL,ip VARCHAR(39) NOT NULL,authid VARCHAR(35) NOT NULL, lastseen INT(10) NOT NULL,INDEX(name,authid))");
29 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", CreateInto);
30
31 formatex(CreateInto, 1000, "CREATE TABLE IF NOT EXISTS uq_jumps (pid INT(11) NOT NULL,type VARCHAR(32) NOT NULL,distance INT(10) NOT NULL,maxspeed INT(10) NOT NULL,prestrafe INT(10) NOT NULL,strafes INT(2) NOT NULL,sync INT(3) NOT NULL,ddbh INT(3) NOT NULL DEFAULT '0',pspeed INT(3) NOT NULL,wpn VARCHAR(32) NOT NULL,INDEX(pid,type,distance,maxspeed,prestrafe,strafes,sync,ddbh,pspeed,wpn))");
32 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", CreateInto);
33
34 formatex(CreateInto, 1000, "CREATE TABLE IF NOT EXISTS uq_block_tops (pid INT(11) NOT NULL,type VARCHAR(32) NOT NULL,distance INT(10) NOT NULL,jumpoff INT(10) NOT NULL,block VARCHAR(5) NOT NULL,pspeed INT(3) NOT NULL,wpn VARCHAR(32) NOT NULL,INDEX(pid,type,distance,jumpoff,pspeed,wpn))");
35 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", CreateInto);
36
37 formatex(CreateInto, 1000, "CREATE TABLE IF NOT EXISTS info_stats (cvar varchar(32) NOT NULL,val varchar(16) NOT NULL, UNIQUE(cvar))");
38 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", CreateInto);
39
40 return PLUGIN_CONTINUE;
41 }
42
43 public QueryHandle(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
44 {
45 if( iFailState != TQUERY_SUCCESS )
46 {
47 log_amx("uq_jumpstats: SQL Error #%d - %s", iErrnum, szError);
48 client_print(0,print_chat, "SQL error. Data has not been not saved.");
49 }
50
51 SQL_FreeHandle(hQuery);
52 return PLUGIN_CONTINUE;
53 }
54 public retrying_getinfo(id)
55 {
56 id=id-213134;
57 player_load_info(id);
58 }
59 public player_load_info(id)
60 {
61 new Handle:query,sql_rankby[128],error[128],sql_query[128],quoted_name[65];
62
63 SQL_QuoteString(SqlConnection, quoted_name, 64, g_playername[id]);
64
65 switch(rankby)
66 {
67 case 0:
68 { // by name
69 // bugfix (happened once o_O)
70 if(strlen(g_playername[id]) < 1)
71 {
72 log_amx("NAME BUG; output: %s; player id: %d", g_playername[id], get_user_userid(id));
73 get_user_name(id, g_playername[id], 64);
74 }
75
76 formatex(sql_rankby, 127, "name LIKE '%s'", quoted_name);
77 }
78 case 1:
79 { // by ip
80
81 // bugfix (o_O)
82 if(strlen(g_playerip[id]) < 1)
83 {
84 log_amx("IP BUG; output: %s; player id: %d", g_playerip[id], get_user_userid(id));
85 get_user_ip(id, g_playerip[id], 64);
86 }
87
88 formatex(sql_rankby, 127, "ip LIKE '%s'", g_playerip[id]);
89 }
90 case 2:
91 { // steamid
92
93 // bugfix (o_O)
94 if(strlen(g_playerip[id]) < 1)
95 {
96 log_amx("STEAM BUG; output: %s; player id: %d", g_playersteam[id], get_user_userid(id));
97 get_user_authid(id, g_playersteam[id], 64);
98 }
99
100 formatex(sql_rankby, 127, "authid LIKE '%s'", g_playersteam[id]);
101 }
102 }
103
104 query = SQL_PrepareQuery(SqlConnection, "SELECT `id` FROM uq_players WHERE %s", sql_rankby);
105
106 if (!SQL_Execute(query))
107 {
108 SQL_QueryError(query, error, 127);
109
110 new tmp_error;
111 SqlConnection = SQL_Connect(DB_TUPLE,tmp_error,g_error,511);
112
113 set_task(2.0,"retrying_getinfo",213134+id);
114 }
115 else if (!SQL_NumResults(query))
116 {
117 new cData[1];
118 cData[0] = id;
119 formatex(sql_query, 511, "INSERT INTO uq_players(name,ip,authid,lastseen) VALUES('%s','%s','%s',UNIX_TIMESTAMP())", quoted_name, g_playerip[id], g_playersteam[id]);
120 SQL_ThreadQuery(DB_TUPLE,"QueryHandle_newPlayer", sql_query, cData, strlen(cData[0]));
121 } else {
122 new cData[1];
123 cData[0] = id;
124 g_sql_pid[id] = SQL_ReadResult(query, 0);
125
126 format(sql_query, 511, "SELECT type,distance,maxspeed,prestrafe,strafes,sync,ddbh,pspeed,wpn FROM uq_jumps WHERE pid=%d", g_sql_pid[id]);
127 SQL_ThreadQuery(DB_TUPLE,"QueryHandle_LoadPlayerTops", sql_query, cData, strlen(cData[0]));
128 format(sql_query, 511, "SELECT type,distance,jumpoff,block,pspeed,wpn FROM uq_block_tops WHERE pid=%d", g_sql_pid[id]);
129 SQL_ThreadQuery(DB_TUPLE,"QueryHandle_LoadPlayerTopsBlock", sql_query, cData, strlen(cData[0]));
130
131 if(g_eCvarSQL[UPDATE_AUTH] <= 0)
132 format(sql_query, 511, "UPDATE uq_players SET lastseen=UNIX_TIMESTAMP() WHERE `id`=%d", g_sql_pid[id]);
133 else
134 format(sql_query, 511, "UPDATE uq_players SET name='%s',ip='%s',authid='%s',lastseen=UNIX_TIMESTAMP() WHERE `id`=%d", quoted_name, g_playerip[id], g_playersteam[id], g_sql_pid[id]);
135
136 SQL_ThreadQuery(DB_TUPLE,"QueryHandle",sql_query);
137 }
138
139 return PLUGIN_CONTINUE;
140 }
141
142 public QueryHandle_LoadPlayerTops(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
143 {
144 if(iFailState != TQUERY_SUCCESS)
145 {
146 log_amx("uq_jumpstats: SQL Error #%d - %s", iErrnum, szError);
147 return PLUGIN_HANDLED;
148 }
149
150 new type[64], distance, maxspeed, prestrafe, strafes, sync, ddbh, tmp_str[12], pspeed, wpn[33];
151
152 while(SQL_MoreResults(hQuery))
153 {
154 SQL_ReadResult(hQuery,0,type,64);
155 distance = SQL_ReadResult(hQuery,1);
156 maxspeed = SQL_ReadResult(hQuery,2);
157 prestrafe = SQL_ReadResult(hQuery,3);
158 strafes = SQL_ReadResult(hQuery,4);
159 sync = SQL_ReadResult(hQuery,5);
160 ddbh = SQL_ReadResult(hQuery,6);
161 pspeed = SQL_ReadResult(hQuery,7);
162 SQL_ReadResult(hQuery,8,wpn,32);
163
164 new Trie:JumpStat;
165 JumpStat = TrieCreate();
166
167 TrieSetString(JumpStat, "type", type);
168 TrieSetCell(JumpStat, "distance", distance);
169 TrieSetCell(JumpStat, "maxspeed", maxspeed);
170 TrieSetCell(JumpStat, "prestrafe", prestrafe);
171 TrieSetCell(JumpStat, "strafes", strafes);
172 TrieSetCell(JumpStat, "sync", sync);
173 TrieSetCell(JumpStat, "ddbh", ddbh);
174 TrieSetCell(JumpStat, "pspeed", pspeed);
175 TrieSetString(JumpStat, "wpn", wpn);
176
177 num_to_str(g_sql_pid[cData[0]],tmp_str,11);
178
179 format(type, 64, "%s_%s_%d", tmp_str, type, pspeed);
180 TrieSetCell(JumpPlayers, type, JumpStat);
181
182 SQL_NextRow(hQuery);
183 }
184
185 SQL_FreeHandle(hQuery);
186 return PLUGIN_CONTINUE;
187 }
188
189 public QueryHandle_LoadPlayerTopsBlock(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
190 {
191 if(iFailState != TQUERY_SUCCESS)
192 {
193 log_amx("uq_jumpstats: SQL Error #%d - %s", iErrnum, szError);
194 return PLUGIN_HANDLED;
195 }
196
197 new type[65],distance,jumpoff,block,tmp_str[12],pspeed,wpn[33];
198
199 while(SQL_MoreResults(hQuery))
200 {
201 SQL_ReadResult(hQuery,0,type,64);
202 distance = SQL_ReadResult(hQuery,1);
203 jumpoff = SQL_ReadResult(hQuery,2);
204 block = SQL_ReadResult(hQuery,3);
205 pspeed = SQL_ReadResult(hQuery,4);
206 SQL_ReadResult(hQuery,5,wpn,32);
207
208 new Trie:JumpStat;
209 JumpStat = TrieCreate();
210
211 TrieSetString(JumpStat, "type", type);
212 TrieSetCell(JumpStat, "distance", distance);
213 TrieSetCell(JumpStat, "jumpoff", jumpoff);
214 TrieSetCell(JumpStat, "block", block);
215 TrieSetCell(JumpStat, "pspeed", pspeed);
216 TrieSetString(JumpStat, "wpn", wpn);
217
218 num_to_str(g_sql_pid[cData[0]],tmp_str,11);
219 format(type, 64, "block_%s_%s_%d", tmp_str, type, pspeed);
220
221 TrieSetCell(JumpPlayers, type, JumpStat); // Array[] => ["id"] = Datas
222
223 SQL_NextRow(hQuery);
224 }
225
226 SQL_FreeHandle(hQuery);
227 return PLUGIN_CONTINUE;
228 }
229
230 public QueryHandle_newPlayer(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
231 {
232 if( iFailState != TQUERY_SUCCESS )
233 {
234 log_amx("uq_jumpstats: SQL Error #%d - %s", iErrnum, szError);
235 return PLUGIN_HANDLED;
236 }
237
238 new task_data[2];
239 task_data[0] = SQL_GetInsertId(hQuery);
240 task_data[1] = cData[0];
241
242 set_task(1.0,"sql_check_reg",TASK_CHECK_SQL_PLAYER+cData[0],task_data,2);
243
244 SQL_FreeHandle(hQuery);
245 return PLUGIN_CONTINUE;
246 }
247
248 public sql_check_reg(args[]) {
249 new id = args[1];
250 new inserted_id = args[0];
251
252 if(0 > id || id > get_maxplayers())
253 {
254 log_amx("Invalid id (%d) in sql_check_reg()", id);
255 return PLUGIN_HANDLED;
256 }
257 else
258 {
259 if(inserted_id > 0)
260 {
261 new tmp_str[12];
262 new Trie:JumpData;
263 JumpData = TrieCreate();
264
265 g_sql_pid[id] = inserted_id;
266 num_to_str(inserted_id,tmp_str,11);
267 TrieSetCell(JumpPlayers, tmp_str, JumpData);
268 }
269 else
270 {
271 log_amx("Bug? %s sql_id is %d; Retrying.",g_playername[id],inserted_id);
272 player_load_info(id);
273 }
274 }
275
276 return PLUGIN_HANDLED;
277 }
278
279 /////////////////////////For future use/////////////////////////////
280 public save_info_sql()
281 {
282 new createinto[256];
283
284 for(new i=0;i<SQLCVARSNUM;i++)
285 {
286 formatex(createinto, 255, "INSERT INTO info_stats(cvar,val) VALUES('%s', '%d') ON DUPLICATE KEY UPDATE cvar='%s', val=%d",sql_Cvars[i], sql_Cvars_num[i],sql_Cvars[i], sql_Cvars_num[i]);
287 SQL_ThreadQuery(DB_TUPLE, "QueryHandle", createinto);
288 }
289
290 return PLUGIN_CONTINUE;
291 }
292
293 public PlayerSaveData_to_SQL(id, cData[]) {
294 new jt_str[65], Trie:JS, tmp_str[12], distance, query[512], wpn[33], type[33];
295 num_to_str(g_sql_pid[id],tmp_str,11);
296
297 if(cData[8])
298 {
299 get_weaponname(cData[8],wpn,32);
300 replace(wpn,32,"weapon_","");
301 }
302 else formatex(wpn,32,"Unknow");
303
304 formatex(type,32,"%s",Type_List[cData[6]]);
305 formatex(jt_str,64,"%s_%s_%d",tmp_str,Type_List[cData[6]],cData[7]);
306
307 if(TrieKeyExists(JumpPlayers, jt_str)) // player is in trie
308 {
309 TrieGetCell(JumpPlayers, jt_str, JS);
310
311 if(TrieKeyExists(JS, "distance")) // get distance
312 {
313 TrieGetCell(JS, "distance", distance);
314
315 if(distance < cData[0]) // jumpdist > old jumpdist, updating
316 {
317 formatex(query, 511, "UPDATE uq_jumps SET distance=%d,maxspeed=%d,prestrafe=%d,strafes=%d,sync=%d,ddbh=%d,pspeed=%d,wpn='%s' WHERE pid=%d AND type LIKE '%s' AND pspeed=%d", cData[0], cData[1], cData[2], cData[3], cData[4], cData[5], cData[7], wpn, g_sql_pid[id], type, cData[7]);
318
319 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", query);
320 TrieSetString(JS, "type", type);
321 TrieSetCell(JS, "distance", cData[0]);
322 TrieSetCell(JS, "maxspeed", cData[1]);
323 TrieSetCell(JS, "prestrafe", cData[2]);
324 TrieSetCell(JS, "strafes", cData[3]);
325 TrieSetCell(JS, "sync", cData[4]);
326 TrieSetCell(JS, "ddbh", cData[5]);
327 TrieSetCell(JS, "pspeed", cData[7]);
328 TrieSetString(JS, "wpn", wpn);
329
330 TrieSetCell(JumpPlayers, jt_str, JS);
331 }
332 }
333 else
334 {
335 log_amx("WTF?!");
336 }
337 }
338 else // new jumptype to player, insert
339 {
340 formatex(query, 511, "INSERT INTO uq_jumps(pid,type,distance,maxspeed,prestrafe,strafes,sync,ddbh,pspeed,wpn) VALUES(%d,'%s',%d,%d,%d,%d,%d,%d,%d,'%s')", g_sql_pid[id], type, cData[0], cData[1], cData[2], cData[3], cData[4], cData[5], cData[7], wpn);
341
342 new Trie:JumpStat;
343 JumpStat = TrieCreate();
344
345 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", query);
346 TrieSetString(JumpStat, "type", type);
347 TrieSetCell(JumpStat, "distance", cData[0]);
348 TrieSetCell(JumpStat, "maxspeed", cData[1]);
349 TrieSetCell(JumpStat, "prestrafe", cData[2]);
350 TrieSetCell(JumpStat, "strafes", cData[3]);
351 TrieSetCell(JumpStat, "sync", cData[4]);
352 TrieSetCell(JumpStat, "ddbh", cData[5]);
353 TrieSetCell(JumpStat, "pspeed", cData[7]);
354 TrieSetString(JumpStat, "wpn", wpn);
355
356 TrieSetCell(JumpPlayers, jt_str, JumpStat);
357 }
358 }
359 public PlayerSaveData_to_SQL_block(id, cData[]) {
360 new block,jt_str[65], Trie:JS, tmp_str[12], distance, query[512], wpn[33], type[33];
361 num_to_str(g_sql_pid[id],tmp_str,11);
362
363 if(cData[5])
364 {
365 get_weaponname(cData[5],wpn,32);
366 replace(wpn,32,"weapon_","");
367 }
368 else formatex(wpn,32,"Unknow");
369
370 if(cData[3]==6)
371 {
372 formatex(type,32,"hj");
373 formatex(jt_str,64,"block_%s_hj_%d",tmp_str,cData[4]);
374 }
375 else
376 {
377 formatex(type,32,"%s",Type_List[cData[3]]);
378 formatex(jt_str,64,"block_%s_%s_%d",tmp_str,Type_List[cData[3]],cData[4]);
379 }
380
381
382 if(TrieKeyExists(JumpPlayers, jt_str)) // player is in trie
383 {
384 TrieGetCell(JumpPlayers, jt_str, JS);
385
386 if(TrieKeyExists(JS, "block")) // get block
387 {
388 TrieGetCell(JS, "block", block);
389
390 if(block <= cData[2]) // block >= oldblock
391 {
392 if(TrieKeyExists(JS, "distance")) // get distance
393 {
394 TrieGetCell(JS, "distance", distance);
395
396 if((distance < cData[0] && block==cData[2]) || (block<cData[2])) // jumpdist > old jumpdist if block=oldblock or update if block<oldblock, updating
397 {
398
399 formatex(query, 511, "UPDATE uq_block_tops SET distance=%d,jumpoff=%d,block=%d,pspeed=%d,wpn='%s' WHERE pid=%d AND type LIKE '%s' AND pspeed=%d", cData[0], cData[1], cData[2], cData[4], wpn, g_sql_pid[id], type, cData[4]);
400
401 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", query);
402 TrieSetString(JS, "type", type);
403 TrieSetCell(JS, "distance", cData[0]);
404 TrieSetCell(JS, "jumpoff", cData[1]);
405 TrieSetCell(JS, "block", cData[2]);
406 TrieSetCell(JS, "pspeed", cData[4]);
407 TrieSetString(JS, "wpn", wpn);
408
409 TrieSetCell(JumpPlayers, jt_str, JS);
410 }
411 }
412 else
413 {
414 log_amx("WTF?!");
415 }
416 }
417 }
418 else
419 {
420 log_amx("WTF?!");
421 }
422 }
423 else // new jumptype to player, insert
424 {
425
426 formatex(query, 511, "INSERT INTO uq_block_tops(pid,type,distance,jumpoff,block,pspeed,wpn) VALUES(%d,'%s',%d,%d,%d,%d,'%s')", g_sql_pid[id], type, cData[0], cData[1], cData[2], cData[4], wpn);
427
428 new Trie:JumpStat;
429 JumpStat = TrieCreate();
430
431 SQL_ThreadQuery(DB_TUPLE,"QueryHandle", query);
432 TrieSetString(JumpStat, "type", type);
433 TrieSetCell(JumpStat, "distance", cData[0]);
434 TrieSetCell(JumpStat, "jumpoff", cData[1]);
435 TrieSetCell(JumpStat, "block", cData[2]);
436 TrieSetCell(JumpStat, "pspeed", cData[4]);
437 TrieSetString(JumpStat, "wpn", wpn);
438
439 TrieSetCell(JumpPlayers, jt_str, JumpStat);
440 }
441 }
442 /* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
443 *{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
444 */
445