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_RANK);
33 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
35 w = get_weaponinfo(i);
37 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
38 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
40 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
41 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
43 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
46 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
47 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
48 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
49 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
50 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
51 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
52 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
53 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
54 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
55 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
58 void PlayerStats_AddPlayer(entity e)
62 if(playerstats_db < 0)
68 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
70 else if(clienttype(e) == CLIENTTYPE_BOT)
71 s = sprintf("bot#%g#%s", skill, e.cleanname);
73 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
75 if(clienttype(e) == CLIENTTYPE_BOT)
76 s = sprintf("bot#%d", e.playerid);
78 s = sprintf("player#%d", e.playerid);
81 e.playerstats_id = strzone(s);
84 key = sprintf("%s:*", e.playerstats_id);
87 p = db_get(playerstats_db, key);
92 db_put(playerstats_db, key, playerstats_last);
93 strunzone(playerstats_last);
96 db_put(playerstats_db, key, "#");
97 playerstats_last = strzone(e.playerstats_id);
101 void PlayerStats_AddTeam(float t) // TODO: doesn't this remain unused?
103 if(playerstats_db < 0)
107 key = sprintf("%d", t);
110 p = db_get(playerstats_db, key);
115 db_put(playerstats_db, key, teamstats_last);
116 strunzone(teamstats_last);
119 db_put(playerstats_db, key, "#");
120 teamstats_last = strzone(key);
124 void PlayerStats_AddEvent(string event_id)
126 if(playerstats_db < 0)
130 key = sprintf("*:%s", event_id);
133 p = db_get(playerstats_db, key);
138 db_put(playerstats_db, key, events_last);
139 strunzone(events_last);
142 db_put(playerstats_db, key, "#");
143 events_last = strzone(event_id);
147 void PlayerStats_Event(entity e, string event_id, float value)
149 if((e.playerstats_id == "") || playerstats_db < 0)
154 key = sprintf("%s:%s", e.playerstats_id, event_id);
155 val = stof(db_get(playerstats_db, key));
157 db_put(playerstats_db, key, ftos(val));
160 void PlayerStats_TeamScore(float t, string event_id, float value) // TODO: doesn't this remain unused?
164 key = sprintf("team#%d:%s", t, event_id);
165 val = stof(db_get(playerstats_db, key));
167 db_put(playerstats_db, key, ftos(val));
173 A collection of lines of the format <key> SPACE <value> NEWLINE, where
174 <key> is always a single character.
176 The following keys are defined:
178 V: format version (always a fixed number) - this MUST be the first line!
179 #: comment (MUST be ignored by any parser)
180 R: release information on the server
181 T: time at which the game ended
183 O: mod name (icon request) as in server browser
185 I: match ID (see "matchid" in g_world.qc
186 S: "hostname" of the server
187 C: number of "unpure" cvar changes
188 U: UDP port number of the server
189 D: duration of the match
190 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
191 n: nickname of the player (optional)
194 e: followed by an event name, a space, and the event count/score
196 alivetime: total playing time of the player
197 avglatency: average network latency compounded throughout the match
198 wins: number of games won (can only be set if matches is set)
199 matches: number of matches played to the end (not aborted by map switch)
200 joins: number of matches joined (always 1 unless player never played during the match)
201 scoreboardvalid: set to 1 if the player was there at the end of the match
202 total-<scoreboardname>: total score of that scoreboard item
203 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
204 achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
205 kills-<index>: number of kills against the indexed player
206 rank <number>: rank of player
207 acc-<weapon netname>-hit: total damage dealt
208 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
209 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
210 acc-<weapon netname>-cnt-fired: amount of fired shots
211 acc-<weapon netname>-frags: amount of frags dealt by weapon
213 Response format (not used yet): see https://gist.github.com/4284222
216 void PlayerStats_ready(entity fh, entity pass, float status)
225 case URL_READY_CANWRITE:
226 url_fputs(fh, "V 5\n");
228 url_fputs(fh, sprintf("R %s\n", WATERMARK));
230 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
231 url_fputs(fh, sprintf("G %s\n", GetGametype()));
232 url_fputs(fh, sprintf("O %s\n", modname));
233 url_fputs(fh, sprintf("M %s\n", GetMapname()));
234 url_fputs(fh, sprintf("I %s\n", matchid));
235 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
236 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
237 url_fputs(fh, sprintf("U %d\n", cvar("port")));
238 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
239 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
241 url_fputs(fh, sprintf("P %s\n", p));
242 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
244 url_fputs(fh, sprintf("i %s\n", nn));
245 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
247 url_fputs(fh, sprintf("n %s\n", nn));
250 tt = db_get(playerstats_db, sprintf("%s:_team", p));
251 url_fputs(fh, sprintf("t %s\n", tt));
253 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
256 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
258 url_fputs(fh, sprintf("e %s %g\n", e, v));
264 case URL_READY_CANREAD:
265 // url_fclose is processing, we got a response for writing the data
266 // this must come from HTTP
267 print("Got response from player stats server:\n");
268 while((s = url_fgets(fh)))
270 print("End of response.\n");
273 case URL_READY_CLOSED:
274 // url_fclose has finished
275 print("Player stats written\n");
276 playerstats_waitforme = TRUE;
277 db_close(playerstats_db);
280 case URL_READY_ERROR:
282 print("Player stats writing failed: ", ftos(status), "\n");
283 playerstats_waitforme = TRUE;
284 if(playerstats_db >= 0)
286 db_close(playerstats_db);
293 //#NO AUTOCVARS START
294 void PlayerStats_Shutdown()
298 if(playerstats_db < 0)
301 uri = autocvar_g_playerstats_uri;
304 playerstats_waitforme = FALSE;
305 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
309 playerstats_waitforme = TRUE;
310 db_close(playerstats_db);
316 void PlayerStats_Accuracy(entity p)
322 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
324 w = get_weaponinfo(i);
326 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
327 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
329 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
330 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
332 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
334 //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
337 void PlayerStats_AddGlobalInfo(entity p)
339 if(playerstats_db < 0)
341 if((p.playerstats_id == "") || playerstats_db < 0)
343 p.playerstats_addedglobalinfo = TRUE;
348 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
352 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
354 if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
355 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
358 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
360 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
361 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
363 PlayerStats_Accuracy(p);
365 strunzone(p.playerstats_id);
366 p.playerstats_id = string_null;
369 .float scoreboard_pos;
370 void PlayerStats_EndMatch(float finished)
373 PlayerScore_Sort(score_dummyfield, 0, 0);
374 PlayerScore_Sort(scoreboard_pos, 1, 1);
375 FOR_EACH_CLIENT(p) // spectators intentionally not included
377 //PlayerStats_Accuracy(p); // stats are already written with PlayerStats_AddGlobalInfo(entity), don't double them up.
379 if((g_arena || g_lms || g_ca) && (p.alivetime <= 0)) { continue; }
380 else if(p.classname != "player") { continue; }
382 if(clienttype(p) == CLIENTTYPE_REAL)
386 float latency = (p.latency_sum / p.latency_cnt);
387 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
391 PlayerScore_PlayerStats(p);
392 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
395 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
396 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
397 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
398 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);