]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/playerstats.qc
7d6a713bad477dca33278e559eb9ba197d199b91
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / playerstats.qc
1 #ifdef SVQC
2
3 float playerstats_db;
4 string teamstats_last;
5 string playerstats_last;
6 string events_last;
7 .float playerstats_addedglobalinfo;
8 .string playerstats_id;
9
10 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
11 {
12         string uri;
13         playerstats_db = -1;
14         playerstats_waitforme = TRUE;
15         uri = autocvar_g_playerstats_uri;
16         if(uri == "")
17                 return;
18         playerstats_db = db_create();
19         if(playerstats_db >= 0)
20                 playerstats_waitforme = FALSE; // must wait for it at match end
21
22         serverflags |= SERVERFLAG_PLAYERSTATS;
23
24         PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
25         PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
26         PlayerStats_AddEvent(PLAYERSTATS_WINS);
27         PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
28         PlayerStats_AddEvent(PLAYERSTATS_JOINS);
29         PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
30         PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
31         PlayerStats_AddEvent(PLAYERSTATS_RANK);
32
33     // accuracy stats
34     entity w;
35     float i;
36     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
37     {
38         w = get_weaponinfo(i);
39
40         PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
41         PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
42
43         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
44         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
45
46         PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
47     }
48
49         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
50         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
51         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
52         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
53         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
54         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
55         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
56         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
57         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
58         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
59 }
60
61 void PlayerStats_AddPlayer(entity e)
62 {
63         string s;
64
65         if(playerstats_db < 0)
66                 return;
67         if(e.playerstats_id)
68                 return;
69
70         s = string_null;
71         if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
72                 s = e.crypto_idfp;
73         else if(IS_BOT_CLIENT(e))
74                 s = sprintf("bot#%g#%s", skill, e.cleanname);
75
76         if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
77         {
78                 if(IS_BOT_CLIENT(e))
79                         s = sprintf("bot#%d", e.playerid);
80                 else
81                         s = sprintf("player#%d", e.playerid);
82         }
83
84         e.playerstats_id = strzone(s);
85
86         string key;
87         key = sprintf("%s:*", e.playerstats_id);
88
89         string p;
90         p = db_get(playerstats_db, key);
91         if(p == "")
92         {
93                 if(playerstats_last)
94                 {
95                         db_put(playerstats_db, key, playerstats_last);
96                         strunzone(playerstats_last);
97                 }
98                 else
99                         db_put(playerstats_db, key, "#");
100                 playerstats_last = strzone(e.playerstats_id);
101         }
102 }
103
104 void PlayerStats_AddTeam(float t)
105 {
106         if(playerstats_db < 0)
107                 return;
108
109         string key;
110         key = sprintf("%d", t);
111
112         string p;
113         p = db_get(playerstats_db, key);
114         if(p == "")
115         {
116                 if(teamstats_last)
117                 {
118                         db_put(playerstats_db, key, teamstats_last);
119                         strunzone(teamstats_last);
120                 }
121                 else
122                         db_put(playerstats_db, key, "#");
123                 teamstats_last = strzone(key);
124         }
125 }
126
127 void PlayerStats_AddEvent(string event_id)
128 {
129         if(playerstats_db < 0)
130                 return;
131
132         string key;
133         key = sprintf("*:%s", event_id);
134
135         string p;
136         p = db_get(playerstats_db, key);
137         if(p == "")
138         {
139                 if(events_last)
140                 {
141                         db_put(playerstats_db, key, events_last);
142                         strunzone(events_last);
143                 }
144                 else
145                         db_put(playerstats_db, key, "#");
146                 events_last = strzone(event_id);
147         }
148 }
149
150 float PlayerStats_Event(entity e, string event_id, float value)
151 {
152         if((e.playerstats_id == "") || playerstats_db < 0)
153                 return 0;
154
155         string key;
156         float val;
157         key = sprintf("%s:%s", e.playerstats_id, event_id);
158         val = stof(db_get(playerstats_db, key));
159         val += value;
160         db_put(playerstats_db, key, ftos(val));
161         return val;
162 }
163
164 float PlayerStats_TeamScore(float t, string event_id, float value)
165 {
166         if(playerstats_db < 0)
167                 return 0;
168
169         string key;
170         float val;
171         key = sprintf("team#%d:%s", t, event_id);
172         val = stof(db_get(playerstats_db, key));
173         val += value;
174         db_put(playerstats_db, key, ftos(val));
175         return val;
176 }
177
178 /*
179         format spec:
180
181         A collection of lines of the format <key> SPACE <value> NEWLINE, where
182         <key> is always a single character.
183
184         The following keys are defined:
185
186         V: format version (always a fixed number) - this MUST be the first line!
187         #: comment (MUST be ignored by any parser)
188         R: release information on the server
189         T: time at which the game ended
190         G: game type
191         O: mod name (icon request) as in server browser
192         M: map name
193         I: match ID (see "matchid" in g_world.qc
194         S: "hostname" of the server
195         C: number of "unpure" cvar changes
196         U: UDP port number of the server
197         D: duration of the match
198         P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
199         Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
200         n: nickname of the player (optional)
201         t: team ID
202         i: player index
203         e: followed by an event name, a space, and the event count/score
204                 event names can be:
205                         alivetime: total playing time of the player
206                         avglatency: average network latency compounded throughout the match
207                         wins: number of games won (can only be set if matches is set)
208                         matches: number of matches played to the end (not aborted by map switch)
209                         joins: number of matches joined (always 1 unless player never played during the match)
210                         scoreboardvalid: set to 1 if the player was there at the end of the match
211                         total-<scoreboardname>: total score of that scoreboard item
212                         scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
213                         achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
214                         kills-<index>: number of kills against the indexed player
215                         rank <number>: rank of player
216                         acc-<weapon netname>-hit: total damage dealt
217                         acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
218                         acc-<weapon netname>-cnt-hit: amount of shots that actually hit
219                         acc-<weapon netname>-cnt-fired: amount of fired shots
220                         acc-<weapon netname>-frags: amount of frags dealt by weapon
221
222         Response format (not used yet): see https://gist.github.com/4284222
223 */
224
225 void PlayerStats_ready(entity fh, entity pass, float status)
226 {
227         string t, tn;
228         string p, pn;
229         string e, en;
230         string nn, tt;
231         string s;
232
233         switch(status)
234         {
235                 case URL_READY_CANWRITE:
236                         url_fputs(fh, "V 7\n");
237 #ifdef WATERMARK
238                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
239 #endif
240                         url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
241                         url_fputs(fh, sprintf("G %s\n", GetGametype()));
242                         url_fputs(fh, sprintf("O %s\n", modname));
243                         url_fputs(fh, sprintf("M %s\n", GetMapname()));
244                         url_fputs(fh, sprintf("I %s\n", matchid));
245                         url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
246                         url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
247                         url_fputs(fh, sprintf("U %d\n", cvar("port")));
248                         url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
249                         if(teamplay)
250                         {
251                                 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
252                                 {
253                                         url_fputs(fh, sprintf("Q team#%s\n", t));
254                                         for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
255                                         {
256                                                 float v;
257                                                 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
258                                                 if(v != 0)
259                                                         url_fputs(fh, sprintf("e %s %g\n", e, v));
260                                         }
261                                 }
262                         }
263                         for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
264                         {
265                                 url_fputs(fh, sprintf("P %s\n", p));
266                                 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
267                                 if(nn != "")
268                                         url_fputs(fh, sprintf("i %s\n", nn));
269                                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
270                                 if(nn != "")
271                                         url_fputs(fh, sprintf("n %s\n", nn));
272                                 if(teamplay)
273                                 {
274                                         tt = db_get(playerstats_db, sprintf("%s:_team", p));
275                                         url_fputs(fh, sprintf("t %s\n", tt));
276                                 }
277                                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
278                                 {
279                                         float v;
280                                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
281                                         if(v != 0)
282                                                 url_fputs(fh, sprintf("e %s %g\n", e, v));
283                                 }
284                         }
285                         url_fputs(fh, "\n");
286                         url_fclose(fh);
287                         break;
288                 case URL_READY_CANREAD:
289                         // url_fclose is processing, we got a response for writing the data
290                         // this must come from HTTP
291                         print("Got response from player stats server:\n");
292                         while((s = url_fgets(fh)))
293                                 print("  ", s, "\n");
294                         print("End of response.\n");
295                         url_fclose(fh);
296                         break;
297                 case URL_READY_CLOSED:
298                         // url_fclose has finished
299                         print("Player stats written\n");
300                         playerstats_waitforme = TRUE;
301                         db_close(playerstats_db);
302                         playerstats_db = -1;
303                         break;
304                 case URL_READY_ERROR:
305                 default:
306                         print("Player stats writing failed: ", ftos(status), "\n");
307                         playerstats_waitforme = TRUE;
308                         if(playerstats_db >= 0)
309                         {
310                                 db_close(playerstats_db);
311                                 playerstats_db = -1;
312                         }
313                         break;
314         }
315 }
316
317 //#NO AUTOCVARS START
318 void PlayerStats_Shutdown()
319 {
320         string uri;
321
322         if(playerstats_db < 0)
323                 return;
324
325         uri = autocvar_g_playerstats_uri;
326         if(uri != "")
327         {
328                 playerstats_waitforme = FALSE;
329                 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
330         }
331         else
332         {
333                 playerstats_waitforme = TRUE;
334                 db_close(playerstats_db);
335                 playerstats_db = -1;
336         }
337 }
338 //#NO AUTOCVARS END
339
340 void PlayerStats_Accuracy(entity p)
341 {
342     entity a, w;
343     a = p.accuracy;
344     float i;
345
346     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
347     {
348         w = get_weaponinfo(i);
349
350         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
351         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
352
353         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
354         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
355
356         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
357     }
358     //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
359 }
360
361 void PlayerStats_AddGlobalInfo(entity p)
362 {
363         if(playerstats_db < 0)
364                 return;
365         if((p.playerstats_id == "") || playerstats_db < 0)
366                 return;
367         p.playerstats_addedglobalinfo = TRUE;
368
369         // add global info!
370         if(p.alivetime)
371         {
372                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
373                 p.alivetime = 0;
374         }
375
376         db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
377
378         if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
379                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
380
381         if(teamplay)
382                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
383
384         if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
385                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
386
387         PlayerStats_Accuracy(p);
388
389         if(IS_REAL_CLIENT(p))
390         {
391                 if(p.latency_cnt)
392                 {
393                         float latency = (p.latency_sum / p.latency_cnt);
394                         if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
395                 }
396         }
397
398         strunzone(p.playerstats_id);
399         p.playerstats_id = string_null;
400 }
401
402 .float scoreboard_pos;
403 void PlayerStats_EndMatch(float finished)
404 {
405         entity p;
406         PlayerScore_Sort(score_dummyfield, 0, 0, 0);
407         PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
408         if(teamplay)
409                 PlayerScore_TeamStats();
410         FOR_EACH_CLIENT(p)
411         {
412                 // add personal score rank
413                 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
414
415                 if(!p.scoreboard_pos)
416                         continue;
417
418                 // scoreboard is valid!
419                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
420
421                 // add scoreboard position
422                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
423
424                 // add scoreboard data
425                 PlayerScore_PlayerStats(p);
426
427                 // if the match ended normally, add winning info
428                 if(finished)
429                 {
430                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
431                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
432                 }
433         }
434 }
435
436 #endif // SVQC
437
438
439
440
441 //// WIP -zykure /////////////////////////////////////////////////////
442
443
444
445
446 float playerinfo_db;
447 string playerinfo_last;
448 string playerinfo_events_last;
449 .float playerid;
450
451 void PlayerInfo_AddPlayer(entity e)
452 {
453         if(playerinfo_db < 0)
454                 return;
455
456         string key;
457         key = sprintf("#%d:*", e.playerid); // TODO: use hashkey instead?
458
459         string p;
460         p = db_get(playerinfo_db, key);
461         if(p == "")
462         {
463                 if(playerinfo_last)
464                 {
465                         db_put(playerinfo_db, key, playerinfo_last);
466                         strunzone(playerinfo_last);
467                 }
468                 else
469                         db_put(playerinfo_db, key, "#");
470                 playerinfo_last = strzone(ftos(e.playerid));
471                 print("  Added player ", ftos(e.playerid), " to playerinfo_db\n");//DEBUG//
472         }
473 }
474
475 void PlayerInfo_AddItem(entity e, string item_id, string val)
476 {
477         if(playerinfo_db < 0)
478                 return;
479
480         string key;
481         key = sprintf("*:%s", item_id);
482
483         string p;
484         p = db_get(playerinfo_db, key);
485         if(p == "")
486         {
487                 if(playerinfo_events_last)
488                 {
489                         db_put(playerinfo_db, key, playerinfo_events_last);
490                         strunzone(playerinfo_events_last);
491                 }
492                 else
493                         db_put(playerinfo_db, key, "#");
494                 playerinfo_events_last = strzone(item_id);
495         }
496
497         key = sprintf("#%d:%s", e.playerid, item_id);
498         db_put(playerinfo_db, key, val);
499
500         print("  Added item ", key, "=", val, " to playerinfo_db\n");//DEBUG//
501 }
502
503 string PlayerInfo_GetItem(entity e, string item_id)
504 {
505         if(playerinfo_db < 0)
506                 return string_null;
507
508         string key;
509         key = sprintf("#%d:%s",  e.playerid, item_id);
510         return db_get(playerinfo_db, key);
511 }
512
513 void PlayerInfo_ready(entity fh, entity p, float status)
514 {
515         float n;
516         string s;
517
518         PlayerInfo_AddPlayer(p);
519
520         switch(status)
521         {
522                 case URL_READY_CANREAD:
523                         print("-- Got response from player stats server:\n");
524                         float in_group = FALSE;
525                         string gametype = string_null;
526                         while((s = url_fgets(fh)))
527                         {
528                                 print("  >> ", s, "\n");
529
530                                 string key = string_null, value = string_null, data = string_null;
531
532                                 n = tokenizebyseparator(s, " "); // key (value) data
533                                 if (n == 1)
534                                 {
535                                         if (argv(0) == "}")
536                                                 in_group = FALSE;
537                                 }
538                                 else if (n == 2)
539                                 {
540                                         key = argv(0);
541                                         data = argv(1);
542                                 }
543                                 else if (n == 3)
544                                 {
545                                         if (argv(0) == "{")
546                                         {
547                                                 in_group = TRUE;
548                                                 key = argv(1);
549                                                 data = argv(2);
550                                         }
551                                         else
552                                         {
553                                                 key = argv(0);
554                                                 value = argv(1);
555                                                 data = argv(2);
556                                         }
557                                 }
558                                 else if (n == 4)
559                                 {
560                                         key = argv(0);
561                                         value = argv(1);
562                                         data = argv(2);
563                                 }
564                                 else
565                                         continue;
566
567                                 if (data == "")
568                                         continue;
569
570                                 if (key == "#")
571                                         // comment
572                                         continue;
573                                 else if (key == "V")
574                                         // version
575                                         PlayerInfo_AddItem(p, "_version", data);
576                                 else if (key == "R")
577                                         // watermark
578                                         PlayerInfo_AddItem(p, "_release", data);
579                                 else if (key == "T")
580                                         // timestamp
581                                         PlayerInfo_AddItem(p, "_time", data);
582                                 else if (key == "S")
583                                         // stats page URL
584                                         PlayerInfo_AddItem(p, "_statsurl", data);
585                                 else if (key == "P")
586                                         // player hashkey
587                                         PlayerInfo_AddItem(p, "_hashkey", data);
588                                 else if (key == "n")
589                                         // player nick
590                                         PlayerInfo_AddItem(p, "_playernick", data);
591                                 else if (key == "i")
592                                         // xonstats id
593                                         PlayerInfo_AddItem(p, "_playerid", data);
594                                 else if (key == "G" && in_group)
595                                         gametype = data;
596                                 else if (key == "e" && value != "")
597                                 {
598                                         if (gametype == "")
599                                                 PlayerInfo_AddItem(p, value, data);
600                                         else
601                                                 PlayerInfo_AddItem(p, sprintf("%s-%s", gametype, value), data);
602                                 }
603                                 else
604                                         continue;
605                         }
606                         print("-- End of response.\n");
607                         url_fclose(fh);
608                         break;
609                 case URL_READY_CLOSED:
610                         // url_fclose has finished
611                         print("Player stats received from server\n");
612                         break;
613                 case URL_READY_ERROR:
614                 default:
615                         print("Receiving player stats failed: ", ftos(status), "\n");
616                         break;
617         }
618 }
619
620 void PlayerInfo_Init()
621 {
622         playerinfo_db = -1;
623         playerinfo_db = db_create();
624 }
625
626 #ifdef SVQC
627 void PlayerInfo_Basic(entity p)
628 {
629         print("-- Getting basic PlayerInfo for player ",ftos(p.playerid)," (SVQC)\n");
630
631         if(playerinfo_db < 0)
632                 return;
633
634         string uri;
635         uri = playerinfo_uri; // FIXME
636         if(uri != "" && p.crypto_idfp != "")
637         {
638                 uri = strcat(uri, "/elo/", uri_escape(p.crypto_idfp));
639                 print("Retrieving playerstats from URL: ", uri, "\n");
640                 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
641         }
642 }
643 #endif
644
645 #ifdef MENUQC
646 void PlayerInfo_Details()
647 {
648         print("-- Getting detailed PlayerInfo for local player (MENUQC)\n");
649
650         if(playerinfo_db < 0)
651                 return;
652
653         string uri;
654         uri = playerinfo_uri; // FIXME
655         if(uri != "" && crypto_getmyidstatus(0) > 0)
656         {
657                 entity p = spawn();
658                 p.playerid = 0; // TODO: okay to use 0 for local player? or does local player already has an entity in MENUQC?
659                 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
660                 print("Retrieving playerstats from URL: ", uri, "\n");
661                 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
662         }
663 }
664 #endif
665
666 #ifdef CSQC
667 /*
668  * FIXME - crypto_* builtin functions missing in CSQC (csprogsdefs.qc:885)
669 void PlayerInfo_Details()
670 {
671         print("-- Getting detailed PlayerInfo for local player (CSQC)\n");
672
673         if(playerinfo_db < 0)
674                 return;
675
676         string uri;
677         uri = playerinfo_uri; // FIXME
678         if(uri != "" && crypto_getmyidstatus(0) > 0)
679         {
680                 entity p = spawn();
681                 p.playerid = 0; // TODO: okay to use -1 for local player? or does local player already has an entity in MENUQC?
682                 uri = strcat(uri, "/player/", uri_escape(crypto_getmyidfp(0)));
683                 print("Retrieving playerstats from URL: ", uri, "\n");
684                 url_single_fopen(uri, FILE_READ, PlayerInfo_ready, p);
685         }
686 }
687 */
688 #endif