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