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 != "") && (CS(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), CS(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(CS(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))
149 if(CS(p).latency_cnt)
151 float latency = (CS(p).latency_sum / CS(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 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
265 // probably duel, but let's make sure
267 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
271 return GetGametype();
274 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
284 // ======================================
285 // -- OUTGOING GAME REPORT INFORMATION --
286 // ======================================
288 * V: format version (always a fixed number) - this MUST be the first line!
289 * #: comment (MUST be ignored by any parser)
290 * R: release information on the server
292 * O: mod name (icon request) as in server browser
294 * I: match ID (see "matchid" in g_world.qc)
295 * S: "hostname" of the server
296 * C: number of "unpure" cvar changes
297 * U: UDP port number of the server
298 * D: duration of the match
299 * L: "ladder" in which the server is participating in
300 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
301 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
303 * n: nickname of the player (optional)
305 * e: followed by an event name, a space, and the event count/score
306 * event names can be:
307 * alivetime: total playing time of the player
308 * avglatency: average network latency compounded throughout the match
309 * wins: number of games won (can only be set if matches is set)
310 * matches: number of matches played to the end (not aborted by map switch)
311 * joins: number of matches joined (always 1 unless player never played during the match)
312 * scoreboardvalid: set to 1 if the player was there at the end of the match
313 * total-<scoreboardname>: total score of that scoreboard item
314 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
315 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
316 * kills-<index>: number of kills against the indexed player
317 * rank <number>: rank of player
318 * acc-<weapon netname>-hit: total damage dealt
319 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
320 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
321 * acc-<weapon netname>-cnt-fired: amount of fired shots
322 * acc-<weapon netname>-frags: amount of frags dealt by weapon
324 case URL_READY_CANWRITE:
326 url_fputs(fh, "V 9\n");
328 url_fputs(fh, sprintf("R %s\n", WATERMARK));
330 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
331 url_fputs(fh, sprintf("O %s\n", modname));
332 url_fputs(fh, sprintf("M %s\n", GetMapname()));
333 url_fputs(fh, sprintf("I %s\n", matchid));
334 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
335 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
336 url_fputs(fh, sprintf("U %d\n", cvar("port")));
337 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
338 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
343 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
345 // start team section
346 url_fputs(fh, sprintf("Q team#%s\n", t));
348 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
349 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
351 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
352 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
358 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
360 // start player section
361 url_fputs(fh, sprintf("P %s\n", p));
363 // playerid/index (entity number for this server)
364 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
365 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
368 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
369 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
371 // team identification number
374 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
375 url_fputs(fh, sprintf("t %s\n", tt));
378 // output player events
379 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
381 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
382 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
390 // ======================================
391 // -- INCOMING GAME REPORT INFORMATION --
392 // ======================================
396 case URL_READY_CANREAD:
398 // url_fclose is processing, we got a response for writing the data
399 // this must come from HTTP
400 LOG_TRACE("Got response from player stats server:");
401 while((s = url_fgets(fh))) { LOG_TRACE(" ", s); }
402 LOG_TRACE("End of response.");
407 case URL_READY_CLOSED:
409 // url_fclose has finished
410 LOG_TRACE("Player stats written");
411 PlayerStats_GameReport_DelayMapVote = false;
412 if(PS_GR_OUT_DB >= 0)
414 db_close(PS_GR_OUT_DB);
420 case URL_READY_ERROR:
423 LOG_INFO("Player stats writing failed: ", ftos(status), "\n");
424 PlayerStats_GameReport_DelayMapVote = false;
425 if(PS_GR_OUT_DB >= 0)
427 db_close(PS_GR_OUT_DB);
435 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
437 PlayerScore_Add(joiningplayer, SP_ELO, -1);
438 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
439 if(autocvar_g_playerstats_playerbasic_uri != "")
441 string uri = autocvar_g_playerstats_playerbasic_uri;
442 if (joiningplayer.crypto_idfp == "") {
443 PlayerScore_Add(joiningplayer, SP_ELO, -1);
445 // create the database if it doesn't already exist
447 PS_B_IN_DB = db_create();
449 // now request the information
450 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
451 LOG_TRACE("Retrieving playerstats from URL: ", uri);
455 PlayerStats_PlayerBasic_Handler,
459 // set status appropriately // todo: check whether the player info exists in the database previously
462 // database still contains useful information, so don't clear it of a useful status
463 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
467 // database was previously empty or never hit received status for some reason
468 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
474 // server has this disabled, kill the DB and set status to idle
475 PlayerScore_Add(joiningplayer, SP_ELO, -1);
478 db_close(PS_B_IN_DB);
481 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
486 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
488 // determine whether we should retrieve playerbasic information again
490 LOG_TRACEF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
491 joiningplayer.netname,
495 // TODO: check to see if this playerid is inside the database already somehow...
496 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
497 // although maybe we should just submit another request ANYWAY?
498 if(!joiningplayer.playerstats_basicstatus)
500 PlayerStats_PlayerBasic(
502 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
507 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
511 case URL_READY_CANWRITE:
513 LOG_TRACE("-- Sending data to player stats server");
514 /*url_fputs(fh, "V 1\n");
516 url_fputs(fh, sprintf("R %s\n", WATERMARK));
518 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
519 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
520 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
521 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
522 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
523 */url_fputs(fh, "\n");
528 case URL_READY_CANREAD:
530 bool handled = false;
531 string gt = string_null;
532 for (string s = ""; (s = url_fgets(fh)); ) {
533 int n = tokenizebyseparator(s, " "); // key value? data
534 if (n == 1) continue;
535 string key = "", value = "", data = "";
546 // PlayerInfo_AddItem(p, "_version", data);
549 // PlayerInfo_AddItem(p, "_release", data);
552 // PlayerInfo_AddItem(p, "_time", data);
555 // PlayerInfo_AddItem(p, "_statsurl", data);
558 // PlayerInfo_AddItem(p, "_hashkey", data);
561 // PlayerInfo_AddItem(p, "_playernick", data);
564 // PlayerInfo_AddItem(p, "_playerid", data);
565 // p.xonstat_id = stof(data);
571 LOG_TRACE("G: ", gt);
572 LOG_TRACE("e: ", data);
573 if (gt == PlayerStats_GetGametype()) {
575 float e = stof(data);
576 PlayerScore_Add(p, SP_ELO, +1 + e);
579 // PlayerInfo_AddItem(p, value, data);
581 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
590 case URL_READY_CLOSED:
592 // url_fclose has finished
593 LOG_INFO("Player stats synchronized with server\n");
597 case URL_READY_ERROR:
600 LOG_INFO("Receiving player stats failed: ", ftos(status), "\n");
604 PlayerScore_Add(p, SP_ELO, -1);
611 #if 0 // reading the entire DB at once
612 string e = "", en = "";
614 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
616 LOG_INFO(sprintf("%d:%s:%s\n", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e))));
621 void PlayerStats_PlayerDetail_AddItem(string event, string data)
623 if(PS_D_IN_DB < 0) { return; }
625 // create a marker for the event so that we can access it later
626 string marker = sprintf("%s", event);
627 if(db_get(PS_D_IN_DB, marker) == "")
631 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
632 strunzone(PS_D_IN_EVL);
634 else { db_put(PS_D_IN_DB, marker, "#"); }
635 PS_D_IN_EVL = strzone(marker);
638 // now actually set the event data
639 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
640 LOG_TRACE("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
643 void PlayerStats_PlayerDetail()
645 // http://stats.xonotic.org/player/me
646 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
648 // create the database if it doesn't already exist
650 PS_D_IN_DB = db_create();
652 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
653 LOG_TRACE("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
655 autocvar_g_playerstats_playerdetail_uri,
657 PlayerStats_PlayerDetail_Handler,
661 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
665 // player has this disabled, kill the DB and set status to idle
668 db_close(PS_D_IN_DB);
672 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
676 void PlayerStats_PlayerDetail_CheckUpdate()
678 // determine whether we should retrieve playerdetail information again
679 float gamecount = cvar("cl_matchcount");
682 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d\n",
691 (time >= PS_D_NEXTUPDATETIME)
693 (gamecount > PS_D_LASTGAMECOUNT)
696 PlayerStats_PlayerDetail();
697 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
698 PS_D_LASTGAMECOUNT = gamecount;
702 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
706 case URL_READY_CANWRITE:
708 LOG_TRACE("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
709 url_fputs(fh, "V 1\n");
711 url_fputs(fh, sprintf("R %s\n", WATERMARK));
713 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
714 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
715 //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
716 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
717 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
723 case URL_READY_CANREAD:
725 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
727 string gametype = "overall";
728 while((input = url_fgets(fh)))
730 float count = tokenizebyseparator(input, " ");
731 string key = "", event = "", data = "";
733 if(argv(0) == "#") { continue; }
738 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
744 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
751 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
752 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
753 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
756 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
757 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
758 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
759 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
762 case "G": gametype = data; break;
765 if(event != "" && data != "")
767 PlayerStats_PlayerDetail_AddItem(
782 "PlayerStats_PlayerDetail_Handler(): ERROR: "
783 "Key went unhandled? Is our version outdated?\n"
784 "PlayerStats_PlayerDetail_Handler(): "
785 "Key '%s', Event '%s', Data '%s'\n",
796 "PlayerStats_PlayerDetail_Handler(): "
797 "Key '%s', Event '%s', Data '%s'\n",
804 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
806 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
807 statslist.getStats(statslist);
811 case URL_READY_CLOSED:
813 // url_fclose has finished
814 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.\n");
818 case URL_READY_ERROR:
821 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status), "\n");
822 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
825 db_close(PS_D_IN_DB);
835 void PlayerInfo_AddPlayer(entity e)
837 if(playerinfo_db < 0)
841 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
844 p = db_get(playerinfo_db, key);
849 db_put(playerinfo_db, key, playerinfo_last);
850 strunzone(playerinfo_last);
853 db_put(playerinfo_db, key, "#");
854 playerinfo_last = strzone(ftos(e.playerid));
855 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
859 void PlayerInfo_AddItem(entity e, string item_id, string val)
861 if(playerinfo_db < 0)
865 key = sprintf("*:%s", item_id);
868 p = db_get(playerinfo_db, key);
871 if(playerinfo_events_last)
873 db_put(playerinfo_db, key, playerinfo_events_last);
874 strunzone(playerinfo_events_last);
877 db_put(playerinfo_db, key, "#");
878 playerinfo_events_last = strzone(item_id);
881 key = sprintf("#%d:%s", e.playerid, item_id);
882 db_put(playerinfo_db, key, val);
883 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
886 string PlayerInfo_GetItem(entity e, string item_id)
888 if(playerinfo_db < 0)
892 key = sprintf("#%d:%s", e.playerid, item_id);
893 return db_get(playerinfo_db, key);
896 string PlayerInfo_GetItemLocal(string item_id)
900 return PlayerInfo_GetItem(p, item_id);
903 void PlayerInfo_ready(entity fh, entity p, float status)
908 PlayerInfo_AddPlayer(p);
912 case URL_READY_CANWRITE:
913 print("-- Sending data to player stats server\n");
914 url_fputs(fh, "V 1\n");
916 url_fputs(fh, sprintf("R %s\n", WATERMARK));
919 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
920 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
921 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
922 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
923 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
928 case URL_READY_CANREAD:
929 print("-- Got response from player stats server:\n");
930 string gametype = string_null;
931 while((s = url_fgets(fh)))
935 string key = "", value = "", data = "";
937 n = tokenizebyseparator(s, " "); // key (value) data
958 PlayerInfo_AddItem(p, "_version", data);
960 PlayerInfo_AddItem(p, "_release", data);
962 PlayerInfo_AddItem(p, "_time", data);
964 PlayerInfo_AddItem(p, "_statsurl", data);
966 PlayerInfo_AddItem(p, "_hashkey", data);
968 PlayerInfo_AddItem(p, "_playernick", data);
970 PlayerInfo_AddItem(p, "_playerid", data);
973 else if (key == "e" && value != "")
976 PlayerInfo_AddItem(p, value, data);
978 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
983 print("-- End of response.\n");
986 case URL_READY_CLOSED:
987 // url_fclose has finished
988 print("Player stats synchronized with server\n");
990 case URL_READY_ERROR:
992 print("Receiving player stats failed: ", ftos(status), "\n");
997 void PlayerInfo_Init()
999 playerinfo_db = db_create();
1003 void PlayerInfo_Basic(entity p)
1005 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1007 if(playerinfo_db < 0)
1011 uri = autocvar_g_playerinfo_uri;
1012 if(uri != "" && p.crypto_idfp != "")
1014 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1015 print("Retrieving playerstats from URL: ", uri, "\n");
1016 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1022 void PlayerInfo_Details()
1024 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1026 if(playerinfo_db < 0)
1030 uri = autocvar_g_playerinfo_uri; // FIXME
1031 if(uri != "" && crypto_getmyidstatus(0) > 0)
1033 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1034 uri = strcat(uri, "/player/me");
1035 print("Retrieving playerstats from URL: ", uri, "\n");
1036 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1042 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1043 void PlayerInfo_Details()
1045 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1047 if(playerinfo_db < 0)
1051 uri = autocvar_g_playerinfo_uri; // FIXME
1052 if(uri != "" && crypto_getmyidstatus(0) > 0)
1054 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1055 print("Retrieving playerstats from URL: ", uri, "\n");
1056 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);