]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
Merge branch 'master' into sev/luma
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / playerstats.qc
1 float playerstats_db;
2 string teamstats_last;
3 string playerstats_last;
4 string events_last;
5 .float playerstats_addedglobalinfo;
6 .string playerstats_id;
7
8 // Note that _time isn't mentioned here. That one is special.
9 #define ALL_ANTICHEATS \
10         ANTICHEAT("speedhack"); \
11         ANTICHEAT("speedhack_m1"); \
12         ANTICHEAT("speedhack_m2"); \
13         ANTICHEAT("speedhack_m3"); \
14         ANTICHEAT("speedhack_m4"); \
15         ANTICHEAT("speedhack_m5"); \
16         ANTICHEAT("div0_strafebot_old"); \
17         ANTICHEAT("div0_strafebot_new"); \
18         ANTICHEAT("div0_evade"); \
19         ANTICHEAT("idle_snapaim"); \
20         ANTICHEAT("idle_snapaim_signal"); \
21         ANTICHEAT("idle_snapaim_noise"); \
22         ANTICHEAT("idle_snapaim_m2"); \
23         ANTICHEAT("idle_snapaim_m3"); \
24         ANTICHEAT("idle_snapaim_m4"); \
25         ANTICHEAT("idle_snapaim_m7"); \
26         ANTICHEAT("idle_snapaim_m10");
27
28 void PlayerStats_Init() // initiated before InitGameplayMode so that scores are added properly
29 {
30         string uri;
31         playerstats_db = -1;
32         playerstats_waitforme = TRUE;
33         uri = autocvar_g_playerstats_uri;
34         if(uri == "")
35                 return;
36         playerstats_db = db_create();
37         if(playerstats_db >= 0)
38                 playerstats_waitforme = FALSE; // must wait for it at match end
39
40         serverflags |= SERVERFLAG_PLAYERSTATS;
41
42         PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
43         PlayerStats_AddEvent(PLAYERSTATS_AVGLATENCY);
44         PlayerStats_AddEvent(PLAYERSTATS_WINS);
45         PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
46         PlayerStats_AddEvent(PLAYERSTATS_JOINS);
47         PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
48         PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_POS);
49         PlayerStats_AddEvent(PLAYERSTATS_RANK);
50
51     // accuracy stats
52     entity w;
53     float i;
54     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
55     {
56         w = get_weaponinfo(i);
57
58         PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
59         PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
60
61         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
62         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
63
64         PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
65     }
66
67         PlayerStats_AddEvent("anticheat-_time");
68 #define ANTICHEAT(name) \
69         PlayerStats_AddEvent("anticheat-" name)
70         ALL_ANTICHEATS
71 #undef ANTICHEAT
72
73         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
74         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
75         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
76         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
77         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
78         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
79         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
80         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
81         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
82         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
83 }
84
85 void PlayerStats_AddPlayer(entity e)
86 {
87         string s;
88
89         if(playerstats_db < 0)
90                 return;
91         if(e.playerstats_id)
92                 return;
93
94         s = string_null;
95         if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
96                 s = e.crypto_idfp;
97         else if(IS_BOT_CLIENT(e))
98                 s = sprintf("bot#%g#%s", skill, e.cleanname);
99
100         if((s == "") || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
101         {
102                 if(IS_BOT_CLIENT(e))
103                         s = sprintf("bot#%d", e.playerid);
104                 else
105                         s = sprintf("player#%d", e.playerid);
106         }
107
108         e.playerstats_id = strzone(s);
109
110         string key;
111         key = sprintf("%s:*", e.playerstats_id);
112
113         string p;
114         p = db_get(playerstats_db, key);
115         if(p == "")
116         {
117                 if(playerstats_last)
118                 {
119                         db_put(playerstats_db, key, playerstats_last);
120                         strunzone(playerstats_last);
121                 }
122                 else
123                         db_put(playerstats_db, key, "#");
124                 playerstats_last = strzone(e.playerstats_id);
125         }
126 }
127
128 void PlayerStats_AddTeam(float t)
129 {
130         if(playerstats_db < 0)
131                 return;
132
133         string key;
134         key = sprintf("%d", t);
135
136         string p;
137         p = db_get(playerstats_db, key);
138         if(p == "")
139         {
140                 if(teamstats_last)
141                 {
142                         db_put(playerstats_db, key, teamstats_last);
143                         strunzone(teamstats_last);
144                 }
145                 else
146                         db_put(playerstats_db, key, "#");
147                 teamstats_last = strzone(key);
148         }
149 }
150
151 void PlayerStats_AddEvent(string event_id)
152 {
153         if(playerstats_db < 0)
154                 return;
155
156         string key;
157         key = sprintf("*:%s", event_id);
158
159         string p;
160         p = db_get(playerstats_db, key);
161         if(p == "")
162         {
163                 if(events_last)
164                 {
165                         db_put(playerstats_db, key, events_last);
166                         strunzone(events_last);
167                 }
168                 else
169                         db_put(playerstats_db, key, "#");
170                 events_last = strzone(event_id);
171         }
172 }
173
174 float PlayerStats_Event(entity e, string event_id, float value)
175 {
176         if((e.playerstats_id == "") || playerstats_db < 0)
177                 return 0;
178
179         string key;
180         float val;
181         key = sprintf("%s:%s", e.playerstats_id, event_id);
182         val = stof(db_get(playerstats_db, key));
183         val += value;
184         db_put(playerstats_db, key, ftos(val));
185         return val;
186 }
187
188 float PlayerStats_TeamScore(float t, string event_id, float value)
189 {
190         if(playerstats_db < 0)
191                 return 0;
192
193         string key;
194         float val;
195         key = sprintf("team#%d:%s", t, event_id);
196         val = stof(db_get(playerstats_db, key));
197         val += value;
198         db_put(playerstats_db, key, ftos(val));
199         return val;
200 }
201
202 /*
203         format spec:
204
205         A collection of lines of the format <key> SPACE <value> NEWLINE, where
206         <key> is always a single character.
207
208         The following keys are defined:
209
210         V: format version (always a fixed number) - this MUST be the first line!
211         #: comment (MUST be ignored by any parser)
212         R: release information on the server
213         T: time at which the game ended
214         G: game type
215         O: mod name (icon request) as in server browser
216         M: map name
217         I: match ID (see "matchid" in g_world.qc
218         S: "hostname" of the server
219         C: number of "unpure" cvar changes
220         U: UDP port number of the server
221         D: duration of the match
222         P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
223         Q: team number of an existing team (format: team#NN); this also sets the owner for all following "e" lines (lower case!)
224         n: nickname of the player (optional)
225         t: team ID
226         i: player index
227         e: followed by an event name, a space, and the event count/score
228                 event names can be:
229                         alivetime: total playing time of the player
230                         avglatency: average network latency compounded throughout the match
231                         wins: number of games won (can only be set if matches is set)
232                         matches: number of matches played to the end (not aborted by map switch)
233                         joins: number of matches joined (always 1 unless player never played during the match)
234                         scoreboardvalid: set to 1 if the player was there at the end of the match
235                         total-<scoreboardname>: total score of that scoreboard item
236                         scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
237                         achievement-<achievementname>: achievement counters (their "count" is usually 1 if nonzero at all)
238                         kills-<index>: number of kills against the indexed player
239                         rank <number>: rank of player
240                         acc-<weapon netname>-hit: total damage dealt
241                         acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
242                         acc-<weapon netname>-cnt-hit: amount of shots that actually hit
243                         acc-<weapon netname>-cnt-fired: amount of fired shots
244                         acc-<weapon netname>-frags: amount of frags dealt by weapon
245
246         Response format (not used yet): see https://gist.github.com/4284222
247 */
248
249 void PlayerStats_ready(entity fh, entity pass, float status)
250 {
251         string t, tn;
252         string p, pn;
253         string e, en;
254         string nn, tt;
255         string s;
256
257         switch(status)
258         {
259                 case URL_READY_CANWRITE:
260                         url_fputs(fh, "V 7\n");
261 #ifdef WATERMARK
262                         url_fputs(fh, sprintf("R %s\n", WATERMARK));
263 #endif
264                         url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
265                         url_fputs(fh, sprintf("G %s\n", GetGametype()));
266                         url_fputs(fh, sprintf("O %s\n", modname));
267                         url_fputs(fh, sprintf("M %s\n", GetMapname()));
268                         url_fputs(fh, sprintf("I %s\n", matchid));
269                         url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
270                         url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
271                         url_fputs(fh, sprintf("U %d\n", cvar("port")));
272                         url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
273                         if(teamplay)
274                         {
275                                 for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%d", stof(t)))) != ""; t = tn)
276                                 {
277                                         url_fputs(fh, sprintf("Q team#%s\n", t));
278                                         for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
279                                         {
280                                                 float v;
281                                                 v = stof(db_get(playerstats_db, sprintf("team#%d:%s", stof(t), e)));
282                                                 if(v != 0)
283                                                         url_fputs(fh, sprintf("e %s %g\n", e, v));
284                                         }
285                                 }
286                         }
287                         for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
288                         {
289                                 url_fputs(fh, sprintf("P %s\n", p));
290                                 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
291                                 if(nn != "")
292                                         url_fputs(fh, sprintf("i %s\n", nn));
293                                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
294                                 if(nn != "")
295                                         url_fputs(fh, sprintf("n %s\n", nn));
296                                 if(teamplay)
297                                 {
298                                         tt = db_get(playerstats_db, sprintf("%s:_team", p));
299                                         url_fputs(fh, sprintf("t %s\n", tt));
300                                 }
301                                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
302                                 {
303                                         float v;
304                                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
305                                         if(v != 0)
306                                                 url_fputs(fh, sprintf("e %s %g\n", e, v));
307                                 }
308                         }
309                         url_fputs(fh, "\n");
310                         url_fclose(fh);
311                         break;
312                 case URL_READY_CANREAD:
313                         // url_fclose is processing, we got a response for writing the data
314                         // this must come from HTTP
315                         print("Got response from player stats server:\n");
316                         while((s = url_fgets(fh)))
317                                 print("  ", s, "\n");
318                         print("End of response.\n");
319                         url_fclose(fh);
320                         break;
321                 case URL_READY_CLOSED:
322                         // url_fclose has finished
323                         print("Player stats written\n");
324                         playerstats_waitforme = TRUE;
325                         db_close(playerstats_db);
326                         playerstats_db = -1;
327                         break;
328                 case URL_READY_ERROR:
329                 default:
330                         print("Player stats writing failed: ", ftos(status), "\n");
331                         playerstats_waitforme = TRUE;
332                         if(playerstats_db >= 0)
333                         {
334                                 db_close(playerstats_db);
335                                 playerstats_db = -1;
336                         }
337                         break;
338         }
339 }
340
341 //#NO AUTOCVARS START
342 void PlayerStats_Shutdown()
343 {
344         string uri;
345
346         if(playerstats_db < 0)
347                 return;
348
349         uri = autocvar_g_playerstats_uri;
350         if(uri != "")
351         {
352                 playerstats_waitforme = FALSE;
353                 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
354         }
355         else
356         {
357                 playerstats_waitforme = TRUE;
358                 db_close(playerstats_db);
359                 playerstats_db = -1;
360         }
361 }
362 //#NO AUTOCVARS END
363
364 void PlayerStats_Accuracy(entity p)
365 {
366     entity a, w;
367     a = p.accuracy;
368     float i;
369
370     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
371     {
372         w = get_weaponinfo(i);
373
374         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
375         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
376
377         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
378         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
379
380         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
381     }
382     //backtrace(strcat("adding player stat accuracy for ", p.netname, ".\n"));
383 }
384
385 void PlayerStats_Anticheat(entity p)
386 {
387         entity oldself = self;
388         self = p;
389
390         float t0 = PlayerStats_Event(p, "anticheat-_time", 0);
391         float dt = anticheat_getvalue("_time");
392         PlayerStats_Event(p, "anticheat-_time", dt);
393         float f = dt / (t0 + dt);
394 #define ANTICHEAT(name) do { \
395                 float prev = PlayerStats_Event(p, "anticheat-" name, 0); \
396                 float change = (anticheat_getvalue(name) - prev) * f; \
397                 PlayerStats_Event(p, "anticheat-" name, change); \
398         } while(0)
399         ALL_ANTICHEATS
400 #undef ANTICHEAT
401         self = oldself;
402 }
403
404 void PlayerStats_AddGlobalInfo(entity p)
405 {
406         if(playerstats_db < 0)
407                 return;
408         if((p.playerstats_id == "") || playerstats_db < 0)
409                 return;
410         p.playerstats_addedglobalinfo = TRUE;
411
412         // add global info!
413         if(p.alivetime)
414         {
415                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
416                 p.alivetime = 0;
417         }
418
419         db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
420
421         if(p.cvar_cl_allow_uid2name == 1 || IS_BOT_CLIENT(p))
422                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
423
424         if(teamplay)
425                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
426
427         if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
428                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
429
430         PlayerStats_Accuracy(p);
431
432         PlayerStats_Anticheat(p);
433
434         if(IS_REAL_CLIENT(p))
435         {
436                 if(p.latency_cnt)
437                 {
438                         float latency = (p.latency_sum / p.latency_cnt);
439                         if(latency) { PlayerStats_Event(p, PLAYERSTATS_AVGLATENCY, latency); }
440                 }
441         }
442
443         strunzone(p.playerstats_id);
444         p.playerstats_id = string_null;
445 }
446
447 .float scoreboard_pos;
448 void PlayerStats_EndMatch(float finished)
449 {
450         entity p;
451         PlayerScore_Sort(score_dummyfield, 0, 0, 0);
452         PlayerScore_Sort(scoreboard_pos, 1, 1, 1);
453         if(teamplay)
454                 PlayerScore_TeamStats();
455         FOR_EACH_CLIENT(p)
456         {
457                 // add personal score rank
458                 PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
459
460                 if(!p.scoreboard_pos)
461                         continue;
462
463                 // scoreboard is valid!
464                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
465
466                 // add scoreboard position
467                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_POS, p.scoreboard_pos);
468
469                 // add scoreboard data
470                 PlayerScore_PlayerStats(p);
471
472                 // if the match ended normally, add winning info
473                 if(finished)
474                 {
475                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
476                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
477                 }
478         }
479 }
480
481 #undef ALL_ANTICHEATS