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