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);
211 // if previous average latency exists (player disconnected and reconnected)
212 // make the average of previous and current average latency
213 float prev_latency = PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, 0);
214 float new_latency = !prev_latency ? latency : (prev_latency + latency) / 2;
215 PlayerStats_GameReport_Event_Player(p, PLAYERSTATS_AVGLATENCY, -prev_latency + new_latency);
219 db_put(PS_GR_OUT_DB, sprintf("%s:_ranked", p.playerstats_id), ftos(CS_CVAR(p).cvar_cl_allow_uidranking));
222 strfree(p.playerstats_id);
225 void PlayerStats_GameReport(bool finished)
227 if(PS_GR_OUT_DB < 0) { return; }
229 PlayerScore_Sort(score_dummyfield, 0, false, false);
230 PlayerScore_Sort(scoreboard_pos, 1, true, true);
231 if(teamplay) { PlayerScore_TeamStats(); }
233 FOREACH_CLIENT(true, {
234 // add personal score rank
235 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_RANK, it.score_dummyfield);
238 if(it.scoreboard_pos)
240 // scoreboard is valid!
241 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
243 // add scoreboard position
244 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
246 // add scoreboard data
247 PlayerScore_PlayerStats(it);
249 // if the match ended normally, add winning info
252 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_WINS, it.winning);
253 PlayerStats_GameReport_Event_Player(it, PLAYERSTATS_MATCHES, 1);
257 // collect final player information
258 PlayerStats_GameReport_FinalizePlayer(it);
261 if(autocvar_g_playerstats_gamereport_uri != "")
263 PlayerStats_GameReport_DelayMapVote = true;
265 autocvar_g_playerstats_gamereport_uri,
267 PlayerStats_GameReport_Handler,
273 PlayerStats_GameReport_DelayMapVote = false;
274 db_close(PS_GR_OUT_DB);
279 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
281 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
283 PS_GR_OUT_DB = db_create();
285 if(PS_GR_OUT_DB >= 0)
287 PlayerStats_GameReport_DelayMapVote = true;
289 serverflags |= SERVERFLAG_PLAYERSTATS;
290 if(autocvar_g_playerstats_gamereport_uri != cvar_defstring("g_playerstats_gamereport_uri"))
292 serverflags |= SERVERFLAG_PLAYERSTATS_CUSTOM;
295 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
296 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
297 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
298 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
299 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
300 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
301 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
302 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
305 FOREACH(Weapons, it != WEP_Null, {
306 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
307 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
308 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
309 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
310 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
313 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
314 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
315 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
316 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
317 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
318 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
319 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
320 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
321 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
322 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
324 anticheat_register_to_playerstats();
326 else { PlayerStats_GameReport_DelayMapVote = false; }
329 // this... is a hack, a temporary one until we get a proper duel gametype
330 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
331 string PlayerStats_GetGametype()
333 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
335 // probably duel, but let's make sure
337 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
341 return GetGametype();
344 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
354 // ======================================
355 // -- OUTGOING GAME REPORT INFORMATION --
356 // ======================================
358 * V: format version (always a fixed number) - this MUST be the first line!
359 * #: comment (MUST be ignored by any parser)
360 * R: release information on the server
362 * O: mod name (icon request) as in server browser
364 * I: match ID (see "matchid" in world.qc)
365 * S: "hostname" of the server
366 * C: number of "unpure" cvar changes
367 * U: UDP port number of the server
368 * D: duration of the match
369 * L: "ladder" in which the server is participating in
370 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
371 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
373 * n: nickname of the player (optional)
375 * r: player ranking enabled / disabled
376 * e: followed by an event name, a space, and the event count/score
377 * event names can be:
378 * alivetime: total playing time of the player
379 * avglatency: average network latency compounded throughout the match
380 * wins: number of games won (can only be set if matches is set)
381 * matches: number of matches played to the end (not aborted by map switch)
382 * joins: number of matches joined (always 1 unless player never played during the match)
383 * scoreboardvalid: set to 1 if the player was there at the end of the match
384 * total-<scoreboardname>: total score of that scoreboard item
385 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
386 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
387 * kills-<index>: number of kills against the indexed player
388 * rank <number>: rank of player
389 * acc-<weapon netname>-hit: total damage dealt
390 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
391 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
392 * acc-<weapon netname>-cnt-fired: amount of fired shots
393 * acc-<weapon netname>-frags: amount of frags dealt by weapon
395 case URL_READY_CANWRITE:
397 url_fputs(fh, "V 9\n");
399 url_fputs(fh, sprintf("R %s\n", WATERMARK));
401 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
402 url_fputs(fh, sprintf("O %s\n", modname));
403 url_fputs(fh, sprintf("M %s\n", GetMapname()));
404 url_fputs(fh, sprintf("I %s\n", matchid));
405 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
406 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
407 url_fputs(fh, sprintf("U %d\n", cvar("port")));
408 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
409 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
414 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
416 // start team section
417 url_fputs(fh, sprintf("Q team#%s\n", t));
419 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
420 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
422 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
423 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
429 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
431 // start player section
432 url_fputs(fh, sprintf("P %s\n", p));
434 // playerid/index (entity number for this server)
435 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
436 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
439 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
440 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
442 // team identification number
445 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
446 url_fputs(fh, sprintf("t %s\n", tt));
449 // elo ranking enabled
450 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
451 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
453 // output player events
454 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
456 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
457 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
465 // ======================================
466 // -- INCOMING GAME REPORT INFORMATION --
467 // ======================================
471 case URL_READY_CANREAD:
473 // url_fclose is processing, we got a response for writing the data
474 // this must come from HTTP
475 LOG_DEBUG("Got response from player stats server:");
476 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
477 LOG_DEBUG("End of response.");
482 case URL_READY_CLOSED:
484 // url_fclose has finished
485 LOG_DEBUG("Player stats written");
486 PlayerStats_GameReport_DelayMapVote = false;
487 if(PS_GR_OUT_DB >= 0)
489 db_close(PS_GR_OUT_DB);
495 case URL_READY_ERROR:
498 LOG_INFO("Player stats writing failed: ", ftos(status));
499 PlayerStats_GameReport_DelayMapVote = false;
500 if(PS_GR_OUT_DB >= 0)
502 db_close(PS_GR_OUT_DB);
510 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
512 GameRules_scoring_add(joiningplayer, ELO, -1);
513 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
514 if(autocvar_g_playerstats_playerbasic_uri != "")
516 string uri = autocvar_g_playerstats_playerbasic_uri;
517 if (joiningplayer.crypto_idfp == "") {
518 GameRules_scoring_add(joiningplayer, ELO, -1);
520 // create the database if it doesn't already exist
522 PS_B_IN_DB = db_create();
524 // now request the information
525 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
526 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
530 PlayerStats_PlayerBasic_Handler,
534 // set status appropriately // todo: check whether the player info exists in the database previously
537 // database still contains useful information, so don't clear it of a useful status
538 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
542 // database was previously empty or never hit received status for some reason
543 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
549 // server has this disabled, kill the DB and set status to idle
550 GameRules_scoring_add(joiningplayer, ELO, -1);
553 db_close(PS_B_IN_DB);
556 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
561 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
565 db_close(PS_B_IN_DB);
569 if(PS_GR_OUT_DB >= 0)
571 db_close(PS_GR_OUT_DB);
576 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
578 // determine whether we should retrieve playerbasic information again
580 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
581 joiningplayer.netname,
585 // TODO: check to see if this playerid is inside the database already somehow...
586 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
587 // although maybe we should just submit another request ANYWAY?
588 if(!joiningplayer.playerstats_basicstatus)
590 PlayerStats_PlayerBasic(
592 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
597 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
601 case URL_READY_CANWRITE:
603 LOG_DEBUG("-- Sending data to player stats server");
604 /*url_fputs(fh, "V 1\n");
606 url_fputs(fh, sprintf("R %s\n", WATERMARK));
608 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
609 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
610 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
611 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
612 */url_fputs(fh, "\n");
617 case URL_READY_CANREAD:
619 bool handled = false;
620 string gt = string_null;
621 for (string s = ""; (s = url_fgets(fh)); ) {
622 int n = tokenizebyseparator(s, " "); // key value? data
623 if (n == 1) continue;
624 string key = "", value = "", data = "";
635 // PlayerInfo_AddItem(p, "_version", data);
638 // PlayerInfo_AddItem(p, "_release", data);
641 // PlayerInfo_AddItem(p, "_time", data);
644 // PlayerInfo_AddItem(p, "_statsurl", data);
647 // PlayerInfo_AddItem(p, "_hashkey", data);
650 // PlayerInfo_AddItem(p, "_playernick", data);
653 // PlayerInfo_AddItem(p, "_playerid", data);
654 // p.xonstat_id = stof(data);
660 //LOG_TRACE("G: ", gt);
661 //LOG_TRACE("e: ", data);
662 if (gt == PlayerStats_GetGametype()) {
664 float e = stof(data);
665 GameRules_scoring_add(p, ELO, +1 + e);
668 // PlayerInfo_AddItem(p, value, data);
670 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
679 case URL_READY_CLOSED:
681 // url_fclose has finished
682 LOG_INFO("Player stats synchronized with server");
686 case URL_READY_ERROR:
689 LOG_INFO("Receiving player stats failed: ", ftos(status));
693 GameRules_scoring_add(p, ELO, -1);
700 #if 0 // reading the entire DB at once
701 string e = "", en = "";
703 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
705 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
710 void PlayerStats_PlayerDetail_AddItem(string event, string data)
712 if(PS_D_IN_DB < 0) { return; }
714 // create a marker for the event so that we can access it later
715 string marker = sprintf("%s", event);
716 if(db_get(PS_D_IN_DB, marker) == "")
720 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
721 strunzone(PS_D_IN_EVL);
723 else { db_put(PS_D_IN_DB, marker, "#"); }
724 PS_D_IN_EVL = strzone(marker);
727 // now actually set the event data
728 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
729 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
732 void PlayerStats_PlayerDetail()
734 // http://stats.xonotic.org/player/me
735 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
737 // create the database if it doesn't already exist
739 PS_D_IN_DB = db_create();
741 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
742 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
744 autocvar_g_playerstats_playerdetail_uri,
746 PlayerStats_PlayerDetail_Handler,
750 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
754 // player has this disabled, kill the DB and set status to idle
757 db_close(PS_D_IN_DB);
761 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
765 void PlayerStats_PlayerDetail_CheckUpdate()
767 // determine whether we should retrieve playerdetail information again
768 float gamecount = cvar("cl_matchcount");
771 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
780 (time >= PS_D_NEXTUPDATETIME)
782 (gamecount > PS_D_LASTGAMECOUNT)
785 PlayerStats_PlayerDetail();
786 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
787 PS_D_LASTGAMECOUNT = gamecount;
791 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
795 case URL_READY_CANWRITE:
797 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
798 url_fputs(fh, "V 1\n");
800 url_fputs(fh, sprintf("R %s\n", WATERMARK));
802 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
803 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
804 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
805 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
811 case URL_READY_CANREAD:
813 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
815 string gametype = "overall";
816 while((input = url_fgets(fh)))
818 float count = tokenizebyseparator(input, " ");
819 string key = "", event = "", data = "";
821 if(argv(0) == "#") { continue; }
826 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
832 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
839 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
840 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
841 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
844 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
845 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
846 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
847 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
850 case "G": gametype = data; break;
853 if(event != "" && data != "")
855 PlayerStats_PlayerDetail_AddItem(
870 "PlayerStats_PlayerDetail_Handler(): ERROR: "
871 "Key went unhandled? Is our version outdated?\n"
872 "PlayerStats_PlayerDetail_Handler(): "
873 "Key '%s', Event '%s', Data '%s'",
884 "PlayerStats_PlayerDetail_Handler(): "
885 "Key '%s', Event '%s', Data '%s'",
892 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
894 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
895 statslist.getStats(statslist);
899 case URL_READY_CLOSED:
901 // url_fclose has finished
902 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
906 case URL_READY_ERROR:
909 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
910 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
913 db_close(PS_D_IN_DB);
923 void PlayerInfo_AddPlayer(entity e)
925 if(playerinfo_db < 0)
929 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
932 p = db_get(playerinfo_db, key);
937 db_put(playerinfo_db, key, playerinfo_last);
938 strunzone(playerinfo_last);
941 db_put(playerinfo_db, key, "#");
942 playerinfo_last = strzone(ftos(e.playerid));
943 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
947 void PlayerInfo_AddItem(entity e, string item_id, string val)
949 if(playerinfo_db < 0)
953 key = sprintf("*:%s", item_id);
956 p = db_get(playerinfo_db, key);
959 if(playerinfo_events_last)
961 db_put(playerinfo_db, key, playerinfo_events_last);
962 strunzone(playerinfo_events_last);
965 db_put(playerinfo_db, key, "#");
966 playerinfo_events_last = strzone(item_id);
969 key = sprintf("#%d:%s", e.playerid, item_id);
970 db_put(playerinfo_db, key, val);
971 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
974 string PlayerInfo_GetItem(entity e, string item_id)
976 if(playerinfo_db < 0)
980 key = sprintf("#%d:%s", e.playerid, item_id);
981 return db_get(playerinfo_db, key);
984 string PlayerInfo_GetItemLocal(string item_id)
988 return PlayerInfo_GetItem(p, item_id);
991 void PlayerInfo_ready(entity fh, entity p, float status)
996 PlayerInfo_AddPlayer(p);
1000 case URL_READY_CANWRITE:
1001 print("-- Sending data to player stats server\n");
1002 url_fputs(fh, "V 1\n");
1004 url_fputs(fh, sprintf("R %s\n", WATERMARK));
1007 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
1008 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
1009 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
1010 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
1012 url_fputs(fh, "\n");
1015 case URL_READY_CANREAD:
1016 print("-- Got response from player stats server:\n");
1017 string gametype = string_null;
1018 while((s = url_fgets(fh)))
1020 print(" ", s, "\n");
1022 string key = "", value = "", data = "";
1024 n = tokenizebyseparator(s, " "); // key (value) data
1044 else if (key == "V")
1045 PlayerInfo_AddItem(p, "_version", data);
1046 else if (key == "R")
1047 PlayerInfo_AddItem(p, "_release", data);
1048 else if (key == "T")
1049 PlayerInfo_AddItem(p, "_time", data);
1050 else if (key == "S")
1051 PlayerInfo_AddItem(p, "_statsurl", data);
1052 else if (key == "P")
1053 PlayerInfo_AddItem(p, "_hashkey", data);
1054 else if (key == "n")
1055 PlayerInfo_AddItem(p, "_playernick", data);
1056 else if (key == "i")
1057 PlayerInfo_AddItem(p, "_playerid", data);
1058 else if (key == "G")
1060 else if (key == "e" && value != "")
1063 PlayerInfo_AddItem(p, value, data);
1065 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1070 print("-- End of response.\n");
1073 case URL_READY_CLOSED:
1074 // url_fclose has finished
1075 print("Player stats synchronized with server\n");
1077 case URL_READY_ERROR:
1079 print("Receiving player stats failed: ", ftos(status), "\n");
1084 void PlayerInfo_Init()
1086 playerinfo_db = db_create();
1090 void PlayerInfo_Basic(entity p)
1092 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1094 if(playerinfo_db < 0)
1098 uri = autocvar_g_playerinfo_uri;
1099 if(uri != "" && p.crypto_idfp != "")
1101 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1102 print("Retrieving playerstats from URL: ", uri, "\n");
1103 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1109 void PlayerInfo_Details()
1111 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1113 if(playerinfo_db < 0)
1117 uri = autocvar_g_playerinfo_uri; // FIXME
1118 if(uri != "" && crypto_getmyidstatus(0) > 0)
1120 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1121 uri = strcat(uri, "/player/me");
1122 print("Retrieving playerstats from URL: ", uri, "\n");
1123 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1129 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1130 void PlayerInfo_Details()
1132 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1134 if(playerinfo_db < 0)
1138 uri = autocvar_g_playerinfo_uri; // FIXME
1139 if(uri != "" && crypto_getmyidstatus(0) > 0)
1141 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1142 print("Retrieving playerstats from URL: ", uri, "\n");
1143 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);