2 #define PS_PM_IN_DB playerstats_prematch_in_db // db for info COLLECTED at the beginning of a match
3 #define PS_GR_OUT_DB playerstats_gamereport_out_db // db of info SENT at the end of a match
4 #define PS_GR_IN_DB playerstats_gamereport_in_db // db for info COLLECTED at the end of a match
5 #define PS_B_IN_DB playerstats_playerbasic_in_db // db for info COLLECTED for basic player info (ELO)
13 #define PS_D_IN_DB playerstats_playerdetail_in_db // db for info COLLECTED for detailed player profile display
18 #define PS_PM_IN_EVL playerstats_prematch_in_events_last
19 #define PS_GR_OUT_TL playerstats_gamereport_out_teams_last
20 #define PS_GR_OUT_PL playerstats_gamereport_out_players_last
21 #define PS_GR_OUT_EVL playerstats_gamereport_out_events_last
22 #define PS_GR_IN_PL playerstats_gamereport_in_players_last
23 #define PS_GR_IN_EVL playerstats_gamereport_in_events_last
24 #define PS_B_IN_PL playerstats_playerbasic_in_players_last
25 #define PS_B_IN_EVL playerstats_playerbasic_in_events_last
37 #define PS_D_IN_EVL playerstats_playerdetail_in_events_last
42 void PlayerStats_Prematch(void)
47 .string playerstats_id;
49 void PlayerStats_GameReport_AddPlayer(entity e)
53 if(PS_GR_OUT_DB < 0) { return; }
54 if(e.playerstats_id) { return; }
57 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
58 { s = e.crypto_idfp; }
59 else if(IS_BOT_CLIENT(e))
60 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
62 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
65 { s = sprintf("bot#%d", e.playerid); }
67 { s = sprintf("player#%d", e.playerid); }
70 e.playerstats_id = strzone(s);
72 string key = sprintf("%s:*", e.playerstats_id);
73 string p = db_get(PS_GR_OUT_DB, key);
79 db_put(PS_GR_OUT_DB, key, playerstats_last);
80 strunzone(playerstats_last);
82 else { db_put(PS_GR_OUT_DB, key, "#"); }
83 playerstats_last = strzone(e.playerstats_id);
87 void PlayerStats_GameReport_AddTeam(float t)
89 if(PS_GR_OUT_DB < 0) { return; }
91 string key = sprintf("%d", t);
92 string p = db_get(PS_GR_OUT_DB, key);
98 db_put(PS_GR_OUT_DB, key, teamstats_last);
99 strunzone(teamstats_last);
101 else { db_put(PS_GR_OUT_DB, key, "#"); }
102 teamstats_last = strzone(key);
106 void PlayerStats_GameReport_AddEvent(string event_id)
108 if(PS_GR_OUT_DB < 0) { return; }
110 string key = sprintf("*:%s", event_id);
111 string p = db_get(PS_GR_OUT_DB, key);
117 db_put(PS_GR_OUT_DB, key, events_last);
118 strunzone(events_last);
120 else { db_put(PS_GR_OUT_DB, key, "#"); }
121 events_last = strzone(event_id);
125 float PlayerStats_GameReport_SetPlayerEvent(entity e, string event_id, float value)
127 if((e.playerstats_id == "") || PS_GR_OUT_DB < 0) { return 0; }
129 string key = sprintf("%s:%s", e.playerstats_id, event_id);
130 float val = stof(db_get(PS_GR_OUT_DB, key));
132 db_put(PS_GR_OUT_DB, key, ftos(val));
136 float PlayerStats_GameReport_SetTeamEvent(float t, string event_id, float value)
138 if(PS_GR_OUT_DB < 0) { return 0; }
140 string key = sprintf("team#%d:%s", t, event_id);
141 float val = stof(db_get(PS_GR_OUT_DB, key));
143 db_put(PS_GR_OUT_DB, key, ftos(val));
147 void PlayerStats_GameReport_Accuracy(entity p)
152 #define PAC p.accuracy
153 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
155 w = get_weaponinfo(i);
156 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), PAC.(accuracy_hit[i-1]));
157 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), PAC.(accuracy_fired[i-1]));
158 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), PAC.(accuracy_cnt_hit[i-1]));
159 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), PAC.(accuracy_cnt_fired[i-1]));
160 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), PAC.(accuracy_frags[i-1]));
165 void PlayerStats_GameReport_AddGlobalInfo(entity p)
167 if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
172 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
176 db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
178 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
179 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), p.netname);
182 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
184 if(stof(db_get(PS_GR_OUT_DB, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
185 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
187 PlayerStats_Accuracy(p);
189 if(IS_REAL_CLIENT(p))
193 float latency = (p.latency_sum / p.latency_cnt);
194 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
198 strunzone(p.playerstats_id);
199 p.playerstats_id = string_null;
202 .float scoreboard_pos;
203 void PlayerStats_GameReport_EndMatch(float finished)
206 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
207 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
208 if(teamplay) { PlayerScore_TeamStats(); }
211 // add personal score rank
212 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
214 if(!p.scoreboard_pos) { continue; }
216 // scoreboard is valid!
217 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
219 // add scoreboard position
220 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
222 // add scoreboard data
223 PlayerScore_PlayerStats(p);
225 // if the match ended normally, add winning info
228 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
229 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
234 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
238 playerstats_waitforme = TRUE;
239 uri = autocvar_g_playerstats_uri;
242 PS_GR_OUT_DB = db_create();
243 if(PS_GR_OUT_DB >= 0)
244 playerstats_waitforme = FALSE; // must wait for it at match end
246 serverflags |= SERVERFLAG_PLAYERSTATS;
248 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
249 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
250 PlayerStats_AddEvent(PLAYERSTATS_WINS);
251 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
252 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
253 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
254 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
255 PlayerStats_AddEvent(PLAYERSTATS_RANK);
260 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
262 w = get_weaponinfo(i);
263 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
264 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
265 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
266 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
267 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
270 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
271 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
272 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
273 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
274 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
275 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
276 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
277 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
278 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
279 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
282 void PlayerStats_GameReport_Shutdown()
286 if(PS_GR_OUT_DB < 0) { return; }
288 uri = autocvar_g_playerstats_uri;
291 playerstats_waitforme = FALSE;
292 url_multi_fopen(uri, FILE_APPEND, PlayerStats_GameReport_Handler, world);
296 playerstats_waitforme = TRUE;
297 db_close(PS_GR_OUT_DB);
302 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
312 // ======================================
313 // -- OUTGOING GAME REPORT INFORMATION --
314 // ======================================
316 * V: format version (always a fixed number) - this MUST be the first line!
317 * #: comment (MUST be ignored by any parser)
318 * R: release information on the server
320 * O: mod name (icon request) as in server browser
322 * I: match ID (see "matchid" in g_world.qc)
323 * S: "hostname" of the server
324 * C: number of "unpure" cvar changes
325 * U: UDP port number of the server
326 * D: duration of the match
327 * L: "ladder" in which the server is participating in
328 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
329 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
331 * n: nickname of the player (optional)
333 * e: followed by an event name, a space, and the event count/score
334 * event names can be:
335 * alivetime: total playing time of the player
336 * avglatency: average network latency compounded throughout the match
337 * wins: number of games won (can only be set if matches is set)
338 * matches: number of matches played to the end (not aborted by map switch)
339 * joins: number of matches joined (always 1 unless player never played during the match)
340 * scoreboardvalid: set to 1 if the player was there at the end of the match
341 * total-<scoreboardname>: total score of that scoreboard item
342 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
343 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
344 * kills-<index>: number of kills against the indexed player
345 * rank <number>: rank of player
346 * acc-<weapon netname>-hit: total damage dealt
347 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
348 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
349 * acc-<weapon netname>-cnt-fired: amount of fired shots
350 * acc-<weapon netname>-frags: amount of frags dealt by weapon
352 case URL_READY_CANWRITE:
354 url_fputs(fh, "V 9\n");
356 url_fputs(fh, sprintf("R %s\n", WATERMARK));
358 url_fputs(fh, sprintf("G %s\n", GetGametype()));
359 url_fputs(fh, sprintf("O %s\n", modname));
360 url_fputs(fh, sprintf("M %s\n", GetMapname()));
361 url_fputs(fh, sprintf("I %s\n", matchid));
362 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
363 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
364 url_fputs(fh, sprintf("U %d\n", cvar("port")));
365 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
366 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_ladder));
371 for(t = teamstats_last; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
373 // start team section
374 url_fputs(fh, sprintf("Q team#%s\n", t));
376 // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
377 for(e = events_last; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
379 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
380 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
386 for(p = playerstats_last; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
388 // start player section
389 url_fputs(fh, sprintf("P %s\n", p));
391 // playerid/index (entity number for this server)
392 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
393 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
396 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
397 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
399 // team identification number
402 tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
403 url_fputs(fh, sprintf("t %s\n", tt));
406 // output player events
407 for(e = events_last; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
409 float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
410 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
418 // ======================================
419 // -- INCOMING GAME REPORT INFORMATION --
420 // ======================================
424 case URL_READY_CANREAD:
426 // url_fclose is processing, we got a response for writing the data
427 // this must come from HTTP
428 print("Got response from player stats server:\n");
429 while((s = url_fgets(fh))) { print(" ", s, "\n"); }
430 print("End of response.\n");
435 case URL_READY_CLOSED:
437 // url_fclose has finished
438 print("Player stats written\n");
439 playerstats_waitforme = TRUE;
440 db_close(PS_GR_OUT_DB);
445 case URL_READY_ERROR:
448 print("Player stats writing failed: ", ftos(status), "\n");
449 playerstats_waitforme = TRUE;
450 if(PS_GR_OUT_DB >= 0)
452 db_close(PS_GR_OUT_DB);
461 void PlayerInfo_AddPlayer(entity e)
463 if(playerinfo_db < 0)
467 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
470 p = db_get(playerinfo_db, key);
475 db_put(playerinfo_db, key, playerinfo_last);
476 strunzone(playerinfo_last);
479 db_put(playerinfo_db, key, "#");
480 playerinfo_last = strzone(ftos(e.playerid));
481 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
485 void PlayerInfo_AddItem(entity e, string item_id, string val)
487 if(playerinfo_db < 0)
491 key = sprintf("*:%s", item_id);
494 p = db_get(playerinfo_db, key);
497 if(playerinfo_events_last)
499 db_put(playerinfo_db, key, playerinfo_events_last);
500 strunzone(playerinfo_events_last);
503 db_put(playerinfo_db, key, "#");
504 playerinfo_events_last = strzone(item_id);
507 key = sprintf("#%d:%s", e.playerid, item_id);
508 db_put(playerinfo_db, key, val);
509 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
512 string PlayerInfo_GetItem(entity e, string item_id)
514 if(playerinfo_db < 0)
518 key = sprintf("#%d:%s", e.playerid, item_id);
519 return db_get(playerinfo_db, key);
522 string PlayerInfo_GetItemLocal(string item_id)
526 return PlayerInfo_GetItem(p, item_id);
529 void PlayerInfo_ready(entity fh, entity p, float status)
534 PlayerInfo_AddPlayer(p);
538 case URL_READY_CANWRITE:
539 print("-- Sending data to player stats server\n");
540 url_fputs(fh, "V 1\n");
542 url_fputs(fh, sprintf("R %s\n", WATERMARK));
545 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
546 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
547 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
548 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
549 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
554 case URL_READY_CANREAD:
555 print("-- Got response from player stats server:\n");
556 string gametype = string_null;
557 while((s = url_fgets(fh)))
561 string key = "", value = "", data = "";
563 n = tokenizebyseparator(s, " "); // key (value) data
584 PlayerInfo_AddItem(p, "_version", data);
586 PlayerInfo_AddItem(p, "_release", data);
588 PlayerInfo_AddItem(p, "_time", data);
590 PlayerInfo_AddItem(p, "_statsurl", data);
592 PlayerInfo_AddItem(p, "_hashkey", data);
594 PlayerInfo_AddItem(p, "_playernick", data);
596 PlayerInfo_AddItem(p, "_playerid", data);
599 else if (key == "e" && value != "")
602 PlayerInfo_AddItem(p, value, data);
604 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
609 print("-- End of response.\n");
612 case URL_READY_CLOSED:
613 // url_fclose has finished
614 print("Player stats synchronized with server\n");
616 case URL_READY_ERROR:
618 print("Receiving player stats failed: ", ftos(status), "\n");
623 void PlayerInfo_Init()
626 playerinfo_db = db_create();
630 void PlayerInfo_Basic(entity p)
632 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
634 if(playerinfo_db < 0)
638 uri = autocvar_g_playerinfo_uri;
639 if(uri != "" && p.crypto_idfp != "")
641 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
642 print("Retrieving playerstats from URL: ", uri, "\n");
643 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
649 void PlayerInfo_Details()
651 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
653 if(playerinfo_db < 0)
657 uri = autocvar_g_playerinfo_uri; // FIXME
658 if(uri != "" && crypto_getmyidstatus(0) > 0)
660 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
661 uri = strcat(uri, "/player/me");
662 print("Retrieving playerstats from URL: ", uri, "\n");
663 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
670 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
671 void PlayerInfo_Details()
673 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
675 if(playerinfo_db < 0)
679 uri = autocvar_g_playerinfo_uri; // FIXME
680 if(uri != "" && crypto_getmyidstatus(0) > 0)
682 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
683 print("Retrieving playerstats from URL: ", uri, "\n");
684 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);