2 //float PS_PM_IN_DB; // playerstats_prematch_in_db // db for info COLLECTED at the beginning of a match
3 float PS_GR_OUT_DB; // playerstats_gamereport_out_db // db of info SENT at the end of a match
4 //float PS_GR_IN_DB; // playerstats_gamereport_in_db // db for info COLLECTED at the end of a match
5 //float PS_B_IN_DB; // playerstats_playerbasic_in_db // db for info COLLECTED for basic player info (ELO)
6 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
10 //float PS_D_IN_DB; // playerstats_playerdetail_in_db // db for info COLLECTED for detailed player profile display
11 // http://stats.xonotic.org/player/me
15 //string PS_PM_IN_EVL; // playerstats_prematch_in_events_last
16 string PS_GR_OUT_TL; // playerstats_gamereport_out_teams_last
17 string PS_GR_OUT_PL; // playerstats_gamereport_out_players_las
18 string PS_GR_OUT_EVL; // playerstats_gamereport_out_events_last
19 //string PS_GR_IN_PL; // playerstats_gamereport_in_players_last
20 //string PS_GR_IN_EVL; // playerstats_gamereport_in_events_last
21 //string PS_B_IN_PL; // playerstats_playerbasic_in_players_las
22 //string PS_B_IN_EVL; // playerstats_playerbasic_in_events_last
26 //string PS_D_IN_EVL; // playerstats_playerdetail_in_events_last
30 void PlayerStats_Prematch(void)
35 void PlayerStats_GameReport_AddPlayer(entity e)
37 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
39 // set up player identification
40 string s = string_null;
42 if((e.crypto_idfp != "") && (e.cvar_cl_allow_uidtracking == 1))
43 { s = e.crypto_idfp; }
44 else if(IS_BOT_CLIENT(e))
45 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
47 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
50 { s = sprintf("bot#%d", e.playerid); }
52 { s = sprintf("player#%d", e.playerid); }
55 e.playerstats_id = strzone(s);
57 // now add the player to the database
58 string key = sprintf("%s:*", e.playerstats_id);
59 string p = db_get(PS_GR_OUT_DB, key);
65 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
66 strunzone(PS_GR_OUT_PL);
68 else { db_put(PS_GR_OUT_DB, key, "#"); }
69 PS_GR_OUT_PL = strzone(e.playerstats_id);
73 void PlayerStats_GameReport_AddTeam(float t)
75 if(PS_GR_OUT_DB < 0) { return; }
77 string key = sprintf("%d", t);
78 string p = db_get(PS_GR_OUT_DB, key);
84 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
85 strunzone(PS_GR_OUT_TL);
87 else { db_put(PS_GR_OUT_DB, key, "#"); }
88 PS_GR_OUT_TL = strzone(key);
92 void PlayerStats_GameReport_AddEvent(string event_id)
94 if(PS_GR_OUT_DB < 0) { return; }
96 string key = sprintf("*:%s", event_id);
97 string p = db_get(PS_GR_OUT_DB, key);
103 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
104 strunzone(PS_GR_OUT_EVL);
106 else { db_put(PS_GR_OUT_DB, key, "#"); }
107 PS_GR_OUT_EVL = strzone(event_id);
111 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
112 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
114 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
116 string key = sprintf("%s:%s", prefix, event_id);
117 float val = stof(db_get(PS_GR_OUT_DB, key));
119 db_put(PS_GR_OUT_DB, key, ftos(val));
123 void PlayerStats_GameReport_Accuracy(entity p)
128 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
130 w = get_weaponinfo(i);
132 #define ACCMAC(suffix,field) \
133 PS_GR_P_ADDVAL(p, sprintf("acc-%s-%s", w.netname, suffix), p.accuracy.(field[i-1]));
135 ACCMAC("hit", accuracy_hit)
136 ACCMAC("fired", accuracy_fired)
137 ACCMAC("cnt-hit", accuracy_cnt_hit)
138 ACCMAC("cnt-fired", accuracy_cnt_fired)
139 ACCMAC("frags", accuracy_frags)
145 void PlayerStats_GameReport_FinalizePlayer(entity p)
147 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
152 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
156 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
158 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
159 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), p.netname);
162 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
164 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
165 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
167 PlayerStats_GameReport_Accuracy(p);
169 if(IS_REAL_CLIENT(p))
173 float latency = (p.latency_sum / p.latency_cnt);
174 if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
178 strunzone(p.playerstats_id);
179 p.playerstats_id = string_null;
182 void PlayerStats_GameReport(float finished)
184 if(PS_GR_OUT_DB < 0) { return; }
186 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
187 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
188 if(teamplay) { PlayerScore_TeamStats(); }
193 // add personal score rank
194 PS_GR_P_ADDVAL(p, PLAYERSTATS_RANK, p.score_dummyfield);
199 // scoreboard is valid!
200 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
202 // add scoreboard position
203 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
205 // add scoreboard data
206 PlayerScore_PlayerStats(p);
208 // if the match ended normally, add winning info
211 PS_GR_P_ADDVAL(p, PLAYERSTATS_WINS, p.winning);
212 PS_GR_P_ADDVAL(p, PLAYERSTATS_MATCHES, 1);
216 // collect final player information
217 PlayerStats_GameReport_FinalizePlayer(p);
220 if(autocvar_g_playerstats_gamereport_uri != "")
222 PlayerStats_GameReport_DelayMapVote = TRUE;
224 autocvar_g_playerstats_gamereport_uri,
226 PlayerStats_GameReport_Handler,
232 PlayerStats_GameReport_DelayMapVote = FALSE;
233 db_close(PS_GR_OUT_DB);
238 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
240 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
243 PS_GR_OUT_DB = db_create();
245 if(PS_GR_OUT_DB >= 0)
247 PlayerStats_GameReport_DelayMapVote = TRUE;
249 serverflags |= SERVERFLAG_PLAYERSTATS;
251 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
252 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
253 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
254 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
255 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
256 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
257 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
258 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
263 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
265 w = get_weaponinfo(i);
266 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-hit"));
267 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-fired"));
268 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
269 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
270 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-frags"));
273 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
274 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
275 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
276 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
277 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
278 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
279 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
280 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
281 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
282 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
284 else { PlayerStats_GameReport_DelayMapVote = FALSE; }
287 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
297 // ======================================
298 // -- OUTGOING GAME REPORT INFORMATION --
299 // ======================================
301 * V: format version (always a fixed number) - this MUST be the first line!
302 * #: comment (MUST be ignored by any parser)
303 * R: release information on the server
305 * O: mod name (icon request) as in server browser
307 * I: match ID (see "matchid" in g_world.qc)
308 * S: "hostname" of the server
309 * C: number of "unpure" cvar changes
310 * U: UDP port number of the server
311 * D: duration of the match
312 * L: "ladder" in which the server is participating in
313 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
314 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
316 * n: nickname of the player (optional)
318 * e: followed by an event name, a space, and the event count/score
319 * event names can be:
320 * alivetime: total playing time of the player
321 * avglatency: average network latency compounded throughout the match
322 * wins: number of games won (can only be set if matches is set)
323 * matches: number of matches played to the end (not aborted by map switch)
324 * joins: number of matches joined (always 1 unless player never played during the match)
325 * scoreboardvalid: set to 1 if the player was there at the end of the match
326 * total-<scoreboardname>: total score of that scoreboard item
327 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
328 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
329 * kills-<index>: number of kills against the indexed player
330 * rank <number>: rank of player
331 * acc-<weapon netname>-hit: total damage dealt
332 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
333 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
334 * acc-<weapon netname>-cnt-fired: amount of fired shots
335 * acc-<weapon netname>-frags: amount of frags dealt by weapon
337 case URL_READY_CANWRITE:
339 url_fputs(fh, "V 9\n");
341 url_fputs(fh, sprintf("R %s\n", WATERMARK));
343 url_fputs(fh, sprintf("G %s\n", GetGametype()));
344 url_fputs(fh, sprintf("O %s\n", modname));
345 url_fputs(fh, sprintf("M %s\n", GetMapname()));
346 url_fputs(fh, sprintf("I %s\n", matchid));
347 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
348 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
349 url_fputs(fh, sprintf("U %d\n", cvar("port")));
350 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
351 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
356 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
358 // start team section
359 url_fputs(fh, sprintf("Q team#%s\n", t));
361 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
362 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
364 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
365 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
371 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
373 // start player section
374 url_fputs(fh, sprintf("P %s\n", p));
376 // playerid/index (entity number for this server)
377 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
378 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
381 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
382 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
384 // team identification number
387 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
388 url_fputs(fh, sprintf("t %s\n", tt));
391 // output player events
392 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
394 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
395 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
403 // ======================================
404 // -- INCOMING GAME REPORT INFORMATION --
405 // ======================================
409 case URL_READY_CANREAD:
411 // url_fclose is processing, we got a response for writing the data
412 // this must come from HTTP
413 print("Got response from player stats server:\n");
414 while((s = url_fgets(fh))) { print(" ", s, "\n"); }
415 print("End of response.\n");
420 case URL_READY_CLOSED:
422 // url_fclose has finished
423 print("Player stats written\n");
424 PlayerStats_GameReport_DelayMapVote = FALSE;
425 db_close(PS_GR_OUT_DB);
430 case URL_READY_ERROR:
433 print("Player stats writing failed: ", ftos(status), "\n");
434 PlayerStats_GameReport_DelayMapVote = FALSE;
435 if(PS_GR_OUT_DB >= 0)
437 db_close(PS_GR_OUT_DB);
447 void PlayerStats_PlayerDetail_Handler(entity fh, entity p, float status)
451 case URL_READY_CANWRITE:
453 print("-- Sending data to player stats server\n");
454 /*url_fputs(fh, "V 1\n");
456 url_fputs(fh, sprintf("R %s\n", WATERMARK));
458 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
459 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
460 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
461 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
462 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
463 */url_fputs(fh, "\n");
468 case URL_READY_CANREAD:
471 print("-- Got response from player stats server:\n");
472 //string gametype = string_null;
473 while((s = url_fgets(fh)))
477 string key = "", value = "", data = "";
479 n = tokenizebyseparator(s, " "); // key (value) data
500 PlayerInfo_AddItem(p, "_version", data);
502 PlayerInfo_AddItem(p, "_release", data);
504 PlayerInfo_AddItem(p, "_time", data);
506 PlayerInfo_AddItem(p, "_statsurl", data);
508 PlayerInfo_AddItem(p, "_hashkey", data);
510 PlayerInfo_AddItem(p, "_playernick", data);
512 PlayerInfo_AddItem(p, "_playerid", data);
515 else if (key == "e" && value != "")
518 PlayerInfo_AddItem(p, value, data);
520 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
526 print("-- End of response.\n");
530 case URL_READY_CLOSED:
532 // url_fclose has finished
533 print("Player stats synchronized with server\n");
537 case URL_READY_ERROR:
540 print("Receiving player stats failed: ", ftos(status), "\n");
546 void PlayerStats_PlayerDetail()
549 //PS_D_IN_DB = db_create();
551 //if(PS_D_IN_DB < 0) { return; }
553 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
555 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
556 print("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri, "\n");
558 autocvar_g_playerstats_playerdetail_uri,
560 PlayerStats_PlayerDetail_Handler,
568 void PlayerInfo_AddPlayer(entity e)
570 if(playerinfo_db < 0)
574 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
577 p = db_get(playerinfo_db, key);
582 db_put(playerinfo_db, key, playerinfo_last);
583 strunzone(playerinfo_last);
586 db_put(playerinfo_db, key, "#");
587 playerinfo_last = strzone(ftos(e.playerid));
588 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
592 void PlayerInfo_AddItem(entity e, string item_id, string val)
594 if(playerinfo_db < 0)
598 key = sprintf("*:%s", item_id);
601 p = db_get(playerinfo_db, key);
604 if(playerinfo_events_last)
606 db_put(playerinfo_db, key, playerinfo_events_last);
607 strunzone(playerinfo_events_last);
610 db_put(playerinfo_db, key, "#");
611 playerinfo_events_last = strzone(item_id);
614 key = sprintf("#%d:%s", e.playerid, item_id);
615 db_put(playerinfo_db, key, val);
616 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
619 string PlayerInfo_GetItem(entity e, string item_id)
621 if(playerinfo_db < 0)
625 key = sprintf("#%d:%s", e.playerid, item_id);
626 return db_get(playerinfo_db, key);
629 string PlayerInfo_GetItemLocal(string item_id)
633 return PlayerInfo_GetItem(p, item_id);
636 void PlayerInfo_ready(entity fh, entity p, float status)
641 PlayerInfo_AddPlayer(p);
645 case URL_READY_CANWRITE:
646 print("-- Sending data to player stats server\n");
647 url_fputs(fh, "V 1\n");
649 url_fputs(fh, sprintf("R %s\n", WATERMARK));
652 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
653 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
654 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
655 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
656 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
661 case URL_READY_CANREAD:
662 print("-- Got response from player stats server:\n");
663 string gametype = string_null;
664 while((s = url_fgets(fh)))
668 string key = "", value = "", data = "";
670 n = tokenizebyseparator(s, " "); // key (value) data
691 PlayerInfo_AddItem(p, "_version", data);
693 PlayerInfo_AddItem(p, "_release", data);
695 PlayerInfo_AddItem(p, "_time", data);
697 PlayerInfo_AddItem(p, "_statsurl", data);
699 PlayerInfo_AddItem(p, "_hashkey", data);
701 PlayerInfo_AddItem(p, "_playernick", data);
703 PlayerInfo_AddItem(p, "_playerid", data);
706 else if (key == "e" && value != "")
709 PlayerInfo_AddItem(p, value, data);
711 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
716 print("-- End of response.\n");
719 case URL_READY_CLOSED:
720 // url_fclose has finished
721 print("Player stats synchronized with server\n");
723 case URL_READY_ERROR:
725 print("Receiving player stats failed: ", ftos(status), "\n");
730 void PlayerInfo_Init()
733 playerinfo_db = db_create();
737 void PlayerInfo_Basic(entity p)
739 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
741 if(playerinfo_db < 0)
745 uri = autocvar_g_playerinfo_uri;
746 if(uri != "" && p.crypto_idfp != "")
748 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
749 print("Retrieving playerstats from URL: ", uri, "\n");
750 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
756 void PlayerInfo_Details()
758 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
760 if(playerinfo_db < 0)
764 uri = autocvar_g_playerinfo_uri; // FIXME
765 if(uri != "" && crypto_getmyidstatus(0) > 0)
767 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
768 uri = strcat(uri, "/player/me");
769 print("Retrieving playerstats from URL: ", uri, "\n");
770 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
777 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
778 void PlayerInfo_Details()
780 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
782 if(playerinfo_db < 0)
786 uri = autocvar_g_playerinfo_uri; // FIXME
787 if(uri != "" && crypto_getmyidstatus(0) > 0)
789 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
790 print("Retrieving playerstats from URL: ", uri, "\n");
791 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);