3 string playerstats_last;
5 .float playerstats_addedglobalinfo;
6 float playerstats_requested;
7 .string playerstats_id;
9 void PlayerStats_Init()
13 playerstats_waitforme = TRUE;
14 uri = autocvar_g_playerstats_uri;
17 playerstats_db = db_create();
18 if(playerstats_db >= 0)
19 playerstats_waitforme = FALSE; // must wait for it at match end
21 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
22 PlayerStats_AddEvent(PLAYERSTATS_WINS);
23 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
24 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
25 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
26 PlayerStats_AddEvent(PLAYERSTATS_RANK);
31 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
33 w = get_weaponinfo(i);
35 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
36 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
38 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
39 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
41 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
44 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
45 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
46 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
47 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
48 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
49 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
50 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
51 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
52 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
53 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
56 void PlayerStats_AddPlayer(entity e)
58 if(playerstats_db < 0)
63 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
64 e.playerstats_id = strzone(e.crypto_idfp);
65 else if(clienttype(e) == CLIENTTYPE_BOT)
66 e.playerstats_id = strzone(sprintf("bot#%d", e.playerid));
68 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
71 key = sprintf("%s:*", e.playerstats_id);
74 p = db_get(playerstats_db, key);
79 db_put(playerstats_db, key, playerstats_last);
80 strunzone(playerstats_last);
83 db_put(playerstats_db, key, "#");
84 playerstats_last = strzone(e.playerstats_id);
88 void PlayerStats_AddTeam(float t) // TODO: doesn't this remain unused?
90 if(playerstats_db < 0)
94 key = sprintf("%d", t);
97 p = db_get(playerstats_db, key);
102 db_put(playerstats_db, key, teamstats_last);
103 strunzone(teamstats_last);
106 db_put(playerstats_db, key, "#");
107 teamstats_last = strzone(key);
111 void PlayerStats_AddEvent(string event_id)
113 if(playerstats_db < 0)
117 key = sprintf("*:%s", event_id);
120 p = db_get(playerstats_db, key);
125 db_put(playerstats_db, key, events_last);
126 strunzone(events_last);
129 db_put(playerstats_db, key, "#");
130 events_last = strzone(event_id);
134 void PlayerStats_Event(entity e, string event_id, float value)
136 if(!e.playerstats_id || playerstats_db < 0)
141 key = sprintf("%s:%s", e.playerstats_id, event_id);
142 val = stof(db_get(playerstats_db, key));
144 db_put(playerstats_db, key, ftos(val));
147 void PlayerStats_TeamScore(float t, string event_id, float value) // TODO: doesn't this remain unused?
151 key = sprintf("team#%d:%s", t, event_id);
152 val = stof(db_get(playerstats_db, key));
154 db_put(playerstats_db, key, ftos(val));
157 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
159 if(playerstats_requested)
160 playerstats_waitforme = TRUE;
166 A collection of lines of the format <key> SPACE <value> NEWLINE, where
167 <key> is always a single character.
169 The following keys are defined:
171 V: format version (always 1) - this MUST be the first line!
172 #: comment (MUST be ignored by any parser)
173 R: release information on the server
174 T: time at which the game ended
177 S: "hostname" of the server
178 C: number of "unpure" cvar changes
180 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
181 n: nickname of the player (optional)
183 e: followed by an event name, a space, and the event count/score
185 alivetime: total playing time of the player
186 wins: number of games won (can only be set if matches is set)
187 matches: number of matches played to the end (not aborted by map switch)
188 joins: number of matches joined (always 1 unless player never played during the match)
189 scoreboardvalid: set to 1 if the player was there at the end of the match
190 total-<scoreboardname>: total score of that scoreboard item
191 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
192 achievement-<achievementname>: achievement counters
193 rank <number>: rank of player
194 acc-<weapon netname>-hit: total damage dealt
195 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
196 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
197 acc-<weapon netname>-cnt-fired: amount of fired shots
198 acc-<weapon netname>-frags: amount of frags dealt by weapon
201 void PlayerStats_ready(entity fh, entity pass, float status)
210 case URL_READY_CANWRITE:
211 url_fputs(fh, "V 1\n");
213 url_fputs(fh, sprintf("R %s\n", WATERMARK()));
215 url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
216 url_fputs(fh, sprintf("G %s\n", GetGametype()));
217 url_fputs(fh, sprintf("M %s\n", GetMapname()));
218 url_fputs(fh, sprintf("I %s\n", matchid));
219 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
220 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
221 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
223 url_fputs(fh, sprintf("P %s\n", p));
224 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
226 url_fputs(fh, sprintf("i %s\n", nn));
227 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
229 url_fputs(fh, sprintf("n %s\n", nn));
232 tt = db_get(playerstats_db, sprintf("%s:_team", p));
233 url_fputs(fh, sprintf("t %s\n", tt));
235 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
238 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
240 url_fputs(fh, sprintf("e %s %g\n", e, v));
244 db_close(playerstats_db);
246 url_fclose(fh, PlayerStats_ready, world);
248 case URL_READY_CANREAD:
249 // url_fclose is processing, we got a response for writing the data
250 // this must come from HTTP
251 print("Got response from player stats server:\n");
252 while((s = url_fgets(fh)))
254 print("End of response.\n");
255 url_fclose(fh, PlayerStats_ready, world);
257 case URL_READY_CLOSED:
258 // url_fclose has finished
259 print("Player stats written\n");
260 playerstats_waitforme = TRUE;
262 case URL_READY_ERROR:
264 print("Player stats writing failed: ", ftos(status), "\n");
265 playerstats_waitforme = TRUE;
266 if(playerstats_db >= 0)
268 db_close(playerstats_db);
275 //#NO AUTOCVARS START
276 void PlayerStats_Shutdown()
280 if(playerstats_db < 0)
283 uri = autocvar_g_playerstats_uri;
286 url_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
287 playerstats_waitforme = FALSE;
291 playerstats_waitforme = TRUE;
292 db_close(playerstats_db);
298 void PlayerStats_AddGlobalInfo(entity p)
300 if(playerstats_db < 0)
302 if(!p.playerstats_id || playerstats_db < 0)
304 p.playerstats_addedglobalinfo = TRUE;
309 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
313 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
315 if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
316 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
319 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
321 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
322 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
324 strunzone(p.playerstats_id);
325 p.playerstats_id = string_null;
328 void PlayerStats_Accuracy(entity p)
334 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
336 w = get_weaponinfo(i);
338 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.accuracy_hit[i-1]);
339 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.accuracy_fired[i-1]);
341 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.accuracy_cnt_hit[i-1]);
342 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.accuracy_cnt_fired[i-1]);
344 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.accuracy_frags[i-1]);
348 void PlayerStats_EndMatch(float finished)
351 winner = PlayerScore_Sort(score_dummyfield);
352 FOR_EACH_PLAYER(p) // spectators intentionally not included
354 PlayerScore_PlayerStats(p);
355 PlayerStats_Accuracy(p);
356 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
359 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
360 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
361 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);