1 #include "playerstats.qh"
5 #include "constants.qh"
7 #include <common/weapons/_all.qh>
8 #include <server/client.qh>
9 #include "../server/anticheat.qh"
10 #include <common/stats.qh>
11 #include "../server/scores.qh"
12 #include <server/world.qh>
13 #include "../server/weapons/accuracy.qh"
17 void PlayerStats_Prematch()
22 void PlayerStats_GameReport_AddPlayer(entity e)
24 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
26 // set up player identification
29 if((e.crypto_idfp != "") && (CS(e).cvar_cl_allow_uidtracking == 1))
30 { s = e.crypto_idfp; }
31 else if(IS_BOT_CLIENT(e))
32 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
34 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
37 { s = sprintf("bot#%d", e.playerid); }
39 { s = sprintf("player#%d", e.playerid); }
42 e.playerstats_id = strzone(s);
44 // now add the player to the database
45 string key = sprintf("%s:*", e.playerstats_id);
46 string p = db_get(PS_GR_OUT_DB, key);
52 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
53 strunzone(PS_GR_OUT_PL);
55 else { db_put(PS_GR_OUT_DB, key, "#"); }
56 PS_GR_OUT_PL = strzone(e.playerstats_id);
60 void PlayerStats_GameReport_AddTeam(int t)
62 if(PS_GR_OUT_DB < 0) { return; }
64 string key = sprintf("%d", t);
65 string p = db_get(PS_GR_OUT_DB, key);
71 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
72 strunzone(PS_GR_OUT_TL);
74 else { db_put(PS_GR_OUT_DB, key, "#"); }
75 PS_GR_OUT_TL = strzone(key);
79 void PlayerStats_GameReport_AddEvent(string event_id)
81 if(PS_GR_OUT_DB < 0) { return; }
83 string key = sprintf("*:%s", event_id);
84 string p = db_get(PS_GR_OUT_DB, key);
90 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
91 strunzone(PS_GR_OUT_EVL);
93 else { db_put(PS_GR_OUT_DB, key, "#"); }
94 PS_GR_OUT_EVL = strzone(event_id);
98 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
100 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
102 string key = sprintf("%s:%s", prefix, event_id);
103 float val = stof(db_get(PS_GR_OUT_DB, key));
105 db_put(PS_GR_OUT_DB, key, ftos(val));
109 void PlayerStats_GameReport_Accuracy(entity p)
111 #define ACCMAC(suffix, field) \
112 PlayerStats_GameReport_Event_Player(p, \
113 sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
114 FOREACH(Weapons, it != WEP_Null, {
115 ACCMAC("hit", accuracy_hit)
116 ACCMAC("fired", accuracy_fired)
117 ACCMAC("cnt-hit", accuracy_cnt_hit)
118 ACCMAC("cnt-fired", accuracy_cnt_fired)
119 ACCMAC("frags", accuracy_frags)
124 void PlayerStats_GameReport_FinalizePlayer(entity p)
126 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
131 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
135 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
137 if(CS(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
138 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p, false));
141 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
143 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
144 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_JOINS, 1);
146 PlayerStats_GameReport_Accuracy(p);
147 anticheat_report_to_playerstats(p);
149 if(IS_REAL_CLIENT(p))
151 if(CS(p).latency_cnt)
153 float latency = (CS(p).latency_sum / CS(p).latency_cnt);
155 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, latency);
158 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS(p).cvar_cl_allow_uidranking));
161 strfree(p.playerstats_id);
164 void PlayerStats_GameReport(bool finished)
166 if(PS_GR_OUT_DB < 0) { return; }
168 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
169 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
170 if(teamplay) { PlayerScore_TeamStats(); }
172 FOREACH_CLIENT(true, {
173 // add personal score rank
174 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
177 if(it.scoreboard_pos)
179 // scoreboard is valid!
180 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
182 // add scoreboard position
183 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
185 // add scoreboard data
186 PlayerScore_PlayerStats(it);
188 // if the match ended normally, add winning info
191 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
192 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
196 // collect final player information
197 PlayerStats_GameReport_FinalizePlayer(it);
200 if(autocvar_g_playerstats_gamereport_uri != "")
202 PlayerStats_GameReport_DelayMapVote = true;
204 autocvar_g_playerstats_gamereport_uri,
206 PlayerStats_GameReport_Handler,
212 PlayerStats_GameReport_DelayMapVote = false;
213 db_close(PS_GR_OUT_DB);
218 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
220 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
222 PS_GR_OUT_DB = db_create();
224 if(PS_GR_OUT_DB >= 0)
226 PlayerStats_GameReport_DelayMapVote = true;
228 serverflags |= SERVERFLAG_PLAYERSTATS;
230 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
231 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
232 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
233 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
234 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
235 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
236 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
237 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
240 FOREACH(Weapons, it != WEP_Null, {
241 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
242 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
243 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
244 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
245 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
248 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
249 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
250 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
251 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
252 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
253 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
254 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
255 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
256 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
257 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
259 anticheat_register_to_playerstats();
261 else { PlayerStats_GameReport_DelayMapVote = false; }
264 // this... is a hack, a temporary one until we get a proper duel gametype
265 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
266 string PlayerStats_GetGametype()
268 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
270 // probably duel, but let's make sure
272 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
276 return GetGametype();
279 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
289 // ======================================
290 // -- OUTGOING GAME REPORT INFORMATION --
291 // ======================================
293 * V: format version (always a fixed number) - this MUST be the first line!
294 * #: comment (MUST be ignored by any parser)
295 * R: release information on the server
297 * O: mod name (icon request) as in server browser
299 * I: match ID (see "matchid" in world.qc)
300 * S: "hostname" of the server
301 * C: number of "unpure" cvar changes
302 * U: UDP port number of the server
303 * D: duration of the match
304 * L: "ladder" in which the server is participating in
305 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
306 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
308 * n: nickname of the player (optional)
310 * e: followed by an event name, a space, and the event count/score
311 * event names can be:
312 * alivetime: total playing time of the player
313 * avglatency: average network latency compounded throughout the match
314 * wins: number of games won (can only be set if matches is set)
315 * matches: number of matches played to the end (not aborted by map switch)
316 * joins: number of matches joined (always 1 unless player never played during the match)
317 * scoreboardvalid: set to 1 if the player was there at the end of the match
318 * total-<scoreboardname>: total score of that scoreboard item
319 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
320 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
321 * kills-<index>: number of kills against the indexed player
322 * rank <number>: rank of player
323 * acc-<weapon netname>-hit: total damage dealt
324 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
325 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
326 * acc-<weapon netname>-cnt-fired: amount of fired shots
327 * acc-<weapon netname>-frags: amount of frags dealt by weapon
329 case URL_READY_CANWRITE:
331 url_fputs(fh, "V 9\n");
333 url_fputs(fh, sprintf("R %s\n", WATERMARK));
335 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
336 url_fputs(fh, sprintf("O %s\n", modname));
337 url_fputs(fh, sprintf("M %s\n", GetMapname()));
338 url_fputs(fh, sprintf("I %s\n", matchid));
339 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
340 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
341 url_fputs(fh, sprintf("U %d\n", cvar("port")));
342 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
343 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
348 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
350 // start team section
351 url_fputs(fh, sprintf("Q team#%s\n", t));
353 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
354 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
356 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
357 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
363 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
365 // start player section
366 url_fputs(fh, sprintf("P %s\n", p));
368 // playerid/index (entity number for this server)
369 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
370 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
373 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
374 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
376 // team identification number
379 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
380 url_fputs(fh, sprintf("t %s\n", tt));
383 // elo ranking enabled
384 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
385 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
387 // output player events
388 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
390 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
391 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
399 // ======================================
400 // -- INCOMING GAME REPORT INFORMATION --
401 // ======================================
405 case URL_READY_CANREAD:
407 // url_fclose is processing, we got a response for writing the data
408 // this must come from HTTP
409 LOG_TRACE("Got response from player stats server:");
410 while((s = url_fgets(fh))) { LOG_TRACE(" ", s); }
411 LOG_TRACE("End of response.");
416 case URL_READY_CLOSED:
418 // url_fclose has finished
419 LOG_TRACE("Player stats written");
420 PlayerStats_GameReport_DelayMapVote = false;
421 if(PS_GR_OUT_DB >= 0)
423 db_close(PS_GR_OUT_DB);
429 case URL_READY_ERROR:
432 LOG_INFO("Player stats writing failed: ", ftos(status));
433 PlayerStats_GameReport_DelayMapVote = false;
434 if(PS_GR_OUT_DB >= 0)
436 db_close(PS_GR_OUT_DB);
444 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
446 GameRules_scoring_add(joiningplayer, ELO, -1);
447 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
448 if(autocvar_g_playerstats_playerbasic_uri != "")
450 string uri = autocvar_g_playerstats_playerbasic_uri;
451 if (joiningplayer.crypto_idfp == "") {
452 GameRules_scoring_add(joiningplayer, ELO, -1);
454 // create the database if it doesn't already exist
456 PS_B_IN_DB = db_create();
458 // now request the information
459 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
460 LOG_TRACE("Retrieving playerstats from URL: ", uri);
464 PlayerStats_PlayerBasic_Handler,
468 // set status appropriately // todo: check whether the player info exists in the database previously
471 // database still contains useful information, so don't clear it of a useful status
472 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
476 // database was previously empty or never hit received status for some reason
477 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
483 // server has this disabled, kill the DB and set status to idle
484 GameRules_scoring_add(joiningplayer, ELO, -1);
487 db_close(PS_B_IN_DB);
490 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
495 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
499 db_close(PS_B_IN_DB);
503 if(PS_GR_OUT_DB >= 0)
505 db_close(PS_GR_OUT_DB);
510 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
512 // determine whether we should retrieve playerbasic information again
514 LOG_TRACEF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
515 joiningplayer.netname,
519 // TODO: check to see if this playerid is inside the database already somehow...
520 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
521 // although maybe we should just submit another request ANYWAY?
522 if(!joiningplayer.playerstats_basicstatus)
524 PlayerStats_PlayerBasic(
526 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
531 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
535 case URL_READY_CANWRITE:
537 LOG_TRACE("-- Sending data to player stats server");
538 /*url_fputs(fh, "V 1\n");
540 url_fputs(fh, sprintf("R %s\n", WATERMARK));
542 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
543 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
544 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
545 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
546 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
547 */url_fputs(fh, "\n");
552 case URL_READY_CANREAD:
554 bool handled = false;
555 string gt = string_null;
556 for (string s = ""; (s = url_fgets(fh)); ) {
557 int n = tokenizebyseparator(s, " "); // key value? data
558 if (n == 1) continue;
559 string key = "", value = "", data = "";
570 // PlayerInfo_AddItem(p, "_version", data);
573 // PlayerInfo_AddItem(p, "_release", data);
576 // PlayerInfo_AddItem(p, "_time", data);
579 // PlayerInfo_AddItem(p, "_statsurl", data);
582 // PlayerInfo_AddItem(p, "_hashkey", data);
585 // PlayerInfo_AddItem(p, "_playernick", data);
588 // PlayerInfo_AddItem(p, "_playerid", data);
589 // p.xonstat_id = stof(data);
595 LOG_TRACE("G: ", gt);
596 LOG_TRACE("e: ", data);
597 if (gt == PlayerStats_GetGametype()) {
599 float e = stof(data);
600 GameRules_scoring_add(p, ELO, +1 + e);
603 // PlayerInfo_AddItem(p, value, data);
605 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
614 case URL_READY_CLOSED:
616 // url_fclose has finished
617 LOG_INFO("Player stats synchronized with server");
621 case URL_READY_ERROR:
624 LOG_INFO("Receiving player stats failed: ", ftos(status));
628 GameRules_scoring_add(p, ELO, -1);
635 #if 0 // reading the entire DB at once
636 string e = "", en = "";
638 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
640 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
645 void PlayerStats_PlayerDetail_AddItem(string event, string data)
647 if(PS_D_IN_DB < 0) { return; }
649 // create a marker for the event so that we can access it later
650 string marker = sprintf("%s", event);
651 if(db_get(PS_D_IN_DB, marker) == "")
655 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
656 strunzone(PS_D_IN_EVL);
658 else { db_put(PS_D_IN_DB, marker, "#"); }
659 PS_D_IN_EVL = strzone(marker);
662 // now actually set the event data
663 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
664 LOG_TRACE("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
667 void PlayerStats_PlayerDetail()
669 // http://stats.xonotic.org/player/me
670 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
672 // create the database if it doesn't already exist
674 PS_D_IN_DB = db_create();
676 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
677 LOG_TRACE("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
679 autocvar_g_playerstats_playerdetail_uri,
681 PlayerStats_PlayerDetail_Handler,
685 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
689 // player has this disabled, kill the DB and set status to idle
692 db_close(PS_D_IN_DB);
696 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
700 void PlayerStats_PlayerDetail_CheckUpdate()
702 // determine whether we should retrieve playerdetail information again
703 float gamecount = cvar("cl_matchcount");
706 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
715 (time >= PS_D_NEXTUPDATETIME)
717 (gamecount > PS_D_LASTGAMECOUNT)
720 PlayerStats_PlayerDetail();
721 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
722 PS_D_LASTGAMECOUNT = gamecount;
726 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
730 case URL_READY_CANWRITE:
732 LOG_TRACE("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
733 url_fputs(fh, "V 1\n");
735 url_fputs(fh, sprintf("R %s\n", WATERMARK));
737 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
738 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
739 //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
740 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
741 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
747 case URL_READY_CANREAD:
749 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
751 string gametype = "overall";
752 while((input = url_fgets(fh)))
754 float count = tokenizebyseparator(input, " ");
755 string key = "", event = "", data = "";
757 if(argv(0) == "#") { continue; }
762 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
768 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
775 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
776 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
777 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
780 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
781 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
782 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
783 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
786 case "G": gametype = data; break;
789 if(event != "" && data != "")
791 PlayerStats_PlayerDetail_AddItem(
806 "PlayerStats_PlayerDetail_Handler(): ERROR: "
807 "Key went unhandled? Is our version outdated?\n"
808 "PlayerStats_PlayerDetail_Handler(): "
809 "Key '%s', Event '%s', Data '%s'",
820 "PlayerStats_PlayerDetail_Handler(): "
821 "Key '%s', Event '%s', Data '%s'",
828 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
830 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
831 statslist.getStats(statslist);
835 case URL_READY_CLOSED:
837 // url_fclose has finished
838 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
842 case URL_READY_ERROR:
845 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
846 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
849 db_close(PS_D_IN_DB);
859 void PlayerInfo_AddPlayer(entity e)
861 if(playerinfo_db < 0)
865 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
868 p = db_get(playerinfo_db, key);
873 db_put(playerinfo_db, key, playerinfo_last);
874 strunzone(playerinfo_last);
877 db_put(playerinfo_db, key, "#");
878 playerinfo_last = strzone(ftos(e.playerid));
879 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
883 void PlayerInfo_AddItem(entity e, string item_id, string val)
885 if(playerinfo_db < 0)
889 key = sprintf("*:%s", item_id);
892 p = db_get(playerinfo_db, key);
895 if(playerinfo_events_last)
897 db_put(playerinfo_db, key, playerinfo_events_last);
898 strunzone(playerinfo_events_last);
901 db_put(playerinfo_db, key, "#");
902 playerinfo_events_last = strzone(item_id);
905 key = sprintf("#%d:%s", e.playerid, item_id);
906 db_put(playerinfo_db, key, val);
907 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
910 string PlayerInfo_GetItem(entity e, string item_id)
912 if(playerinfo_db < 0)
916 key = sprintf("#%d:%s", e.playerid, item_id);
917 return db_get(playerinfo_db, key);
920 string PlayerInfo_GetItemLocal(string item_id)
924 return PlayerInfo_GetItem(p, item_id);
927 void PlayerInfo_ready(entity fh, entity p, float status)
932 PlayerInfo_AddPlayer(p);
936 case URL_READY_CANWRITE:
937 print("-- Sending data to player stats server\n");
938 url_fputs(fh, "V 1\n");
940 url_fputs(fh, sprintf("R %s\n", WATERMARK));
943 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
944 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
945 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
946 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
947 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
952 case URL_READY_CANREAD:
953 print("-- Got response from player stats server:\n");
954 string gametype = string_null;
955 while((s = url_fgets(fh)))
959 string key = "", value = "", data = "";
961 n = tokenizebyseparator(s, " "); // key (value) data
982 PlayerInfo_AddItem(p, "_version", data);
984 PlayerInfo_AddItem(p, "_release", data);
986 PlayerInfo_AddItem(p, "_time", data);
988 PlayerInfo_AddItem(p, "_statsurl", data);
990 PlayerInfo_AddItem(p, "_hashkey", data);
992 PlayerInfo_AddItem(p, "_playernick", data);
994 PlayerInfo_AddItem(p, "_playerid", data);
997 else if (key == "e" && value != "")
1000 PlayerInfo_AddItem(p, value, data);
1002 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1007 print("-- End of response.\n");
1010 case URL_READY_CLOSED:
1011 // url_fclose has finished
1012 print("Player stats synchronized with server\n");
1014 case URL_READY_ERROR:
1016 print("Receiving player stats failed: ", ftos(status), "\n");
1021 void PlayerInfo_Init()
1023 playerinfo_db = db_create();
1027 void PlayerInfo_Basic(entity p)
1029 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1031 if(playerinfo_db < 0)
1035 uri = autocvar_g_playerinfo_uri;
1036 if(uri != "" && p.crypto_idfp != "")
1038 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1039 print("Retrieving playerstats from URL: ", uri, "\n");
1040 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1046 void PlayerInfo_Details()
1048 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1050 if(playerinfo_db < 0)
1054 uri = autocvar_g_playerinfo_uri; // FIXME
1055 if(uri != "" && crypto_getmyidstatus(0) > 0)
1057 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1058 uri = strcat(uri, "/player/me");
1059 print("Retrieving playerstats from URL: ", uri, "\n");
1060 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1066 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1067 void PlayerInfo_Details()
1069 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1071 if(playerinfo_db < 0)
1075 uri = autocvar_g_playerinfo_uri; // FIXME
1076 if(uri != "" && crypto_getmyidstatus(0) > 0)
1078 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1079 print("Retrieving playerstats from URL: ", uri, "\n");
1080 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);