4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "constants.qh"
9 #include "weapons/all.qh"
10 #include "../server/weapons/accuracy.qh"
11 #include "../server/defs.qh"
12 #include "playerstats.qh"
13 #include "../server/scores.qh"
17 void PlayerStats_Prematch(void)
22 void PlayerStats_GameReport_AddPlayer(entity e)
24 if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
26 // set up player identification
27 string s = string_null;
29 if((e.crypto_idfp != "") && (e.cvar_cl_allow_uidtracking == 1))
30 { s = e.crypto_idfp; }
31 else if(IS_BOT_CLIENT(e))
32 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
34 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
37 { s = sprintf("bot#%d", e.playerid); }
39 { s = sprintf("player#%d", e.playerid); }
42 e.playerstats_id = strzone(s);
44 // now add the player to the database
45 string key = sprintf("%s:*", e.playerstats_id);
46 string p = db_get(PS_GR_OUT_DB, key);
52 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
53 strunzone(PS_GR_OUT_PL);
55 else { db_put(PS_GR_OUT_DB, key, "#"); }
56 PS_GR_OUT_PL = strzone(e.playerstats_id);
60 void PlayerStats_GameReport_AddTeam(float t)
62 if(PS_GR_OUT_DB < 0) { return; }
64 string key = sprintf("%d", t);
65 string p = db_get(PS_GR_OUT_DB, key);
71 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
72 strunzone(PS_GR_OUT_TL);
74 else { db_put(PS_GR_OUT_DB, key, "#"); }
75 PS_GR_OUT_TL = strzone(key);
79 void PlayerStats_GameReport_AddEvent(string event_id)
81 if(PS_GR_OUT_DB < 0) { return; }
83 string key = sprintf("*:%s", event_id);
84 string p = db_get(PS_GR_OUT_DB, key);
90 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
91 strunzone(PS_GR_OUT_EVL);
93 else { db_put(PS_GR_OUT_DB, key, "#"); }
94 PS_GR_OUT_EVL = strzone(event_id);
98 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
99 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
101 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
103 string key = sprintf("%s:%s", prefix, event_id);
104 float val = stof(db_get(PS_GR_OUT_DB, key));
106 db_put(PS_GR_OUT_DB, key, ftos(val));
110 void PlayerStats_GameReport_Accuracy(entity p)
114 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
116 entity w = get_weaponinfo(i);
118 #define ACCMAC(suffix,field) \
119 PS_GR_P_ADDVAL(p, sprintf("acc-%s-%s", w.netname, suffix), p.accuracy.(field[i-1]));
121 ACCMAC("hit", accuracy_hit)
122 ACCMAC("fired", accuracy_fired)
123 ACCMAC("cnt-hit", accuracy_cnt_hit)
124 ACCMAC("cnt-fired", accuracy_cnt_fired)
125 ACCMAC("frags", accuracy_frags)
131 void PlayerStats_GameReport_FinalizePlayer(entity p)
133 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
138 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
142 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
144 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
145 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), p.netname);
148 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
150 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
151 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
153 PlayerStats_GameReport_Accuracy(p);
155 if(IS_REAL_CLIENT(p))
159 float latency = (p.latency_sum / p.latency_cnt);
160 if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
164 strunzone(p.playerstats_id);
165 p.playerstats_id = string_null;
168 void PlayerStats_GameReport(float finished)
170 if(PS_GR_OUT_DB < 0) { return; }
172 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
173 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
174 if(teamplay) { PlayerScore_TeamStats(); }
179 // add personal score rank
180 PS_GR_P_ADDVAL(p, PLAYERSTATS_RANK, p.score_dummyfield);
185 // scoreboard is valid!
186 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
188 // add scoreboard position
189 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
191 // add scoreboard data
192 PlayerScore_PlayerStats(p);
194 // if the match ended normally, add winning info
197 PS_GR_P_ADDVAL(p, PLAYERSTATS_WINS, p.winning);
198 PS_GR_P_ADDVAL(p, PLAYERSTATS_MATCHES, 1);
202 // collect final player information
203 PlayerStats_GameReport_FinalizePlayer(p);
206 if(autocvar_g_playerstats_gamereport_uri != "")
208 PlayerStats_GameReport_DelayMapVote = true;
210 autocvar_g_playerstats_gamereport_uri,
212 PlayerStats_GameReport_Handler,
218 PlayerStats_GameReport_DelayMapVote = false;
219 db_close(PS_GR_OUT_DB);
224 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
226 if(autocvar_g_playerstats_gamereport_uri == "") { return; }
228 PS_GR_OUT_DB = db_create();
230 if(PS_GR_OUT_DB >= 0)
232 PlayerStats_GameReport_DelayMapVote = true;
234 serverflags |= SERVERFLAG_PLAYERSTATS;
236 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
237 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
238 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
239 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
240 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
241 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
242 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
243 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
248 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
250 w = get_weaponinfo(i);
251 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-hit"));
252 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-fired"));
253 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
254 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
255 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-frags"));
258 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
259 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
260 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
261 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
262 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
263 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
264 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
265 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
266 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
267 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
269 else { PlayerStats_GameReport_DelayMapVote = false; }
272 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
282 // ======================================
283 // -- OUTGOING GAME REPORT INFORMATION --
284 // ======================================
286 * V: format version (always a fixed number) - this MUST be the first line!
287 * #: comment (MUST be ignored by any parser)
288 * R: release information on the server
290 * O: mod name (icon request) as in server browser
292 * I: match ID (see "matchid" in g_world.qc)
293 * S: "hostname" of the server
294 * C: number of "unpure" cvar changes
295 * U: UDP port number of the server
296 * D: duration of the match
297 * L: "ladder" in which the server is participating in
298 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
299 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
301 * n: nickname of the player (optional)
303 * e: followed by an event name, a space, and the event count/score
304 * event names can be:
305 * alivetime: total playing time of the player
306 * avglatency: average network latency compounded throughout the match
307 * wins: number of games won (can only be set if matches is set)
308 * matches: number of matches played to the end (not aborted by map switch)
309 * joins: number of matches joined (always 1 unless player never played during the match)
310 * scoreboardvalid: set to 1 if the player was there at the end of the match
311 * total-<scoreboardname>: total score of that scoreboard item
312 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
313 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
314 * kills-<index>: number of kills against the indexed player
315 * rank <number>: rank of player
316 * acc-<weapon netname>-hit: total damage dealt
317 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
318 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
319 * acc-<weapon netname>-cnt-fired: amount of fired shots
320 * acc-<weapon netname>-frags: amount of frags dealt by weapon
322 case URL_READY_CANWRITE:
324 url_fputs(fh, "V 9\n");
326 url_fputs(fh, sprintf("R %s\n", WATERMARK));
328 url_fputs(fh, sprintf("G %s\n", GetGametype()));
329 url_fputs(fh, sprintf("O %s\n", modname));
330 url_fputs(fh, sprintf("M %s\n", GetMapname()));
331 url_fputs(fh, sprintf("I %s\n", matchid));
332 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
333 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
334 url_fputs(fh, sprintf("U %d\n", cvar("port")));
335 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
336 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
341 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
343 // start team section
344 url_fputs(fh, sprintf("Q team#%s\n", t));
346 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
347 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
349 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
350 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
356 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
358 // start player section
359 url_fputs(fh, sprintf("P %s\n", p));
361 // playerid/index (entity number for this server)
362 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
363 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
366 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
367 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
369 // team identification number
372 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
373 url_fputs(fh, sprintf("t %s\n", tt));
376 // output player events
377 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
379 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
380 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
388 // ======================================
389 // -- INCOMING GAME REPORT INFORMATION --
390 // ======================================
394 case URL_READY_CANREAD:
396 // url_fclose is processing, we got a response for writing the data
397 // this must come from HTTP
398 dprint("Got response from player stats server:\n");
399 while((s = url_fgets(fh))) { dprint(" ", s, "\n"); }
400 dprint("End of response.\n");
405 case URL_READY_CLOSED:
407 // url_fclose has finished
408 dprint("Player stats written\n");
409 PlayerStats_GameReport_DelayMapVote = false;
410 if(PS_GR_OUT_DB >= 0)
412 db_close(PS_GR_OUT_DB);
418 case URL_READY_ERROR:
421 print("Player stats writing failed: ", ftos(status), "\n");
422 PlayerStats_GameReport_DelayMapVote = false;
423 if(PS_GR_OUT_DB >= 0)
425 db_close(PS_GR_OUT_DB);
433 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
435 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
436 if(autocvar_g_playerstats_playerbasic_uri != "")
438 string uri = autocvar_g_playerstats_playerbasic_uri;
439 if(joiningplayer.crypto_idfp != "")
441 // create the database if it doesn't already exist
443 PS_B_IN_DB = db_create();
445 // now request the information
446 uri = strcat(uri, "/player/", uri_escape(uri_escape(joiningplayer.crypto_idfp)), "/elo.txt");
447 dprint("Retrieving playerstats from URL: ", uri, "\n");
451 PlayerStats_PlayerBasic_Handler,
455 // set status appropriately // todo: check whether the player info exists in the database previously
458 // database still contains useful information, so don't clear it of a useful status
459 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
463 // database was previously empty or never hit received status for some reason
464 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
470 // server has this disabled, kill the DB and set status to idle
475 db_close(PS_B_IN_DB);
478 FOR_EACH_REALCLIENT(player) { player.playerstats_basicstatus = PS_B_STATUS_IDLE; }
483 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
485 // determine whether we should retrieve playerbasic information again
488 printf("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f\n",
489 joiningplayer.netname,
494 // TODO: check to see if this playerid is inside the database already somehow...
495 // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
496 // although maybe we should just submit another request ANYWAY?
497 if(!joiningplayer.playerstats_basicstatus)
499 PlayerStats_PlayerBasic(
501 (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
506 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
510 case URL_READY_CANWRITE:
512 dprint("-- Sending data to player stats server\n");
513 /*url_fputs(fh, "V 1\n");
515 url_fputs(fh, sprintf("R %s\n", WATERMARK));
517 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
518 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
519 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
520 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
521 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
522 */url_fputs(fh, "\n");
527 case URL_READY_CANREAD:
530 dprint("-- Got response from player stats server:\n");
531 //string gametype = string_null;
532 while((s = url_fgets(fh)))
534 dprint(" ", s, "\n");
536 string key = "", value = "", data = "";
538 n = tokenizebyseparator(s, " "); // key (value) data
559 PlayerInfo_AddItem(p, "_version", data);
561 PlayerInfo_AddItem(p, "_release", data);
563 PlayerInfo_AddItem(p, "_time", data);
565 PlayerInfo_AddItem(p, "_statsurl", data);
567 PlayerInfo_AddItem(p, "_hashkey", data);
569 PlayerInfo_AddItem(p, "_playernick", data);
571 PlayerInfo_AddItem(p, "_playerid", data);
574 else if (key == "e" && value != "")
577 PlayerInfo_AddItem(p, value, data);
579 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
585 dprint("-- End of response.\n");
589 case URL_READY_CLOSED:
591 // url_fclose has finished
592 print("Player stats synchronized with server\n");
596 case URL_READY_ERROR:
599 print("Receiving player stats failed: ", ftos(status), "\n");
609 #if 0 // reading the entire DB at once
610 string e = "", en = "";
612 for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
614 print(sprintf("%d:%s:%s\n", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e))));
619 void PlayerStats_PlayerDetail_AddItem(string event, string data)
621 if(PS_D_IN_DB < 0) { return; }
623 // create a marker for the event so that we can access it later
624 string marker = sprintf("%s", event);
625 if(db_get(PS_D_IN_DB, marker) == "")
629 db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
630 strunzone(PS_D_IN_EVL);
632 else { db_put(PS_D_IN_DB, marker, "#"); }
633 PS_D_IN_EVL = strzone(marker);
636 // now actually set the event data
637 db_put(PS_D_IN_DB, sprintf("#%s", event), data);
638 dprint("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB\n");
641 void PlayerStats_PlayerDetail(void)
643 // http://stats.xonotic.org/player/me
644 if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
646 // create the database if it doesn't already exist
648 PS_D_IN_DB = db_create();
650 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
651 dprint("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri, "\n");
653 autocvar_g_playerstats_playerdetail_uri,
655 PlayerStats_PlayerDetail_Handler,
659 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
663 // player has this disabled, kill the DB and set status to idle
666 db_close(PS_D_IN_DB);
670 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
674 void PlayerStats_PlayerDetail_CheckUpdate(void)
676 // determine whether we should retrieve playerdetail information again
677 float gamecount = cvar("cl_matchcount");
680 printf("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d\n",
689 (time >= PS_D_NEXTUPDATETIME)
691 (gamecount > PS_D_LASTGAMECOUNT)
694 PlayerStats_PlayerDetail();
695 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
696 PS_D_LASTGAMECOUNT = gamecount;
700 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
704 case URL_READY_CANWRITE:
706 dprint("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...\n");
707 url_fputs(fh, "V 1\n");
709 url_fputs(fh, sprintf("R %s\n", WATERMARK));
711 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
712 //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
713 //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
714 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
715 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
721 case URL_READY_CANREAD:
723 //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
725 string gametype = "overall";
726 while((input = url_fgets(fh)))
728 float count = tokenizebyseparator(input, " ");
729 string key = "", event = "", data = "";
731 if(argv(0) == "#") { continue; }
736 data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
742 data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
749 case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
750 case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
751 case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
754 case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
755 case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
756 case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
757 case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
760 case "G": gametype = data; break;
763 if(event != "" && data != "")
765 PlayerStats_PlayerDetail_AddItem(
780 "PlayerStats_PlayerDetail_Handler(): ERROR: "
781 "Key went unhandled? Is our version outdated?\n"
782 "PlayerStats_PlayerDetail_Handler(): "
783 "Key '%s', Event '%s', Data '%s'\n",
794 "PlayerStats_PlayerDetail_Handler(): "
795 "Key '%s', Event '%s', Data '%s'\n",
802 //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
804 PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
805 statslist.getStats(statslist);
809 case URL_READY_CLOSED:
811 // url_fclose has finished
812 print("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.\n");
816 case URL_READY_ERROR:
819 print("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status), "\n");
820 PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
823 db_close(PS_D_IN_DB);
833 void PlayerInfo_AddPlayer(entity e)
835 if(playerinfo_db < 0)
839 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
842 p = db_get(playerinfo_db, key);
847 db_put(playerinfo_db, key, playerinfo_last);
848 strunzone(playerinfo_last);
851 db_put(playerinfo_db, key, "#");
852 playerinfo_last = strzone(ftos(e.playerid));
853 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
857 void PlayerInfo_AddItem(entity e, string item_id, string val)
859 if(playerinfo_db < 0)
863 key = sprintf("*:%s", item_id);
866 p = db_get(playerinfo_db, key);
869 if(playerinfo_events_last)
871 db_put(playerinfo_db, key, playerinfo_events_last);
872 strunzone(playerinfo_events_last);
875 db_put(playerinfo_db, key, "#");
876 playerinfo_events_last = strzone(item_id);
879 key = sprintf("#%d:%s", e.playerid, item_id);
880 db_put(playerinfo_db, key, val);
881 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
884 string PlayerInfo_GetItem(entity e, string item_id)
886 if(playerinfo_db < 0)
890 key = sprintf("#%d:%s", e.playerid, item_id);
891 return db_get(playerinfo_db, key);
894 string PlayerInfo_GetItemLocal(string item_id)
898 return PlayerInfo_GetItem(p, item_id);
901 void PlayerInfo_ready(entity fh, entity p, float status)
906 PlayerInfo_AddPlayer(p);
910 case URL_READY_CANWRITE:
911 print("-- Sending data to player stats server\n");
912 url_fputs(fh, "V 1\n");
914 url_fputs(fh, sprintf("R %s\n", WATERMARK));
917 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
918 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
919 url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
920 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
921 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
926 case URL_READY_CANREAD:
927 print("-- Got response from player stats server:\n");
928 string gametype = string_null;
929 while((s = url_fgets(fh)))
933 string key = "", value = "", data = "";
935 n = tokenizebyseparator(s, " "); // key (value) data
956 PlayerInfo_AddItem(p, "_version", data);
958 PlayerInfo_AddItem(p, "_release", data);
960 PlayerInfo_AddItem(p, "_time", data);
962 PlayerInfo_AddItem(p, "_statsurl", data);
964 PlayerInfo_AddItem(p, "_hashkey", data);
966 PlayerInfo_AddItem(p, "_playernick", data);
968 PlayerInfo_AddItem(p, "_playerid", data);
971 else if (key == "e" && value != "")
974 PlayerInfo_AddItem(p, value, data);
976 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
981 print("-- End of response.\n");
984 case URL_READY_CLOSED:
985 // url_fclose has finished
986 print("Player stats synchronized with server\n");
988 case URL_READY_ERROR:
990 print("Receiving player stats failed: ", ftos(status), "\n");
995 void PlayerInfo_Init()
997 playerinfo_db = db_create();
1001 void PlayerInfo_Basic(entity p)
1003 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1005 if(playerinfo_db < 0)
1009 uri = autocvar_g_playerinfo_uri;
1010 if(uri != "" && p.crypto_idfp != "")
1012 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1013 print("Retrieving playerstats from URL: ", uri, "\n");
1014 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1020 void PlayerInfo_Details()
1022 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1024 if(playerinfo_db < 0)
1028 uri = autocvar_g_playerinfo_uri; // FIXME
1029 if(uri != "" && crypto_getmyidstatus(0) > 0)
1031 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1032 uri = strcat(uri, "/player/me");
1033 print("Retrieving playerstats from URL: ", uri, "\n");
1034 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
1041 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1042 void PlayerInfo_Details()
1044 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1046 if(playerinfo_db < 0)
1050 uri = autocvar_g_playerinfo_uri; // FIXME
1051 if(uri != "" && crypto_getmyidstatus(0) > 0)
1053 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1054 print("Retrieving playerstats from URL: ", uri, "\n");
1055 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);