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 = max(0, 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;
282 if(autocvar_g_playerstats_gamereport_uri != cvar_defstring("g_playerstats_gamereport_uri"))
284 serverflags |= SERVERFLAG_PLAYERSTATS_CUSTOM;
287 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
288 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
289 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
290 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
291 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
292 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
293 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
294 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
297 FOREACH(Weapons, it != WEP_Null, {
298 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
299 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
300 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
301 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
302 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
305 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
306 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
307 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
308 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
309 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
310 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
311 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
312 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
313 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
314 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
316 anticheat_register_to_playerstats();
318 else { PlayerStats_GameReport_DelayMapVote = false; }
321 // this... is a hack, a temporary one until we get a proper duel gametype
322 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
323 string PlayerStats_GetGametype()
325 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
327 // probably duel, but let's make sure
329 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
333 return GetGametype();
336 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
346 // ======================================
347 // -- OUTGOING GAME REPORT INFORMATION --
348 // ======================================
350 * V: format version (always a fixed number) - this MUST be the first line!
351 * #: comment (MUST be ignored by any parser)
352 * R: release information on the server
354 * O: mod name (icon request) as in server browser
356 * I: match ID (see "matchid" in world.qc)
357 * S: "hostname" of the server
358 * C: number of "unpure" cvar changes
359 * U: UDP port number of the server
360 * D: duration of the match
361 * L: "ladder" in which the server is participating in
362 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
363 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
365 * n: nickname of the player (optional)
367 * e: followed by an event name, a space, and the event count/score
368 * event names can be:
369 * alivetime: total playing time of the player
370 * avglatency: average network latency compounded throughout the match
371 * wins: number of games won (can only be set if matches is set)
372 * matches: number of matches played to the end (not aborted by map switch)
373 * joins: number of matches joined (always 1 unless player never played during the match)
374 * scoreboardvalid: set to 1 if the player was there at the end of the match
375 * total-<scoreboardname>: total score of that scoreboard item
376 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
377 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
378 * kills-<index>: number of kills against the indexed player
379 * rank <number>: rank of player
380 * acc-<weapon netname>-hit: total damage dealt
381 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
382 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
383 * acc-<weapon netname>-cnt-fired: amount of fired shots
384 * acc-<weapon netname>-frags: amount of frags dealt by weapon
386 case URL_READY_CANWRITE:
388 url_fputs(fh, "V 9\n");
390 url_fputs(fh, sprintf("R %s\n", WATERMARK));
392 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
393 url_fputs(fh, sprintf("O %s\n", modname));
394 url_fputs(fh, sprintf("M %s\n", GetMapname()));
395 url_fputs(fh, sprintf("I %s\n", matchid));
396 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
397 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
398 url_fputs(fh, sprintf("U %d\n", cvar("port")));
399 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
400 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
405 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
407 // start team section
408 url_fputs(fh, sprintf("Q team#%s\n", t));
410 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
411 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
413 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
414 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
420 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
422 // start player section
423 url_fputs(fh, sprintf("P %s\n", p));
425 // playerid/index (entity number for this server)
426 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
427 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
430 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
431 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
433 // team identification number
436 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
437 url_fputs(fh, sprintf("t %s\n", tt));
440 // elo ranking enabled
441 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
442 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
444 // output player events
445 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
447 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
448 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
456 // ======================================
457 // -- INCOMING GAME REPORT INFORMATION --
458 // ======================================
462 case URL_READY_CANREAD:
464 // url_fclose is processing, we got a response for writing the data
465 // this must come from HTTP
466 LOG_DEBUG("Got response from player stats server:");
467 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
468 LOG_DEBUG("End of response.");
473 case URL_READY_CLOSED:
475 // url_fclose has finished
476 LOG_DEBUG("Player stats written");
477 PlayerStats_GameReport_DelayMapVote = false;
478 if(PS_GR_OUT_DB >= 0)
480 db_close(PS_GR_OUT_DB);
486 case URL_READY_ERROR:
489 LOG_INFO("Player stats writing failed: ", ftos(status));
490 PlayerStats_GameReport_DelayMapVote = false;
491 if(PS_GR_OUT_DB >= 0)
493 db_close(PS_GR_OUT_DB);
501 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
503 GameRules_scoring_add(joiningplayer, ELO, -1);
504 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
505 if(autocvar_g_playerstats_playerbasic_uri != "")
507 string uri = autocvar_g_playerstats_playerbasic_uri;
508 if (joiningplayer.crypto_idfp == "") {
509 GameRules_scoring_add(joiningplayer, ELO, -1);
511 // create the database if it doesn't already exist
513 PS_B_IN_DB = db_create();
515 // now request the information
516 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
517 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
521 PlayerStats_PlayerBasic_Handler,
525 // set status appropriately // todo: check whether the player info exists in the database previously
528 // database still contains useful information, so don't clear it of a useful status
529 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
533 // database was previously empty or never hit received status for some reason
534 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
540 // server has this disabled, kill the DB and set status to idle
541 GameRules_scoring_add(joiningplayer, ELO, -1);
544 db_close(PS_B_IN_DB);
547 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
552 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
556 db_close(PS_B_IN_DB);
560 if(PS_GR_OUT_DB >= 0)
562 db_close(PS_GR_OUT_DB);
567 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
569 // determine whether we should retrieve playerbasic information again
571 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
572 joiningplayer.netname,
576 // TODO: check to see if this playerid is inside the database already somehow...
577 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
578 // although maybe we should just submit another request ANYWAY?
579 if(!joiningplayer.playerstats_basicstatus)
581 PlayerStats_PlayerBasic(
583 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
588 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
592 case URL_READY_CANWRITE:
594 LOG_DEBUG("-- Sending data to player stats server");
595 /*url_fputs(fh, "V 1\n");
597 url_fputs(fh, sprintf("R %s\n", WATERMARK));
599 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
600 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
601 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
602 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
603 */url_fputs(fh, "\n");
608 case URL_READY_CANREAD:
610 bool handled = false;
611 string gt = string_null;
612 for (string s = ""; (s = url_fgets(fh)); ) {
613 int n = tokenizebyseparator(s, " "); // key value? data
614 if (n == 1) continue;
615 string key = "", value = "", data = "";
626 // PlayerInfo_AddItem(p, "_version", data);
629 // PlayerInfo_AddItem(p, "_release", data);
632 // PlayerInfo_AddItem(p, "_time", data);
635 // PlayerInfo_AddItem(p, "_statsurl", data);
638 // PlayerInfo_AddItem(p, "_hashkey", data);
641 // PlayerInfo_AddItem(p, "_playernick", data);
644 // PlayerInfo_AddItem(p, "_playerid", data);
645 // p.xonstat_id = stof(data);
651 //LOG_TRACE("G: ", gt);
652 //LOG_TRACE("e: ", data);
653 if (gt == PlayerStats_GetGametype()) {
655 float e = stof(data);
656 GameRules_scoring_add(p, ELO, +1 + e);
659 // PlayerInfo_AddItem(p, value, data);
661 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
670 case URL_READY_CLOSED:
672 // url_fclose has finished
673 LOG_INFO("Player stats synchronized with server");
677 case URL_READY_ERROR:
680 LOG_INFO("Receiving player stats failed: ", ftos(status));
684 GameRules_scoring_add(p, ELO, -1);
691 #if 0 // reading the entire DB at once
692 string e = "", en = "";
694 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
696 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
701 void PlayerStats_PlayerDetail_AddItem(string event, string data)
703 if(PS_D_IN_DB < 0) { return; }
705 // create a marker for the event so that we can access it later
706 string marker = sprintf("%s", event);
707 if(db_get(PS_D_IN_DB, marker) == "")
711 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
712 strunzone(PS_D_IN_EVL);
714 else { db_put(PS_D_IN_DB, marker, "#"); }
715 PS_D_IN_EVL = strzone(marker);
718 // now actually set the event data
719 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
720 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
723 void PlayerStats_PlayerDetail()
725 // http://stats.xonotic.org/player/me
726 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
728 // create the database if it doesn't already exist
730 PS_D_IN_DB = db_create();
732 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
733 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
735 autocvar_g_playerstats_playerdetail_uri,
737 PlayerStats_PlayerDetail_Handler,
741 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
745 // player has this disabled, kill the DB and set status to idle
748 db_close(PS_D_IN_DB);
752 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
756 void PlayerStats_PlayerDetail_CheckUpdate()
758 // determine whether we should retrieve playerdetail information again
759 float gamecount = cvar("cl_matchcount");
762 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
771 (time >= PS_D_NEXTUPDATETIME)
773 (gamecount > PS_D_LASTGAMECOUNT)
776 PlayerStats_PlayerDetail();
777 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
778 PS_D_LASTGAMECOUNT = gamecount;
782 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
786 case URL_READY_CANWRITE:
788 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
789 url_fputs(fh, "V 1\n");
791 url_fputs(fh, sprintf("R %s\n", WATERMARK));
793 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
794 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
795 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
796 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
802 case URL_READY_CANREAD:
804 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
806 string gametype = "overall";
807 while((input = url_fgets(fh)))
809 float count = tokenizebyseparator(input, " ");
810 string key = "", event = "", data = "";
812 if(argv(0) == "#") { continue; }
817 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
823 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
830 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
831 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
832 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
835 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
836 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
837 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
838 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
841 case "G": gametype = data; break;
844 if(event != "" && data != "")
846 PlayerStats_PlayerDetail_AddItem(
861 "PlayerStats_PlayerDetail_Handler(): ERROR: "
862 "Key went unhandled? Is our version outdated?\n"
863 "PlayerStats_PlayerDetail_Handler(): "
864 "Key '%s', Event '%s', Data '%s'",
875 "PlayerStats_PlayerDetail_Handler(): "
876 "Key '%s', Event '%s', Data '%s'",
883 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
885 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
886 statslist.getStats(statslist);
890 case URL_READY_CLOSED:
892 // url_fclose has finished
893 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
897 case URL_READY_ERROR:
900 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
901 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
904 db_close(PS_D_IN_DB);
914 void PlayerInfo_AddPlayer(entity e)
916 if(playerinfo_db < 0)
920 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
923 p = db_get(playerinfo_db, key);
928 db_put(playerinfo_db, key, playerinfo_last);
929 strunzone(playerinfo_last);
932 db_put(playerinfo_db, key, "#");
933 playerinfo_last = strzone(ftos(e.playerid));
934 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
938 void PlayerInfo_AddItem(entity e, string item_id, string val)
940 if(playerinfo_db < 0)
944 key = sprintf("*:%s", item_id);
947 p = db_get(playerinfo_db, key);
950 if(playerinfo_events_last)
952 db_put(playerinfo_db, key, playerinfo_events_last);
953 strunzone(playerinfo_events_last);
956 db_put(playerinfo_db, key, "#");
957 playerinfo_events_last = strzone(item_id);
960 key = sprintf("#%d:%s", e.playerid, item_id);
961 db_put(playerinfo_db, key, val);
962 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
965 string PlayerInfo_GetItem(entity e, string item_id)
967 if(playerinfo_db < 0)
971 key = sprintf("#%d:%s", e.playerid, item_id);
972 return db_get(playerinfo_db, key);
975 string PlayerInfo_GetItemLocal(string item_id)
979 return PlayerInfo_GetItem(p, item_id);
982 void PlayerInfo_ready(entity fh, entity p, float status)
987 PlayerInfo_AddPlayer(p);
991 case URL_READY_CANWRITE:
992 print("-- Sending data to player stats server\n");
993 url_fputs(fh, "V 1\n");
995 url_fputs(fh, sprintf("R %s\n", WATERMARK));
998 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
999 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
1000 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
1001 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
1003 url_fputs(fh, "\n");
1006 case URL_READY_CANREAD:
1007 print("-- Got response from player stats server:\n");
1008 string gametype = string_null;
1009 while((s = url_fgets(fh)))
1011 print(" ", s, "\n");
1013 string key = "", value = "", data = "";
1015 n = tokenizebyseparator(s, " "); // key (value) data
1035 else if (key == "V")
1036 PlayerInfo_AddItem(p, "_version", data);
1037 else if (key == "R")
1038 PlayerInfo_AddItem(p, "_release", data);
1039 else if (key == "T")
1040 PlayerInfo_AddItem(p, "_time", data);
1041 else if (key == "S")
1042 PlayerInfo_AddItem(p, "_statsurl", data);
1043 else if (key == "P")
1044 PlayerInfo_AddItem(p, "_hashkey", data);
1045 else if (key == "n")
1046 PlayerInfo_AddItem(p, "_playernick", data);
1047 else if (key == "i")
1048 PlayerInfo_AddItem(p, "_playerid", data);
1049 else if (key == "G")
1051 else if (key == "e" && value != "")
1054 PlayerInfo_AddItem(p, value, data);
1056 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1061 print("-- End of response.\n");
1064 case URL_READY_CLOSED:
1065 // url_fclose has finished
1066 print("Player stats synchronized with server\n");
1068 case URL_READY_ERROR:
1070 print("Receiving player stats failed: ", ftos(status), "\n");
1075 void PlayerInfo_Init()
1077 playerinfo_db = db_create();
1081 void PlayerInfo_Basic(entity p)
1083 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1085 if(playerinfo_db < 0)
1089 uri = autocvar_g_playerinfo_uri;
1090 if(uri != "" && p.crypto_idfp != "")
1092 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1093 print("Retrieving playerstats from URL: ", uri, "\n");
1094 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1100 void PlayerInfo_Details()
1102 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1104 if(playerinfo_db < 0)
1108 uri = autocvar_g_playerinfo_uri; // FIXME
1109 if(uri != "" && crypto_getmyidstatus(0) > 0)
1111 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1112 uri = strcat(uri, "/player/me");
1113 print("Retrieving playerstats from URL: ", uri, "\n");
1114 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1120 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1121 void PlayerInfo_Details()
1123 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1125 if(playerinfo_db < 0)
1129 uri = autocvar_g_playerinfo_uri; // FIXME
1130 if(uri != "" && crypto_getmyidstatus(0) > 0)
1132 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1133 print("Retrieving playerstats from URL: ", uri, "\n");
1134 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);