3 string playerstats_last;
5 .float playerstats_addedglobalinfo;
6 .string playerstats_id;
8 // Note that _time isn't mentioned here. That one is special.
9 #define ALL_ANTICHEATS \
10 ANTICHEAT("speedhack"); \
11 ANTICHEAT("speedhack_m1"); \
12 ANTICHEAT("speedhack_m2"); \
13 ANTICHEAT("speedhack_m3"); \
14 ANTICHEAT("speedhack_m4"); \
15 ANTICHEAT("speedhack_m5"); \
16 ANTICHEAT("div0_strafebot_old"); \
17 ANTICHEAT("div0_strafebot_new"); \
18 ANTICHEAT("div0_evade"); \
19 ANTICHEAT("idle_snapaim"); \
20 ANTICHEAT("idle_snapaim_signal"); \
21 ANTICHEAT("idle_snapaim_noise"); \
22 ANTICHEAT("idle_snapaim_m2"); \
23 ANTICHEAT("idle_snapaim_m3"); \
24 ANTICHEAT("idle_snapaim_m4"); \
25 ANTICHEAT("idle_snapaim_m7"); \
26 ANTICHEAT("idle_snapaim_m10");
28 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
32 playerstats_waitforme = TRUE;
33 uri = autocvar_g_playerstats_uri;
36 playerstats_db = db_create();
37 if(playerstats_db >= 0)
38 playerstats_waitforme = FALSE; // must wait for it at match end
40 serverflags |= SERVERFLAG_PLAYERSTATS;
42 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
43 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
44 PlayerStats_AddEvent(PLAYERSTATS_WINS);
45 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
46 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
47 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
48 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
49 PlayerStats_AddEvent(PLAYERSTATS_RANK);
54 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
56 w = get_weaponinfo(i);
58 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
59 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
61 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
62 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
64 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
67 PlayerStats_AddEvent("anticheat-_time");
68 #define ANTICHEAT(name) \
69 PlayerStats_AddEvent("anticheat-" name)
73 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
74 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
75 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
76 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
77 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
78 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
79 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
80 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
81 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
82 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
85 void PlayerStats_AddPlayer(entity e)
89 if(playerstats_db < 0)
95 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
97 else if(IS_BOT_CLIENT(e))
98 s = sprintf("bot#%g#%s", skill, e.cleanname);
100 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
103 s = sprintf("bot#%d", e.playerid);
105 s = sprintf("player#%d", e.playerid);
108 e.playerstats_id = strzone(s);
111 key = sprintf("%s:*", e.playerstats_id);
114 p = db_get(playerstats_db, key);
119 db_put(playerstats_db, key, playerstats_last);
120 strunzone(playerstats_last);
123 db_put(playerstats_db, key, "#");
124 playerstats_last = strzone(e.playerstats_id);
128 void PlayerStats_AddTeam(float t)
130 if(playerstats_db < 0)
134 key = sprintf("%d", t);
137 p = db_get(playerstats_db, key);
142 db_put(playerstats_db, key, teamstats_last);
143 strunzone(teamstats_last);
146 db_put(playerstats_db, key, "#");
147 teamstats_last = strzone(key);
151 void PlayerStats_AddEvent(string event_id)
153 if(playerstats_db < 0)
157 key = sprintf("*:%s", event_id);
160 p = db_get(playerstats_db, key);
165 db_put(playerstats_db, key, events_last);
166 strunzone(events_last);
169 db_put(playerstats_db, key, "#");
170 events_last = strzone(event_id);
174 float PlayerStats_Event(entity e, string event_id, float value)
176 if((e.playerstats_id == "") || playerstats_db < 0)
181 key = sprintf("%s:%s", e.playerstats_id, event_id);
182 val = stof(db_get(playerstats_db, key));
184 db_put(playerstats_db, key, ftos(val));
188 float PlayerStats_TeamScore(float t, string event_id, float value)
190 if(playerstats_db < 0)
195 key = sprintf("team#%d:%s", t, event_id);
196 val = stof(db_get(playerstats_db, key));
198 db_put(playerstats_db, key, ftos(val));
205 A collection of lines of the format <key> SPACE <value> NEWLINE, where
206 <key> is always a single character.
208 The following keys are defined:
210 V: format version (always a fixed number) - this MUST be the first line!
211 #: comment (MUST be ignored by any parser)
212 R: release information on the server
213 T: time at which the game ended
215 O: mod name (icon request) as in server browser
217 I: match ID (see "matchid" in g_world.qc
218 S: "hostname" of the server
219 C: number of "unpure" cvar changes
220 U: UDP port number of the server
221 D: duration of the match
222 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
223 Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
224 n: nickname of the player (optional)
227 e: followed by an event name, a space, and the event count/score
229 alivetime: total playing time of the player
230 avglatency: average network latency compounded throughout the match
231 wins: number of games won (can only be set if matches is set)
232 matches: number of matches played to the end (not aborted by map switch)
233 joins: number of matches joined (always 1 unless player never played during the match)
234 scoreboardvalid: set to 1 if the player was there at the end of the match
235 total-<scoreboardname>: total score of that scoreboard item
236 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
237 achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
238 kills-<index>: number of kills against the indexed player
239 rank <number>: rank of player
240 acc-<weapon netname>-hit: total damage dealt
241 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
242 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
243 acc-<weapon netname>-cnt-fired: amount of fired shots
244 acc-<weapon netname>-frags: amount of frags dealt by weapon
246 Response format (not used yet): see https://gist.github.com/4284222
249 void PlayerStats_ready(entity fh, entity pass, float status)
259 case URL_READY_CANWRITE:
260 url_fputs(fh, "V 7\n");
262 url_fputs(fh, sprintf("R %s\n", WATERMARK));
264 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
265 url_fputs(fh, sprintf("G %s\n", GetGametype()));
266 url_fputs(fh, sprintf("O %s\n", modname));
267 url_fputs(fh, sprintf("M %s\n", GetMapname()));
268 url_fputs(fh, sprintf("I %s\n", matchid));
269 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
270 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
271 url_fputs(fh, sprintf("U %d\n", cvar("port")));
272 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
275 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
277 url_fputs(fh, sprintf("Q team#%s\n", t));
278 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
281 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
283 url_fputs(fh, sprintf("e %s %g\n", e, v));
287 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
289 url_fputs(fh, sprintf("P %s\n", p));
290 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
292 url_fputs(fh, sprintf("i %s\n", nn));
293 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
295 url_fputs(fh, sprintf("n %s\n", nn));
298 tt = db_get(playerstats_db, sprintf("%s:_team", p));
299 url_fputs(fh, sprintf("t %s\n", tt));
301 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
304 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
306 url_fputs(fh, sprintf("e %s %g\n", e, v));
312 case URL_READY_CANREAD:
313 // url_fclose is processing, we got a response for writing the data
314 // this must come from HTTP
315 print("Got response from player stats server:\n");
316 while((s = url_fgets(fh)))
318 print("End of response.\n");
321 case URL_READY_CLOSED:
322 // url_fclose has finished
323 print("Player stats written\n");
324 playerstats_waitforme = TRUE;
325 db_close(playerstats_db);
328 case URL_READY_ERROR:
330 print("Player stats writing failed: ", ftos(status), "\n");
331 playerstats_waitforme = TRUE;
332 if(playerstats_db >= 0)
334 db_close(playerstats_db);
341 //#NO AUTOCVARS START
342 void PlayerStats_Shutdown()
346 if(playerstats_db < 0)
349 uri = autocvar_g_playerstats_uri;
352 playerstats_waitforme = FALSE;
353 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
357 playerstats_waitforme = TRUE;
358 db_close(playerstats_db);
364 void PlayerStats_Accuracy(entity p)
370 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
372 w = get_weaponinfo(i);
374 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
375 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
377 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
378 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
380 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
382 //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
385 void PlayerStats_Anticheat(entity p)
387 entity oldself = self;
390 float t0 = PlayerStats_Event(p, "anticheat-_time", 0);
391 float dt = anticheat_getvalue("_time");
392 PlayerStats_Event(p, "anticheat-_time", dt);
393 float f = dt / (t0 + dt);
394 #define ANTICHEAT(name) do { \
395 float prev = PlayerStats_Event(p, "anticheat-" name, 0); \
396 float change = (anticheat_getvalue(name) - prev) * f; \
397 PlayerStats_Event(p, "anticheat-" name, change); \
404 void PlayerStats_AddGlobalInfo(entity p)
406 if(playerstats_db < 0)
408 if((p.playerstats_id == "") || playerstats_db < 0)
410 p.playerstats_addedglobalinfo = TRUE;
415 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
419 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
421 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
422 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
425 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
427 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
428 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
430 PlayerStats_Accuracy(p);
432 PlayerStats_Anticheat(p);
434 if(IS_REAL_CLIENT(p))
438 float latency = (p.latency_sum / p.latency_cnt);
439 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
443 strunzone(p.playerstats_id);
444 p.playerstats_id = string_null;
447 .float scoreboard_pos;
448 void PlayerStats_EndMatch(float finished)
451 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
452 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
454 PlayerScore_TeamStats();
457 // add personal score rank
458 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
460 if(!p.scoreboard_pos)
463 // scoreboard is valid!
464 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
466 // add scoreboard position
467 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
469 // add scoreboard data
470 PlayerScore_PlayerStats(p);
472 // if the match ended normally, add winning info
475 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
476 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
481 #undef ALL_ANTICHEATS