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