2 //float PS_PM_IN_DB; // playerstats_prematch_in_db // db for info COLLECTED at the beginning of a match
3 float PS_GR_OUT_DB; // playerstats_gamereport_out_db // db of info SENT at the end of a match
4 //float PS_GR_IN_DB; // playerstats_gamereport_in_db // db for info COLLECTED at the end of a match
5 //float PS_B_IN_DB; // playerstats_playerbasic_in_db // db for info COLLECTED for basic player info (ELO)
6 // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
10 //float PS_D_IN_DB; // playerstats_playerdetail_in_db // db for info COLLECTED for detailed player profile display
11 // http://stats.xonotic.org/player/me
15 //string PS_PM_IN_EVL; // playerstats_prematch_in_events_last
16 string PS_GR_OUT_TL; // playerstats_gamereport_out_teams_last
17 string PS_GR_OUT_PL; // playerstats_gamereport_out_players_las
18 string PS_GR_OUT_EVL; // playerstats_gamereport_out_events_last
19 //string PS_GR_IN_PL; // playerstats_gamereport_in_players_last
20 //string PS_GR_IN_EVL; // playerstats_gamereport_in_events_last
21 //string PS_B_IN_PL; // playerstats_playerbasic_in_players_las
22 //string PS_B_IN_EVL; // playerstats_playerbasic_in_events_last
26 //string PS_D_IN_EVL; // playerstats_playerdetail_in_events_last
30 void PlayerStats_Prematch(void)
35 void PlayerStats_GameReport_AddPlayer(entity e)
39 if(PS_GR_OUT_DB < 0) { return; }
40 if(e.playerstats_id) { return; }
43 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
44 { s = e.crypto_idfp; }
45 else if(IS_BOT_CLIENT(e))
46 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
48 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
51 { s = sprintf("bot#%d", e.playerid); }
53 { s = sprintf("player#%d", e.playerid); }
56 e.playerstats_id = strzone(s);
58 string key = sprintf("%s:*", e.playerstats_id);
59 string p = db_get(PS_GR_OUT_DB, key);
65 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
66 strunzone(PS_GR_OUT_PL);
68 else { db_put(PS_GR_OUT_DB, key, "#"); }
69 PS_GR_OUT_PL = strzone(e.playerstats_id);
73 void PlayerStats_GameReport_AddTeam(float t)
75 if(PS_GR_OUT_DB < 0) { return; }
77 string key = sprintf("%d", t);
78 string p = db_get(PS_GR_OUT_DB, key);
84 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
85 strunzone(PS_GR_OUT_TL);
87 else { db_put(PS_GR_OUT_DB, key, "#"); }
88 PS_GR_OUT_TL = strzone(key);
92 void PlayerStats_GameReport_AddEvent(string event_id)
94 if(PS_GR_OUT_DB < 0) { return; }
96 string key = sprintf("*:%s", event_id);
97 string p = db_get(PS_GR_OUT_DB, key);
103 db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
104 strunzone(PS_GR_OUT_EVL);
106 else { db_put(PS_GR_OUT_DB, key, "#"); }
107 PS_GR_OUT_EVL = strzone(event_id);
111 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
112 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
114 if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
116 string key = sprintf("%s:%s", prefix, event_id);
117 float val = stof(db_get(PS_GR_OUT_DB, key));
119 db_put(PS_GR_OUT_DB, key, ftos(val));
123 void PlayerStats_GameReport_Accuracy(entity p)
128 #define PAC p.accuracy
129 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
131 w = get_weaponinfo(i);
132 PS_GR_P_ADDVAL(p, strcat("acc-", w.netname, "-hit"), PAC.(accuracy_hit[i-1]));
133 PS_GR_P_ADDVAL(p, strcat("acc-", w.netname, "-fired"), PAC.(accuracy_fired[i-1]));
134 PS_GR_P_ADDVAL(p, strcat("acc-", w.netname, "-cnt-hit"), PAC.(accuracy_cnt_hit[i-1]));
135 PS_GR_P_ADDVAL(p, strcat("acc-", w.netname, "-cnt-fired"), PAC.(accuracy_cnt_fired[i-1]));
136 PS_GR_P_ADDVAL(p, strcat("acc-", w.netname, "-frags"), PAC.(accuracy_frags[i-1]));
141 void PlayerStats_GameReport_FinalizePlayer(entity p)
143 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
148 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
152 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
154 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
155 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), p.netname);
158 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
160 if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
161 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
163 PlayerStats_GameReport_Accuracy(p);
165 if(IS_REAL_CLIENT(p))
169 float latency = (p.latency_sum / p.latency_cnt);
170 if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
174 strunzone(p.playerstats_id);
175 p.playerstats_id = string_null;
178 .float scoreboard_pos;
179 void PlayerStats_GameReport_EndMatch(float finished)
181 if(PS_GR_OUT_DB < 0) { return; }
183 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
184 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
185 if(teamplay) { PlayerScore_TeamStats(); }
190 // add personal score rank
191 PS_GR_P_ADDVAL(p, PLAYERSTATS_RANK, p.score_dummyfield);
196 // scoreboard is valid!
197 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
199 // add scoreboard position
200 PS_GR_P_ADDVAL(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
202 // add scoreboard data
203 PlayerScore_PlayerStats(p);
205 // if the match ended normally, add winning info
208 PS_GR_P_ADDVAL(p, PLAYERSTATS_WINS, p.winning);
209 PS_GR_P_ADDVAL(p, PLAYERSTATS_MATCHES, 1);
213 // collect final player information
214 PlayerStats_GameReport_FinalizePlayer(p);
217 if(autocvar_g_playerstats_gamereport_uri != "")
219 PlayerStats_GameReport_DelayMapVote = TRUE;
221 autocvar_g_playerstats_gamereport_uri,
223 PlayerStats_GameReport_Handler,
229 PlayerStats_GameReport_DelayMapVote = FALSE;
230 db_close(PS_GR_OUT_DB);
235 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
239 PlayerStats_GameReport_DelayMapVote = FALSE;
240 uri = autocvar_g_playerstats_uri;
243 PS_GR_OUT_DB = db_create();
244 if(PS_GR_OUT_DB >= 0)
245 PlayerStats_GameReport_DelayMapVote = TRUE; // must wait for it at match end
247 serverflags |= SERVERFLAG_PLAYERSTATS;
249 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
250 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
251 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
252 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
253 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
254 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
255 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
256 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
261 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
263 w = get_weaponinfo(i);
264 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-hit"));
265 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-fired"));
266 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
267 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
268 PlayerStats_GameReport_AddEvent(strcat("acc-", w.netname, "-frags"));
271 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
272 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
273 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
274 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
275 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
276 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
277 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
278 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
279 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
280 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
283 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
293 // ======================================
294 // -- OUTGOING GAME REPORT INFORMATION --
295 // ======================================
297 * V: format version (always a fixed number) - this MUST be the first line!
298 * #: comment (MUST be ignored by any parser)
299 * R: release information on the server
301 * O: mod name (icon request) as in server browser
303 * I: match ID (see "matchid" in g_world.qc)
304 * S: "hostname" of the server
305 * C: number of "unpure" cvar changes
306 * U: UDP port number of the server
307 * D: duration of the match
308 * L: "ladder" in which the server is participating in
309 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
310 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
312 * n: nickname of the player (optional)
314 * e: followed by an event name, a space, and the event count/score
315 * event names can be:
316 * alivetime: total playing time of the player
317 * avglatency: average network latency compounded throughout the match
318 * wins: number of games won (can only be set if matches is set)
319 * matches: number of matches played to the end (not aborted by map switch)
320 * joins: number of matches joined (always 1 unless player never played during the match)
321 * scoreboardvalid: set to 1 if the player was there at the end of the match
322 * total-<scoreboardname>: total score of that scoreboard item
323 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
324 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
325 * kills-<index>: number of kills against the indexed player
326 * rank <number>: rank of player
327 * acc-<weapon netname>-hit: total damage dealt
328 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
329 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
330 * acc-<weapon netname>-cnt-fired: amount of fired shots
331 * acc-<weapon netname>-frags: amount of frags dealt by weapon
333 case URL_READY_CANWRITE:
335 url_fputs(fh, "V 9\n");
337 url_fputs(fh, sprintf("R %s\n", WATERMARK));
339 url_fputs(fh, sprintf("G %s\n", GetGametype()));
340 url_fputs(fh, sprintf("O %s\n", modname));
341 url_fputs(fh, sprintf("M %s\n", GetMapname()));
342 url_fputs(fh, sprintf("I %s\n", matchid));
343 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
344 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
345 url_fputs(fh, sprintf("U %d\n", cvar("port")));
346 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
347 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
352 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
354 // start team section
355 url_fputs(fh, sprintf("Q team#%s\n", t));
357 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
358 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
360 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
361 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
367 for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
369 // start player section
370 url_fputs(fh, sprintf("P %s\n", p));
372 // playerid/index (entity number for this server)
373 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
374 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
377 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
378 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
380 // team identification number
383 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
384 url_fputs(fh, sprintf("t %s\n", tt));
387 // output player events
388 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
390 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
391 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
399 // ======================================
400 // -- INCOMING GAME REPORT INFORMATION --
401 // ======================================
405 case URL_READY_CANREAD:
407 // url_fclose is processing, we got a response for writing the data
408 // this must come from HTTP
409 print("Got response from player stats server:\n");
410 while((s = url_fgets(fh))) { print(" ", s, "\n"); }
411 print("End of response.\n");
416 case URL_READY_CLOSED:
418 // url_fclose has finished
419 print("Player stats written\n");
420 PlayerStats_GameReport_DelayMapVote = FALSE;
421 db_close(PS_GR_OUT_DB);
426 case URL_READY_ERROR:
429 print("Player stats writing failed: ", ftos(status), "\n");
430 PlayerStats_GameReport_DelayMapVote = FALSE;
431 if(PS_GR_OUT_DB >= 0)
433 db_close(PS_GR_OUT_DB);
442 void PlayerInfo_AddPlayer(entity e)
444 if(playerinfo_db < 0)
448 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
451 p = db_get(playerinfo_db, key);
456 db_put(playerinfo_db, key, playerinfo_last);
457 strunzone(playerinfo_last);
460 db_put(playerinfo_db, key, "#");
461 playerinfo_last = strzone(ftos(e.playerid));
462 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
466 void PlayerInfo_AddItem(entity e, string item_id, string val)
468 if(playerinfo_db < 0)
472 key = sprintf("*:%s", item_id);
475 p = db_get(playerinfo_db, key);
478 if(playerinfo_events_last)
480 db_put(playerinfo_db, key, playerinfo_events_last);
481 strunzone(playerinfo_events_last);
484 db_put(playerinfo_db, key, "#");
485 playerinfo_events_last = strzone(item_id);
488 key = sprintf("#%d:%s", e.playerid, item_id);
489 db_put(playerinfo_db, key, val);
490 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
493 string PlayerInfo_GetItem(entity e, string item_id)
495 if(playerinfo_db < 0)
499 key = sprintf("#%d:%s", e.playerid, item_id);
500 return db_get(playerinfo_db, key);
503 string PlayerInfo_GetItemLocal(string item_id)
507 return PlayerInfo_GetItem(p, item_id);
510 void PlayerInfo_ready(entity fh, entity p, float status)
515 PlayerInfo_AddPlayer(p);
519 case URL_READY_CANWRITE:
520 print("-- Sending data to player stats server\n");
521 url_fputs(fh, "V 1\n");
523 url_fputs(fh, sprintf("R %s\n", WATERMARK));
526 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
527 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
528 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
529 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
530 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
535 case URL_READY_CANREAD:
536 print("-- Got response from player stats server:\n");
537 string gametype = string_null;
538 while((s = url_fgets(fh)))
542 string key = "", value = "", data = "";
544 n = tokenizebyseparator(s, " "); // key (value) data
565 PlayerInfo_AddItem(p, "_version", data);
567 PlayerInfo_AddItem(p, "_release", data);
569 PlayerInfo_AddItem(p, "_time", data);
571 PlayerInfo_AddItem(p, "_statsurl", data);
573 PlayerInfo_AddItem(p, "_hashkey", data);
575 PlayerInfo_AddItem(p, "_playernick", data);
577 PlayerInfo_AddItem(p, "_playerid", data);
580 else if (key == "e" && value != "")
583 PlayerInfo_AddItem(p, value, data);
585 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
590 print("-- End of response.\n");
593 case URL_READY_CLOSED:
594 // url_fclose has finished
595 print("Player stats synchronized with server\n");
597 case URL_READY_ERROR:
599 print("Receiving player stats failed: ", ftos(status), "\n");
604 void PlayerInfo_Init()
607 playerinfo_db = db_create();
611 void PlayerInfo_Basic(entity p)
613 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
615 if(playerinfo_db < 0)
619 uri = autocvar_g_playerinfo_uri;
620 if(uri != "" && p.crypto_idfp != "")
622 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
623 print("Retrieving playerstats from URL: ", uri, "\n");
624 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
630 void PlayerInfo_Details()
632 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
634 if(playerinfo_db < 0)
638 uri = autocvar_g_playerinfo_uri; // FIXME
639 if(uri != "" && crypto_getmyidstatus(0) > 0)
641 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
642 uri = strcat(uri, "/player/me");
643 print("Retrieving playerstats from URL: ", uri, "\n");
644 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
651 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
652 void PlayerInfo_Details()
654 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
656 if(playerinfo_db < 0)
660 uri = autocvar_g_playerinfo_uri; // FIXME
661 if(uri != "" && crypto_getmyidstatus(0) > 0)
663 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
664 print("Retrieving playerstats from URL: ", uri, "\n");
665 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);