4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "constants.qh"
8 #include "weapons/all.qh"
9 #include "../server/weapons/accuracy.qh"
10 #include "../server/defs.qh"
11 #include "playerstats.qh"
12 #include "../server/scores.qh"
16 void PlayerStats_Prematch(void)
21 void PlayerStats_GameReport_AddPlayer(entity e)
23 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
25 // set up player identification
26 string s = string_null;
28 if((e.crypto_idfp != "") && (e.cvar_cl_allow_uidtracking == 1))
29 { s = e.crypto_idfp; }
30 else if(IS_BOT_CLIENT(e))
31 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
33 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
36 { s = sprintf("bot#%d", e.playerid); }
38 { s = sprintf("player#%d", e.playerid); }
41 e.playerstats_id = strzone(s);
43 // now add the player to the database
44 string key = sprintf("%s:*", e.playerstats_id);
45 string p = db_get(PS_GR_OUT_DB, key);
51 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
52 strunzone(PS_GR_OUT_PL);
54 else { db_put(PS_GR_OUT_DB, key, "#"); }
55 PS_GR_OUT_PL = strzone(e.playerstats_id);
59 void PlayerStats_GameReport_AddTeam(float t)
61 if(PS_GR_OUT_DB < 0) { return; }
63 string key = sprintf("%d", t);
64 string p = db_get(PS_GR_OUT_DB, key);
70 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
71 strunzone(PS_GR_OUT_TL);
73 else { db_put(PS_GR_OUT_DB, key, "#"); }
74 PS_GR_OUT_TL = strzone(key);
78 void PlayerStats_GameReport_AddEvent(string event_id)
80 if(PS_GR_OUT_DB < 0) { return; }
82 string key = sprintf("*:%s", event_id);
83 string p = db_get(PS_GR_OUT_DB, key);
89 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
90 strunzone(PS_GR_OUT_EVL);
92 else { db_put(PS_GR_OUT_DB, key, "#"); }
93 PS_GR_OUT_EVL = strzone(event_id);
97 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
98 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
100 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
102 string key = sprintf("%s:%s", prefix, event_id);
103 float val = stof(db_get(PS_GR_OUT_DB, key));
105 db_put(PS_GR_OUT_DB, key, ftos(val));
109 void PlayerStats_GameReport_Accuracy(entity p)
113 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
115 entity w = get_weaponinfo(i);
117 #define ACCMAC(suffix,field) \
118 PS_GR_P_ADDVAL(p, sprintf("acc-%s-%s", w.netname, suffix), p.accuracy.(field[i-1]));
120 ACCMAC("hit", accuracy_hit)
121 ACCMAC("fired", accuracy_fired)
122 ACCMAC("cnt-hit", accuracy_cnt_hit)
123 ACCMAC("cnt-fired", accuracy_cnt_fired)
124 ACCMAC("frags", accuracy_frags)
130 void PlayerStats_GameReport_FinalizePlayer(entity p)
132 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
137 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
141 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
143 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
144 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), p.netname);
147 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
149 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
150 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
152 PlayerStats_GameReport_Accuracy(p);
154 if(IS_REAL_CLIENT(p))
158 float latency = (p.latency_sum / p.latency_cnt);
159 if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
163 strunzone(p.playerstats_id);
164 p.playerstats_id = string_null;
167 void PlayerStats_GameReport(float finished)
169 if(PS_GR_OUT_DB < 0) { return; }
171 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
172 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
173 if(teamplay) { PlayerScore_TeamStats(); }
178 // add personal score rank
179 PS_GR_P_ADDVAL(p, PLAYERSTATS_RANK, p.score_dummyfield);
184 // scoreboard is valid!
185 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
187 // add scoreboard position
188 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
190 // add scoreboard data
191 PlayerScore_PlayerStats(p);
193 // if the match ended normally, add winning info
196 PS_GR_P_ADDVAL(p, PLAYERSTATS_WINS, p.winning);
197 PS_GR_P_ADDVAL(p, PLAYERSTATS_MATCHES, 1);
201 // collect final player information
202 PlayerStats_GameReport_FinalizePlayer(p);
205 if(autocvar_g_playerstats_gamereport_uri != "")
207 PlayerStats_GameReport_DelayMapVote = true;
209 autocvar_g_playerstats_gamereport_uri,
211 PlayerStats_GameReport_Handler,
217 PlayerStats_GameReport_DelayMapVote = false;
218 db_close(PS_GR_OUT_DB);
223 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
225 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
227 PS_GR_OUT_DB = db_create();
229 if(PS_GR_OUT_DB >= 0)
231 PlayerStats_GameReport_DelayMapVote = true;
233 serverflags |= SERVERFLAG_PLAYERSTATS;
235 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
236 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
237 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
238 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
239 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
240 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
241 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
242 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
247 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
249 w = get_weaponinfo(i);
250 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-hit"));
251 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-fired"));
252 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
253 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
254 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-frags"));
257 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
258 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
259 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
260 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
261 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
262 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
263 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
264 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
265 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
266 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
268 else { PlayerStats_GameReport_DelayMapVote = false; }
271 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
281 // ======================================
282 // -- OUTGOING GAME REPORT INFORMATION --
283 // ======================================
285 * V: format version (always a fixed number) - this MUST be the first line!
286 * #: comment (MUST be ignored by any parser)
287 * R: release information on the server
289 * O: mod name (icon request) as in server browser
291 * I: match ID (see "matchid" in g_world.qc)
292 * S: "hostname" of the server
293 * C: number of "unpure" cvar changes
294 * U: UDP port number of the server
295 * D: duration of the match
296 * L: "ladder" in which the server is participating in
297 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
298 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
300 * n: nickname of the player (optional)
302 * e: followed by an event name, a space, and the event count/score
303 * event names can be:
304 * alivetime: total playing time of the player
305 * avglatency: average network latency compounded throughout the match
306 * wins: number of games won (can only be set if matches is set)
307 * matches: number of matches played to the end (not aborted by map switch)
308 * joins: number of matches joined (always 1 unless player never played during the match)
309 * scoreboardvalid: set to 1 if the player was there at the end of the match
310 * total-<scoreboardname>: total score of that scoreboard item
311 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
312 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
313 * kills-<index>: number of kills against the indexed player
314 * rank <number>: rank of player
315 * acc-<weapon netname>-hit: total damage dealt
316 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
317 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
318 * acc-<weapon netname>-cnt-fired: amount of fired shots
319 * acc-<weapon netname>-frags: amount of frags dealt by weapon
321 case URL_READY_CANWRITE:
323 url_fputs(fh, "V 9\n");
325 url_fputs(fh, sprintf("R %s\n", WATERMARK));
327 url_fputs(fh, sprintf("G %s\n", GetGametype()));
328 url_fputs(fh, sprintf("O %s\n", modname));
329 url_fputs(fh, sprintf("M %s\n", GetMapname()));
330 url_fputs(fh, sprintf("I %s\n", matchid));
331 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
332 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
333 url_fputs(fh, sprintf("U %d\n", cvar("port")));
334 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
335 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
340 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
342 // start team section
343 url_fputs(fh, sprintf("Q team#%s\n", t));
345 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
346 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
348 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
349 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
355 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
357 // start player section
358 url_fputs(fh, sprintf("P %s\n", p));
360 // playerid/index (entity number for this server)
361 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
362 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
365 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
366 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
368 // team identification number
371 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
372 url_fputs(fh, sprintf("t %s\n", tt));
375 // output player events
376 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
378 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
379 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
387 // ======================================
388 // -- INCOMING GAME REPORT INFORMATION --
389 // ======================================
393 case URL_READY_CANREAD:
395 // url_fclose is processing, we got a response for writing the data
396 // this must come from HTTP
397 LOG_TRACE("Got response from player stats server:\n");
398 while((s = url_fgets(fh))) { LOG_TRACE(" ", s, "\n"); }
399 LOG_TRACE("End of response.\n");
404 case URL_READY_CLOSED:
406 // url_fclose has finished
407 LOG_TRACE("Player stats written\n");
408 PlayerStats_GameReport_DelayMapVote = false;
409 if(PS_GR_OUT_DB >= 0)
411 db_close(PS_GR_OUT_DB);
417 case URL_READY_ERROR:
420 LOG_INFO("Player stats writing failed: ", ftos(status), "\n");
421 PlayerStats_GameReport_DelayMapVote = false;
422 if(PS_GR_OUT_DB >= 0)
424 db_close(PS_GR_OUT_DB);
432 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
434 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
435 if(autocvar_g_playerstats_playerbasic_uri != "")
437 string uri = autocvar_g_playerstats_playerbasic_uri;
438 if(joiningplayer.crypto_idfp != "")
440 // create the database if it doesn't already exist
442 PS_B_IN_DB = db_create();
444 // now request the information
445 uri = strcat(uri, "/player/", uri_escape(uri_escape(joiningplayer.crypto_idfp)), "/elo.txt");
446 LOG_TRACE("Retrieving playerstats from URL: ", uri, "\n");
450 PlayerStats_PlayerBasic_Handler,
454 // set status appropriately // todo: check whether the player info exists in the database previously
457 // database still contains useful information, so don't clear it of a useful status
458 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
462 // database was previously empty or never hit received status for some reason
463 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
469 // server has this disabled, kill the DB and set status to idle
474 db_close(PS_B_IN_DB);
477 FOR_EACH_REALCLIENT(player) { player.playerstats_basicstatus = PS_B_STATUS_IDLE; }
482 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
484 // determine whether we should retrieve playerbasic information again
487 LOG_INFOF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f\n",
488 joiningplayer.netname,
493 // TODO: check to see if this playerid is inside the database already somehow...
494 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
495 // although maybe we should just submit another request ANYWAY?
496 if(!joiningplayer.playerstats_basicstatus)
498 PlayerStats_PlayerBasic(
500 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
505 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
509 case URL_READY_CANWRITE:
511 LOG_TRACE("-- Sending data to player stats server\n");
512 /*url_fputs(fh, "V 1\n");
514 url_fputs(fh, sprintf("R %s\n", WATERMARK));
516 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
517 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
518 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
519 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
520 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
521 */url_fputs(fh, "\n");
526 case URL_READY_CANREAD:
529 LOG_TRACE("-- Got response from player stats server:\n");
530 //string gametype = string_null;
531 while((s = url_fgets(fh)))
533 LOG_TRACE(" ", s, "\n");
535 string key = "", value = "", data = "";
537 n = tokenizebyseparator(s, " "); // key (value) data
558 PlayerInfo_AddItem(p, "_version", data);
560 PlayerInfo_AddItem(p, "_release", data);
562 PlayerInfo_AddItem(p, "_time", data);
564 PlayerInfo_AddItem(p, "_statsurl", data);
566 PlayerInfo_AddItem(p, "_hashkey", data);
568 PlayerInfo_AddItem(p, "_playernick", data);
570 PlayerInfo_AddItem(p, "_playerid", data);
573 else if (key == "e" && value != "")
576 PlayerInfo_AddItem(p, value, data);
578 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
584 LOG_TRACE("-- End of response.\n");
588 case URL_READY_CLOSED:
590 // url_fclose has finished
591 LOG_INFO("Player stats synchronized with server\n");
595 case URL_READY_ERROR:
598 LOG_INFO("Receiving player stats failed: ", ftos(status), "\n");
608 #if 0 // reading the entire DB at once
609 string e = "", en = "";
611 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
613 LOG_INFO(sprintf("%d:%s:%s\n", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e))));
618 void PlayerStats_PlayerDetail_AddItem(string event, string data)
620 if(PS_D_IN_DB < 0) { return; }
622 // create a marker for the event so that we can access it later
623 string marker = sprintf("%s", event);
624 if(db_get(PS_D_IN_DB, marker) == "")
628 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
629 strunzone(PS_D_IN_EVL);
631 else { db_put(PS_D_IN_DB, marker, "#"); }
632 PS_D_IN_EVL = strzone(marker);
635 // now actually set the event data
636 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
637 LOG_TRACE("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB\n");
640 void PlayerStats_PlayerDetail(void)
642 // http://stats.xonotic.org/player/me
643 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
645 // create the database if it doesn't already exist
647 PS_D_IN_DB = db_create();
649 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
650 LOG_TRACE("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri, "\n");
652 autocvar_g_playerstats_playerdetail_uri,
654 PlayerStats_PlayerDetail_Handler,
658 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
662 // player has this disabled, kill the DB and set status to idle
665 db_close(PS_D_IN_DB);
669 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
673 void PlayerStats_PlayerDetail_CheckUpdate(void)
675 // determine whether we should retrieve playerdetail information again
676 float gamecount = cvar("cl_matchcount");
679 LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d\n",
688 (time >= PS_D_NEXTUPDATETIME)
690 (gamecount > PS_D_LASTGAMECOUNT)
693 PlayerStats_PlayerDetail();
694 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
695 PS_D_LASTGAMECOUNT = gamecount;
699 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
703 case URL_READY_CANWRITE:
705 LOG_TRACE("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...\n");
706 url_fputs(fh, "V 1\n");
708 url_fputs(fh, sprintf("R %s\n", WATERMARK));
710 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
711 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
712 //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
713 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
714 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
720 case URL_READY_CANREAD:
722 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
724 string gametype = "overall";
725 while((input = url_fgets(fh)))
727 float count = tokenizebyseparator(input, " ");
728 string key = "", event = "", data = "";
730 if(argv(0) == "#") { continue; }
735 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
741 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
748 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
749 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
750 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
753 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
754 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
755 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
756 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
759 case "G": gametype = data; break;
762 if(event != "" && data != "")
764 PlayerStats_PlayerDetail_AddItem(
779 "PlayerStats_PlayerDetail_Handler(): ERROR: "
780 "Key went unhandled? Is our version outdated?\n"
781 "PlayerStats_PlayerDetail_Handler(): "
782 "Key '%s', Event '%s', Data '%s'\n",
793 "PlayerStats_PlayerDetail_Handler(): "
794 "Key '%s', Event '%s', Data '%s'\n",
801 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
803 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
804 statslist.getStats(statslist);
808 case URL_READY_CLOSED:
810 // url_fclose has finished
811 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.\n");
815 case URL_READY_ERROR:
818 LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status), "\n");
819 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
822 db_close(PS_D_IN_DB);
832 void PlayerInfo_AddPlayer(entity e)
834 if(playerinfo_db < 0)
838 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
841 p = db_get(playerinfo_db, key);
846 db_put(playerinfo_db, key, playerinfo_last);
847 strunzone(playerinfo_last);
850 db_put(playerinfo_db, key, "#");
851 playerinfo_last = strzone(ftos(e.playerid));
852 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
856 void PlayerInfo_AddItem(entity e, string item_id, string val)
858 if(playerinfo_db < 0)
862 key = sprintf("*:%s", item_id);
865 p = db_get(playerinfo_db, key);
868 if(playerinfo_events_last)
870 db_put(playerinfo_db, key, playerinfo_events_last);
871 strunzone(playerinfo_events_last);
874 db_put(playerinfo_db, key, "#");
875 playerinfo_events_last = strzone(item_id);
878 key = sprintf("#%d:%s", e.playerid, item_id);
879 db_put(playerinfo_db, key, val);
880 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
883 string PlayerInfo_GetItem(entity e, string item_id)
885 if(playerinfo_db < 0)
889 key = sprintf("#%d:%s", e.playerid, item_id);
890 return db_get(playerinfo_db, key);
893 string PlayerInfo_GetItemLocal(string item_id)
897 return PlayerInfo_GetItem(p, item_id);
900 void PlayerInfo_ready(entity fh, entity p, float status)
905 PlayerInfo_AddPlayer(p);
909 case URL_READY_CANWRITE:
910 print("-- Sending data to player stats server\n");
911 url_fputs(fh, "V 1\n");
913 url_fputs(fh, sprintf("R %s\n", WATERMARK));
916 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
917 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
918 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
919 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
920 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
925 case URL_READY_CANREAD:
926 print("-- Got response from player stats server:\n");
927 string gametype = string_null;
928 while((s = url_fgets(fh)))
932 string key = "", value = "", data = "";
934 n = tokenizebyseparator(s, " "); // key (value) data
955 PlayerInfo_AddItem(p, "_version", data);
957 PlayerInfo_AddItem(p, "_release", data);
959 PlayerInfo_AddItem(p, "_time", data);
961 PlayerInfo_AddItem(p, "_statsurl", data);
963 PlayerInfo_AddItem(p, "_hashkey", data);
965 PlayerInfo_AddItem(p, "_playernick", data);
967 PlayerInfo_AddItem(p, "_playerid", data);
970 else if (key == "e" && value != "")
973 PlayerInfo_AddItem(p, value, data);
975 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
980 print("-- End of response.\n");
983 case URL_READY_CLOSED:
984 // url_fclose has finished
985 print("Player stats synchronized with server\n");
987 case URL_READY_ERROR:
989 print("Receiving player stats failed: ", ftos(status), "\n");
994 void PlayerInfo_Init()
996 playerinfo_db = db_create();
1000 void PlayerInfo_Basic(entity p)
1002 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1004 if(playerinfo_db < 0)
1008 uri = autocvar_g_playerinfo_uri;
1009 if(uri != "" && p.crypto_idfp != "")
1011 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1012 print("Retrieving playerstats from URL: ", uri, "\n");
1013 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1019 void PlayerInfo_Details()
1021 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1023 if(playerinfo_db < 0)
1027 uri = autocvar_g_playerinfo_uri; // FIXME
1028 if(uri != "" && crypto_getmyidstatus(0) > 0)
1030 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1031 uri = strcat(uri, "/player/me");
1032 print("Retrieving playerstats from URL: ", uri, "\n");
1033 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
1039 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1040 void PlayerInfo_Details()
1042 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1044 if(playerinfo_db < 0)
1048 uri = autocvar_g_playerinfo_uri; // FIXME
1049 if(uri != "" && crypto_getmyidstatus(0) > 0)
1051 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1052 print("Retrieving playerstats from URL: ", uri, "\n");
1053 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);