3 string playerstats_last;
5 .float playerstats_addedglobalinfo;
6 .string playerstats_id;
8 void PlayerStats_Init()
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_WINS);
24 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
25 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
26 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
27 PlayerStats_AddEvent(PLAYERSTATS_RANK);
32 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
34 w = get_weaponinfo(i);
36 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
37 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
39 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
40 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
42 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
45 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
46 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
47 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
48 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
49 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
50 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
51 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
52 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
53 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
54 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
57 void PlayerStats_AddPlayer(entity e)
59 if(playerstats_db < 0)
64 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
65 e.playerstats_id = strzone(e.crypto_idfp);
66 else if(clienttype(e) == CLIENTTYPE_BOT)
67 e.playerstats_id = strzone(sprintf("bot#%d", e.playerid));
69 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
72 key = sprintf("%s:*", e.playerstats_id);
75 p = db_get(playerstats_db, key);
80 db_put(playerstats_db, key, playerstats_last);
81 strunzone(playerstats_last);
84 db_put(playerstats_db, key, "#");
85 playerstats_last = strzone(e.playerstats_id);
89 void PlayerStats_AddTeam(float t) // TODO: doesn't this remain unused?
91 if(playerstats_db < 0)
95 key = sprintf("%d", t);
98 p = db_get(playerstats_db, key);
103 db_put(playerstats_db, key, teamstats_last);
104 strunzone(teamstats_last);
107 db_put(playerstats_db, key, "#");
108 teamstats_last = strzone(key);
112 void PlayerStats_AddEvent(string event_id)
114 if(playerstats_db < 0)
118 key = sprintf("*:%s", event_id);
121 p = db_get(playerstats_db, key);
126 db_put(playerstats_db, key, events_last);
127 strunzone(events_last);
130 db_put(playerstats_db, key, "#");
131 events_last = strzone(event_id);
135 void PlayerStats_Event(entity e, string event_id, float value)
137 if(!e.playerstats_id || playerstats_db < 0)
142 key = sprintf("%s:%s", e.playerstats_id, event_id);
143 val = stof(db_get(playerstats_db, key));
145 db_put(playerstats_db, key, ftos(val));
148 void PlayerStats_TeamScore(float t, string event_id, float value) // TODO: doesn't this remain unused?
152 key = sprintf("team#%d:%s", t, event_id);
153 val = stof(db_get(playerstats_db, key));
155 db_put(playerstats_db, key, ftos(val));
161 A collection of lines of the format <key> SPACE <value> NEWLINE, where
162 <key> is always a single character.
164 The following keys are defined:
166 V: format version (always 1) - this MUST be the first line!
167 #: comment (MUST be ignored by any parser)
168 R: release information on the server
169 T: time at which the game ended
172 S: "hostname" of the server
173 C: number of "unpure" cvar changes
174 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
175 I: match ID (see "matchid" in g_world.qc
176 n: nickname of the player (optional)
178 e: followed by an event name, a space, and the event count/score
180 alivetime: total playing time of the player
181 wins: number of games won (can only be set if matches is set)
182 matches: number of matches played to the end (not aborted by map switch)
183 joins: number of matches joined (always 1 unless player never played during the match)
184 scoreboardvalid: set to 1 if the player was there at the end of the match
185 total-<scoreboardname>: total score of that scoreboard item
186 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
187 achievement-<achievementname>: achievement counters
188 rank <number>: rank of player
189 acc-<weapon netname>-hit: total damage dealt
190 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
191 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
192 acc-<weapon netname>-cnt-fired: amount of fired shots
193 acc-<weapon netname>-frags: amount of frags dealt by weapon
196 void PlayerStats_ready(entity fh, entity pass, float status)
205 case URL_READY_CANWRITE:
206 url_fputs(fh, "V 1\n");
208 url_fputs(fh, sprintf("R %s\n", WATERMARK()));
210 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
211 url_fputs(fh, sprintf("G %s\n", GetGametype()));
212 url_fputs(fh, sprintf("M %s\n", GetMapname()));
213 url_fputs(fh, sprintf("I %s\n", matchid));
214 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
215 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
216 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
218 url_fputs(fh, sprintf("P %s\n", p));
219 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
221 url_fputs(fh, sprintf("i %s\n", nn));
222 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
224 url_fputs(fh, sprintf("n %s\n", nn));
227 tt = db_get(playerstats_db, sprintf("%s:_team", p));
228 url_fputs(fh, sprintf("t %s\n", tt));
230 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
233 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
235 url_fputs(fh, sprintf("e %s %g\n", e, v));
241 case URL_READY_CANREAD:
242 // url_fclose is processing, we got a response for writing the data
243 // this must come from HTTP
244 print("Got response from player stats server:\n");
245 while((s = url_fgets(fh)))
247 print("End of response.\n");
250 case URL_READY_CLOSED:
251 // url_fclose has finished
252 print("Player stats written\n");
253 playerstats_waitforme = TRUE;
254 db_close(playerstats_db);
257 case URL_READY_ERROR:
259 print("Player stats writing failed: ", ftos(status), "\n");
260 playerstats_waitforme = TRUE;
261 if(playerstats_db >= 0)
263 db_close(playerstats_db);
270 //#NO AUTOCVARS START
271 void PlayerStats_Shutdown()
275 if(playerstats_db < 0)
278 uri = autocvar_g_playerstats_uri;
281 playerstats_waitforme = FALSE;
282 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
286 playerstats_waitforme = TRUE;
287 db_close(playerstats_db);
293 void PlayerStats_Accuracy(entity p)
299 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
301 w = get_weaponinfo(i);
303 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
304 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
306 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
307 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
309 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
313 void PlayerStats_AddGlobalInfo(entity p)
315 if(playerstats_db < 0)
317 if(!p.playerstats_id || playerstats_db < 0)
319 p.playerstats_addedglobalinfo = TRUE;
324 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
328 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
330 if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
331 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
334 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
336 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
337 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
339 PlayerStats_Accuracy(p);
341 strunzone(p.playerstats_id);
342 p.playerstats_id = string_null;
345 void PlayerStats_EndMatch(float finished)
348 winner = PlayerScore_Sort(score_dummyfield);
349 FOR_EACH_CLIENT(p) // spectators intentionally not included
351 PlayerStats_Accuracy(p);
352 if(g_arena || g_lms || g_ca)
359 if(p.classname != "player")
362 PlayerScore_PlayerStats(p);
363 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
366 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
367 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
368 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);