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