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 if (IS_BOT_CLIENT(it) || CS_CVAR(it).cvar_cl_allow_uidtracking)
46 PlayerStats_GameReport_AddPlayer(it);
48 FOREACH(Scores, true, {
49 string label = scores_label(it);
52 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
53 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
55 for(int i = 0; i < MAX_TEAMSCORE; ++i)
57 string label = teamscores_label(i);
60 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
61 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
65 void PlayerStats_GameReport_AddPlayer(entity e)
67 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
69 // set up player identification
72 if((e.crypto_idfp != "") && (CS_CVAR(e).cvar_cl_allow_uidtracking == 1))
73 { s = e.crypto_idfp; }
74 else if(IS_BOT_CLIENT(e))
75 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
77 if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
80 { s = sprintf("bot#%d", e.playerid); }
82 { s = sprintf("player#%d", e.playerid); }
85 e.playerstats_id = strzone(s);
87 // now add the player to the database
88 string key = sprintf("%s:*", e.playerstats_id);
89 string p = db_get(PS_GR_OUT_DB, key);
95 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
96 strunzone(PS_GR_OUT_PL);
98 else { db_put(PS_GR_OUT_DB, key, "#"); }
99 PS_GR_OUT_PL = strzone(e.playerstats_id);
103 void PlayerStats_GameReport_AddTeam(int t)
105 if(PS_GR_OUT_DB < 0) { return; }
107 string key = sprintf("%d", t);
108 string p = db_get(PS_GR_OUT_DB, key);
114 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
115 strunzone(PS_GR_OUT_TL);
117 else { db_put(PS_GR_OUT_DB, key, "#"); }
118 PS_GR_OUT_TL = strzone(key);
122 void PlayerStats_GameReport_AddEvent(string event_id)
124 if(PS_GR_OUT_DB < 0) { return; }
126 string key = sprintf("*:%s", event_id);
127 string p = db_get(PS_GR_OUT_DB, key);
133 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
134 strunzone(PS_GR_OUT_EVL);
136 else { db_put(PS_GR_OUT_DB, key, "#"); }
137 PS_GR_OUT_EVL = strzone(event_id);
141 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
143 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
145 string key = sprintf("%s:%s", prefix, event_id);
146 float val = stof(db_get(PS_GR_OUT_DB, key));
148 db_put(PS_GR_OUT_DB, key, ftos(val));
152 void PlayerStats_GameReport_Accuracy(entity p)
154 #define ACCMAC(suffix, field) \
155 PlayerStats_GameReport_Event_Player(p, \
156 sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
157 FOREACH(Weapons, it != WEP_Null, {
158 ACCMAC("hit", accuracy_hit)
159 ACCMAC("fired", accuracy_fired)
160 ACCMAC("cnt-hit", accuracy_cnt_hit)
161 ACCMAC("cnt-fired", accuracy_cnt_fired)
162 ACCMAC("frags", accuracy_frags)
167 void PlayerStats_GameReport_FinalizePlayer(entity p)
169 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
174 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
178 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
180 if(CS_CVAR(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
181 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p.netname, p.team, false));
184 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
186 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
187 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_JOINS, 1);
189 PlayerStats_GameReport_Accuracy(p);
190 anticheat_report_to_playerstats(p);
192 if(IS_REAL_CLIENT(p))
194 if(CS(p).latency_cnt)
196 float latency = (CS(p).latency_sum / CS(p).latency_cnt);
198 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, latency);
201 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS_CVAR(p).cvar_cl_allow_uidranking));
204 strfree(p.playerstats_id);
207 void PlayerStats_GameReport(bool finished)
209 if(PS_GR_OUT_DB < 0) { return; }
211 PlayerScore_Sort(score_dummyfield, 0, false, false);
212 PlayerScore_Sort(scoreboard_pos, 1, true, true);
213 if(teamplay) { PlayerScore_TeamStats(); }
215 FOREACH_CLIENT(true, {
216 // add personal score rank
217 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
220 if(it.scoreboard_pos)
222 // scoreboard is valid!
223 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
225 // add scoreboard position
226 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
228 // add scoreboard data
229 PlayerScore_PlayerStats(it);
231 // if the match ended normally, add winning info
234 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
235 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
239 // collect final player information
240 PlayerStats_GameReport_FinalizePlayer(it);
243 if(autocvar_g_playerstats_gamereport_uri != "")
245 PlayerStats_GameReport_DelayMapVote = true;
247 autocvar_g_playerstats_gamereport_uri,
249 PlayerStats_GameReport_Handler,
255 PlayerStats_GameReport_DelayMapVote = false;
256 db_close(PS_GR_OUT_DB);
261 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
263 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
265 PS_GR_OUT_DB = db_create();
267 if(PS_GR_OUT_DB >= 0)
269 PlayerStats_GameReport_DelayMapVote = true;
271 serverflags |= SERVERFLAG_PLAYERSTATS;
273 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
274 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
275 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
276 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
277 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
278 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
279 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
280 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
283 FOREACH(Weapons, it != WEP_Null, {
284 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
285 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
286 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
287 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
288 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
291 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
292 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
293 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
294 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
295 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
296 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
297 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
298 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
299 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
300 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
302 anticheat_register_to_playerstats();
304 else { PlayerStats_GameReport_DelayMapVote = false; }
307 // this... is a hack, a temporary one until we get a proper duel gametype
308 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
309 string PlayerStats_GetGametype()
311 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
313 // probably duel, but let's make sure
315 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
319 return GetGametype();
322 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
332 // ======================================
333 // -- OUTGOING GAME REPORT INFORMATION --
334 // ======================================
336 * V: format version (always a fixed number) - this MUST be the first line!
337 * #: comment (MUST be ignored by any parser)
338 * R: release information on the server
340 * O: mod name (icon request) as in server browser
342 * I: match ID (see "matchid" in world.qc)
343 * S: "hostname" of the server
344 * C: number of "unpure" cvar changes
345 * U: UDP port number of the server
346 * D: duration of the match
347 * L: "ladder" in which the server is participating in
348 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
349 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
351 * n: nickname of the player (optional)
353 * e: followed by an event name, a space, and the event count/score
354 * event names can be:
355 * alivetime: total playing time of the player
356 * avglatency: average network latency compounded throughout the match
357 * wins: number of games won (can only be set if matches is set)
358 * matches: number of matches played to the end (not aborted by map switch)
359 * joins: number of matches joined (always 1 unless player never played during the match)
360 * scoreboardvalid: set to 1 if the player was there at the end of the match
361 * total-<scoreboardname>: total score of that scoreboard item
362 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
363 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
364 * kills-<index>: number of kills against the indexed player
365 * rank <number>: rank of player
366 * acc-<weapon netname>-hit: total damage dealt
367 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
368 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
369 * acc-<weapon netname>-cnt-fired: amount of fired shots
370 * acc-<weapon netname>-frags: amount of frags dealt by weapon
372 case URL_READY_CANWRITE:
374 url_fputs(fh, "V 9\n");
376 url_fputs(fh, sprintf("R %s\n", WATERMARK));
378 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
379 url_fputs(fh, sprintf("O %s\n", modname));
380 url_fputs(fh, sprintf("M %s\n", GetMapname()));
381 url_fputs(fh, sprintf("I %s\n", matchid));
382 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
383 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
384 url_fputs(fh, sprintf("U %d\n", cvar("port")));
385 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
386 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
391 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
393 // start team section
394 url_fputs(fh, sprintf("Q team#%s\n", t));
396 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
397 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
399 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
400 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
406 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
408 // start player section
409 url_fputs(fh, sprintf("P %s\n", p));
411 // playerid/index (entity number for this server)
412 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
413 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
416 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
417 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
419 // team identification number
422 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
423 url_fputs(fh, sprintf("t %s\n", tt));
426 // elo ranking enabled
427 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
428 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
430 // output player events
431 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
433 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
434 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
442 // ======================================
443 // -- INCOMING GAME REPORT INFORMATION --
444 // ======================================
448 case URL_READY_CANREAD:
450 // url_fclose is processing, we got a response for writing the data
451 // this must come from HTTP
452 LOG_DEBUG("Got response from player stats server:");
453 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
454 LOG_DEBUG("End of response.");
459 case URL_READY_CLOSED:
461 // url_fclose has finished
462 LOG_DEBUG("Player stats written");
463 PlayerStats_GameReport_DelayMapVote = false;
464 if(PS_GR_OUT_DB >= 0)
466 db_close(PS_GR_OUT_DB);
472 case URL_READY_ERROR:
475 LOG_INFO("Player stats writing failed: ", ftos(status));
476 PlayerStats_GameReport_DelayMapVote = false;
477 if(PS_GR_OUT_DB >= 0)
479 db_close(PS_GR_OUT_DB);
487 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
489 GameRules_scoring_add(joiningplayer, ELO, -1);
490 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
491 if(autocvar_g_playerstats_playerbasic_uri != "")
493 string uri = autocvar_g_playerstats_playerbasic_uri;
494 if (joiningplayer.crypto_idfp == "") {
495 GameRules_scoring_add(joiningplayer, ELO, -1);
497 // create the database if it doesn't already exist
499 PS_B_IN_DB = db_create();
501 // now request the information
502 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
503 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
507 PlayerStats_PlayerBasic_Handler,
511 // set status appropriately // todo: check whether the player info exists in the database previously
514 // database still contains useful information, so don't clear it of a useful status
515 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
519 // database was previously empty or never hit received status for some reason
520 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
526 // server has this disabled, kill the DB and set status to idle
527 GameRules_scoring_add(joiningplayer, ELO, -1);
530 db_close(PS_B_IN_DB);
533 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
538 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
542 db_close(PS_B_IN_DB);
546 if(PS_GR_OUT_DB >= 0)
548 db_close(PS_GR_OUT_DB);
553 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
555 // determine whether we should retrieve playerbasic information again
557 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
558 joiningplayer.netname,
562 // TODO: check to see if this playerid is inside the database already somehow...
563 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
564 // although maybe we should just submit another request ANYWAY?
565 if(!joiningplayer.playerstats_basicstatus)
567 PlayerStats_PlayerBasic(
569 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
574 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
578 case URL_READY_CANWRITE:
580 LOG_DEBUG("-- Sending data to player stats server");
581 /*url_fputs(fh, "V 1\n");
583 url_fputs(fh, sprintf("R %s\n", WATERMARK));
585 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
586 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
587 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
588 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
589 */url_fputs(fh, "\n");
594 case URL_READY_CANREAD:
596 bool handled = false;
597 string gt = string_null;
598 for (string s = ""; (s = url_fgets(fh)); ) {
599 int n = tokenizebyseparator(s, " "); // key value? data
600 if (n == 1) continue;
601 string key = "", value = "", data = "";
612 // PlayerInfo_AddItem(p, "_version", data);
615 // PlayerInfo_AddItem(p, "_release", data);
618 // PlayerInfo_AddItem(p, "_time", data);
621 // PlayerInfo_AddItem(p, "_statsurl", data);
624 // PlayerInfo_AddItem(p, "_hashkey", data);
627 // PlayerInfo_AddItem(p, "_playernick", data);
630 // PlayerInfo_AddItem(p, "_playerid", data);
631 // p.xonstat_id = stof(data);
637 //LOG_TRACE("G: ", gt);
638 //LOG_TRACE("e: ", data);
639 if (gt == PlayerStats_GetGametype()) {
641 float e = stof(data);
642 GameRules_scoring_add(p, ELO, +1 + e);
645 // PlayerInfo_AddItem(p, value, data);
647 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
656 case URL_READY_CLOSED:
658 // url_fclose has finished
659 LOG_INFO("Player stats synchronized with server");
663 case URL_READY_ERROR:
666 LOG_INFO("Receiving player stats failed: ", ftos(status));
670 GameRules_scoring_add(p, ELO, -1);
677 #if 0 // reading the entire DB at once
678 string e = "", en = "";
680 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
682 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
687 void PlayerStats_PlayerDetail_AddItem(string event, string data)
689 if(PS_D_IN_DB < 0) { return; }
691 // create a marker for the event so that we can access it later
692 string marker = sprintf("%s", event);
693 if(db_get(PS_D_IN_DB, marker) == "")
697 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
698 strunzone(PS_D_IN_EVL);
700 else { db_put(PS_D_IN_DB, marker, "#"); }
701 PS_D_IN_EVL = strzone(marker);
704 // now actually set the event data
705 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
706 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
709 void PlayerStats_PlayerDetail()
711 // http://stats.xonotic.org/player/me
712 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
714 // create the database if it doesn't already exist
716 PS_D_IN_DB = db_create();
718 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
719 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
721 autocvar_g_playerstats_playerdetail_uri,
723 PlayerStats_PlayerDetail_Handler,
727 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
731 // player has this disabled, kill the DB and set status to idle
734 db_close(PS_D_IN_DB);
738 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
742 void PlayerStats_PlayerDetail_CheckUpdate()
744 // determine whether we should retrieve playerdetail information again
745 float gamecount = cvar("cl_matchcount");
748 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
757 (time >= PS_D_NEXTUPDATETIME)
759 (gamecount > PS_D_LASTGAMECOUNT)
762 PlayerStats_PlayerDetail();
763 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
764 PS_D_LASTGAMECOUNT = gamecount;
768 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
772 case URL_READY_CANWRITE:
774 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
775 url_fputs(fh, "V 1\n");
777 url_fputs(fh, sprintf("R %s\n", WATERMARK));
779 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
780 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
781 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
782 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
788 case URL_READY_CANREAD:
790 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
792 string gametype = "overall";
793 while((input = url_fgets(fh)))
795 float count = tokenizebyseparator(input, " ");
796 string key = "", event = "", data = "";
798 if(argv(0) == "#") { continue; }
803 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
809 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
816 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
817 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
818 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
821 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
822 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
823 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
824 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
827 case "G": gametype = data; break;
830 if(event != "" && data != "")
832 PlayerStats_PlayerDetail_AddItem(
847 "PlayerStats_PlayerDetail_Handler(): ERROR: "
848 "Key went unhandled? Is our version outdated?\n"
849 "PlayerStats_PlayerDetail_Handler(): "
850 "Key '%s', Event '%s', Data '%s'",
861 "PlayerStats_PlayerDetail_Handler(): "
862 "Key '%s', Event '%s', Data '%s'",
869 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
871 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
872 statslist.getStats(statslist);
876 case URL_READY_CLOSED:
878 // url_fclose has finished
879 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
883 case URL_READY_ERROR:
886 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
887 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
890 db_close(PS_D_IN_DB);
900 void PlayerInfo_AddPlayer(entity e)
902 if(playerinfo_db < 0)
906 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
909 p = db_get(playerinfo_db, key);
914 db_put(playerinfo_db, key, playerinfo_last);
915 strunzone(playerinfo_last);
918 db_put(playerinfo_db, key, "#");
919 playerinfo_last = strzone(ftos(e.playerid));
920 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
924 void PlayerInfo_AddItem(entity e, string item_id, string val)
926 if(playerinfo_db < 0)
930 key = sprintf("*:%s", item_id);
933 p = db_get(playerinfo_db, key);
936 if(playerinfo_events_last)
938 db_put(playerinfo_db, key, playerinfo_events_last);
939 strunzone(playerinfo_events_last);
942 db_put(playerinfo_db, key, "#");
943 playerinfo_events_last = strzone(item_id);
946 key = sprintf("#%d:%s", e.playerid, item_id);
947 db_put(playerinfo_db, key, val);
948 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
951 string PlayerInfo_GetItem(entity e, string item_id)
953 if(playerinfo_db < 0)
957 key = sprintf("#%d:%s", e.playerid, item_id);
958 return db_get(playerinfo_db, key);
961 string PlayerInfo_GetItemLocal(string item_id)
965 return PlayerInfo_GetItem(p, item_id);
968 void PlayerInfo_ready(entity fh, entity p, float status)
973 PlayerInfo_AddPlayer(p);
977 case URL_READY_CANWRITE:
978 print("-- Sending data to player stats server\n");
979 url_fputs(fh, "V 1\n");
981 url_fputs(fh, sprintf("R %s\n", WATERMARK));
984 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
985 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
986 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
987 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
992 case URL_READY_CANREAD:
993 print("-- Got response from player stats server:\n");
994 string gametype = string_null;
995 while((s = url_fgets(fh)))
999 string key = "", value = "", data = "";
1001 n = tokenizebyseparator(s, " "); // key (value) data
1021 else if (key == "V")
1022 PlayerInfo_AddItem(p, "_version", data);
1023 else if (key == "R")
1024 PlayerInfo_AddItem(p, "_release", data);
1025 else if (key == "T")
1026 PlayerInfo_AddItem(p, "_time", data);
1027 else if (key == "S")
1028 PlayerInfo_AddItem(p, "_statsurl", data);
1029 else if (key == "P")
1030 PlayerInfo_AddItem(p, "_hashkey", data);
1031 else if (key == "n")
1032 PlayerInfo_AddItem(p, "_playernick", data);
1033 else if (key == "i")
1034 PlayerInfo_AddItem(p, "_playerid", data);
1035 else if (key == "G")
1037 else if (key == "e" && value != "")
1040 PlayerInfo_AddItem(p, value, data);
1042 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1047 print("-- End of response.\n");
1050 case URL_READY_CLOSED:
1051 // url_fclose has finished
1052 print("Player stats synchronized with server\n");
1054 case URL_READY_ERROR:
1056 print("Receiving player stats failed: ", ftos(status), "\n");
1061 void PlayerInfo_Init()
1063 playerinfo_db = db_create();
1067 void PlayerInfo_Basic(entity p)
1069 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1071 if(playerinfo_db < 0)
1075 uri = autocvar_g_playerinfo_uri;
1076 if(uri != "" && p.crypto_idfp != "")
1078 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1079 print("Retrieving playerstats from URL: ", uri, "\n");
1080 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1086 void PlayerInfo_Details()
1088 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1090 if(playerinfo_db < 0)
1094 uri = autocvar_g_playerinfo_uri; // FIXME
1095 if(uri != "" && crypto_getmyidstatus(0) > 0)
1097 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1098 uri = strcat(uri, "/player/me");
1099 print("Retrieving playerstats from URL: ", uri, "\n");
1100 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1106 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1107 void PlayerInfo_Details()
1109 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1111 if(playerinfo_db < 0)
1115 uri = autocvar_g_playerinfo_uri; // FIXME
1116 if(uri != "" && crypto_getmyidstatus(0) > 0)
1118 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1119 print("Retrieving playerstats from URL: ", uri, "\n");
1120 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);