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>
19 void PlayerStats_Prematch()
24 // Deletes current playerstats DB, creates a new one and fully initializes it
25 void PlayerStats_GameReport_Reset_All()
27 strfree(PS_GR_OUT_TL);
28 strfree(PS_GR_OUT_PL);
29 strfree(PS_GR_OUT_EVL);
31 if (PS_GR_OUT_DB >= 0)
32 db_close(PS_GR_OUT_DB);
33 PlayerStats_GameReport_Init();
37 for (int i = 0; i < 16; i++)
38 if (teamscorekeepers[i])
39 PlayerStats_GameReport_AddTeam(i + 1);
40 FOREACH_CLIENT(true, {
41 // NOTE Adding back a player we are applying any cl_allow_uidtracking change
42 // usually only possible by reconnecting to the server
43 strfree(it.playerstats_id);
44 PlayerStats_GameReport_AddEvent(sprintf("kills-%d", it.playerid));
45 PlayerStats_GameReport_AddPlayer(it);
47 FOREACH(Scores, true, {
48 string label = scores_label(it);
51 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
52 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
54 for(int i = 0; i < MAX_TEAMSCORE; ++i)
56 string label = teamscores_label(i);
59 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
60 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
64 void PlayerStats_GameReport_AddPlayer(entity e)
66 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
68 // set up player identification
71 if((e.crypto_idfp != "") && (CS_CVAR(e).cvar_cl_allow_uidtracking == 1))
72 { s = e.crypto_idfp; }
73 else if(IS_BOT_CLIENT(e))
74 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
76 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
79 { s = sprintf("bot#%d", e.playerid); }
81 { s = sprintf("player#%d", e.playerid); }
84 e.playerstats_id = strzone(s);
86 // now add the player to the database
87 string key = sprintf("%s:*", e.playerstats_id);
88 string p = db_get(PS_GR_OUT_DB, key);
94 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
95 strunzone(PS_GR_OUT_PL);
97 else { db_put(PS_GR_OUT_DB, key, "#"); }
98 PS_GR_OUT_PL = strzone(e.playerstats_id);
102 void PlayerStats_GameReport_AddTeam(int t)
104 if(PS_GR_OUT_DB < 0) { return; }
106 string key = sprintf("%d", t);
107 string p = db_get(PS_GR_OUT_DB, key);
113 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
114 strunzone(PS_GR_OUT_TL);
116 else { db_put(PS_GR_OUT_DB, key, "#"); }
117 PS_GR_OUT_TL = strzone(key);
121 void PlayerStats_GameReport_AddEvent(string event_id)
123 if(PS_GR_OUT_DB < 0) { return; }
125 string key = sprintf("*:%s", event_id);
126 string p = db_get(PS_GR_OUT_DB, key);
132 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
133 strunzone(PS_GR_OUT_EVL);
135 else { db_put(PS_GR_OUT_DB, key, "#"); }
136 PS_GR_OUT_EVL = strzone(event_id);
140 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
142 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
144 string key = sprintf("%s:%s", prefix, event_id);
145 float val = stof(db_get(PS_GR_OUT_DB, key));
147 db_put(PS_GR_OUT_DB, key, ftos(val));
151 void PlayerStats_GameReport_Accuracy(entity p)
153 #define ACCMAC(suffix, field) \
154 PlayerStats_GameReport_Event_Player(p, \
155 sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
156 FOREACH(Weapons, it != WEP_Null, {
157 ACCMAC("hit", accuracy_hit)
158 ACCMAC("fired", accuracy_fired)
159 ACCMAC("cnt-hit", accuracy_cnt_hit)
160 ACCMAC("cnt-fired", accuracy_cnt_fired)
161 ACCMAC("frags", accuracy_frags)
166 void PlayerStats_GameReport_FinalizePlayer(entity p)
168 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
173 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
177 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
179 if(CS_CVAR(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
180 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p.netname, p.team, false));
183 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
185 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
186 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_JOINS, 1);
188 PlayerStats_GameReport_Accuracy(p);
189 anticheat_report_to_playerstats(p);
191 if(IS_REAL_CLIENT(p))
193 if(CS(p).latency_cnt)
195 float latency = (CS(p).latency_sum / CS(p).latency_cnt);
197 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, latency);
200 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS_CVAR(p).cvar_cl_allow_uidranking));
203 strfree(p.playerstats_id);
206 void PlayerStats_GameReport(bool finished)
208 if(PS_GR_OUT_DB < 0) { return; }
210 PlayerScore_Sort(score_dummyfield, 0, false, false);
211 PlayerScore_Sort(scoreboard_pos, 1, true, true);
212 if(teamplay) { PlayerScore_TeamStats(); }
214 FOREACH_CLIENT(true, {
215 // add personal score rank
216 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
219 if(it.scoreboard_pos)
221 // scoreboard is valid!
222 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
224 // add scoreboard position
225 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
227 // add scoreboard data
228 PlayerScore_PlayerStats(it);
230 // if the match ended normally, add winning info
233 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
234 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
238 // collect final player information
239 PlayerStats_GameReport_FinalizePlayer(it);
242 if(autocvar_g_playerstats_gamereport_uri != "")
244 PlayerStats_GameReport_DelayMapVote = true;
246 autocvar_g_playerstats_gamereport_uri,
248 PlayerStats_GameReport_Handler,
254 PlayerStats_GameReport_DelayMapVote = false;
255 db_close(PS_GR_OUT_DB);
260 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
262 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
264 PS_GR_OUT_DB = db_create();
266 if(PS_GR_OUT_DB >= 0)
268 PlayerStats_GameReport_DelayMapVote = true;
270 serverflags |= SERVERFLAG_PLAYERSTATS;
272 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
273 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
274 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
275 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
276 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
277 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
278 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
279 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
282 FOREACH(Weapons, it != WEP_Null, {
283 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
284 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
285 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
286 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
287 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
290 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
291 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
292 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
293 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
294 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
295 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
296 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
297 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
298 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
299 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
301 anticheat_register_to_playerstats();
303 else { PlayerStats_GameReport_DelayMapVote = false; }
306 // this... is a hack, a temporary one until we get a proper duel gametype
307 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
308 string PlayerStats_GetGametype()
310 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
312 // probably duel, but let's make sure
314 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
318 return GetGametype();
321 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
331 // ======================================
332 // -- OUTGOING GAME REPORT INFORMATION --
333 // ======================================
335 * V: format version (always a fixed number) - this MUST be the first line!
336 * #: comment (MUST be ignored by any parser)
337 * R: release information on the server
339 * O: mod name (icon request) as in server browser
341 * I: match ID (see "matchid" in world.qc)
342 * S: "hostname" of the server
343 * C: number of "unpure" cvar changes
344 * U: UDP port number of the server
345 * D: duration of the match
346 * L: "ladder" in which the server is participating in
347 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
348 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
350 * n: nickname of the player (optional)
352 * e: followed by an event name, a space, and the event count/score
353 * event names can be:
354 * alivetime: total playing time of the player
355 * avglatency: average network latency compounded throughout the match
356 * wins: number of games won (can only be set if matches is set)
357 * matches: number of matches played to the end (not aborted by map switch)
358 * joins: number of matches joined (always 1 unless player never played during the match)
359 * scoreboardvalid: set to 1 if the player was there at the end of the match
360 * total-<scoreboardname>: total score of that scoreboard item
361 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
362 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
363 * kills-<index>: number of kills against the indexed player
364 * rank <number>: rank of player
365 * acc-<weapon netname>-hit: total damage dealt
366 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
367 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
368 * acc-<weapon netname>-cnt-fired: amount of fired shots
369 * acc-<weapon netname>-frags: amount of frags dealt by weapon
371 case URL_READY_CANWRITE:
373 url_fputs(fh, "V 9\n");
375 url_fputs(fh, sprintf("R %s\n", WATERMARK));
377 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
378 url_fputs(fh, sprintf("O %s\n", modname));
379 url_fputs(fh, sprintf("M %s\n", GetMapname()));
380 url_fputs(fh, sprintf("I %s\n", matchid));
381 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
382 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
383 url_fputs(fh, sprintf("U %d\n", cvar("port")));
384 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
385 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
390 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
392 // start team section
393 url_fputs(fh, sprintf("Q team#%s\n", t));
395 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
396 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
398 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
399 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
405 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
407 // start player section
408 url_fputs(fh, sprintf("P %s\n", p));
410 // playerid/index (entity number for this server)
411 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
412 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
415 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
416 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
418 // team identification number
421 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
422 url_fputs(fh, sprintf("t %s\n", tt));
425 // elo ranking enabled
426 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
427 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
429 // output player events
430 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
432 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
433 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
441 // ======================================
442 // -- INCOMING GAME REPORT INFORMATION --
443 // ======================================
447 case URL_READY_CANREAD:
449 // url_fclose is processing, we got a response for writing the data
450 // this must come from HTTP
451 LOG_DEBUG("Got response from player stats server:");
452 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
453 LOG_DEBUG("End of response.");
458 case URL_READY_CLOSED:
460 // url_fclose has finished
461 LOG_DEBUG("Player stats written");
462 PlayerStats_GameReport_DelayMapVote = false;
463 if(PS_GR_OUT_DB >= 0)
465 db_close(PS_GR_OUT_DB);
471 case URL_READY_ERROR:
474 LOG_INFO("Player stats writing failed: ", ftos(status));
475 PlayerStats_GameReport_DelayMapVote = false;
476 if(PS_GR_OUT_DB >= 0)
478 db_close(PS_GR_OUT_DB);
486 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
488 GameRules_scoring_add(joiningplayer, ELO, -1);
489 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
490 if(autocvar_g_playerstats_playerbasic_uri != "")
492 string uri = autocvar_g_playerstats_playerbasic_uri;
493 if (joiningplayer.crypto_idfp == "") {
494 GameRules_scoring_add(joiningplayer, ELO, -1);
496 // create the database if it doesn't already exist
498 PS_B_IN_DB = db_create();
500 // now request the information
501 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
502 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
506 PlayerStats_PlayerBasic_Handler,
510 // set status appropriately // todo: check whether the player info exists in the database previously
513 // database still contains useful information, so don't clear it of a useful status
514 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
518 // database was previously empty or never hit received status for some reason
519 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
525 // server has this disabled, kill the DB and set status to idle
526 GameRules_scoring_add(joiningplayer, ELO, -1);
529 db_close(PS_B_IN_DB);
532 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
537 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
541 db_close(PS_B_IN_DB);
545 if(PS_GR_OUT_DB >= 0)
547 db_close(PS_GR_OUT_DB);
552 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
554 // determine whether we should retrieve playerbasic information again
556 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
557 joiningplayer.netname,
561 // TODO: check to see if this playerid is inside the database already somehow...
562 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
563 // although maybe we should just submit another request ANYWAY?
564 if(!joiningplayer.playerstats_basicstatus)
566 PlayerStats_PlayerBasic(
568 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
573 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
577 case URL_READY_CANWRITE:
579 LOG_DEBUG("-- Sending data to player stats server");
580 /*url_fputs(fh, "V 1\n");
582 url_fputs(fh, sprintf("R %s\n", WATERMARK));
584 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
585 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
586 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
587 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
588 */url_fputs(fh, "\n");
593 case URL_READY_CANREAD:
595 bool handled = false;
596 string gt = string_null;
597 for (string s = ""; (s = url_fgets(fh)); ) {
598 int n = tokenizebyseparator(s, " "); // key value? data
599 if (n == 1) continue;
600 string key = "", value = "", data = "";
611 // PlayerInfo_AddItem(p, "_version", data);
614 // PlayerInfo_AddItem(p, "_release", data);
617 // PlayerInfo_AddItem(p, "_time", data);
620 // PlayerInfo_AddItem(p, "_statsurl", data);
623 // PlayerInfo_AddItem(p, "_hashkey", data);
626 // PlayerInfo_AddItem(p, "_playernick", data);
629 // PlayerInfo_AddItem(p, "_playerid", data);
630 // p.xonstat_id = stof(data);
636 //LOG_TRACE("G: ", gt);
637 //LOG_TRACE("e: ", data);
638 if (gt == PlayerStats_GetGametype()) {
640 float e = stof(data);
641 GameRules_scoring_add(p, ELO, +1 + e);
644 // PlayerInfo_AddItem(p, value, data);
646 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
655 case URL_READY_CLOSED:
657 // url_fclose has finished
658 LOG_INFO("Player stats synchronized with server");
662 case URL_READY_ERROR:
665 LOG_INFO("Receiving player stats failed: ", ftos(status));
669 GameRules_scoring_add(p, ELO, -1);
676 #if 0 // reading the entire DB at once
677 string e = "", en = "";
679 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
681 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
686 void PlayerStats_PlayerDetail_AddItem(string event, string data)
688 if(PS_D_IN_DB < 0) { return; }
690 // create a marker for the event so that we can access it later
691 string marker = sprintf("%s", event);
692 if(db_get(PS_D_IN_DB, marker) == "")
696 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
697 strunzone(PS_D_IN_EVL);
699 else { db_put(PS_D_IN_DB, marker, "#"); }
700 PS_D_IN_EVL = strzone(marker);
703 // now actually set the event data
704 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
705 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
708 void PlayerStats_PlayerDetail()
710 // http://stats.xonotic.org/player/me
711 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
713 // create the database if it doesn't already exist
715 PS_D_IN_DB = db_create();
717 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
718 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
720 autocvar_g_playerstats_playerdetail_uri,
722 PlayerStats_PlayerDetail_Handler,
726 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
730 // player has this disabled, kill the DB and set status to idle
733 db_close(PS_D_IN_DB);
737 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
741 void PlayerStats_PlayerDetail_CheckUpdate()
743 // determine whether we should retrieve playerdetail information again
744 float gamecount = cvar("cl_matchcount");
747 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
756 (time >= PS_D_NEXTUPDATETIME)
758 (gamecount > PS_D_LASTGAMECOUNT)
761 PlayerStats_PlayerDetail();
762 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
763 PS_D_LASTGAMECOUNT = gamecount;
767 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
771 case URL_READY_CANWRITE:
773 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
774 url_fputs(fh, "V 1\n");
776 url_fputs(fh, sprintf("R %s\n", WATERMARK));
778 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
779 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
780 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
781 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
787 case URL_READY_CANREAD:
789 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
791 string gametype = "overall";
792 while((input = url_fgets(fh)))
794 float count = tokenizebyseparator(input, " ");
795 string key = "", event = "", data = "";
797 if(argv(0) == "#") { continue; }
802 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
808 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
815 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
816 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
817 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
820 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
821 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
822 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
823 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
826 case "G": gametype = data; break;
829 if(event != "" && data != "")
831 PlayerStats_PlayerDetail_AddItem(
846 "PlayerStats_PlayerDetail_Handler(): ERROR: "
847 "Key went unhandled? Is our version outdated?\n"
848 "PlayerStats_PlayerDetail_Handler(): "
849 "Key '%s', Event '%s', Data '%s'",
860 "PlayerStats_PlayerDetail_Handler(): "
861 "Key '%s', Event '%s', Data '%s'",
868 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
870 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
871 statslist.getStats(statslist);
875 case URL_READY_CLOSED:
877 // url_fclose has finished
878 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
882 case URL_READY_ERROR:
885 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
886 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
889 db_close(PS_D_IN_DB);
899 void PlayerInfo_AddPlayer(entity e)
901 if(playerinfo_db < 0)
905 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
908 p = db_get(playerinfo_db, key);
913 db_put(playerinfo_db, key, playerinfo_last);
914 strunzone(playerinfo_last);
917 db_put(playerinfo_db, key, "#");
918 playerinfo_last = strzone(ftos(e.playerid));
919 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
923 void PlayerInfo_AddItem(entity e, string item_id, string val)
925 if(playerinfo_db < 0)
929 key = sprintf("*:%s", item_id);
932 p = db_get(playerinfo_db, key);
935 if(playerinfo_events_last)
937 db_put(playerinfo_db, key, playerinfo_events_last);
938 strunzone(playerinfo_events_last);
941 db_put(playerinfo_db, key, "#");
942 playerinfo_events_last = strzone(item_id);
945 key = sprintf("#%d:%s", e.playerid, item_id);
946 db_put(playerinfo_db, key, val);
947 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
950 string PlayerInfo_GetItem(entity e, string item_id)
952 if(playerinfo_db < 0)
956 key = sprintf("#%d:%s", e.playerid, item_id);
957 return db_get(playerinfo_db, key);
960 string PlayerInfo_GetItemLocal(string item_id)
964 return PlayerInfo_GetItem(p, item_id);
967 void PlayerInfo_ready(entity fh, entity p, float status)
972 PlayerInfo_AddPlayer(p);
976 case URL_READY_CANWRITE:
977 print("-- Sending data to player stats server\n");
978 url_fputs(fh, "V 1\n");
980 url_fputs(fh, sprintf("R %s\n", WATERMARK));
983 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
984 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
985 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
986 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
991 case URL_READY_CANREAD:
992 print("-- Got response from player stats server:\n");
993 string gametype = string_null;
994 while((s = url_fgets(fh)))
998 string key = "", value = "", data = "";
1000 n = tokenizebyseparator(s, " "); // key (value) data
1020 else if (key == "V")
1021 PlayerInfo_AddItem(p, "_version", data);
1022 else if (key == "R")
1023 PlayerInfo_AddItem(p, "_release", data);
1024 else if (key == "T")
1025 PlayerInfo_AddItem(p, "_time", data);
1026 else if (key == "S")
1027 PlayerInfo_AddItem(p, "_statsurl", data);
1028 else if (key == "P")
1029 PlayerInfo_AddItem(p, "_hashkey", data);
1030 else if (key == "n")
1031 PlayerInfo_AddItem(p, "_playernick", data);
1032 else if (key == "i")
1033 PlayerInfo_AddItem(p, "_playerid", data);
1034 else if (key == "G")
1036 else if (key == "e" && value != "")
1039 PlayerInfo_AddItem(p, value, data);
1041 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1046 print("-- End of response.\n");
1049 case URL_READY_CLOSED:
1050 // url_fclose has finished
1051 print("Player stats synchronized with server\n");
1053 case URL_READY_ERROR:
1055 print("Receiving player stats failed: ", ftos(status), "\n");
1060 void PlayerInfo_Init()
1062 playerinfo_db = db_create();
1066 void PlayerInfo_Basic(entity p)
1068 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1070 if(playerinfo_db < 0)
1074 uri = autocvar_g_playerinfo_uri;
1075 if(uri != "" && p.crypto_idfp != "")
1077 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1078 print("Retrieving playerstats from URL: ", uri, "\n");
1079 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1085 void PlayerInfo_Details()
1087 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1089 if(playerinfo_db < 0)
1093 uri = autocvar_g_playerinfo_uri; // FIXME
1094 if(uri != "" && crypto_getmyidstatus(0) > 0)
1096 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1097 uri = strcat(uri, "/player/me");
1098 print("Retrieving playerstats from URL: ", uri, "\n");
1099 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1105 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1106 void PlayerInfo_Details()
1108 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1110 if(playerinfo_db < 0)
1114 uri = autocvar_g_playerinfo_uri; // FIXME
1115 if(uri != "" && crypto_getmyidstatus(0) > 0)
1117 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1118 print("Retrieving playerstats from URL: ", uri, "\n");
1119 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);