3 string playerstats_last;
5 .float playerstats_addedglobalinfo;
6 .string playerstats_id;
8 #define ALL_ANTICHEATS \
10 ANTICHEAT("speedhack"); \
11 ANTICHEAT("div0_strafebot_old"); \
12 ANTICHEAT("div0_strafebot_new"); \
13 ANTICHEAT("div0_evade"); \
14 ANTICHEAT("idle_snapaim"); \
15 ANTICHEAT("idle_snapaim_signal"); \
16 ANTICHEAT("idle_snapaim_noise");
18 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
22 playerstats_waitforme = TRUE;
23 uri = autocvar_g_playerstats_uri;
26 playerstats_db = db_create();
27 if(playerstats_db >= 0)
28 playerstats_waitforme = FALSE; // must wait for it at match end
30 serverflags |= SERVERFLAG_PLAYERSTATS;
32 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
33 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
34 PlayerStats_AddEvent(PLAYERSTATS_WINS);
35 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
36 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
37 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
38 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
39 PlayerStats_AddEvent(PLAYERSTATS_RANK);
44 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
46 w = get_weaponinfo(i);
48 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
49 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
51 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
52 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
54 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
57 #define ANTICHEAT(name) \
58 PlayerStats_AddEvent("anticheat-" name)
62 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
63 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
64 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
65 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
66 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
67 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
68 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
69 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
70 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
71 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
74 void PlayerStats_AddPlayer(entity e)
78 if(playerstats_db < 0)
84 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
86 else if(IS_BOT_CLIENT(e))
87 s = sprintf("bot#%g#%s", skill, e.cleanname);
89 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
92 s = sprintf("bot#%d", e.playerid);
94 s = sprintf("player#%d", e.playerid);
97 e.playerstats_id = strzone(s);
100 key = sprintf("%s:*", e.playerstats_id);
103 p = db_get(playerstats_db, key);
108 db_put(playerstats_db, key, playerstats_last);
109 strunzone(playerstats_last);
112 db_put(playerstats_db, key, "#");
113 playerstats_last = strzone(e.playerstats_id);
117 void PlayerStats_AddTeam(float t)
119 if(playerstats_db < 0)
123 key = sprintf("%d", t);
126 p = db_get(playerstats_db, key);
131 db_put(playerstats_db, key, teamstats_last);
132 strunzone(teamstats_last);
135 db_put(playerstats_db, key, "#");
136 teamstats_last = strzone(key);
140 void PlayerStats_AddEvent(string event_id)
142 if(playerstats_db < 0)
146 key = sprintf("*:%s", event_id);
149 p = db_get(playerstats_db, key);
154 db_put(playerstats_db, key, events_last);
155 strunzone(events_last);
158 db_put(playerstats_db, key, "#");
159 events_last = strzone(event_id);
163 float PlayerStats_Event(entity e, string event_id, float value)
165 if((e.playerstats_id == "") || playerstats_db < 0)
170 key = sprintf("%s:%s", e.playerstats_id, event_id);
171 val = stof(db_get(playerstats_db, key));
173 db_put(playerstats_db, key, ftos(val));
177 float PlayerStats_TeamScore(float t, string event_id, float value)
179 if(playerstats_db < 0)
184 key = sprintf("team#%d:%s", t, event_id);
185 val = stof(db_get(playerstats_db, key));
187 db_put(playerstats_db, key, ftos(val));
194 A collection of lines of the format <key> SPACE <value> NEWLINE, where
195 <key> is always a single character.
197 The following keys are defined:
199 V: format version (always a fixed number) - this MUST be the first line!
200 #: comment (MUST be ignored by any parser)
201 R: release information on the server
202 T: time at which the game ended
204 O: mod name (icon request) as in server browser
206 I: match ID (see "matchid" in g_world.qc
207 S: "hostname" of the server
208 C: number of "unpure" cvar changes
209 U: UDP port number of the server
210 D: duration of the match
211 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
212 Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
213 n: nickname of the player (optional)
216 e: followed by an event name, a space, and the event count/score
218 alivetime: total playing time of the player
219 avglatency: average network latency compounded throughout the match
220 wins: number of games won (can only be set if matches is set)
221 matches: number of matches played to the end (not aborted by map switch)
222 joins: number of matches joined (always 1 unless player never played during the match)
223 scoreboardvalid: set to 1 if the player was there at the end of the match
224 total-<scoreboardname>: total score of that scoreboard item
225 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
226 achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
227 kills-<index>: number of kills against the indexed player
228 rank <number>: rank of player
229 acc-<weapon netname>-hit: total damage dealt
230 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
231 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
232 acc-<weapon netname>-cnt-fired: amount of fired shots
233 acc-<weapon netname>-frags: amount of frags dealt by weapon
235 Response format (not used yet): see https://gist.github.com/4284222
238 void PlayerStats_ready(entity fh, entity pass, float status)
248 case URL_READY_CANWRITE:
249 url_fputs(fh, "V 7\n");
251 url_fputs(fh, sprintf("R %s\n", WATERMARK));
253 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
254 url_fputs(fh, sprintf("G %s\n", GetGametype()));
255 url_fputs(fh, sprintf("O %s\n", modname));
256 url_fputs(fh, sprintf("M %s\n", GetMapname()));
257 url_fputs(fh, sprintf("I %s\n", matchid));
258 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
259 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
260 url_fputs(fh, sprintf("U %d\n", cvar("port")));
261 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
264 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
266 url_fputs(fh, sprintf("Q team#%s\n", t));
267 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
270 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
272 url_fputs(fh, sprintf("e %s %g\n", e, v));
276 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
278 url_fputs(fh, sprintf("P %s\n", p));
279 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
281 url_fputs(fh, sprintf("i %s\n", nn));
282 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
284 url_fputs(fh, sprintf("n %s\n", nn));
287 tt = db_get(playerstats_db, sprintf("%s:_team", p));
288 url_fputs(fh, sprintf("t %s\n", tt));
290 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
293 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
295 url_fputs(fh, sprintf("e %s %g\n", e, v));
301 case URL_READY_CANREAD:
302 // url_fclose is processing, we got a response for writing the data
303 // this must come from HTTP
304 print("Got response from player stats server:\n");
305 while((s = url_fgets(fh)))
307 print("End of response.\n");
310 case URL_READY_CLOSED:
311 // url_fclose has finished
312 print("Player stats written\n");
313 playerstats_waitforme = TRUE;
314 db_close(playerstats_db);
317 case URL_READY_ERROR:
319 print("Player stats writing failed: ", ftos(status), "\n");
320 playerstats_waitforme = TRUE;
321 if(playerstats_db >= 0)
323 db_close(playerstats_db);
330 //#NO AUTOCVARS START
331 void PlayerStats_Shutdown()
335 if(playerstats_db < 0)
338 uri = autocvar_g_playerstats_uri;
341 playerstats_waitforme = FALSE;
342 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
346 playerstats_waitforme = TRUE;
347 db_close(playerstats_db);
353 void PlayerStats_Accuracy(entity p)
359 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
361 w = get_weaponinfo(i);
363 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
364 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
366 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
367 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
369 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
371 //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
374 void PlayerStats_Anticheat(entity p)
376 entity oldself = self;
379 #define ANTICHEAT(name) \
380 PlayerStats_Event(p, "anticheat-" name, anticheat_getvalue(name))
386 void PlayerStats_AddGlobalInfo(entity p)
388 if(playerstats_db < 0)
390 if((p.playerstats_id == "") || playerstats_db < 0)
392 p.playerstats_addedglobalinfo = TRUE;
397 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
401 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
403 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
404 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
407 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
409 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
410 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
412 PlayerStats_Accuracy(p);
414 PlayerStats_Anticheat(p);
416 if(IS_REAL_CLIENT(p))
420 float latency = (p.latency_sum / p.latency_cnt);
421 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
425 strunzone(p.playerstats_id);
426 p.playerstats_id = string_null;
429 .float scoreboard_pos;
430 void PlayerStats_EndMatch(float finished)
433 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
434 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
436 PlayerScore_TeamStats();
439 // add personal score rank
440 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
442 if(!p.scoreboard_pos)
445 // scoreboard is valid!
446 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
448 // add scoreboard position
449 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
451 // add scoreboard data
452 PlayerScore_PlayerStats(p);
454 // if the match ended normally, add winning info
457 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
458 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
463 #undef ALL_ANTICHEATS