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 == "" || warmup_stage)
263 PlayerStats_GameReport_DelayMapVote = false;
264 db_close(PS_GR_OUT_DB);
269 PlayerStats_GameReport_DelayMapVote = true;
271 autocvar_g_playerstats_gamereport_uri,
273 PlayerStats_GameReport_Handler,
278 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
280 serverflags &= ~SERVERFLAG_PLAYERSTATS;
281 serverflags &= ~SERVERFLAG_PLAYERSTATS_CUSTOM;
283 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
285 PS_GR_OUT_DB = db_create();
287 if(PS_GR_OUT_DB >= 0)
289 PlayerStats_GameReport_DelayMapVote = true;
291 if(autocvar_g_playerstats_gamereport_uri != cvar_defstring("g_playerstats_gamereport_uri"))
292 serverflags |= SERVERFLAG_PLAYERSTATS | SERVERFLAG_PLAYERSTATS_CUSTOM;
293 else if(checkextension("DP_CRYPTO")) // xonstat requires player and server IDs
294 serverflags |= SERVERFLAG_PLAYERSTATS;
296 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
297 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
298 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
299 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
300 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
301 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
302 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
303 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
306 FOREACH(Weapons, it != WEP_Null, {
307 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
308 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
309 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
310 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
311 PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
314 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
315 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
316 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
317 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
318 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
319 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
320 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
321 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
322 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
323 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
325 anticheat_register_to_playerstats();
327 else { PlayerStats_GameReport_DelayMapVote = false; }
330 // this... is a hack, a temporary one until we get a proper duel gametype
331 // TODO: remove duel hack after servers have migrated to the proper duel gametype!
332 string PlayerStats_GetGametype()
334 if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
336 // probably duel, but let's make sure
338 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
342 return GetGametype();
345 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
355 // ======================================
356 // -- OUTGOING GAME REPORT INFORMATION --
357 // ======================================
359 * V: format version (always a fixed number) - this MUST be the first line!
360 * #: comment (MUST be ignored by any parser)
361 * R: release information on the server
363 * O: mod name (icon request) as in server browser
365 * I: match ID (see "matchid" in world.qc)
366 * S: "hostname" of the server
367 * C: number of "unpure" cvar changes
368 * U: UDP port number of the server
369 * D: duration of the match
370 * RP: number of rounds played
371 * L: "ladder" in which the server is participating in
372 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
373 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
375 * n: nickname of the player (optional)
377 * r: player ranking enabled / disabled
378 * e: followed by an event name, a space, and the event count/score
379 * event names can be:
380 * alivetime: total playing time of the player
381 * avglatency: average network latency compounded throughout the match
382 * wins: number of games won (can only be set if matches is set)
383 * matches: number of matches played to the end (not aborted by map switch)
384 * joins: number of matches joined (always 1 unless player never played during the match)
385 * scoreboardvalid: set to 1 if the player was there at the end of the match
386 * total-<scoreboardname>: total score of that scoreboard item
387 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
388 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
389 * kills-<index>: number of kills against the indexed player
390 * rank <number>: rank of player
391 * acc-<weapon netname>-hit: total damage dealt
392 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
393 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
394 * acc-<weapon netname>-cnt-fired: amount of fired shots
395 * acc-<weapon netname>-frags: amount of frags dealt by weapon
397 case URL_READY_CANWRITE:
399 url_fputs(fh, "V 9\n");
401 url_fputs(fh, sprintf("R %s\n", WATERMARK));
403 url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
404 url_fputs(fh, sprintf("O %s\n", modname));
405 url_fputs(fh, sprintf("M %s\n", GetMapname()));
406 url_fputs(fh, sprintf("I %s\n", matchid));
407 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
408 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
409 url_fputs(fh, sprintf("U %d\n", cvar("port")));
410 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
411 if (rounds_played > 0)
412 url_fputs(fh, sprintf("RP %d\n", rounds_played));
413 if (autocvar_g_playerstats_gamereport_ladder != "")
414 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
419 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
421 // start team section
422 url_fputs(fh, sprintf("Q team#%s\n", t));
424 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
425 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
427 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
428 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
434 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
436 // start player section
437 url_fputs(fh, sprintf("P %s\n", p));
439 // playerid/index (entity number for this server)
440 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
441 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
444 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
445 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
447 // team identification number
450 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
451 url_fputs(fh, sprintf("t %s\n", tt));
454 // elo ranking enabled
455 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_ranked", p));
456 if(nn != "") { url_fputs(fh, sprintf("r %s\n", nn)); }
458 // output player events
459 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
461 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
462 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
470 // ======================================
471 // -- INCOMING GAME REPORT INFORMATION --
472 // ======================================
476 case URL_READY_CANREAD:
478 // url_fclose is processing, we got a response for writing the data
479 // this must come from HTTP
480 LOG_DEBUG("Got response from player stats server:");
481 while((s = url_fgets(fh))) { LOG_DEBUG(" ", s); }
482 LOG_DEBUG("End of response.");
487 case URL_READY_CLOSED:
489 // url_fclose has finished
490 LOG_DEBUG("Player stats written");
491 PlayerStats_GameReport_DelayMapVote = false;
492 if(PS_GR_OUT_DB >= 0)
494 db_close(PS_GR_OUT_DB);
500 case URL_READY_ERROR:
503 LOG_INFO("Player stats writing failed: ", ftos(status));
504 PlayerStats_GameReport_DelayMapVote = false;
505 if(PS_GR_OUT_DB >= 0)
507 db_close(PS_GR_OUT_DB);
515 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
517 GameRules_scoring_add(joiningplayer, ELO, -1);
518 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
519 if(autocvar_g_playerstats_playerbasic_uri != "")
521 string uri = autocvar_g_playerstats_playerbasic_uri;
522 if (joiningplayer.crypto_idfp == "") {
523 GameRules_scoring_add(joiningplayer, ELO, -1);
525 // create the database if it doesn't already exist
527 PS_B_IN_DB = db_create();
529 // now request the information
530 uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
531 LOG_DEBUG("Retrieving playerstats from URL: ", uri);
535 PlayerStats_PlayerBasic_Handler,
539 // set status appropriately // todo: check whether the player info exists in the database previously
542 // database still contains useful information, so don't clear it of a useful status
543 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
547 // database was previously empty or never hit received status for some reason
548 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
554 // server has this disabled, kill the DB and set status to idle
555 GameRules_scoring_add(joiningplayer, ELO, -1);
558 db_close(PS_B_IN_DB);
561 FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
566 SHUTDOWN(PlayerStats_PlayerBasic_Shutdown)
570 db_close(PS_B_IN_DB);
574 if(PS_GR_OUT_DB >= 0)
576 db_close(PS_GR_OUT_DB);
581 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
583 // determine whether we should retrieve playerbasic information again
585 LOG_DEBUGF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
586 joiningplayer.netname,
590 // TODO: check to see if this playerid is inside the database already somehow...
591 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
592 // although maybe we should just submit another request ANYWAY?
593 if(!joiningplayer.playerstats_basicstatus)
595 PlayerStats_PlayerBasic(
597 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
602 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
606 case URL_READY_CANWRITE:
608 LOG_DEBUG("-- Sending data to player stats server");
609 /*url_fputs(fh, "V 1\n");
611 url_fputs(fh, sprintf("R %s\n", WATERMARK));
613 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
614 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
615 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
616 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
617 */url_fputs(fh, "\n");
622 case URL_READY_CANREAD:
624 bool handled = false;
625 string gt = string_null;
626 for (string s = ""; (s = url_fgets(fh)); ) {
627 int n = tokenizebyseparator(s, " "); // key value? data
628 if (n == 1) continue;
629 string key = "", value = "", data = "";
640 // PlayerInfo_AddItem(p, "_version", data);
643 // PlayerInfo_AddItem(p, "_release", data);
646 // PlayerInfo_AddItem(p, "_time", data);
649 // PlayerInfo_AddItem(p, "_statsurl", data);
652 // PlayerInfo_AddItem(p, "_hashkey", data);
655 // PlayerInfo_AddItem(p, "_playernick", data);
658 // PlayerInfo_AddItem(p, "_playerid", data);
659 // p.xonstat_id = stof(data);
665 //LOG_TRACE("G: ", gt);
666 //LOG_TRACE("e: ", data);
667 if (gt == PlayerStats_GetGametype()) {
669 float e = stof(data);
670 GameRules_scoring_add(p, ELO, +1 + e);
673 // PlayerInfo_AddItem(p, value, data);
675 // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
684 case URL_READY_CLOSED:
686 // url_fclose has finished
687 LOG_INFO("Player stats synchronized with server");
691 case URL_READY_ERROR:
694 LOG_INFO("Receiving player stats failed: ", ftos(status));
698 GameRules_scoring_add(p, ELO, -1);
705 #if 0 // reading the entire DB at once
706 string e = "", en = "";
708 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
710 LOG_INFOF("%d:%s:%s", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e)));
715 void PlayerStats_PlayerDetail_AddItem(string event, string data)
717 if(PS_D_IN_DB < 0) { return; }
719 // create a marker for the event so that we can access it later
720 string marker = sprintf("%s", event);
721 if(db_get(PS_D_IN_DB, marker) == "")
725 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
726 strunzone(PS_D_IN_EVL);
728 else { db_put(PS_D_IN_DB, marker, "#"); }
729 PS_D_IN_EVL = strzone(marker);
732 // now actually set the event data
733 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
734 LOG_DEBUG("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
737 void PlayerStats_PlayerDetail()
739 // http://stats.xonotic.org/player/me
740 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
742 // create the database if it doesn't already exist
744 PS_D_IN_DB = db_create();
746 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
747 LOG_DEBUG("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
749 autocvar_g_playerstats_playerdetail_uri,
751 PlayerStats_PlayerDetail_Handler,
755 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
759 // player has this disabled, kill the DB and set status to idle
762 db_close(PS_D_IN_DB);
766 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
770 void PlayerStats_PlayerDetail_CheckUpdate()
772 // determine whether we should retrieve playerdetail information again
773 float gamecount = cvar("cl_matchcount");
776 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d",
785 (time >= PS_D_NEXTUPDATETIME)
787 (gamecount > PS_D_LASTGAMECOUNT)
790 PlayerStats_PlayerDetail();
791 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
792 PS_D_LASTGAMECOUNT = gamecount;
796 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
800 case URL_READY_CANWRITE:
802 LOG_DEBUG("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
803 url_fputs(fh, "V 1\n");
805 url_fputs(fh, sprintf("R %s\n", WATERMARK));
807 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
808 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
809 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
810 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
816 case URL_READY_CANREAD:
818 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
820 string gametype = "overall";
821 while((input = url_fgets(fh)))
823 float count = tokenizebyseparator(input, " ");
824 string key = "", event = "", data = "";
826 if(argv(0) == "#") { continue; }
831 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
837 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
844 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
845 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
846 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
849 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
850 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
851 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
852 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
855 case "G": gametype = data; break;
858 if(event != "" && data != "")
860 PlayerStats_PlayerDetail_AddItem(
875 "PlayerStats_PlayerDetail_Handler(): ERROR: "
876 "Key went unhandled? Is our version outdated?\n"
877 "PlayerStats_PlayerDetail_Handler(): "
878 "Key '%s', Event '%s', Data '%s'",
889 "PlayerStats_PlayerDetail_Handler(): "
890 "Key '%s', Event '%s', Data '%s'",
897 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
899 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
900 statslist.getStats(statslist);
904 case URL_READY_CLOSED:
906 // url_fclose has finished
907 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.");
911 case URL_READY_ERROR:
914 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status));
915 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
918 db_close(PS_D_IN_DB);
928 void PlayerInfo_AddPlayer(entity e)
930 if(playerinfo_db < 0)
934 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
937 p = db_get(playerinfo_db, key);
942 db_put(playerinfo_db, key, playerinfo_last);
943 strunzone(playerinfo_last);
946 db_put(playerinfo_db, key, "#");
947 playerinfo_last = strzone(ftos(e.playerid));
948 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
952 void PlayerInfo_AddItem(entity e, string item_id, string val)
954 if(playerinfo_db < 0)
958 key = sprintf("*:%s", item_id);
961 p = db_get(playerinfo_db, key);
964 if(playerinfo_events_last)
966 db_put(playerinfo_db, key, playerinfo_events_last);
967 strunzone(playerinfo_events_last);
970 db_put(playerinfo_db, key, "#");
971 playerinfo_events_last = strzone(item_id);
974 key = sprintf("#%d:%s", e.playerid, item_id);
975 db_put(playerinfo_db, key, val);
976 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
979 string PlayerInfo_GetItem(entity e, string item_id)
981 if(playerinfo_db < 0)
985 key = sprintf("#%d:%s", e.playerid, item_id);
986 return db_get(playerinfo_db, key);
989 string PlayerInfo_GetItemLocal(string item_id)
993 return PlayerInfo_GetItem(p, item_id);
996 void PlayerInfo_ready(entity fh, entity p, float status)
1001 PlayerInfo_AddPlayer(p);
1005 case URL_READY_CANWRITE:
1006 print("-- Sending data to player stats server\n");
1007 url_fputs(fh, "V 1\n");
1009 url_fputs(fh, sprintf("R %s\n", WATERMARK));
1012 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
1013 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
1014 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
1015 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
1017 url_fputs(fh, "\n");
1020 case URL_READY_CANREAD:
1021 print("-- Got response from player stats server:\n");
1022 string gametype = string_null;
1023 while((s = url_fgets(fh)))
1025 print(" ", s, "\n");
1027 string key = "", value = "", data = "";
1029 n = tokenizebyseparator(s, " "); // key (value) data
1049 else if (key == "V")
1050 PlayerInfo_AddItem(p, "_version", data);
1051 else if (key == "R")
1052 PlayerInfo_AddItem(p, "_release", data);
1053 else if (key == "T")
1054 PlayerInfo_AddItem(p, "_time", data);
1055 else if (key == "S")
1056 PlayerInfo_AddItem(p, "_statsurl", data);
1057 else if (key == "P")
1058 PlayerInfo_AddItem(p, "_hashkey", data);
1059 else if (key == "n")
1060 PlayerInfo_AddItem(p, "_playernick", data);
1061 else if (key == "i")
1062 PlayerInfo_AddItem(p, "_playerid", data);
1063 else if (key == "G")
1065 else if (key == "e" && value != "")
1068 PlayerInfo_AddItem(p, value, data);
1070 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
1075 print("-- End of response.\n");
1078 case URL_READY_CLOSED:
1079 // url_fclose has finished
1080 print("Player stats synchronized with server\n");
1082 case URL_READY_ERROR:
1084 print("Receiving player stats failed: ", ftos(status), "\n");
1089 void PlayerInfo_Init()
1091 playerinfo_db = db_create();
1095 void PlayerInfo_Basic(entity p)
1097 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1099 if(playerinfo_db < 0)
1103 uri = autocvar_g_playerinfo_uri;
1104 if(uri != "" && p.crypto_idfp != "")
1106 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1107 print("Retrieving playerstats from URL: ", uri, "\n");
1108 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1114 void PlayerInfo_Details()
1116 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1118 if(playerinfo_db < 0)
1122 uri = autocvar_g_playerinfo_uri; // FIXME
1123 if(uri != "" && crypto_getmyidstatus(0) > 0)
1125 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1126 uri = strcat(uri, "/player/me");
1127 print("Retrieving playerstats from URL: ", uri, "\n");
1128 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1134 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1135 void PlayerInfo_Details()
1137 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1139 if(playerinfo_db < 0)
1143 uri = autocvar_g_playerinfo_uri; // FIXME
1144 if(uri != "" && crypto_getmyidstatus(0) > 0)
1146 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1147 print("Retrieving playerstats from URL: ", uri, "\n");
1148 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);