]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/playerstats.qc
0015da784e338f62650a30632f303b35a63018ec
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / playerstats.qc
1 #include "playerstats.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include "constants.qh"
6     #include "util.qh"
7     #include <common/weapons/_all.qh>
8     #include "../server/anticheat.qh"
9     #include "../server/defs.qh"
10     #include "../server/scores.qh"
11     #include "../server/weapons/accuracy.qh"
12 #endif
13
14 #ifdef SVQC
15 void PlayerStats_Prematch()
16 {
17         //foobar
18 }
19
20 void PlayerStats_GameReport_AddPlayer(entity e)
21 {
22         if((PS_GR_OUT_DB < 0) || (e.playerstats_id)) { return; }
23
24         // set up player identification
25         string s = "";
26
27         if((e.crypto_idfp != "") && (CS(e).cvar_cl_allow_uidtracking == 1))
28                 { s = e.crypto_idfp; }
29         else if(IS_BOT_CLIENT(e))
30                 { s = sprintf("bot#%g#%s", skill, e.cleanname); }
31
32         if((s == "") || find(NULL, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
33         {
34                 if(IS_BOT_CLIENT(e))
35                         { s = sprintf("bot#%d", e.playerid); }
36                 else
37                         { s = sprintf("player#%d", e.playerid); }
38         }
39
40         e.playerstats_id = strzone(s);
41
42         // now add the player to the database
43         string key = sprintf("%s:*", e.playerstats_id);
44         string p = db_get(PS_GR_OUT_DB, key);
45
46         if(p == "")
47         {
48                 if(PS_GR_OUT_PL)
49                 {
50                         db_put(PS_GR_OUT_DB, key, PS_GR_OUT_PL);
51                         strunzone(PS_GR_OUT_PL);
52                 }
53                 else { db_put(PS_GR_OUT_DB, key, "#"); }
54                 PS_GR_OUT_PL = strzone(e.playerstats_id);
55         }
56 }
57
58 void PlayerStats_GameReport_AddTeam(float t)
59 {
60         if(PS_GR_OUT_DB < 0) { return; }
61
62         string key = sprintf("%d", t);
63         string p = db_get(PS_GR_OUT_DB, key);
64
65         if(p == "")
66         {
67                 if(PS_GR_OUT_TL)
68                 {
69                         db_put(PS_GR_OUT_DB, key, PS_GR_OUT_TL);
70                         strunzone(PS_GR_OUT_TL);
71                 }
72                 else { db_put(PS_GR_OUT_DB, key, "#"); }
73                 PS_GR_OUT_TL = strzone(key);
74         }
75 }
76
77 void PlayerStats_GameReport_AddEvent(string event_id)
78 {
79         if(PS_GR_OUT_DB < 0) { return; }
80
81         string key = sprintf("*:%s", event_id);
82         string p = db_get(PS_GR_OUT_DB, key);
83
84         if(p == "")
85         {
86                 if(PS_GR_OUT_EVL)
87                 {
88                         db_put(PS_GR_OUT_DB, key, PS_GR_OUT_EVL);
89                         strunzone(PS_GR_OUT_EVL);
90                 }
91                 else { db_put(PS_GR_OUT_DB, key, "#"); }
92                 PS_GR_OUT_EVL = strzone(event_id);
93         }
94 }
95
96 // referred to by PS_GR_P_ADDVAL and PS_GR_T_ADDVAL
97 float PlayerStats_GameReport_Event(string prefix, string event_id, float value)
98 {
99         if((prefix == "") || PS_GR_OUT_DB < 0) { return 0; }
100
101         string key = sprintf("%s:%s", prefix, event_id);
102         float val = stof(db_get(PS_GR_OUT_DB, key));
103         val += value;
104         db_put(PS_GR_OUT_DB, key, ftos(val));
105         return val;
106 }
107
108 void PlayerStats_GameReport_Accuracy(entity p)
109 {
110         #define ACCMAC(suffix, field) \
111                 PS_GR_P_ADDVAL(p, sprintf("acc-%s-%s", it.netname, suffix), CS(p).accuracy.(field[i-1]));
112         FOREACH(Weapons, it != WEP_Null, {
113                 ACCMAC("hit", accuracy_hit)
114                 ACCMAC("fired", accuracy_fired)
115                 ACCMAC("cnt-hit", accuracy_cnt_hit)
116                 ACCMAC("cnt-fired", accuracy_cnt_fired)
117                 ACCMAC("frags", accuracy_frags)
118         });
119         #undef ACCMAC
120 }
121
122 void PlayerStats_GameReport_FinalizePlayer(entity p)
123 {
124         if((p.playerstats_id == "") || PS_GR_OUT_DB < 0) { return; }
125
126         // add global info!
127         if(p.alivetime)
128         {
129                 PS_GR_P_ADDVAL(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
130                 p.alivetime = 0;
131         }
132
133         db_put(PS_GR_OUT_DB, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
134
135         if(CS(p).cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
136                 db_put(PS_GR_OUT_DB, sprintf("%s:_netname", p.playerstats_id), playername(p, false));
137
138         if(teamplay)
139                 db_put(PS_GR_OUT_DB, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
140
141         if(stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
142                 PS_GR_P_ADDVAL(p, PLAYERSTATS_JOINS, 1);
143
144         PlayerStats_GameReport_Accuracy(p);
145         anticheat_report_to_playerstats(p);
146
147         if(IS_REAL_CLIENT(p))
148         {
149                 if(CS(p).latency_cnt)
150                 {
151                         float latency = (CS(p).latency_sum / CS(p).latency_cnt);
152                         if(latency) { PS_GR_P_ADDVAL(p, PLAYERSTATS_AVGLATENCY, latency); }
153                 }
154         }
155
156         strunzone(p.playerstats_id);
157         p.playerstats_id = string_null;
158 }
159
160 void PlayerStats_GameReport(float finished)
161 {
162         if(PS_GR_OUT_DB < 0) { return; }
163
164         PlayerScore_Sort(score_dummyfield, 0, 0, 0);
165         PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
166         if(teamplay) { PlayerScore_TeamStats(); }
167
168         FOREACH_CLIENT(true, {
169                 // add personal score rank
170                 PS_GR_P_ADDVAL(it, PLAYERSTATS_RANK, it.score_dummyfield);
171
172                 // scoreboard data
173                 if(it.scoreboard_pos)
174                 {
175                         // scoreboard is valid!
176                         PS_GR_P_ADDVAL(it, PLAYERSTATS_SCOREBOARD_VALID, 1);
177
178                         // add scoreboard position
179                         PS_GR_P_ADDVAL(it, PLAYERSTATS_SCOREBOARD_POS, it.scoreboard_pos);
180
181                         // add scoreboard data
182                         PlayerScore_PlayerStats(it);
183
184                         // if the match ended normally, add winning info
185                         if(finished)
186                         {
187                                 PS_GR_P_ADDVAL(it, PLAYERSTATS_WINS, it.winning);
188                                 PS_GR_P_ADDVAL(it, PLAYERSTATS_MATCHES, 1);
189                         }
190                 }
191
192                 // collect final player information
193                 PlayerStats_GameReport_FinalizePlayer(it);
194         });
195
196         if(autocvar_g_playerstats_gamereport_uri != "")
197         {
198                 PlayerStats_GameReport_DelayMapVote = true;
199                 url_multi_fopen(
200                         autocvar_g_playerstats_gamereport_uri,
201                         FILE_APPEND,
202                         PlayerStats_GameReport_Handler,
203                         NULL
204                 );
205         }
206         else
207         {
208                 PlayerStats_GameReport_DelayMapVote = false;
209                 db_close(PS_GR_OUT_DB);
210                 PS_GR_OUT_DB = -1;
211         }
212 }
213
214 void PlayerStats_GameReport_Init() // initiated before InitGameplayMode so that scores are added properly
215 {
216         if(autocvar_g_playerstats_gamereport_uri == "") { return; }
217
218         PS_GR_OUT_DB = db_create();
219
220         if(PS_GR_OUT_DB >= 0)
221         {
222                 PlayerStats_GameReport_DelayMapVote = true;
223
224                 serverflags |= SERVERFLAG_PLAYERSTATS;
225
226                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ALIVETIME);
227                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_AVGLATENCY);
228                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_WINS);
229                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_MATCHES);
230                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_JOINS);
231                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
232                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
233                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_RANK);
234
235                 // accuracy stats
236                 FOREACH(Weapons, it != WEP_Null, {
237                         PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-hit"));
238                         PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-fired"));
239                         PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-hit"));
240                         PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-cnt-fired"));
241                         PlayerStats_GameReport_AddEvent(strcat("acc-", it.netname, "-frags"));
242                 });
243
244                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
245                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
246                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
247                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
248                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
249                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
250                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
251                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
252                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
253                 PlayerStats_GameReport_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
254
255                 anticheat_register_to_playerstats();
256         }
257         else { PlayerStats_GameReport_DelayMapVote = false; }
258 }
259
260 // this... is a hack, a temporary one until we get a proper duel gametype
261 string PlayerStats_GetGametype()
262 {
263         if(IS_GAMETYPE(DEATHMATCH) && autocvar_g_maxplayers == 2)
264         {
265                 // probably duel, but let's make sure
266                 int plcount = 0;
267                 FOREACH_CLIENT(IS_PLAYER(it), ++plcount);
268                 if(plcount <= 2)
269                         return "duel";
270         }
271         return GetGametype();
272 }
273
274 void PlayerStats_GameReport_Handler(entity fh, entity pass, float status)
275 {
276         string t, tn;
277         string p, pn;
278         string e, en;
279         string nn, tt;
280         string s;
281
282         switch(status)
283         {
284                 // ======================================
285                 // -- OUTGOING GAME REPORT INFORMATION --
286                 // ======================================
287                 /* SPECIFICATIONS:
288                  * V: format version (always a fixed number) - this MUST be the first line!
289                  * #: comment (MUST be ignored by any parser)
290                  * R: release information on the server
291                  * G: game type
292                  * O: mod name (icon request) as in server browser
293                  * M: map name
294                  * I: match ID (see "matchid" in g_world.qc)
295                  * S: "hostname" of the server
296                  * C: number of "unpure" cvar changes
297                  * U: UDP port number of the server
298                  * D: duration of the match
299                  * L: "ladder" in which the server is participating in
300                  * P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
301                  * Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
302                  * i: player index
303                  * n: nickname of the player (optional)
304                  * t: team ID
305                  * e: followed by an event name, a space, and the event count/score
306                  *  event names can be:
307                  *   alivetime: total playing time of the player
308                  *   avglatency: average network latency compounded throughout the match
309                  *   wins: number of games won (can only be set if matches is set)
310                  *   matches: number of matches played to the end (not aborted by map switch)
311                  *   joins: number of matches joined (always 1 unless player never played during the match)
312                  *   scoreboardvalid: set to 1 if the player was there at the end of the match
313                  *   total-<scoreboardname>: total score of that scoreboard item
314                  *   scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
315                  *   achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
316                  *   kills-<index>: number of kills against the indexed player
317                  *   rank <number>: rank of player
318                  *   acc-<weapon netname>-hit: total damage dealt
319                  *   acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
320                  *   acc-<weapon netname>-cnt-hit: amount of shots that actually hit
321                  *   acc-<weapon netname>-cnt-fired: amount of fired shots
322                  *   acc-<weapon netname>-frags: amount of frags dealt by weapon
323                  */
324                 case URL_READY_CANWRITE:
325                 {
326                         url_fputs(fh, "V 9\n");
327                         #ifdef WATERMARK
328                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
329                         #endif
330                         url_fputs(fh, sprintf("G %s\n", PlayerStats_GetGametype()));
331                         url_fputs(fh, sprintf("O %s\n", modname));
332                         url_fputs(fh, sprintf("M %s\n", GetMapname()));
333                         url_fputs(fh, sprintf("I %s\n", matchid));
334                         url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
335                         url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
336                         url_fputs(fh, sprintf("U %d\n", cvar("port")));
337                         url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
338                         url_fputs(fh, sprintf("L %s\n", autocvar_g_playerstats_gamereport_ladder));
339
340                         // TEAMS
341                         if(teamplay)
342                         {
343                                 for(t = PS_GR_OUT_TL; (tn = db_get(PS_GR_OUT_DB, sprintf("%d", stof(t)))) != ""; t = tn)
344                                 {
345                                         // start team section
346                                         url_fputs(fh, sprintf("Q team#%s\n", t));
347
348                                         // output team events // todo: does this do unnecessary loops? perhaps we should do a separate "team_events_last" tracker..."
349                                         for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
350                                         {
351                                                 float v = stof(db_get(PS_GR_OUT_DB, sprintf("team#%d:%s", stof(t), e)));
352                                                 if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
353                                         }
354                                 }
355                         }
356
357                         // PLAYERS
358                         for(p = PS_GR_OUT_PL; (pn = db_get(PS_GR_OUT_DB, sprintf("%s:*", p))) != ""; p = pn)
359                         {
360                                 // start player section
361                                 url_fputs(fh, sprintf("P %s\n", p));
362
363                                 // playerid/index (entity number for this server)
364                                 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_playerid", p));
365                                 if(nn != "") { url_fputs(fh, sprintf("i %s\n", nn)); }
366
367                                 // player name
368                                 nn = db_get(PS_GR_OUT_DB, sprintf("%s:_netname", p));
369                                 if(nn != "") { url_fputs(fh, sprintf("n %s\n", nn)); }
370
371                                 // team identification number
372                                 if(teamplay)
373                                 {
374                                         tt = db_get(PS_GR_OUT_DB, sprintf("%s:_team", p));
375                                         url_fputs(fh, sprintf("t %s\n", tt));
376                                 }
377
378                                 // output player events
379                                 for(e = PS_GR_OUT_EVL; (en = db_get(PS_GR_OUT_DB, sprintf("*:%s", e))) != ""; e = en)
380                                 {
381                                         float v = stof(db_get(PS_GR_OUT_DB, sprintf("%s:%s", p, e)));
382                                         if(v != 0) { url_fputs(fh, sprintf("e %s %g\n", e, v)); }
383                                 }
384                         }
385                         url_fputs(fh, "\n");
386                         url_fclose(fh);
387                         break;
388                 }
389
390                 // ======================================
391                 // -- INCOMING GAME REPORT INFORMATION --
392                 // ======================================
393                 /* SPECIFICATIONS:
394                  * stuff
395                  */
396                 case URL_READY_CANREAD:
397                 {
398                         // url_fclose is processing, we got a response for writing the data
399                         // this must come from HTTP
400                         LOG_TRACE("Got response from player stats server:");
401                         while((s = url_fgets(fh))) { LOG_TRACE("  ", s); }
402                         LOG_TRACE("End of response.");
403                         url_fclose(fh);
404                         break;
405                 }
406
407                 case URL_READY_CLOSED:
408                 {
409                         // url_fclose has finished
410                         LOG_TRACE("Player stats written");
411                         PlayerStats_GameReport_DelayMapVote = false;
412                         if(PS_GR_OUT_DB >= 0)
413                         {
414                                 db_close(PS_GR_OUT_DB);
415                                 PS_GR_OUT_DB = -1;
416                         }
417                         break;
418                 }
419
420                 case URL_READY_ERROR:
421                 default:
422                 {
423                         LOG_INFO("Player stats writing failed: ", ftos(status), "\n");
424                         PlayerStats_GameReport_DelayMapVote = false;
425                         if(PS_GR_OUT_DB >= 0)
426                         {
427                                 db_close(PS_GR_OUT_DB);
428                                 PS_GR_OUT_DB = -1;
429                         }
430                         break;
431                 }
432         }
433 }
434
435 void PlayerStats_PlayerBasic(entity joiningplayer, float newrequest)
436 {
437         PlayerScore_Add(joiningplayer, SP_ELO, -1);
438         // http://stats.xonotic.org/player/GgXRw6piDtFIbMArMuiAi8JG4tiin8VLjZgsKB60Uds=/elo.txt
439         if(autocvar_g_playerstats_playerbasic_uri != "")
440         {
441                 string uri = autocvar_g_playerstats_playerbasic_uri;
442                 if (joiningplayer.crypto_idfp == "") {
443                         PlayerScore_Add(joiningplayer, SP_ELO, -1);
444                 } else {
445                         // create the database if it doesn't already exist
446                         if(PS_B_IN_DB < 0)
447                                 PS_B_IN_DB = db_create();
448
449                         // now request the information
450                         uri = strcat(uri, "/player/", uri_escape(uri_escape(uri_escape(joiningplayer.crypto_idfp))), "/elo.txt");
451                         LOG_TRACE("Retrieving playerstats from URL: ", uri);
452                         url_single_fopen(
453                                 uri,
454                                 FILE_APPEND,
455                                 PlayerStats_PlayerBasic_Handler,
456                                 joiningplayer
457                         );
458
459                         // set status appropriately // todo: check whether the player info exists in the database previously
460                         if(newrequest)
461                         {
462                                 // database still contains useful information, so don't clear it of a useful status
463                                 joiningplayer.playerstats_basicstatus = PS_B_STATUS_WAITING;
464                         }
465                         else
466                         {
467                                 // database was previously empty or never hit received status for some reason
468                                 joiningplayer.playerstats_basicstatus = PS_B_STATUS_UPDATING;
469                         }
470                 }
471         }
472         else
473         {
474                 // server has this disabled, kill the DB and set status to idle
475                 PlayerScore_Add(joiningplayer, SP_ELO, -1);
476                 if(PS_B_IN_DB >= 0)
477                 {
478                         db_close(PS_B_IN_DB);
479                         PS_B_IN_DB = -1;
480
481                         FOREACH_CLIENT(IS_REAL_CLIENT(it), it.playerstats_basicstatus = PS_B_STATUS_IDLE);
482                 }
483         }
484 }
485
486 void PlayerStats_PlayerBasic_CheckUpdate(entity joiningplayer)
487 {
488         // determine whether we should retrieve playerbasic information again
489
490         LOG_TRACEF("PlayerStats_PlayerBasic_CheckUpdate('%s'): %f",
491                 joiningplayer.netname,
492                 time
493         );
494
495         // TODO: check to see if this playerid is inside the database already somehow...
496         // for now we'll just check the field, but this won't work for players who disconnect and reconnect properly
497         // although maybe we should just submit another request ANYWAY?
498         if(!joiningplayer.playerstats_basicstatus)
499         {
500                 PlayerStats_PlayerBasic(
501                         joiningplayer,
502                         (joiningplayer.playerstats_basicstatus == PS_B_STATUS_RECEIVED)
503                 );
504         }
505 }
506
507 void PlayerStats_PlayerBasic_Handler(entity fh, entity p, float status)
508 {
509         switch(status)
510         {
511                 case URL_READY_CANWRITE:
512                 {
513                         LOG_TRACE("-- Sending data to player stats server");
514                         /*url_fputs(fh, "V 1\n");
515                         #ifdef WATERMARK
516                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
517                         #endif
518                         url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
519                         url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
520                         url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
521                         url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
522                         url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
523                         */url_fputs(fh, "\n");
524                         url_fclose(fh);
525                         return;
526                 }
527
528                 case URL_READY_CANREAD:
529                 {
530                         bool handled = false;
531                         string gt = string_null;
532                         for (string s = ""; (s = url_fgets(fh)); ) {
533                                 int n = tokenizebyseparator(s, " "); // key value? data
534                                 if (n == 1) continue;
535                                 string key = "", value = "", data = "";
536                                 if (n == 2) {
537                     key = argv(0);
538                     data = argv(1);
539                                 } else if (n >= 3) {
540                     key = argv(0);
541                     value = argv(1);
542                     data = argv(2);
543                                 }
544                 switch (key) {
545                     case "V":
546                         // PlayerInfo_AddItem(p, "_version", data);
547                         break;
548                     case "R":
549                         // PlayerInfo_AddItem(p, "_release", data);
550                         break;
551                     case "T":
552                         // PlayerInfo_AddItem(p, "_time", data);
553                         break;
554                     case "S":
555                         // PlayerInfo_AddItem(p, "_statsurl", data);
556                         break;
557                     case "P":
558                         // PlayerInfo_AddItem(p, "_hashkey", data);
559                         break;
560                     case "n":
561                         // PlayerInfo_AddItem(p, "_playernick", data);
562                         break;
563                     case "i":
564                         // PlayerInfo_AddItem(p, "_playerid", data);
565                         // p.xonstat_id = stof(data);
566                         break;
567                     case "G":
568                         gt = data;
569                         break;
570                     case "e":
571                         LOG_TRACE("G: ", gt);
572                         LOG_TRACE("e: ", data);
573                         if (gt == PlayerStats_GetGametype()) {
574                             handled = true;
575                             float e = stof(data);
576                             PlayerScore_Add(p, SP_ELO, +1 + e);
577                         }
578                         if (gt == "") {
579                             // PlayerInfo_AddItem(p, value, data);
580                         } else {
581                             // PlayerInfo_AddItem(p, sprintf("%s/%s", gt, value), data);
582                         }
583                         break;
584                 }
585                         }
586                         url_fclose(fh);
587                         if (handled) return;
588                         break;
589                 }
590                 case URL_READY_CLOSED:
591                 {
592                         // url_fclose has finished
593                         LOG_INFO("Player stats synchronized with server\n");
594                         return;
595                 }
596
597                 case URL_READY_ERROR:
598                 default:
599                 {
600                         LOG_INFO("Receiving player stats failed: ", ftos(status), "\n");
601                         break;
602                 }
603         }
604         PlayerScore_Add(p, SP_ELO, -1);
605 }
606 #endif // SVQC
607
608 #ifdef MENUQC
609
610
611 #if 0 // reading the entire DB at once
612         string e = "", en = "";
613         float i = 0;
614         for(e = PS_D_IN_EVL; (en = db_get(PS_D_IN_DB, e)) != ""; e = en)
615         {
616                 LOG_INFO(sprintf("%d:%s:%s\n", i, e, db_get(PS_D_IN_DB, sprintf("#%s", e))));
617                 ++i;
618         }
619 #endif
620
621 void PlayerStats_PlayerDetail_AddItem(string event, string data)
622 {
623         if(PS_D_IN_DB < 0) { return; }
624
625         // create a marker for the event so that we can access it later
626         string marker = sprintf("%s", event);
627         if(db_get(PS_D_IN_DB, marker) == "")
628         {
629                 if(PS_D_IN_EVL)
630                 {
631                         db_put(PS_D_IN_DB, marker, PS_D_IN_EVL);
632                         strunzone(PS_D_IN_EVL);
633                 }
634                 else { db_put(PS_D_IN_DB, marker, "#"); }
635                 PS_D_IN_EVL = strzone(marker);
636         }
637
638         // now actually set the event data
639         db_put(PS_D_IN_DB, sprintf("#%s", event), data);
640         LOG_TRACE("Added item ", sprintf("#%s", event), "=", data, " to PS_D_IN_DB");
641 }
642
643 void PlayerStats_PlayerDetail()
644 {
645         // http://stats.xonotic.org/player/me
646         if((autocvar_g_playerstats_playerdetail_uri != "") && (crypto_getmyidstatus(0) > 0))
647         {
648                 // create the database if it doesn't already exist
649                 if(PS_D_IN_DB < 0)
650                         PS_D_IN_DB = db_create();
651
652                 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
653                 LOG_TRACE("Retrieving playerstats from URL: ", autocvar_g_playerstats_playerdetail_uri);
654                 url_single_fopen(
655                         autocvar_g_playerstats_playerdetail_uri,
656                         FILE_APPEND,
657                         PlayerStats_PlayerDetail_Handler,
658                         NULL
659                 );
660
661                 PlayerStats_PlayerDetail_Status = PS_D_STATUS_WAITING;
662         }
663         else
664         {
665                 // player has this disabled, kill the DB and set status to idle
666                 if(PS_D_IN_DB >= 0)
667                 {
668                         db_close(PS_D_IN_DB);
669                         PS_D_IN_DB = -1;
670                 }
671
672                 PlayerStats_PlayerDetail_Status = PS_D_STATUS_IDLE;
673         }
674 }
675
676 void PlayerStats_PlayerDetail_CheckUpdate()
677 {
678         // determine whether we should retrieve playerdetail information again
679         float gamecount = cvar("cl_matchcount");
680
681         #if 0
682         LOG_INFOF("PlayerStats_PlayerDetail_CheckUpdate(): %f >= %f, %d > %d\n",
683                 time,
684                 PS_D_NEXTUPDATETIME,
685                 PS_D_LASTGAMECOUNT,
686                 gamecount
687         );
688         #endif
689
690         if(
691                 (time >= PS_D_NEXTUPDATETIME)
692                 ||
693                 (gamecount > PS_D_LASTGAMECOUNT)
694         )
695         {
696                 PlayerStats_PlayerDetail();
697                 PS_D_NEXTUPDATETIME = (time + autocvar_g_playerstats_playerdetail_autoupdatetime);
698                 PS_D_LASTGAMECOUNT = gamecount;
699         }
700 }
701
702 void PlayerStats_PlayerDetail_Handler(entity fh, entity unused, float status)
703 {
704         switch(status)
705         {
706                 case URL_READY_CANWRITE:
707                 {
708                         LOG_TRACE("PlayerStats_PlayerDetail_Handler(): Sending data to player stats server...");
709                         url_fputs(fh, "V 1\n");
710                         #ifdef WATERMARK
711                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
712                         #endif
713                         url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
714                         //url_fputs(fh, sprintf("c %s\n", cvar_string("_cl_country"))); // country
715                         //url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
716                         url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
717                         url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
718                         url_fputs(fh, "\n");
719                         url_fclose(fh);
720                         break;
721                 }
722
723                 case URL_READY_CANREAD:
724                 {
725                         //print("PlayerStats_PlayerDetail_Handler(): Got response from player stats server:\n");
726                         string input = "";
727                         string gametype = "overall";
728                         while((input = url_fgets(fh)))
729                         {
730                                 float count = tokenizebyseparator(input, " ");
731                                 string key = "", event = "", data = "";
732
733                                 if(argv(0) == "#") { continue; }
734
735                                 if(count == 2)
736                                 {
737                                         key = argv(0);
738                                         data = substring(input, argv_start_index(1), strlen(input) - argv_start_index(1));
739                                 }
740                                 else if(count >= 3)
741                                 {
742                                         key = argv(0);
743                                         event = argv(1);
744                                         data = substring(input, argv_start_index(2), strlen(input) - argv_start_index(2));
745                                 }
746                                 else { continue; }
747
748                                 switch(key)
749                                 {
750                                         // general info
751                                         case "V": PlayerStats_PlayerDetail_AddItem("version", data); break;
752                                         case "R": PlayerStats_PlayerDetail_AddItem("release", data); break;
753                                         case "T": PlayerStats_PlayerDetail_AddItem("time", data); break;
754
755                                         // player info
756                                         case "S": PlayerStats_PlayerDetail_AddItem("statsurl", data); break;
757                                         case "P": PlayerStats_PlayerDetail_AddItem("hashkey", data); break;
758                                         case "n": PlayerStats_PlayerDetail_AddItem("playernick", data); break;
759                                         case "i": PlayerStats_PlayerDetail_AddItem("playerid", data); break;
760
761                                         // other/event info
762                                         case "G": gametype = data; break;
763                                         case "e":
764                                         {
765                                                 if(event != "" && data != "")
766                                                 {
767                                                         PlayerStats_PlayerDetail_AddItem(
768                                                                 sprintf(
769                                                                         "%s/%s",
770                                                                         gametype,
771                                                                         event
772                                                                 ),
773                                                                 data
774                                                         );
775                                                 }
776                                                 break;
777                                         }
778
779                                         default:
780                                         {
781                                                 LOG_INFOF(
782                                                         "PlayerStats_PlayerDetail_Handler(): ERROR: "
783                                                         "Key went unhandled? Is our version outdated?\n"
784                                                         "PlayerStats_PlayerDetail_Handler(): "
785                                                         "Key '%s', Event '%s', Data '%s'\n",
786                                                         key,
787                                                         event,
788                                                         data
789                                                 );
790                                                 break;
791                                         }
792                                 }
793
794                                 #if 0
795                                 LOG_INFO(sprintf(
796                                         "PlayerStats_PlayerDetail_Handler(): "
797                                         "Key '%s', Event '%s', Data '%s'\n",
798                                         key,
799                                         event,
800                                         data
801                                 ));
802                                 #endif
803                         }
804                         //print("PlayerStats_PlayerDetail_Handler(): End of response.\n");
805                         url_fclose(fh);
806                         PlayerStats_PlayerDetail_Status = PS_D_STATUS_RECEIVED;
807                         statslist.getStats(statslist);
808                         break;
809                 }
810
811                 case URL_READY_CLOSED:
812                 {
813                         // url_fclose has finished
814                         LOG_INFO("PlayerStats_PlayerDetail_Handler(): Player stats synchronized with server.\n");
815                         break;
816                 }
817
818                 case URL_READY_ERROR:
819                 default:
820                 {
821                         LOG_INFO("PlayerStats_PlayerDetail_Handler(): Receiving player stats failed: ", ftos(status), "\n");
822                         PlayerStats_PlayerDetail_Status = PS_D_STATUS_ERROR;
823                         if(PS_D_IN_DB >= 0)
824                         {
825                                 db_close(PS_D_IN_DB);
826                                 PS_D_IN_DB = -1;
827                         }
828                         break;
829                 }
830         }
831 }
832 #endif
833
834 /*
835 void PlayerInfo_AddPlayer(entity e)
836 {
837         if(playerinfo_db < 0)
838                 return;
839
840         string key;
841         key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
842
843         string p;
844         p = db_get(playerinfo_db, key);
845         if(p == "")
846         {
847                 if(playerinfo_last)
848                 {
849                         db_put(playerinfo_db, key, playerinfo_last);
850                         strunzone(playerinfo_last);
851                 }
852                 else
853                         db_put(playerinfo_db, key, "#");
854                 playerinfo_last = strzone(ftos(e.playerid));
855                 print("  Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
856         }
857 }
858
859 void PlayerInfo_AddItem(entity e, string item_id, string val)
860 {
861         if(playerinfo_db < 0)
862                 return;
863
864         string key;
865         key = sprintf("*:%s", item_id);
866
867         string p;
868         p = db_get(playerinfo_db, key);
869         if(p == "")
870         {
871                 if(playerinfo_events_last)
872                 {
873                         db_put(playerinfo_db, key, playerinfo_events_last);
874                         strunzone(playerinfo_events_last);
875                 }
876                 else
877                         db_put(playerinfo_db, key, "#");
878                 playerinfo_events_last = strzone(item_id);
879         }
880
881         key = sprintf("#%d:%s", e.playerid, item_id);
882         db_put(playerinfo_db, key, val);
883         print("  Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
884 }
885
886 string PlayerInfo_GetItem(entity e, string item_id)
887 {
888         if(playerinfo_db < 0)
889                 return "";
890
891         string key;
892         key = sprintf("#%d:%s",  e.playerid, item_id);
893         return db_get(playerinfo_db, key);
894 }
895
896 string PlayerInfo_GetItemLocal(string item_id)
897 {
898         entity p = spawn();
899         p.playerid = 0;
900         return PlayerInfo_GetItem(p, item_id);
901 }
902
903 void PlayerInfo_ready(entity fh, entity p, float status)
904 {
905         float n;
906         string s;
907
908         PlayerInfo_AddPlayer(p);
909
910         switch(status)
911         {
912                 case URL_READY_CANWRITE:
913                         print("-- Sending data to player stats server\n");
914                         url_fputs(fh, "V 1\n");
915 #ifdef WATERMARK
916                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
917 #endif
918 #ifdef MENUQC
919                         url_fputs(fh, sprintf("l %s\n", cvar_string("_menu_prvm_language"))); // language
920                         url_fputs(fh, sprintf("c %s\n", cvar_string("_menu_prvm_country"))); // country
921                         url_fputs(fh, sprintf("g %s\n", cvar_string("_cl_gender"))); // gender
922                         url_fputs(fh, sprintf("n %s\n", cvar_string("_cl_name"))); // name
923                         url_fputs(fh, sprintf("m %s %s\n", cvar_string("_cl_playermodel"), cvar_string("_cl_playerskin"))); // model/skin
924 #endif
925                         url_fputs(fh, "\n");
926                         url_fclose(fh);
927                         break;
928                 case URL_READY_CANREAD:
929                         print("-- Got response from player stats server:\n");
930                         string gametype = string_null;
931                         while((s = url_fgets(fh)))
932                         {
933                                 print("  ", s, "\n");
934
935                                 string key = "", value = "", data = "";
936
937                                 n = tokenizebyseparator(s, " "); // key (value) data
938                                 if (n == 1)
939                                         continue;
940                                 else if (n == 2)
941                                 {
942                                         key = argv(0);
943                                         data = argv(1);
944                                 }
945                                 else if (n >= 3)
946                                 {
947                                         key = argv(0);
948                                         value = argv(1);
949                                         data = argv(2);
950                                 }
951
952                                 if (data == "")
953                                         continue;
954
955                                 if (key == "#")
956                                         continue;
957                                 else if (key == "V")
958                                         PlayerInfo_AddItem(p, "_version", data);
959                                 else if (key == "R")
960                                         PlayerInfo_AddItem(p, "_release", data);
961                                 else if (key == "T")
962                                         PlayerInfo_AddItem(p, "_time", data);
963                                 else if (key == "S")
964                                         PlayerInfo_AddItem(p, "_statsurl", data);
965                                 else if (key == "P")
966                                         PlayerInfo_AddItem(p, "_hashkey", data);
967                                 else if (key == "n")
968                                         PlayerInfo_AddItem(p, "_playernick", data);
969                                 else if (key == "i")
970                                         PlayerInfo_AddItem(p, "_playerid", data);
971                                 else if (key == "G")
972                                         gametype = data;
973                                 else if (key == "e" && value != "")
974                                 {
975                                         if (gametype == "")
976                                                 PlayerInfo_AddItem(p, value, data);
977                                         else
978                                                 PlayerInfo_AddItem(p, sprintf("%s/%s", gametype, value), data);
979                                 }
980                                 else
981                                         continue;
982                         }
983                         print("-- End of response.\n");
984                         url_fclose(fh);
985                         break;
986                 case URL_READY_CLOSED:
987                         // url_fclose has finished
988                         print("Player stats synchronized with server\n");
989                         break;
990                 case URL_READY_ERROR:
991                 default:
992                         print("Receiving player stats failed: ", ftos(status), "\n");
993                         break;
994         }
995 }
996
997 void PlayerInfo_Init()
998 {
999         playerinfo_db = db_create();
1000 }
1001
1002 #ifdef SVQC
1003 void PlayerInfo_Basic(entity p)
1004 {
1005         print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
1006
1007         if(playerinfo_db < 0)
1008                 return;
1009
1010         string uri;
1011         uri = autocvar_g_playerinfo_uri;
1012         if(uri != "" && p.crypto_idfp != "")
1013         {
1014                 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
1015                 print("Retrieving playerstats from URL: ", uri, "\n");
1016                 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1017         }
1018 }
1019 #endif
1020
1021 #ifdef MENUQC
1022 void PlayerInfo_Details()
1023 {
1024         print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
1025
1026         if(playerinfo_db < 0)
1027                 return;
1028
1029         string uri;
1030         uri = autocvar_g_playerinfo_uri; // FIXME
1031         if(uri != "" && crypto_getmyidstatus(0) > 0)
1032         {
1033                 //uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1034                 uri = strcat(uri, "/player/me");
1035                 print("Retrieving playerstats from URL: ", uri, "\n");
1036                 url_single_fopen(uri, FILE_APPEND, PlayerInfo_ready, NULL);
1037         }
1038 }
1039 #endif
1040
1041 #ifdef CSQC
1042 // FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qh:885)
1043 void PlayerInfo_Details()
1044 {
1045         print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
1046
1047         if(playerinfo_db < 0)
1048                 return;
1049
1050         string uri;
1051         uri = autocvar_g_playerinfo_uri; // FIXME
1052         if(uri != "" && crypto_getmyidstatus(0) > 0)
1053         {
1054                 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
1055                 print("Retrieving playerstats from URL: ", uri, "\n");
1056                 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
1057         }
1058 }
1059
1060 #endif
1061 */