2 void PlayerStats_Prematch
6 string playerstats_last;
8 .float playerstats_addedglobalinfo;
9 .string playerstats_id;
11 void PlayerStats_GameReport_AddPlayer(entity e)
15 if(playerstats_db < 0) { return; }
16 if(e.playerstats_id) { return; }
19 if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
20 { s = e.crypto_idfp; }
21 else if(IS_BOT_CLIENT(e))
22 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
24 if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
27 { s = sprintf("bot#%d", e.playerid); }
29 { s = sprintf("player#%d", e.playerid); }
32 e.playerstats_id = strzone(s);
34 string key = sprintf("%s:*", e.playerstats_id);
35 string p = db_get(playerstats_db, key);
41 db_put(playerstats_db, key, playerstats_last);
42 strunzone(playerstats_last);
44 else { db_put(playerstats_db, key, "#"); }
45 playerstats_last = strzone(e.playerstats_id);
49 void PlayerStats_GameReport_AddTeam(float t)
51 if(playerstats_db < 0) { return; }
53 string key = sprintf("%d", t);
54 string p = db_get(playerstats_db, key);
60 db_put(playerstats_db, key, teamstats_last);
61 strunzone(teamstats_last);
63 else { db_put(playerstats_db, key, "#"); }
64 teamstats_last = strzone(key);
68 void PlayerStats_GameReport_AddEvent(string event_id)
70 if(playerstats_db < 0) { return; }
72 string key = sprintf("*:%s", event_id);
73 string p = db_get(playerstats_db, key);
79 db_put(playerstats_db, key, events_last);
80 strunzone(events_last);
82 else { db_put(playerstats_db, key, "#"); }
83 events_last = strzone(event_id);
87 float PlayerStats_GameReport_Event(entity e, string event_id, float value)
89 if((e.playerstats_id == "") || playerstats_db < 0) { return 0; }
91 string key = sprintf("%s:%s", e.playerstats_id, event_id);
92 float val = stof(db_get(playerstats_db, key));
94 db_put(playerstats_db, key, ftos(val));
98 float PlayerStats_GameReport_TeamScore(float t, string event_id, float value)
100 if(playerstats_db < 0) { return 0; }
102 string key = sprintf("team#%d:%s", t, event_id);
103 float val = stof(db_get(playerstats_db, key));
105 db_put(playerstats_db, key, ftos(val));
109 void PlayerStats_GameReport_Accuracy(entity p)
114 #define PAC p.accuracy
115 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
117 w = get_weaponinfo(i);
118 PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), PAC.(accuracy_hit[i-1]));
119 PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), PAC.(accuracy_fired[i-1]));
120 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), PAC.(accuracy_cnt_hit[i-1]));
121 PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), PAC.(accuracy_cnt_fired[i-1]));
122 PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), PAC.(accuracy_frags[i-1]));
127 void PlayerStats_GameReport_AddGlobalInfo(entity p)
129 if((p.playerstats_id == "") || playerstats_db < 0) { return; }
130 p.playerstats_addedglobalinfo = TRUE; // todo: move to end?
135 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
139 db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
141 if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
142 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
145 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
147 if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
148 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
150 PlayerStats_Accuracy(p);
152 if(IS_REAL_CLIENT(p))
156 float latency = (p.latency_sum / p.latency_cnt);
157 if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
161 strunzone(p.playerstats_id);
162 p.playerstats_id = string_null;
165 .float scoreboard_pos;
166 void PlayerStats_GameReport_EndMatch(float finished)
169 PlayerScore_Sort(score_dummyfield, 0, 0, 0);
170 PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
171 if(teamplay) { PlayerScore_TeamStats(); }
174 // add personal score rank
175 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
177 if(!p.scoreboard_pos) { continue; }
179 // scoreboard is valid!
180 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
182 // add scoreboard position
183 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
185 // add scoreboard data
186 PlayerScore_PlayerStats(p);
188 // if the match ended normally, add winning info
191 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
192 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
197 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
201 playerstats_waitforme = TRUE;
202 uri = autocvar_g_playerstats_uri;
205 playerstats_db = db_create();
206 if(playerstats_db >= 0)
207 playerstats_waitforme = FALSE; // must wait for it at match end
209 serverflags |= SERVERFLAG_PLAYERSTATS;
211 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
212 PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
213 PlayerStats_AddEvent(PLAYERSTATS_WINS);
214 PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
215 PlayerStats_AddEvent(PLAYERSTATS_JOINS);
216 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
217 PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
218 PlayerStats_AddEvent(PLAYERSTATS_RANK);
223 for(i = WEP_FIRST; i <= WEP_LAST; ++i)
225 w = get_weaponinfo(i);
226 PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
227 PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
228 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
229 PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
230 PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
233 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
234 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
235 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
236 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
237 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
238 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
239 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
240 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
241 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
242 PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
245 void PlayerStats_GameReport_Shutdown()
249 if(playerstats_db < 0) { return; }
251 uri = autocvar_g_playerstats_uri;
254 playerstats_waitforme = FALSE;
255 url_multi_fopen(uri, FILE_APPEND, PlayerStats_GameReport_Handler, world);
259 playerstats_waitforme = TRUE;
260 db_close(playerstats_db);
265 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
275 // ======================================
276 // -- OUTGOING GAME REPORT INFORMATION --
277 // ======================================
279 * V: format version (always a fixed number) - this MUST be the first line!
280 * #: comment (MUST be ignored by any parser)
281 * R: release information on the server
283 * O: mod name (icon request) as in server browser
285 * I: match ID (see "matchid" in g_world.qc
286 * S: "hostname" of the server
287 * C: number of "unpure" cvar changes
288 * U: UDP port number of the server
289 * D: duration of the match
290 * L: "ladder" in which the server is participating in
291 * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
292 * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
293 * n: nickname of the player (optional)
296 * e: followed by an event name, a space, and the event count/score
297 * event names can be:
298 * alivetime: total playing time of the player
299 * avglatency: average network latency compounded throughout the match
300 * wins: number of games won (can only be set if matches is set)
301 * matches: number of matches played to the end (not aborted by map switch)
302 * joins: number of matches joined (always 1 unless player never played during the match)
303 * scoreboardvalid: set to 1 if the player was there at the end of the match
304 * total-<scoreboardname>: total score of that scoreboard item
305 * scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
306 * achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
307 * kills-<index>: number of kills against the indexed player
308 * rank <number>: rank of player
309 * acc-<weapon netname>-hit: total damage dealt
310 * acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
311 * acc-<weapon netname>-cnt-hit: amount of shots that actually hit
312 * acc-<weapon netname>-cnt-fired: amount of fired shots
313 * acc-<weapon netname>-frags: amount of frags dealt by weapon
315 case URL_READY_CANWRITE:
317 url_fputs(fh, "V 9\n");
319 url_fputs(fh, sprintf("R %s\n", WATERMARK));
321 url_fputs(fh, sprintf("G %s\n", GetGametype()));
322 url_fputs(fh, sprintf("O %s\n", modname));
323 url_fputs(fh, sprintf("M %s\n", GetMapname()));
324 url_fputs(fh, sprintf("I %s\n", matchid));
325 url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
326 url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
327 url_fputs(fh, sprintf("U %d\n", cvar("port")));
328 url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
329 url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_ladder));
331 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
333 url_fputs(fh, sprintf("Q team#%s\n", t));
334 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
337 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
339 url_fputs(fh, sprintf("e %s %g\n", e, v));
342 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
344 url_fputs(fh, sprintf("P %s\n", p));
345 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
347 url_fputs(fh, sprintf("i %s\n", nn));
348 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
350 url_fputs(fh, sprintf("n %s\n", nn));
353 tt = db_get(playerstats_db, sprintf("%s:_team", p));
354 url_fputs(fh, sprintf("t %s\n", tt));
356 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
359 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
361 url_fputs(fh, sprintf("e %s %g\n", e, v));
369 // ======================================
370 // -- INCOMING GAME REPORT INFORMATION --
371 // ======================================
375 case URL_READY_CANREAD:
377 // url_fclose is processing, we got a response for writing the data
378 // this must come from HTTP
379 print("Got response from player stats server:\n");
380 while((s = url_fgets(fh))) { print(" ", s, "\n"); }
381 print("End of response.\n");
386 case URL_READY_CLOSED:
388 // url_fclose has finished
389 print("Player stats written\n");
390 playerstats_waitforme = TRUE;
391 db_close(playerstats_db);
396 case URL_READY_ERROR:
399 print("Player stats writing failed: ", ftos(status), "\n");
400 playerstats_waitforme = TRUE;
401 if(playerstats_db >= 0)
403 db_close(playerstats_db);
413 string playerinfo_last;
414 string playerinfo_events_last;
417 void PlayerInfo_AddPlayer(entity e)
419 if(playerinfo_db < 0)
423 key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
426 p = db_get(playerinfo_db, key);
431 db_put(playerinfo_db, key, playerinfo_last);
432 strunzone(playerinfo_last);
435 db_put(playerinfo_db, key, "#");
436 playerinfo_last = strzone(ftos(e.playerid));
437 print(" Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
441 void PlayerInfo_AddItem(entity e, string item_id, string val)
443 if(playerinfo_db < 0)
447 key = sprintf("*:%s", item_id);
450 p = db_get(playerinfo_db, key);
453 if(playerinfo_events_last)
455 db_put(playerinfo_db, key, playerinfo_events_last);
456 strunzone(playerinfo_events_last);
459 db_put(playerinfo_db, key, "#");
460 playerinfo_events_last = strzone(item_id);
463 key = sprintf("#%d:%s", e.playerid, item_id);
464 db_put(playerinfo_db, key, val);
465 print(" Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
468 string PlayerInfo_GetItem(entity e, string item_id)
470 if(playerinfo_db < 0)
474 key = sprintf("#%d:%s", e.playerid, item_id);
475 return db_get(playerinfo_db, key);
478 string PlayerInfo_GetItemLocal(string item_id)
482 return PlayerInfo_GetItem(p, item_id);
485 void PlayerInfo_ready(entity fh, entity p, float status)
490 PlayerInfo_AddPlayer(p);
494 case URL_READY_CANWRITE:
495 print("-- Sending data to player stats server\n");
496 url_fputs(fh, "V 1\n");
498 url_fputs(fh, sprintf("R %s\n", WATERMARK));
501 url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
502 url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
503 url_fputs(fh, sprintf("g %s\n", cvar_string("_menu_prvm_gender"))); // gender
504 url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
505 url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
510 case URL_READY_CANREAD:
511 print("-- Got response from player stats server:\n");
512 string gametype = string_null;
513 while((s = url_fgets(fh)))
517 string key = "", value = "", data = "";
519 n = tokenizebyseparator(s, " "); // key (value) data
540 PlayerInfo_AddItem(p, "_version", data);
542 PlayerInfo_AddItem(p, "_release", data);
544 PlayerInfo_AddItem(p, "_time", data);
546 PlayerInfo_AddItem(p, "_statsurl", data);
548 PlayerInfo_AddItem(p, "_hashkey", data);
550 PlayerInfo_AddItem(p, "_playernick", data);
552 PlayerInfo_AddItem(p, "_playerid", data);
555 else if (key == "e" && value != "")
558 PlayerInfo_AddItem(p, value, data);
560 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
565 print("-- End of response.\n");
568 case URL_READY_CLOSED:
569 // url_fclose has finished
570 print("Player stats synchronized with server\n");
572 case URL_READY_ERROR:
574 print("Receiving player stats failed: ", ftos(status), "\n");
579 void PlayerInfo_Init()
582 playerinfo_db = db_create();
586 void PlayerInfo_Basic(entity p)
588 print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
590 if(playerinfo_db < 0)
594 uri = autocvar_g_playerinfo_uri;
595 if(uri != "" && p.crypto_idfp != "")
597 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
598 print("Retrieving playerstats from URL: ", uri, "\n");
599 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
605 void PlayerInfo_Details()
607 print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
609 if(playerinfo_db < 0)
613 uri = autocvar_g_playerinfo_uri; // FIXME
614 if(uri != "" && crypto_getmyidstatus(0) > 0)
616 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
617 uri = strcat(uri, "/player/me");
618 print("Retrieving playerstats from URL: ", uri, "\n");
619 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, world);
626 * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
627 void PlayerInfo_Details()
629 print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
631 if(playerinfo_db < 0)
635 uri = autocvar_g_playerinfo_uri; // FIXME
636 if(uri != "" && crypto_getmyidstatus(0) > 0)
638 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
639 print("Retrieving playerstats from URL: ", uri, "\n");
640 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);