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)
44 db_close(PS_GR_OUT_DB);
45 PlayerStats_GameReport_Init();
50 for (int i = 0; i < 16; i++)
51 if (teamscorekeepers[i])
52 PlayerStats_GameReport_AddTeam(i + 1);
53 FOREACH_CLIENT(true, {
54 // NOTE Adding back a player we are applying any cl_allow_uidtracking change
55 // usually only possible by reconnecting to the server
56 strfree(it.playerstats_id);
57 PlayerStats_GameReport_AddEvent(sprintf("kills-%d", it.playerid));
58 PlayerStats_GameReport_AddPlayer(it);
60 FOREACH(Scores, true, {
61 string label = scores_label(it);
64 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
65 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
67 for(int i = 0; i < MAX_TEAMSCORE; ++i)
69 string label = teamscores_label(i);
72 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
73 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
77 void PlayerStats_GameReport_AddPlayer(entity e)
79 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
81 // set up player identification
84 if((e.crypto_idfp != "") && (CS_CVAR(e).cvar_cl_allow_uidtracking == 1))
85 { s = e.crypto_idfp; }
86 else if(IS_BOT_CLIENT(e))
87 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
89 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
92 { s = sprintf("bot#%d", e.playerid); }
94 { s = sprintf("player#%d", e.playerid); }
97 e.playerstats_id = strzone(s);
99 // now add the player to the database
100 string key = sprintf("%s:*", e.playerstats_id);
101 string p = db_get(PS_GR_OUT_DB, key);
107 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
108 strunzone(PS_GR_OUT_PL);
110 else { db_put(PS_GR_OUT_DB, key, "#"); }
111 PS_GR_OUT_PL = strzone(e.playerstats_id);
115 void PlayerStats_GameReport_AddTeam(int t)
117 if(PS_GR_OUT_DB < 0) { return; }
119 string key = sprintf("%d", t);
120 string p = db_get(PS_GR_OUT_DB, key);
126 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
127 strunzone(PS_GR_OUT_TL);
129 else { db_put(PS_GR_OUT_DB, key, "#"); }
130 PS_GR_OUT_TL = strzone(key);
134 void PlayerStats_GameReport_AddEvent(string event_id)
136 if(PS_GR_OUT_DB < 0) { return; }
138 string key = sprintf("*:%s", event_id);
139 string p = db_get(PS_GR_OUT_DB, key);
145 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
146 strunzone(PS_GR_OUT_EVL);
148 else { db_put(PS_GR_OUT_DB, key, "#"); }
149 PS_GR_OUT_EVL = strzone(event_id);
153 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
155 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
157 string key = sprintf("%s:%s", prefix, event_id);
158 float val = stof(db_get(PS_GR_OUT_DB, key));
160 db_put(PS_GR_OUT_DB, key, ftos(val));
164 void PlayerStats_GameReport_Accuracy(entity p)
166 #define ACCMAC(suffix, field) \
167 PlayerStats_GameReport_Event_Player(p, \
168 sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
169 FOREACH(Weapons, it != WEP_Null, {
170 ACCMAC("hit", accuracy_hit)
171 ACCMAC("fired", accuracy_fired)
172 ACCMAC("cnt-hit", accuracy_cnt_hit)
173 ACCMAC("cnt-fired", accuracy_cnt_fired)
174 ACCMAC("frags", accuracy_frags)
179 void PlayerStats_GameReport_FinalizePlayer(entity p)
181 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
186 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
190 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
192 if(CS_CVAR(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
193 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p.netname, p.team, false));
196 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
198 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
199 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_JOINS, 1);
201 PlayerStats_GameReport_Accuracy(p);
202 anticheat_report_to_playerstats(p);
204 if(IS_REAL_CLIENT(p))
206 if(CS(p).latency_cnt)
208 float latency = max(0, CS(p).latency_sum / CS(p).latency_cnt);
210 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, latency);
213 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS_CVAR(p).cvar_cl_allow_uidranking));
216 strfree(p.playerstats_id);
219 void PlayerStats_GameReport(bool finished)
221 if(PS_GR_OUT_DB < 0) { return; }
223 PlayerScore_Sort(score_dummyfield, 0, false, false);
224 PlayerScore_Sort(scoreboard_pos, 1, true, true);
225 if(teamplay) { PlayerScore_TeamStats(); }
227 FOREACH_CLIENT(true, {
228 // add personal score rank
229 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
232 if(it.scoreboard_pos)
234 // scoreboard is valid!
235 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
237 // add scoreboard position
238 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
240 // add scoreboard data
241 PlayerScore_PlayerStats(it);
243 // if the match ended normally, add winning info
246 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
247 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
251 // collect final player information
252 PlayerStats_GameReport_FinalizePlayer(it);
255 if(autocvar_g_playerstats_gamereport_uri != "")
257 PlayerStats_GameReport_DelayMapVote = true;
259 autocvar_g_playerstats_gamereport_uri,
261 PlayerStats_GameReport_Handler,
267 PlayerStats_GameReport_DelayMapVote = false;
268 db_close(PS_GR_OUT_DB);
273 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
275 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
277 PS_GR_OUT_DB = db_create();
279 if(PS_GR_OUT_DB >= 0)
281 PlayerStats_GameReport_DelayMapVote = true;
283 serverflags |= SERVERFLAG_PLAYERSTATS;
284 if(autocvar_g_playerstats_gamereport_uri != cvar_defstring("g_playerstats_gamereport_uri"))
286 serverflags |= SERVERFLAG_PLAYERSTATS_CUSTOM;
289 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
290 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
291 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
292 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
293 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
294 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
295 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
296 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
299 FOREACH(Weapons, it != WEP_Null, {
300 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
301 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
302 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
303 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
304 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
307 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
308 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
309 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
310 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
311 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
312 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
313 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
314 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
315 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
316 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
318 anticheat_register_to_playerstats();
320 else { PlayerStats_GameReport_DelayMapVote = false; }
323 // this... is a hack, a temporary one until we get a proper duel gametype
324 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
325 string PlayerStats_GetGametype()
327 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
329 // probably duel, but let's make sure
331 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
335 return GetGametype();
338 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
348 // ======================================
349 // -- OUTGOING GAME REPORT INFORMATION --
350 // ======================================
352 * V: format version (always a fixed number) - this MUST be the first line!
353 * #: comment (MUST be ignored by any parser)
354 * R: release information on the server
356 * O: mod name (icon request) as in server browser
358 * I: match ID (see "matchid" in world.qc)
359 * S: "hostname" of the server
360 * C: number of "unpure" cvar changes
361 * U: UDP port number of the server
362 * D: duration of the match
363 * L: "ladder" in which the server is participating in
364 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
365 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
367 * n: nickname of the player (optional)
369 * r: player ranking enabled / disabled
370 * e: followed by an event name, a space, and the event count/score
371 * event names can be:
372 * alivetime: total playing time of the player
373 * avglatency: average network latency compounded throughout the match
374 * wins: number of games won (can only be set if matches is set)
375 * matches: number of matches played to the end (not aborted by map switch)
376 * joins: number of matches joined (always 1 unless player never played during the match)
377 * scoreboardvalid: set to 1 if the player was there at the end of the match
378 * total-<scoreboardname>: total score of that scoreboard item
379 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
380 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
381 * kills-<index>: number of kills against the indexed player
382 * rank <number>: rank of player
383 * acc-<weapon netname>-hit: total damage dealt
384 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
385 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
386 * acc-<weapon netname>-cnt-fired: amount of fired shots
387 * acc-<weapon netname>-frags: amount of frags dealt by weapon
389 case URL_READY_CANWRITE:
391 url_fputs(fh, "V 9\n");
393 url_fputs(fh, sprintf("R %s\n", WATERMARK));
395 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
396 url_fputs(fh, sprintf("O %s\n", modname));
397 url_fputs(fh, sprintf("M %s\n", GetMapname()));
398 url_fputs(fh, sprintf("I %s\n", matchid));
399 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
400 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
401 url_fputs(fh, sprintf("U %d\n", cvar("port")));
402 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
403 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
408 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
410 // start team section
411 url_fputs(fh, sprintf("Q team#%s\n", t));
413 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
414 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
416 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
417 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
423 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
425 // start player section
426 url_fputs(fh, sprintf("P %s\n", p));
428 // playerid/index (entity number for this server)
429 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
430 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
433 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
434 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
436 // team identification number
439 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
440 url_fputs(fh, sprintf("t %s\n", tt));
443 // elo ranking enabled
444 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
445 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
447 // output player events
448 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
450 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
451 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
459 // ======================================
460 // -- INCOMING GAME REPORT INFORMATION --
461 // ======================================
465 case URL_READY_CANREAD:
467 // url_fclose is processing, we got a response for writing the data
468 // this must come from HTTP
469 LOG_DEBUG("Got response from player stats server:");
470 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
471 LOG_DEBUG("End of response.");
476 case URL_READY_CLOSED:
478 // url_fclose has finished
479 LOG_DEBUG("Player stats written");
480 PlayerStats_GameReport_DelayMapVote = false;
481 if(PS_GR_OUT_DB >= 0)
483 db_close(PS_GR_OUT_DB);
489 case URL_READY_ERROR:
492 LOG_INFO("Player stats writing failed: ", ftos(status));
493 PlayerStats_GameReport_DelayMapVote = false;
494 if(PS_GR_OUT_DB >= 0)
496 db_close(PS_GR_OUT_DB);
504 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
506 GameRules_scoring_add(joiningplayer, ELO, -1);
507 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
508 if(autocvar_g_playerstats_playerbasic_uri != "")
510 string uri = autocvar_g_playerstats_playerbasic_uri;
511 if (joiningplayer.crypto_idfp == "") {
512 GameRules_scoring_add(joiningplayer, ELO, -1);
514 // create the database if it doesn't already exist
516 PS_B_IN_DB = db_create();
518 // now request the information
519 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
520 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
524 PlayerStats_PlayerBasic_Handler,
528 // set status appropriately // todo: check whether the player info exists in the database previously
531 // database still contains useful information, so don't clear it of a useful status
532 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
536 // database was previously empty or never hit received status for some reason
537 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
543 // server has this disabled, kill the DB and set status to idle
544 GameRules_scoring_add(joiningplayer, ELO, -1);
547 db_close(PS_B_IN_DB);
550 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
555 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
559 db_close(PS_B_IN_DB);
563 if(PS_GR_OUT_DB >= 0)
565 db_close(PS_GR_OUT_DB);
570 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
572 // determine whether we should retrieve playerbasic information again
574 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
575 joiningplayer.netname,
579 // TODO: check to see if this playerid is inside the database already somehow...
580 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
581 // although maybe we should just submit another request ANYWAY?
582 if(!joiningplayer.playerstats_basicstatus)
584 PlayerStats_PlayerBasic(
586 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
591 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
595 case URL_READY_CANWRITE:
597 LOG_DEBUG("-- Sending data to player stats server");
598 /*url_fputs(fh, "V 1\n");
600 url_fputs(fh, sprintf("R %s\n", WATERMARK));
602 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
603 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
604 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
605 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
606 */url_fputs(fh, "\n");
611 case URL_READY_CANREAD:
613 bool handled = false;
614 string gt = string_null;
615 for (string s = ""; (s = url_fgets(fh)); ) {
616 int n = tokenizebyseparator(s, " "); // key value? data
617 if (n == 1) continue;
618 string key = "", value = "", data = "";
629 // PlayerInfo_AddItem(p, "_version", data);
632 // PlayerInfo_AddItem(p, "_release", data);
635 // PlayerInfo_AddItem(p, "_time", data);
638 // PlayerInfo_AddItem(p, "_statsurl", data);
641 // PlayerInfo_AddItem(p, "_hashkey", data);
644 // PlayerInfo_AddItem(p, "_playernick", data);
647 // PlayerInfo_AddItem(p, "_playerid", data);
648 // p.xonstat_id = stof(data);
654 //LOG_TRACE("G: ", gt);
655 //LOG_TRACE("e: ", data);
656 if (gt == PlayerStats_GetGametype()) {
658 float e = stof(data);
659 GameRules_scoring_add(p, ELO, +1 + e);
662 // PlayerInfo_AddItem(p, value, data);
664 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
673 case URL_READY_CLOSED:
675 // url_fclose has finished
676 LOG_INFO("Player stats synchronized with server");
680 case URL_READY_ERROR:
683 LOG_INFO("Receiving player stats failed: ", ftos(status));
687 GameRules_scoring_add(p, ELO, -1);
694 #if 0 // reading the entire DB at once
695 string e = "", en = "";
697 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
699 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
704 void PlayerStats_PlayerDetail_AddItem(string event, string data)
706 if(PS_D_IN_DB < 0) { return; }
708 // create a marker for the event so that we can access it later
709 string marker = sprintf("%s", event);
710 if(db_get(PS_D_IN_DB, marker) == "")
714 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
715 strunzone(PS_D_IN_EVL);
717 else { db_put(PS_D_IN_DB, marker, "#"); }
718 PS_D_IN_EVL = strzone(marker);
721 // now actually set the event data
722 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
723 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
726 void PlayerStats_PlayerDetail()
728 // http://stats.xonotic.org/player/me
729 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
731 // create the database if it doesn't already exist
733 PS_D_IN_DB = db_create();
735 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
736 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
738 autocvar_g_playerstats_playerdetail_uri,
740 PlayerStats_PlayerDetail_Handler,
744 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
748 // player has this disabled, kill the DB and set status to idle
751 db_close(PS_D_IN_DB);
755 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
759 void PlayerStats_PlayerDetail_CheckUpdate()
761 // determine whether we should retrieve playerdetail information again
762 float gamecount = cvar("cl_matchcount");
765 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
774 (time >= PS_D_NEXTUPDATETIME)
776 (gamecount > PS_D_LASTGAMECOUNT)
779 PlayerStats_PlayerDetail();
780 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
781 PS_D_LASTGAMECOUNT = gamecount;
785 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
789 case URL_READY_CANWRITE:
791 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
792 url_fputs(fh, "V 1\n");
794 url_fputs(fh, sprintf("R %s\n", WATERMARK));
796 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
797 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
798 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
799 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
805 case URL_READY_CANREAD:
807 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
809 string gametype = "overall";
810 while((input = url_fgets(fh)))
812 float count = tokenizebyseparator(input, " ");
813 string key = "", event = "", data = "";
815 if(argv(0) == "#") { continue; }
820 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
826 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
833 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
834 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
835 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
838 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
839 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
840 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
841 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
844 case "G": gametype = data; break;
847 if(event != "" && data != "")
849 PlayerStats_PlayerDetail_AddItem(
864 "PlayerStats_PlayerDetail_Handler(): ERROR: "
865 "Key went unhandled? Is our version outdated?\n"
866 "PlayerStats_PlayerDetail_Handler(): "
867 "Key '%s', Event '%s', Data '%s'",
878 "PlayerStats_PlayerDetail_Handler(): "
879 "Key '%s', Event '%s', Data '%s'",
886 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
888 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
889 statslist.getStats(statslist);
893 case URL_READY_CLOSED:
895 // url_fclose has finished
896 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
900 case URL_READY_ERROR:
903 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
904 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
907 db_close(PS_D_IN_DB);
917 void PlayerInfo_AddPlayer(entity e)
919 if(playerinfo_db < 0)
923 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
926 p = db_get(playerinfo_db, key);
931 db_put(playerinfo_db, key, playerinfo_last);
932 strunzone(playerinfo_last);
935 db_put(playerinfo_db, key, "#");
936 playerinfo_last = strzone(ftos(e.playerid));
937 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
941 void PlayerInfo_AddItem(entity e, string item_id, string val)
943 if(playerinfo_db < 0)
947 key = sprintf("*:%s", item_id);
950 p = db_get(playerinfo_db, key);
953 if(playerinfo_events_last)
955 db_put(playerinfo_db, key, playerinfo_events_last);
956 strunzone(playerinfo_events_last);
959 db_put(playerinfo_db, key, "#");
960 playerinfo_events_last = strzone(item_id);
963 key = sprintf("#%d:%s", e.playerid, item_id);
964 db_put(playerinfo_db, key, val);
965 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
968 string PlayerInfo_GetItem(entity e, string item_id)
970 if(playerinfo_db < 0)
974 key = sprintf("#%d:%s", e.playerid, item_id);
975 return db_get(playerinfo_db, key);
978 string PlayerInfo_GetItemLocal(string item_id)
982 return PlayerInfo_GetItem(p, item_id);
985 void PlayerInfo_ready(entity fh, entity p, float status)
990 PlayerInfo_AddPlayer(p);
994 case URL_READY_CANWRITE:
995 print("-- Sending data to player stats server\n");
996 url_fputs(fh, "V 1\n");
998 url_fputs(fh, sprintf("R %s\n", WATERMARK));
1001 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
1002 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
1003 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
1004 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
1006 url_fputs(fh, "\n");
1009 case URL_READY_CANREAD:
1010 print("-- Got response from player stats server:\n");
1011 string gametype = string_null;
1012 while((s = url_fgets(fh)))
1014 print(" ", s, "\n");
1016 string key = "", value = "", data = "";
1018 n = tokenizebyseparator(s, " "); // key (value) data
1038 else if (key == "V")
1039 PlayerInfo_AddItem(p, "_version", data);
1040 else if (key == "R")
1041 PlayerInfo_AddItem(p, "_release", data);
1042 else if (key == "T")
1043 PlayerInfo_AddItem(p, "_time", data);
1044 else if (key == "S")
1045 PlayerInfo_AddItem(p, "_statsurl", data);
1046 else if (key == "P")
1047 PlayerInfo_AddItem(p, "_hashkey", data);
1048 else if (key == "n")
1049 PlayerInfo_AddItem(p, "_playernick", data);
1050 else if (key == "i")
1051 PlayerInfo_AddItem(p, "_playerid", data);
1052 else if (key == "G")
1054 else if (key == "e" && value != "")
1057 PlayerInfo_AddItem(p, value, data);
1059 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1064 print("-- End of response.\n");
1067 case URL_READY_CLOSED:
1068 // url_fclose has finished
1069 print("Player stats synchronized with server\n");
1071 case URL_READY_ERROR:
1073 print("Receiving player stats failed: ", ftos(status), "\n");
1078 void PlayerInfo_Init()
1080 playerinfo_db = db_create();
1084 void PlayerInfo_Basic(entity p)
1086 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1088 if(playerinfo_db < 0)
1092 uri = autocvar_g_playerinfo_uri;
1093 if(uri != "" && p.crypto_idfp != "")
1095 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1096 print("Retrieving playerstats from URL: ", uri, "\n");
1097 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1103 void PlayerInfo_Details()
1105 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1107 if(playerinfo_db < 0)
1111 uri = autocvar_g_playerinfo_uri; // FIXME
1112 if(uri != "" && crypto_getmyidstatus(0) > 0)
1114 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1115 uri = strcat(uri, "/player/me");
1116 print("Retrieving playerstats from URL: ", uri, "\n");
1117 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1123 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1124 void PlayerInfo_Details()
1126 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1128 if(playerinfo_db < 0)
1132 uri = autocvar_g_playerinfo_uri; // FIXME
1133 if(uri != "" && crypto_getmyidstatus(0) > 0)
1135 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1136 print("Retrieving playerstats from URL: ", uri, "\n");
1137 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);