3 string playerstats_last;
5 .float playerstats_addedglobalinfo;
6 .string playerstats_id;
8 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
12 playerstats_waitforme = TRUE;
13 uri = autocvar_g_playerstats_uri;
16 playerstats_db = db_create();
17 if(playerstats_db >= 0)
18 playerstats_waitforme = FALSE; // must wait for it at match end
20 serverflags |= SERVERFLAG_PLAYERSTATS;
22 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
23 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
24 PlayerStats_AddEvent(PLAYERSTATS_WINS);
25 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
26 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
27 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
28 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
29 PlayerStats_AddEvent(PLAYERSTATS_RANK);
34 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
36 w = get_weaponinfo(i);
38 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
39 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
41 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
42 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
44 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
47 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
48 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
49 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
50 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
51 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
52 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
53 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
54 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
55 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
56 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
59 void PlayerStats_AddPlayer(entity e)
63 if(playerstats_db < 0)
69 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
71 else if(IS_BOT_CLIENT(e))
72 s = sprintf("bot#%g#%s", skill, e.cleanname);
74 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
77 s = sprintf("bot#%d", e.playerid);
79 s = sprintf("player#%d", e.playerid);
82 e.playerstats_id = strzone(s);
85 key = sprintf("%s:*", e.playerstats_id);
88 p = db_get(playerstats_db, key);
93 db_put(playerstats_db, key, playerstats_last);
94 strunzone(playerstats_last);
97 db_put(playerstats_db, key, "#");
98 playerstats_last = strzone(e.playerstats_id);
102 void PlayerStats_AddTeam(float t)
104 if(playerstats_db < 0)
108 key = sprintf("%d", t);
111 p = db_get(playerstats_db, key);
116 db_put(playerstats_db, key, teamstats_last);
117 strunzone(teamstats_last);
120 db_put(playerstats_db, key, "#");
121 teamstats_last = strzone(key);
125 void PlayerStats_AddEvent(string event_id)
127 if(playerstats_db < 0)
131 key = sprintf("*:%s", event_id);
134 p = db_get(playerstats_db, key);
139 db_put(playerstats_db, key, events_last);
140 strunzone(events_last);
143 db_put(playerstats_db, key, "#");
144 events_last = strzone(event_id);
148 float PlayerStats_Event(entity e, string event_id, float value)
150 if((e.playerstats_id == "") || playerstats_db < 0)
155 key = sprintf("%s:%s", e.playerstats_id, event_id);
156 val = stof(db_get(playerstats_db, key));
158 db_put(playerstats_db, key, ftos(val));
162 float PlayerStats_TeamScore(float t, string event_id, float value)
164 if(playerstats_db < 0)
169 key = sprintf("team#%d:%s", t, event_id);
170 val = stof(db_get(playerstats_db, key));
172 db_put(playerstats_db, key, ftos(val));
179 A collection of lines of the format <key> SPACE <value> NEWLINE, where
180 <key> is always a single character.
182 The following keys are defined:
184 V: format version (always a fixed number) - this MUST be the first line!
185 #: comment (MUST be ignored by any parser)
186 R: release information on the server
187 T: time at which the game ended
189 O: mod name (icon request) as in server browser
191 I: match ID (see "matchid" in g_world.qc
192 S: "hostname" of the server
193 C: number of "unpure" cvar changes
194 U: UDP port number of the server
195 D: duration of the match
196 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
197 Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
198 n: nickname of the player (optional)
201 e: followed by an event name, a space, and the event count/score
203 alivetime: total playing time of the player
204 avglatency: average network latency compounded throughout the match
205 wins: number of games won (can only be set if matches is set)
206 matches: number of matches played to the end (not aborted by map switch)
207 joins: number of matches joined (always 1 unless player never played during the match)
208 scoreboardvalid: set to 1 if the player was there at the end of the match
209 total-<scoreboardname>: total score of that scoreboard item
210 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
211 achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
212 kills-<index>: number of kills against the indexed player
213 rank <number>: rank of player
214 acc-<weapon netname>-hit: total damage dealt
215 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
216 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
217 acc-<weapon netname>-cnt-fired: amount of fired shots
218 acc-<weapon netname>-frags: amount of frags dealt by weapon
220 Response format (not used yet): see https://gist.github.com/4284222
223 void PlayerStats_ready(entity fh, entity pass, float status)
233 case URL_READY_CANWRITE:
234 url_fputs(fh, "V 7\n");
236 url_fputs(fh, sprintf("R %s\n", WATERMARK));
238 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
239 url_fputs(fh, sprintf("G %s\n", GetGametype()));
240 url_fputs(fh, sprintf("O %s\n", modname));
241 url_fputs(fh, sprintf("M %s\n", GetMapname()));
242 url_fputs(fh, sprintf("I %s\n", matchid));
243 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
244 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
245 url_fputs(fh, sprintf("U %d\n", cvar("port")));
246 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
249 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
251 url_fputs(fh, sprintf("Q team#%s\n", t));
252 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
255 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
257 url_fputs(fh, sprintf("e %s %g\n", e, v));
261 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
263 url_fputs(fh, sprintf("P %s\n", p));
264 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
266 url_fputs(fh, sprintf("i %s\n", nn));
267 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
269 url_fputs(fh, sprintf("n %s\n", nn));
272 tt = db_get(playerstats_db, sprintf("%s:_team", p));
273 url_fputs(fh, sprintf("t %s\n", tt));
275 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
278 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
280 url_fputs(fh, sprintf("e %s %g\n", e, v));
286 case URL_READY_CANREAD:
287 // url_fclose is processing, we got a response for writing the data
288 // this must come from HTTP
289 print("Got response from player stats server:\n");
290 while((s = url_fgets(fh)))
292 print("End of response.\n");
295 case URL_READY_CLOSED:
296 // url_fclose has finished
297 print("Player stats written\n");
298 playerstats_waitforme = TRUE;
299 db_close(playerstats_db);
302 case URL_READY_ERROR:
304 print("Player stats writing failed: ", ftos(status), "\n");
305 playerstats_waitforme = TRUE;
306 if(playerstats_db >= 0)
308 db_close(playerstats_db);
315 //#NO AUTOCVARS START
316 void PlayerStats_Shutdown()
320 if(playerstats_db < 0)
323 uri = autocvar_g_playerstats_uri;
326 playerstats_waitforme = FALSE;
327 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
331 playerstats_waitforme = TRUE;
332 db_close(playerstats_db);
338 void PlayerStats_Accuracy(entity p)
344 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
346 w = get_weaponinfo(i);
348 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
349 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
351 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
352 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
354 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
356 //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
359 void PlayerStats_AddGlobalInfo(entity p)
361 if(playerstats_db < 0)
363 if((p.playerstats_id == "") || playerstats_db < 0)
365 p.playerstats_addedglobalinfo = TRUE;
370 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
374 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
376 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
377 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
380 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
382 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
383 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
385 PlayerStats_Accuracy(p);
387 if(IS_REAL_CLIENT(p))
391 float latency = (p.latency_sum / p.latency_cnt);
392 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
396 strunzone(p.playerstats_id);
397 p.playerstats_id = string_null;
400 .float scoreboard_pos;
401 void PlayerStats_EndMatch(float finished)
404 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
405 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
407 PlayerScore_TeamStats();
410 // add personal score rank
411 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
413 if(!p.scoreboard_pos)
416 // scoreboard is valid!
417 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
419 // add scoreboard position
420 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
422 // add scoreboard data
423 PlayerScore_PlayerStats(p);
425 // if the match ended normally, add winning info
428 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
429 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);