5 string playerstats_last;
7 .float playerstats_addedglobalinfo;
8 .string playerstats_id;
10 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
14 playerstats_waitforme = TRUE;
15 uri = autocvar_g_playerstats_uri;
18 playerstats_db = db_create();
19 if(playerstats_db >= 0)
20 playerstats_waitforme = FALSE; // must wait for it at match end
22 serverflags |= SERVERFLAG_PLAYERSTATS;
24 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
25 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
26 PlayerStats_AddEvent(PLAYERSTATS_WINS);
27 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
28 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
29 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
30 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
31 PlayerStats_AddEvent(PLAYERSTATS_RANK);
36 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
38 w = get_weaponinfo(i);
40 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
41 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
43 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
44 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
46 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
49 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
50 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
51 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
52 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
53 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
54 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
55 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
56 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
57 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
58 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
61 void PlayerStats_AddPlayer(entity e)
65 if(playerstats_db < 0)
71 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
73 else if(IS_BOT_CLIENT(e))
74 s = sprintf("bot#%g#%s", skill, e.cleanname);
76 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
79 s = sprintf("bot#%d", e.playerid);
81 s = sprintf("player#%d", e.playerid);
84 e.playerstats_id = strzone(s);
87 key = sprintf("%s:*", e.playerstats_id);
90 p = db_get(playerstats_db, key);
95 db_put(playerstats_db, key, playerstats_last);
96 strunzone(playerstats_last);
99 db_put(playerstats_db, key, "#");
100 playerstats_last = strzone(e.playerstats_id);
104 void PlayerStats_AddTeam(float t)
106 if(playerstats_db < 0)
110 key = sprintf("%d", t);
113 p = db_get(playerstats_db, key);
118 db_put(playerstats_db, key, teamstats_last);
119 strunzone(teamstats_last);
122 db_put(playerstats_db, key, "#");
123 teamstats_last = strzone(key);
127 void PlayerStats_AddEvent(string event_id)
129 if(playerstats_db < 0)
133 key = sprintf("*:%s", event_id);
136 p = db_get(playerstats_db, key);
141 db_put(playerstats_db, key, events_last);
142 strunzone(events_last);
145 db_put(playerstats_db, key, "#");
146 events_last = strzone(event_id);
150 float PlayerStats_Event(entity e, string event_id, float value)
152 if((e.playerstats_id == "") || playerstats_db < 0)
157 key = sprintf("%s:%s", e.playerstats_id, event_id);
158 val = stof(db_get(playerstats_db, key));
160 db_put(playerstats_db, key, ftos(val));
164 float PlayerStats_TeamScore(float t, string event_id, float value)
166 if(playerstats_db < 0)
171 key = sprintf("team#%d:%s", t, event_id);
172 val = stof(db_get(playerstats_db, key));
174 db_put(playerstats_db, key, ftos(val));
181 A collection of lines of the format <key> SPACE <value> NEWLINE, where
182 <key> is always a single character.
184 The following keys are defined:
186 V: format version (always a fixed number) - this MUST be the first line!
187 #: comment (MUST be ignored by any parser)
188 R: release information on the server
190 O: mod name (icon request) as in server browser
192 I: match ID (see "matchid" in g_world.qc
193 S: "hostname" of the server
194 C: number of "unpure" cvar changes
195 U: UDP port number of the server
196 D: duration of the match
197 L: "ladder" in which the server is participating in
198 P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
199 Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
200 n: nickname of the player (optional)
203 e: followed by an event name, a space, and the event count/score
205 alivetime: total playing time of the player
206 avglatency: average network latency compounded throughout the match
207 wins: number of games won (can only be set if matches is set)
208 matches: number of matches played to the end (not aborted by map switch)
209 joins: number of matches joined (always 1 unless player never played during the match)
210 scoreboardvalid: set to 1 if the player was there at the end of the match
211 total-<scoreboardname>: total score of that scoreboard item
212 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
213 achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
214 kills-<index>: number of kills against the indexed player
215 rank <number>: rank of player
216 acc-<weapon netname>-hit: total damage dealt
217 acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
218 acc-<weapon netname>-cnt-hit: amount of shots that actually hit
219 acc-<weapon netname>-cnt-fired: amount of fired shots
220 acc-<weapon netname>-frags: amount of frags dealt by weapon
222 Response format (not used yet): see https://gist.github.com/4284222
225 void PlayerStats_ready(entity fh, entity pass, float status)
235 case URL_READY_CANWRITE:
236 url_fputs(fh, "V 9\n");
238 url_fputs(fh, sprintf("R %s\n", WATERMARK));
240 url_fputs(fh, sprintf("G %s\n", GetGametype()));
241 url_fputs(fh, sprintf("O %s\n", modname));
242 url_fputs(fh, sprintf("M %s\n", GetMapname()));
243 url_fputs(fh, sprintf("I %s\n", matchid));
244 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
245 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
246 url_fputs(fh, sprintf("U %d\n", cvar("port")));
247 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
248 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_ladder));
250 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
252 url_fputs(fh, sprintf("Q team#%s\n", t));
253 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
256 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
258 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);
439 //// WIP -zykure /////////////////////////////////////////////////////
445 string playerinfo_last;
446 string playerinfo_events_last;
449 void PlayerInfo_AddPlayer(entity e)
451 if(playerinfo_db < 0)
455 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
458 p = db_get(playerinfo_db, key);
463 db_put(playerinfo_db, key, playerinfo_last);
464 strunzone(playerinfo_last);
467 db_put(playerinfo_db, key, "#");
468 playerinfo_last = strzone(ftos(e.playerid));
469 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
473 void PlayerInfo_AddItem(entity e, string item_id, string val)
475 if(playerinfo_db < 0)
479 key = sprintf("*:%s", item_id);
482 p = db_get(playerinfo_db, key);
485 if(playerinfo_events_last)
487 db_put(playerinfo_db, key, playerinfo_events_last);
488 strunzone(playerinfo_events_last);
491 db_put(playerinfo_db, key, "#");
492 playerinfo_events_last = strzone(item_id);
495 key = sprintf("#%d:%s", e.playerid, item_id);
496 db_put(playerinfo_db, key, val);
497 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
500 string PlayerInfo_GetItem(entity e, string item_id)
502 if(playerinfo_db < 0)
506 key = sprintf("#%d:%s", e.playerid, item_id);
507 return db_get(playerinfo_db, key);
510 string PlayerInfo_GetItemLocal(string item_id)
514 return PlayerInfo_GetItem(p, item_id);
517 void PlayerInfo_ready(entity fh, entity p, float status)
522 PlayerInfo_AddPlayer(p);
526 case URL_READY_CANWRITE:
527 print("-- Sending data to player stats server\n");
528 url_fputs(fh, "V 1\n");
530 url_fputs(fh, sprintf("R %s\n", WATERMARK));
533 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
534 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
535 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
536 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
537 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
542 case URL_READY_CANREAD:
543 print("-- Got response from player stats server:\n");
544 string gametype = string_null;
545 while((s = url_fgets(fh)))
549 string key = "", value = "", data = "";
551 n = tokenizebyseparator(s, " "); // key (value) data
572 PlayerInfo_AddItem(p, "_version", data);
574 PlayerInfo_AddItem(p, "_release", data);
576 PlayerInfo_AddItem(p, "_time", data);
578 PlayerInfo_AddItem(p, "_statsurl", data);
580 PlayerInfo_AddItem(p, "_hashkey", data);
582 PlayerInfo_AddItem(p, "_playernick", data);
584 PlayerInfo_AddItem(p, "_playerid", data);
587 else if (key == "e" && value != "")
590 PlayerInfo_AddItem(p, value, data);
592 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
597 print("-- End of response.\n");
600 case URL_READY_CLOSED:
601 // url_fclose has finished
602 print("Player stats synchronized with server\n");
604 case URL_READY_ERROR:
606 print("Receiving player stats failed: ", ftos(status), "\n");
611 void PlayerInfo_Init()
614 playerinfo_db = db_create();
618 void PlayerInfo_Basic(entity p)
620 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
622 if(playerinfo_db < 0)
626 uri = playerinfo_uri; // FIXME
627 if(uri != "" && p.crypto_idfp != "")
629 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
630 print("Retrieving playerstats from URL: ", uri, "\n");
631 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
637 void PlayerInfo_Details()
639 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
641 if(playerinfo_db < 0)
645 uri = playerinfo_uri; // FIXME
646 if(uri != "" && crypto_getmyidstatus(0) > 0)
648 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
649 uri = strcat(uri, "/player/me");
650 print("Retrieving playerstats from URL: ", uri, "\n");
651 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
658 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
659 void PlayerInfo_Details()
661 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
663 if(playerinfo_db < 0)
667 uri = playerinfo_uri; // FIXME
668 if(uri != "" && crypto_getmyidstatus(0) > 0)
670 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
671 print("Retrieving playerstats from URL: ", uri, "\n");
672 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);