1 #include "playerstats.qh"
5 #include "constants.qh"
7 #include <common/weapons/_all.qh>
8 #include "../server/anticheat.qh"
9 #include "../server/defs.qh"
10 #include "../server/scores.qh"
11 #include "../server/weapons/accuracy.qh"
15 void PlayerStats_Prematch()
20 void PlayerStats_GameReport_AddPlayer(entity e)
22 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
24 // set up player identification
27 if((e.crypto_idfp != "") && (e.cvar_cl_allow_uidtracking == 1))
28 { s = e.crypto_idfp; }
29 else if(IS_BOT_CLIENT(e))
30 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
32 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
35 { s = sprintf("bot#%d", e.playerid); }
37 { s = sprintf("player#%d", e.playerid); }
40 e.playerstats_id = strzone(s);
42 // now add the player to the database
43 string key = sprintf("%s:*", e.playerstats_id);
44 string p = db_get(PS_GR_OUT_DB, key);
50 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
51 strunzone(PS_GR_OUT_PL);
53 else { db_put(PS_GR_OUT_DB, key, "#"); }
54 PS_GR_OUT_PL = strzone(e.playerstats_id);
58 void PlayerStats_GameReport_AddTeam(float t)
60 if(PS_GR_OUT_DB < 0) { return; }
62 string key = sprintf("%d", t);
63 string p = db_get(PS_GR_OUT_DB, key);
69 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
70 strunzone(PS_GR_OUT_TL);
72 else { db_put(PS_GR_OUT_DB, key, "#"); }
73 PS_GR_OUT_TL = strzone(key);
77 void PlayerStats_GameReport_AddEvent(string event_id)
79 if(PS_GR_OUT_DB < 0) { return; }
81 string key = sprintf("*:%s", event_id);
82 string p = db_get(PS_GR_OUT_DB, key);
88 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
89 strunzone(PS_GR_OUT_EVL);
91 else { db_put(PS_GR_OUT_DB, key, "#"); }
92 PS_GR_OUT_EVL = strzone(event_id);
96 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
97 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
99 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
101 string key = sprintf("%s:%s", prefix, event_id);
102 float val = stof(db_get(PS_GR_OUT_DB, key));
104 db_put(PS_GR_OUT_DB, key, ftos(val));
108 void PlayerStats_GameReport_Accuracy(entity p)
110 #define ACCMAC(suffix, field) \
111 PS_GR_P_ADDVAL(p, sprintf("acc-%s-%s", it.netname, suffix), p.accuracy.(field[i-1]));
112 FOREACH(Weapons, it != WEP_Null, {
113 ACCMAC("hit", accuracy_hit)
114 ACCMAC("fired", accuracy_fired)
115 ACCMAC("cnt-hit", accuracy_cnt_hit)
116 ACCMAC("cnt-fired", accuracy_cnt_fired)
117 ACCMAC("frags", accuracy_frags)
122 void PlayerStats_GameReport_FinalizePlayer(entity p)
124 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
129 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
133 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
135 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
136 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p, false));
139 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
141 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
142 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
144 PlayerStats_GameReport_Accuracy(p);
145 anticheat_report_to_playerstats(p);
147 if(IS_REAL_CLIENT(p))
151 float latency = (p.latency_sum / p.latency_cnt);
152 if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
156 strunzone(p.playerstats_id);
157 p.playerstats_id = string_null;
160 void PlayerStats_GameReport(float finished)
162 if(PS_GR_OUT_DB < 0) { return; }
164 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
165 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
166 if(teamplay) { PlayerScore_TeamStats(); }
168 FOREACH_CLIENT(true, {
169 // add personal score rank
170 PS_GR_P_ADDVAL(it, PLAYERSTATS_RANK, it.score_dummyfield);
173 if(it.scoreboard_pos)
175 // scoreboard is valid!
176 PS_GR_P_ADDVAL(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
178 // add scoreboard position
179 PS_GR_P_ADDVAL(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
181 // add scoreboard data
182 PlayerScore_PlayerStats(it);
184 // if the match ended normally, add winning info
187 PS_GR_P_ADDVAL(it, PLAYERSTATS_WINS, it.winning);
188 PS_GR_P_ADDVAL(it, PLAYERSTATS_MATCHES, 1);
192 // collect final player information
193 PlayerStats_GameReport_FinalizePlayer(it);
196 if(autocvar_g_playerstats_gamereport_uri != "")
198 PlayerStats_GameReport_DelayMapVote = true;
200 autocvar_g_playerstats_gamereport_uri,
202 PlayerStats_GameReport_Handler,
208 PlayerStats_GameReport_DelayMapVote = false;
209 db_close(PS_GR_OUT_DB);
214 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
216 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
218 PS_GR_OUT_DB = db_create();
220 if(PS_GR_OUT_DB >= 0)
222 PlayerStats_GameReport_DelayMapVote = true;
224 serverflags |= SERVERFLAG_PLAYERSTATS;
226 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
227 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
228 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
229 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
230 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
231 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
232 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
233 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
236 FOREACH(Weapons, it != WEP_Null, {
237 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
238 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
239 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
240 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
241 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
244 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
245 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
246 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
247 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
248 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
249 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
250 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
251 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
252 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
253 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
255 anticheat_register_to_playerstats();
257 else { PlayerStats_GameReport_DelayMapVote = false; }
260 // this... is a hack, a temporary one until we get a proper duel gametype
261 string PlayerStats_GetGametype()
263 return ((IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2) ? "duel" : GetGametype());
266 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
276 // ======================================
277 // -- OUTGOING GAME REPORT INFORMATION --
278 // ======================================
280 * V: format version (always a fixed number) - this MUST be the first line!
281 * #: comment (MUST be ignored by any parser)
282 * R: release information on the server
284 * O: mod name (icon request) as in server browser
286 * I: match ID (see "matchid" in g_world.qc)
287 * S: "hostname" of the server
288 * C: number of "unpure" cvar changes
289 * U: UDP port number of the server
290 * D: duration of the match
291 * L: "ladder" in which the server is participating in
292 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
293 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
295 * n: nickname of the player (optional)
297 * e: followed by an event name, a space, and the event count/score
298 * event names can be:
299 * alivetime: total playing time of the player
300 * avglatency: average network latency compounded throughout the match
301 * wins: number of games won (can only be set if matches is set)
302 * matches: number of matches played to the end (not aborted by map switch)
303 * joins: number of matches joined (always 1 unless player never played during the match)
304 * scoreboardvalid: set to 1 if the player was there at the end of the match
305 * total-<scoreboardname>: total score of that scoreboard item
306 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
307 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
308 * kills-<index>: number of kills against the indexed player
309 * rank <number>: rank of player
310 * acc-<weapon netname>-hit: total damage dealt
311 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
312 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
313 * acc-<weapon netname>-cnt-fired: amount of fired shots
314 * acc-<weapon netname>-frags: amount of frags dealt by weapon
316 case URL_READY_CANWRITE:
318 url_fputs(fh, "V 9\n");
320 url_fputs(fh, sprintf("R %s\n", WATERMARK));
322 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
323 url_fputs(fh, sprintf("O %s\n", modname));
324 url_fputs(fh, sprintf("M %s\n", GetMapname()));
325 url_fputs(fh, sprintf("I %s\n", matchid));
326 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
327 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
328 url_fputs(fh, sprintf("U %d\n", cvar("port")));
329 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
330 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
335 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
337 // start team section
338 url_fputs(fh, sprintf("Q team#%s\n", t));
340 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
341 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
343 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
344 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
350 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
352 // start player section
353 url_fputs(fh, sprintf("P %s\n", p));
355 // playerid/index (entity number for this server)
356 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
357 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
360 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
361 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
363 // team identification number
366 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
367 url_fputs(fh, sprintf("t %s\n", tt));
370 // output player events
371 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
373 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
374 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
382 // ======================================
383 // -- INCOMING GAME REPORT INFORMATION --
384 // ======================================
388 case URL_READY_CANREAD:
390 // url_fclose is processing, we got a response for writing the data
391 // this must come from HTTP
392 LOG_TRACE("Got response from player stats server:");
393 while((s = url_fgets(fh))) { LOG_TRACE(" ", s); }
394 LOG_TRACE("End of response.");
399 case URL_READY_CLOSED:
401 // url_fclose has finished
402 LOG_TRACE("Player stats written");
403 PlayerStats_GameReport_DelayMapVote = false;
404 if(PS_GR_OUT_DB >= 0)
406 db_close(PS_GR_OUT_DB);
412 case URL_READY_ERROR:
415 LOG_INFO("Player stats writing failed: ", ftos(status), "\n");
416 PlayerStats_GameReport_DelayMapVote = false;
417 if(PS_GR_OUT_DB >= 0)
419 db_close(PS_GR_OUT_DB);
427 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
429 PlayerScore_Add(joiningplayer, SP_ELO, -1);
430 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
431 if(autocvar_g_playerstats_playerbasic_uri != "")
433 string uri = autocvar_g_playerstats_playerbasic_uri;
434 if (joiningplayer.crypto_idfp == "") {
435 PlayerScore_Add(joiningplayer, SP_ELO, -1);
437 // create the database if it doesn't already exist
439 PS_B_IN_DB = db_create();
441 // now request the information
442 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
443 LOG_TRACE("Retrieving playerstats from URL: ", uri);
447 PlayerStats_PlayerBasic_Handler,
451 // set status appropriately // todo: check whether the player info exists in the database previously
454 // database still contains useful information, so don't clear it of a useful status
455 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
459 // database was previously empty or never hit received status for some reason
460 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
466 // server has this disabled, kill the DB and set status to idle
467 PlayerScore_Add(joiningplayer, SP_ELO, -1);
470 db_close(PS_B_IN_DB);
473 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
478 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
480 // determine whether we should retrieve playerbasic information again
482 LOG_TRACEF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
483 joiningplayer.netname,
487 // TODO: check to see if this playerid is inside the database already somehow...
488 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
489 // although maybe we should just submit another request ANYWAY?
490 if(!joiningplayer.playerstats_basicstatus)
492 PlayerStats_PlayerBasic(
494 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
499 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
503 case URL_READY_CANWRITE:
505 LOG_TRACE("-- Sending data to player stats server");
506 /*url_fputs(fh, "V 1\n");
508 url_fputs(fh, sprintf("R %s\n", WATERMARK));
510 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
511 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
512 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
513 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
514 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
515 */url_fputs(fh, "\n");
520 case URL_READY_CANREAD:
522 bool handled = false;
523 string gt = string_null;
524 for (string s = ""; (s = url_fgets(fh)); ) {
525 int n = tokenizebyseparator(s, " "); // key value? data
526 if (n == 1) continue;
527 string key = "", value = "", data = "";
538 // PlayerInfo_AddItem(p, "_version", data);
541 // PlayerInfo_AddItem(p, "_release", data);
544 // PlayerInfo_AddItem(p, "_time", data);
547 // PlayerInfo_AddItem(p, "_statsurl", data);
550 // PlayerInfo_AddItem(p, "_hashkey", data);
553 // PlayerInfo_AddItem(p, "_playernick", data);
556 // PlayerInfo_AddItem(p, "_playerid", data);
557 // p.xonstat_id = stof(data);
563 LOG_TRACE("G: ", gt);
564 LOG_TRACE("e: ", data);
565 if (gt == PlayerStats_GetGametype()) {
567 float e = stof(data);
568 PlayerScore_Add(p, SP_ELO, +1 + e);
571 // PlayerInfo_AddItem(p, value, data);
573 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
582 case URL_READY_CLOSED:
584 // url_fclose has finished
585 LOG_INFO("Player stats synchronized with server\n");
589 case URL_READY_ERROR:
592 LOG_INFO("Receiving player stats failed: ", ftos(status), "\n");
596 PlayerScore_Add(p, SP_ELO, -1);
603 #if 0 // reading the entire DB at once
604 string e = "", en = "";
606 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
608 LOG_INFO(sprintf("%d:%s:%s\n", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e))));
613 void PlayerStats_PlayerDetail_AddItem(string event, string data)
615 if(PS_D_IN_DB < 0) { return; }
617 // create a marker for the event so that we can access it later
618 string marker = sprintf("%s", event);
619 if(db_get(PS_D_IN_DB, marker) == "")
623 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
624 strunzone(PS_D_IN_EVL);
626 else { db_put(PS_D_IN_DB, marker, "#"); }
627 PS_D_IN_EVL = strzone(marker);
630 // now actually set the event data
631 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
632 LOG_TRACE("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
635 void PlayerStats_PlayerDetail()
637 // http://stats.xonotic.org/player/me
638 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
640 // create the database if it doesn't already exist
642 PS_D_IN_DB = db_create();
644 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
645 LOG_TRACE("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
647 autocvar_g_playerstats_playerdetail_uri,
649 PlayerStats_PlayerDetail_Handler,
653 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
657 // player has this disabled, kill the DB and set status to idle
660 db_close(PS_D_IN_DB);
664 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
668 void PlayerStats_PlayerDetail_CheckUpdate()
670 // determine whether we should retrieve playerdetail information again
671 float gamecount = cvar("cl_matchcount");
674 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d\n",
683 (time >= PS_D_NEXTUPDATETIME)
685 (gamecount > PS_D_LASTGAMECOUNT)
688 PlayerStats_PlayerDetail();
689 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
690 PS_D_LASTGAMECOUNT = gamecount;
694 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
698 case URL_READY_CANWRITE:
700 LOG_TRACE("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
701 url_fputs(fh, "V 1\n");
703 url_fputs(fh, sprintf("R %s\n", WATERMARK));
705 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
706 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
707 //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
708 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
709 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
715 case URL_READY_CANREAD:
717 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
719 string gametype = "overall";
720 while((input = url_fgets(fh)))
722 float count = tokenizebyseparator(input, " ");
723 string key = "", event = "", data = "";
725 if(argv(0) == "#") { continue; }
730 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
736 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
743 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
744 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
745 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
748 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
749 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
750 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
751 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
754 case "G": gametype = data; break;
757 if(event != "" && data != "")
759 PlayerStats_PlayerDetail_AddItem(
774 "PlayerStats_PlayerDetail_Handler(): ERROR: "
775 "Key went unhandled? Is our version outdated?\n"
776 "PlayerStats_PlayerDetail_Handler(): "
777 "Key '%s', Event '%s', Data '%s'\n",
788 "PlayerStats_PlayerDetail_Handler(): "
789 "Key '%s', Event '%s', Data '%s'\n",
796 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
798 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
799 statslist.getStats(statslist);
803 case URL_READY_CLOSED:
805 // url_fclose has finished
806 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.\n");
810 case URL_READY_ERROR:
813 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status), "\n");
814 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
817 db_close(PS_D_IN_DB);
827 void PlayerInfo_AddPlayer(entity e)
829 if(playerinfo_db < 0)
833 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
836 p = db_get(playerinfo_db, key);
841 db_put(playerinfo_db, key, playerinfo_last);
842 strunzone(playerinfo_last);
845 db_put(playerinfo_db, key, "#");
846 playerinfo_last = strzone(ftos(e.playerid));
847 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
851 void PlayerInfo_AddItem(entity e, string item_id, string val)
853 if(playerinfo_db < 0)
857 key = sprintf("*:%s", item_id);
860 p = db_get(playerinfo_db, key);
863 if(playerinfo_events_last)
865 db_put(playerinfo_db, key, playerinfo_events_last);
866 strunzone(playerinfo_events_last);
869 db_put(playerinfo_db, key, "#");
870 playerinfo_events_last = strzone(item_id);
873 key = sprintf("#%d:%s", e.playerid, item_id);
874 db_put(playerinfo_db, key, val);
875 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
878 string PlayerInfo_GetItem(entity e, string item_id)
880 if(playerinfo_db < 0)
884 key = sprintf("#%d:%s", e.playerid, item_id);
885 return db_get(playerinfo_db, key);
888 string PlayerInfo_GetItemLocal(string item_id)
892 return PlayerInfo_GetItem(p, item_id);
895 void PlayerInfo_ready(entity fh, entity p, float status)
900 PlayerInfo_AddPlayer(p);
904 case URL_READY_CANWRITE:
905 print("-- Sending data to player stats server\n");
906 url_fputs(fh, "V 1\n");
908 url_fputs(fh, sprintf("R %s\n", WATERMARK));
911 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
912 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
913 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
914 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
915 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
920 case URL_READY_CANREAD:
921 print("-- Got response from player stats server:\n");
922 string gametype = string_null;
923 while((s = url_fgets(fh)))
927 string key = "", value = "", data = "";
929 n = tokenizebyseparator(s, " "); // key (value) data
950 PlayerInfo_AddItem(p, "_version", data);
952 PlayerInfo_AddItem(p, "_release", data);
954 PlayerInfo_AddItem(p, "_time", data);
956 PlayerInfo_AddItem(p, "_statsurl", data);
958 PlayerInfo_AddItem(p, "_hashkey", data);
960 PlayerInfo_AddItem(p, "_playernick", data);
962 PlayerInfo_AddItem(p, "_playerid", data);
965 else if (key == "e" && value != "")
968 PlayerInfo_AddItem(p, value, data);
970 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
975 print("-- End of response.\n");
978 case URL_READY_CLOSED:
979 // url_fclose has finished
980 print("Player stats synchronized with server\n");
982 case URL_READY_ERROR:
984 print("Receiving player stats failed: ", ftos(status), "\n");
989 void PlayerInfo_Init()
991 playerinfo_db = db_create();
995 void PlayerInfo_Basic(entity p)
997 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
999 if(playerinfo_db < 0)
1003 uri = autocvar_g_playerinfo_uri;
1004 if(uri != "" && p.crypto_idfp != "")
1006 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1007 print("Retrieving playerstats from URL: ", uri, "\n");
1008 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1014 void PlayerInfo_Details()
1016 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1018 if(playerinfo_db < 0)
1022 uri = autocvar_g_playerinfo_uri; // FIXME
1023 if(uri != "" && crypto_getmyidstatus(0) > 0)
1025 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1026 uri = strcat(uri, "/player/me");
1027 print("Retrieving playerstats from URL: ", uri, "\n");
1028 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1034 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1035 void PlayerInfo_Details()
1037 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1039 if(playerinfo_db < 0)
1043 uri = autocvar_g_playerinfo_uri; // FIXME
1044 if(uri != "" && crypto_getmyidstatus(0) > 0)
1046 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1047 print("Retrieving playerstats from URL: ", uri, "\n");
1048 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);