1 #include "playerstats.qh"
6 #include <common/constants.qh>
7 #include <common/stats.qh>
8 #include <common/util.qh>
9 #include <common/weapons/_all.qh>
10 #include <server/anticheat.qh>
11 #include <server/client.qh>
12 #include <server/intermission.qh>
13 #include <server/scores.qh>
14 #include <server/weapons/accuracy.qh>
15 #include <server/world.qh>
20 REPLICATE(cvar_cl_allow_uid2name, int, "cl_allow_uid2name");
21 REPLICATE(cvar_cl_allow_uidranking, bool, "cl_allow_uidranking");
22 REPLICATE(cvar_cl_allow_uidtracking, int, "cl_allow_uidtracking");
26 REPLICATE_APPLYCHANGE("cl_allow_uidtracking", { PlayerStats_GameReport_AddPlayer(this); });
30 void PlayerStats_Prematch()
35 // Deletes current playerstats DB, creates a new one and fully initializes it
36 void PlayerStats_GameReport_Reset_All()
38 strfree(PS_GR_OUT_TL);
39 strfree(PS_GR_OUT_PL);
40 strfree(PS_GR_OUT_EVL);
42 if (PS_GR_OUT_DB >= 0)
43 db_close(PS_GR_OUT_DB);
44 PlayerStats_GameReport_Init();
48 for (int i = 0; i < 16; i++)
49 if (teamscorekeepers[i])
50 PlayerStats_GameReport_AddTeam(i + 1);
51 FOREACH_CLIENT(true, {
52 // NOTE Adding back a player we are applying any cl_allow_uidtracking change
53 // usually only possible by reconnecting to the server
54 strfree(it.playerstats_id);
55 PlayerStats_GameReport_AddEvent(sprintf("kills-%d", it.playerid));
56 PlayerStats_GameReport_AddPlayer(it);
58 FOREACH(Scores, true, {
59 string label = scores_label(it);
62 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
63 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
65 for(int i = 0; i < MAX_TEAMSCORE; ++i)
67 string label = teamscores_label(i);
70 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
71 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
75 void PlayerStats_GameReport_AddPlayer(entity e)
77 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
79 // set up player identification
82 if((e.crypto_idfp != "") && (CS_CVAR(e).cvar_cl_allow_uidtracking == 1))
83 { s = e.crypto_idfp; }
84 else if(IS_BOT_CLIENT(e))
85 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
87 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
90 { s = sprintf("bot#%d", e.playerid); }
92 { s = sprintf("player#%d", e.playerid); }
95 e.playerstats_id = strzone(s);
97 // now add the player to the database
98 string key = sprintf("%s:*", e.playerstats_id);
99 string p = db_get(PS_GR_OUT_DB, key);
105 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
106 strunzone(PS_GR_OUT_PL);
108 else { db_put(PS_GR_OUT_DB, key, "#"); }
109 PS_GR_OUT_PL = strzone(e.playerstats_id);
113 void PlayerStats_GameReport_AddTeam(int t)
115 if(PS_GR_OUT_DB < 0) { return; }
117 string key = sprintf("%d", t);
118 string p = db_get(PS_GR_OUT_DB, key);
124 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
125 strunzone(PS_GR_OUT_TL);
127 else { db_put(PS_GR_OUT_DB, key, "#"); }
128 PS_GR_OUT_TL = strzone(key);
132 void PlayerStats_GameReport_AddEvent(string event_id)
134 if(PS_GR_OUT_DB < 0) { return; }
136 string key = sprintf("*:%s", event_id);
137 string p = db_get(PS_GR_OUT_DB, key);
143 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
144 strunzone(PS_GR_OUT_EVL);
146 else { db_put(PS_GR_OUT_DB, key, "#"); }
147 PS_GR_OUT_EVL = strzone(event_id);
151 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
153 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
155 string key = sprintf("%s:%s", prefix, event_id);
156 float val = stof(db_get(PS_GR_OUT_DB, key));
158 db_put(PS_GR_OUT_DB, key, ftos(val));
162 void PlayerStats_GameReport_Accuracy(entity p)
164 #define ACCMAC(suffix, field) \
165 PlayerStats_GameReport_Event_Player(p, \
166 sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
167 FOREACH(Weapons, it != WEP_Null, {
168 ACCMAC("hit", accuracy_hit)
169 ACCMAC("fired", accuracy_fired)
170 ACCMAC("cnt-hit", accuracy_cnt_hit)
171 ACCMAC("cnt-fired", accuracy_cnt_fired)
172 ACCMAC("frags", accuracy_frags)
177 void PlayerStats_GameReport_FinalizePlayer(entity p)
179 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
184 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
188 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
190 if(CS_CVAR(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
191 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p.netname, p.team, false));
194 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
196 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
197 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_JOINS, 1);
199 PlayerStats_GameReport_Accuracy(p);
200 anticheat_report_to_playerstats(p);
202 if(IS_REAL_CLIENT(p))
204 if(CS(p).latency_cnt)
206 float latency = (CS(p).latency_sum / CS(p).latency_cnt);
208 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, latency);
211 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS_CVAR(p).cvar_cl_allow_uidranking));
214 strfree(p.playerstats_id);
217 void PlayerStats_GameReport(bool finished)
219 if(PS_GR_OUT_DB < 0) { return; }
221 PlayerScore_Sort(score_dummyfield, 0, false, false);
222 PlayerScore_Sort(scoreboard_pos, 1, true, true);
223 if(teamplay) { PlayerScore_TeamStats(); }
225 FOREACH_CLIENT(true, {
226 // add personal score rank
227 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
230 if(it.scoreboard_pos)
232 // scoreboard is valid!
233 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
235 // add scoreboard position
236 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
238 // add scoreboard data
239 PlayerScore_PlayerStats(it);
241 // if the match ended normally, add winning info
244 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
245 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
249 // collect final player information
250 PlayerStats_GameReport_FinalizePlayer(it);
253 if(autocvar_g_playerstats_gamereport_uri != "")
255 PlayerStats_GameReport_DelayMapVote = true;
257 autocvar_g_playerstats_gamereport_uri,
259 PlayerStats_GameReport_Handler,
265 PlayerStats_GameReport_DelayMapVote = false;
266 db_close(PS_GR_OUT_DB);
271 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
273 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
275 PS_GR_OUT_DB = db_create();
277 if(PS_GR_OUT_DB >= 0)
279 PlayerStats_GameReport_DelayMapVote = true;
281 serverflags |= SERVERFLAG_PLAYERSTATS;
283 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
284 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
285 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
286 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
287 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
288 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
289 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
290 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
293 FOREACH(Weapons, it != WEP_Null, {
294 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
295 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
296 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
297 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
298 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
301 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
302 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
303 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
304 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
305 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
306 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
307 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
308 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
309 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
310 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
312 anticheat_register_to_playerstats();
314 else { PlayerStats_GameReport_DelayMapVote = false; }
317 // this... is a hack, a temporary one until we get a proper duel gametype
318 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
319 string PlayerStats_GetGametype()
321 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
323 // probably duel, but let's make sure
325 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
329 return GetGametype();
332 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
342 // ======================================
343 // -- OUTGOING GAME REPORT INFORMATION --
344 // ======================================
346 * V: format version (always a fixed number) - this MUST be the first line!
347 * #: comment (MUST be ignored by any parser)
348 * R: release information on the server
350 * O: mod name (icon request) as in server browser
352 * I: match ID (see "matchid" in world.qc)
353 * S: "hostname" of the server
354 * C: number of "unpure" cvar changes
355 * U: UDP port number of the server
356 * D: duration of the match
357 * L: "ladder" in which the server is participating in
358 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
359 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
361 * n: nickname of the player (optional)
363 * e: followed by an event name, a space, and the event count/score
364 * event names can be:
365 * alivetime: total playing time of the player
366 * avglatency: average network latency compounded throughout the match
367 * wins: number of games won (can only be set if matches is set)
368 * matches: number of matches played to the end (not aborted by map switch)
369 * joins: number of matches joined (always 1 unless player never played during the match)
370 * scoreboardvalid: set to 1 if the player was there at the end of the match
371 * total-<scoreboardname>: total score of that scoreboard item
372 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
373 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
374 * kills-<index>: number of kills against the indexed player
375 * rank <number>: rank of player
376 * acc-<weapon netname>-hit: total damage dealt
377 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
378 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
379 * acc-<weapon netname>-cnt-fired: amount of fired shots
380 * acc-<weapon netname>-frags: amount of frags dealt by weapon
382 case URL_READY_CANWRITE:
384 url_fputs(fh, "V 9\n");
386 url_fputs(fh, sprintf("R %s\n", WATERMARK));
388 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
389 url_fputs(fh, sprintf("O %s\n", modname));
390 url_fputs(fh, sprintf("M %s\n", GetMapname()));
391 url_fputs(fh, sprintf("I %s\n", matchid));
392 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
393 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
394 url_fputs(fh, sprintf("U %d\n", cvar("port")));
395 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
396 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
401 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
403 // start team section
404 url_fputs(fh, sprintf("Q team#%s\n", t));
406 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
407 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
409 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
410 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
416 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
418 // start player section
419 url_fputs(fh, sprintf("P %s\n", p));
421 // playerid/index (entity number for this server)
422 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
423 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
426 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
427 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
429 // team identification number
432 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
433 url_fputs(fh, sprintf("t %s\n", tt));
436 // elo ranking enabled
437 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
438 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
440 // output player events
441 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
443 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
444 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
452 // ======================================
453 // -- INCOMING GAME REPORT INFORMATION --
454 // ======================================
458 case URL_READY_CANREAD:
460 // url_fclose is processing, we got a response for writing the data
461 // this must come from HTTP
462 LOG_DEBUG("Got response from player stats server:");
463 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
464 LOG_DEBUG("End of response.");
469 case URL_READY_CLOSED:
471 // url_fclose has finished
472 LOG_DEBUG("Player stats written");
473 PlayerStats_GameReport_DelayMapVote = false;
474 if(PS_GR_OUT_DB >= 0)
476 db_close(PS_GR_OUT_DB);
482 case URL_READY_ERROR:
485 LOG_INFO("Player stats writing failed: ", ftos(status));
486 PlayerStats_GameReport_DelayMapVote = false;
487 if(PS_GR_OUT_DB >= 0)
489 db_close(PS_GR_OUT_DB);
497 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
499 GameRules_scoring_add(joiningplayer, ELO, -1);
500 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
501 if(autocvar_g_playerstats_playerbasic_uri != "")
503 string uri = autocvar_g_playerstats_playerbasic_uri;
504 if (joiningplayer.crypto_idfp == "") {
505 GameRules_scoring_add(joiningplayer, ELO, -1);
507 // create the database if it doesn't already exist
509 PS_B_IN_DB = db_create();
511 // now request the information
512 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
513 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
517 PlayerStats_PlayerBasic_Handler,
521 // set status appropriately // todo: check whether the player info exists in the database previously
524 // database still contains useful information, so don't clear it of a useful status
525 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
529 // database was previously empty or never hit received status for some reason
530 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
536 // server has this disabled, kill the DB and set status to idle
537 GameRules_scoring_add(joiningplayer, ELO, -1);
540 db_close(PS_B_IN_DB);
543 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
548 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
552 db_close(PS_B_IN_DB);
556 if(PS_GR_OUT_DB >= 0)
558 db_close(PS_GR_OUT_DB);
563 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
565 // determine whether we should retrieve playerbasic information again
567 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
568 joiningplayer.netname,
572 // TODO: check to see if this playerid is inside the database already somehow...
573 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
574 // although maybe we should just submit another request ANYWAY?
575 if(!joiningplayer.playerstats_basicstatus)
577 PlayerStats_PlayerBasic(
579 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
584 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
588 case URL_READY_CANWRITE:
590 LOG_DEBUG("-- Sending data to player stats server");
591 /*url_fputs(fh, "V 1\n");
593 url_fputs(fh, sprintf("R %s\n", WATERMARK));
595 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
596 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
597 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
598 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
599 */url_fputs(fh, "\n");
604 case URL_READY_CANREAD:
606 bool handled = false;
607 string gt = string_null;
608 for (string s = ""; (s = url_fgets(fh)); ) {
609 int n = tokenizebyseparator(s, " "); // key value? data
610 if (n == 1) continue;
611 string key = "", value = "", data = "";
622 // PlayerInfo_AddItem(p, "_version", data);
625 // PlayerInfo_AddItem(p, "_release", data);
628 // PlayerInfo_AddItem(p, "_time", data);
631 // PlayerInfo_AddItem(p, "_statsurl", data);
634 // PlayerInfo_AddItem(p, "_hashkey", data);
637 // PlayerInfo_AddItem(p, "_playernick", data);
640 // PlayerInfo_AddItem(p, "_playerid", data);
641 // p.xonstat_id = stof(data);
647 //LOG_TRACE("G: ", gt);
648 //LOG_TRACE("e: ", data);
649 if (gt == PlayerStats_GetGametype()) {
651 float e = stof(data);
652 GameRules_scoring_add(p, ELO, +1 + e);
655 // PlayerInfo_AddItem(p, value, data);
657 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
666 case URL_READY_CLOSED:
668 // url_fclose has finished
669 LOG_INFO("Player stats synchronized with server");
673 case URL_READY_ERROR:
676 LOG_INFO("Receiving player stats failed: ", ftos(status));
680 GameRules_scoring_add(p, ELO, -1);
687 #if 0 // reading the entire DB at once
688 string e = "", en = "";
690 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
692 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
697 void PlayerStats_PlayerDetail_AddItem(string event, string data)
699 if(PS_D_IN_DB < 0) { return; }
701 // create a marker for the event so that we can access it later
702 string marker = sprintf("%s", event);
703 if(db_get(PS_D_IN_DB, marker) == "")
707 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
708 strunzone(PS_D_IN_EVL);
710 else { db_put(PS_D_IN_DB, marker, "#"); }
711 PS_D_IN_EVL = strzone(marker);
714 // now actually set the event data
715 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
716 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
719 void PlayerStats_PlayerDetail()
721 // http://stats.xonotic.org/player/me
722 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
724 // create the database if it doesn't already exist
726 PS_D_IN_DB = db_create();
728 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
729 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
731 autocvar_g_playerstats_playerdetail_uri,
733 PlayerStats_PlayerDetail_Handler,
737 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
741 // player has this disabled, kill the DB and set status to idle
744 db_close(PS_D_IN_DB);
748 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
752 void PlayerStats_PlayerDetail_CheckUpdate()
754 // determine whether we should retrieve playerdetail information again
755 float gamecount = cvar("cl_matchcount");
758 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
767 (time >= PS_D_NEXTUPDATETIME)
769 (gamecount > PS_D_LASTGAMECOUNT)
772 PlayerStats_PlayerDetail();
773 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
774 PS_D_LASTGAMECOUNT = gamecount;
778 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
782 case URL_READY_CANWRITE:
784 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
785 url_fputs(fh, "V 1\n");
787 url_fputs(fh, sprintf("R %s\n", WATERMARK));
789 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
790 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
791 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
792 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
798 case URL_READY_CANREAD:
800 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
802 string gametype = "overall";
803 while((input = url_fgets(fh)))
805 float count = tokenizebyseparator(input, " ");
806 string key = "", event = "", data = "";
808 if(argv(0) == "#") { continue; }
813 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
819 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
826 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
827 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
828 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
831 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
832 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
833 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
834 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
837 case "G": gametype = data; break;
840 if(event != "" && data != "")
842 PlayerStats_PlayerDetail_AddItem(
857 "PlayerStats_PlayerDetail_Handler(): ERROR: "
858 "Key went unhandled? Is our version outdated?\n"
859 "PlayerStats_PlayerDetail_Handler(): "
860 "Key '%s', Event '%s', Data '%s'",
871 "PlayerStats_PlayerDetail_Handler(): "
872 "Key '%s', Event '%s', Data '%s'",
879 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
881 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
882 statslist.getStats(statslist);
886 case URL_READY_CLOSED:
888 // url_fclose has finished
889 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
893 case URL_READY_ERROR:
896 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
897 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
900 db_close(PS_D_IN_DB);
910 void PlayerInfo_AddPlayer(entity e)
912 if(playerinfo_db < 0)
916 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
919 p = db_get(playerinfo_db, key);
924 db_put(playerinfo_db, key, playerinfo_last);
925 strunzone(playerinfo_last);
928 db_put(playerinfo_db, key, "#");
929 playerinfo_last = strzone(ftos(e.playerid));
930 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
934 void PlayerInfo_AddItem(entity e, string item_id, string val)
936 if(playerinfo_db < 0)
940 key = sprintf("*:%s", item_id);
943 p = db_get(playerinfo_db, key);
946 if(playerinfo_events_last)
948 db_put(playerinfo_db, key, playerinfo_events_last);
949 strunzone(playerinfo_events_last);
952 db_put(playerinfo_db, key, "#");
953 playerinfo_events_last = strzone(item_id);
956 key = sprintf("#%d:%s", e.playerid, item_id);
957 db_put(playerinfo_db, key, val);
958 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
961 string PlayerInfo_GetItem(entity e, string item_id)
963 if(playerinfo_db < 0)
967 key = sprintf("#%d:%s", e.playerid, item_id);
968 return db_get(playerinfo_db, key);
971 string PlayerInfo_GetItemLocal(string item_id)
975 return PlayerInfo_GetItem(p, item_id);
978 void PlayerInfo_ready(entity fh, entity p, float status)
983 PlayerInfo_AddPlayer(p);
987 case URL_READY_CANWRITE:
988 print("-- Sending data to player stats server\n");
989 url_fputs(fh, "V 1\n");
991 url_fputs(fh, sprintf("R %s\n", WATERMARK));
994 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
995 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
996 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
997 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
1002 case URL_READY_CANREAD:
1003 print("-- Got response from player stats server:\n");
1004 string gametype = string_null;
1005 while((s = url_fgets(fh)))
1007 print(" ", s, "\n");
1009 string key = "", value = "", data = "";
1011 n = tokenizebyseparator(s, " "); // key (value) data
1031 else if (key == "V")
1032 PlayerInfo_AddItem(p, "_version", data);
1033 else if (key == "R")
1034 PlayerInfo_AddItem(p, "_release", data);
1035 else if (key == "T")
1036 PlayerInfo_AddItem(p, "_time", data);
1037 else if (key == "S")
1038 PlayerInfo_AddItem(p, "_statsurl", data);
1039 else if (key == "P")
1040 PlayerInfo_AddItem(p, "_hashkey", data);
1041 else if (key == "n")
1042 PlayerInfo_AddItem(p, "_playernick", data);
1043 else if (key == "i")
1044 PlayerInfo_AddItem(p, "_playerid", data);
1045 else if (key == "G")
1047 else if (key == "e" && value != "")
1050 PlayerInfo_AddItem(p, value, data);
1052 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1057 print("-- End of response.\n");
1060 case URL_READY_CLOSED:
1061 // url_fclose has finished
1062 print("Player stats synchronized with server\n");
1064 case URL_READY_ERROR:
1066 print("Receiving player stats failed: ", ftos(status), "\n");
1071 void PlayerInfo_Init()
1073 playerinfo_db = db_create();
1077 void PlayerInfo_Basic(entity p)
1079 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1081 if(playerinfo_db < 0)
1085 uri = autocvar_g_playerinfo_uri;
1086 if(uri != "" && p.crypto_idfp != "")
1088 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1089 print("Retrieving playerstats from URL: ", uri, "\n");
1090 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1096 void PlayerInfo_Details()
1098 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1100 if(playerinfo_db < 0)
1104 uri = autocvar_g_playerinfo_uri; // FIXME
1105 if(uri != "" && crypto_getmyidstatus(0) > 0)
1107 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1108 uri = strcat(uri, "/player/me");
1109 print("Retrieving playerstats from URL: ", uri, "\n");
1110 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1116 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1117 void PlayerInfo_Details()
1119 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1121 if(playerinfo_db < 0)
1125 uri = autocvar_g_playerinfo_uri; // FIXME
1126 if(uri != "" && crypto_getmyidstatus(0) > 0)
1128 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1129 print("Retrieving playerstats from URL: ", uri, "\n");
1130 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);