]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
use cleanname, not netname, for bot names in stats
[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 void PlayerStats_Init()
9 {
10         string uri;
11         playerstats_db = -1;
12         playerstats_waitforme = TRUE;
13         uri = autocvar_g_playerstats_uri;
14         if(uri == "")
15                 return;
16         playerstats_db = db_create();
17         if(playerstats_db >= 0)
18                 playerstats_waitforme = FALSE; // must wait for it at match end
19
20         serverflags |= SERVERFLAG_PLAYERSTATS;  
21
22         PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
23         PlayerStats_AddEvent(PLAYERSTATS_WINS);
24         PlayerStats_AddEvent(PLAYERSTATS_MATCHES);
25         PlayerStats_AddEvent(PLAYERSTATS_JOINS);
26         PlayerStats_AddEvent(PLAYERSTATS_SCOREBOARD_VALID);
27         PlayerStats_AddEvent(PLAYERSTATS_RANK);
28
29     // accuracy stats
30     entity w;
31     float i;
32     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
33     {
34         w = get_weaponinfo(i);
35
36         PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
37         PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
38
39         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
40         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
41
42         PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
43     }
44
45         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
46         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
47         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
48         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
49         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
50         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
51         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
52         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
53         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
54         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
55 }
56
57 void PlayerStats_AddPlayer(entity e)
58 {
59         string s;
60
61         if(playerstats_db < 0)
62                 return;
63         if(e.playerstats_id)
64                 return;
65
66         s = string_null;
67         if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
68                 s = e.crypto_idfp;
69         else if(clienttype(e) == CLIENTTYPE_BOT)
70                 s = sprintf("bot#%g#%s", skill, e.cleanname);
71
72         if(!s || find(world, playerstats_id, s)) // already have one of the ID - next one can't be tracked then!
73         {
74                 if(clienttype(e) == CLIENTTYPE_BOT)
75                         s = sprintf("bot#%d", e.playerid);
76                 else
77                         s = sprintf("player#%d", e.playerid);
78         }
79
80         e.playerstats_id = strzone(s);
81
82         string key;
83         key = sprintf("%s:*", e.playerstats_id);
84         
85         string p;
86         p = db_get(playerstats_db, key);
87         if(p == "")
88         {
89                 if(playerstats_last)
90                 {
91                         db_put(playerstats_db, key, playerstats_last);
92                         strunzone(playerstats_last);
93                 }
94                 else
95                         db_put(playerstats_db, key, "#");
96                 playerstats_last = strzone(e.playerstats_id);
97         }
98 }
99
100 void PlayerStats_AddTeam(float t) // TODO: doesn't this remain unused?
101 {
102         if(playerstats_db < 0)
103                 return;
104
105         string key;
106         key = sprintf("%d", t);
107         
108         string p;
109         p = db_get(playerstats_db, key);
110         if(p == "")
111         {
112                 if(teamstats_last)
113                 {
114                         db_put(playerstats_db, key, teamstats_last);
115                         strunzone(teamstats_last);
116                 }
117                 else
118                         db_put(playerstats_db, key, "#");
119                 teamstats_last = strzone(key);
120         }
121 }
122
123 void PlayerStats_AddEvent(string event_id)
124 {
125         if(playerstats_db < 0)
126                 return;
127         
128         string key;
129         key = sprintf("*:%s", event_id);
130         
131         string p;
132         p = db_get(playerstats_db, key);
133         if(p == "")
134         {
135                 if(events_last)
136                 {
137                         db_put(playerstats_db, key, events_last);
138                         strunzone(events_last);
139                 }
140                 else
141                         db_put(playerstats_db, key, "#");
142                 events_last = strzone(event_id);
143         }
144 }
145
146 void PlayerStats_Event(entity e, string event_id, float value)
147 {
148         if(!e.playerstats_id || playerstats_db < 0)
149                 return;
150         
151         string key;
152         float val;
153         key = sprintf("%s:%s", e.playerstats_id, event_id);
154         val = stof(db_get(playerstats_db, key));
155         val += value;
156         db_put(playerstats_db, key, ftos(val));
157 }
158
159 void PlayerStats_TeamScore(float t, string event_id, float value) // TODO: doesn't this remain unused?
160 {
161         string key;
162         float val;
163         key = sprintf("team#%d:%s", t, event_id);
164         val = stof(db_get(playerstats_db, key));
165         val += value;
166         db_put(playerstats_db, key, ftos(val));
167 }
168
169 /*
170         format spec:
171
172         A collection of lines of the format <key> SPACE <value> NEWLINE, where
173         <key> is always a single character.
174
175         The following keys are defined:
176
177         V: format version (always 1) - this MUST be the first line!
178         #: comment (MUST be ignored by any parser)
179         R: release information on the server
180         T: time at which the game ended
181         G: game type
182         M: map name
183         I: match ID (see "matchid" in g_world.qc
184         S: "hostname" of the server
185         C: number of "unpure" cvar changes
186         U: UDP port number of the server
187         P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
188         n: nickname of the player (optional)
189         t: team ID
190         e: followed by an event name, a space, and the event count/score
191                 event names can be:
192                         alivetime: total playing time of the player
193                         wins: number of games won (can only be set if matches is set)
194                         matches: number of matches played to the end (not aborted by map switch)
195                         joins: number of matches joined (always 1 unless player never played during the match)
196                         scoreboardvalid: set to 1 if the player was there at the end of the match
197                         total-<scoreboardname>: total score of that scoreboard item
198                         scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
199                         achievement-<achievementname>: achievement counters
200                         rank <number>: rank of player
201                         acc-<weapon netname>-hit: total damage dealt
202                         acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
203                         acc-<weapon netname>-cnt-hit: amount of shots that actually hit
204                         acc-<weapon netname>-cnt-fired: amount of fired shots
205                         acc-<weapon netname>-frags: amount of frags dealt by weapon
206 */
207
208 void PlayerStats_ready(entity fh, entity pass, float status)
209 {
210         string p, pn;
211         string e, en;
212         string nn, tt;
213         string s;
214
215         switch(status)
216         {
217                 case URL_READY_CANWRITE:
218                         url_fputs(fh, "V 1\n");
219 #ifdef WATERMARK
220                         url_fputs(fh, sprintf("R %s\n", WATERMARK()));
221 #endif
222                         url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
223                         url_fputs(fh, sprintf("G %s\n", GetGametype()));
224                         url_fputs(fh, sprintf("M %s\n", GetMapname()));
225                         url_fputs(fh, sprintf("I %s\n", matchid));
226                         url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
227                         url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
228                         url_fputs(fh, sprintf("U %d\n", cvar("port")));
229                         for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
230                         {
231                                 url_fputs(fh, sprintf("P %s\n", p));
232                                 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
233                                 if(nn != "")
234                                         url_fputs(fh, sprintf("i %s\n", nn));
235                                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
236                                 if(nn != "")
237                                         url_fputs(fh, sprintf("n %s\n", nn));
238                                 if(teamplay)
239                                 {
240                                         tt = db_get(playerstats_db, sprintf("%s:_team", p));
241                                         url_fputs(fh, sprintf("t %s\n", tt));
242                                 }
243                                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
244                                 {
245                                         float v;
246                                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
247                                         if(v != 0)
248                                                 url_fputs(fh, sprintf("e %s %g\n", e, v));
249                                 }
250                         }
251                         url_fputs(fh, "\n");
252                         url_fclose(fh);
253                         break;
254                 case URL_READY_CANREAD:
255                         // url_fclose is processing, we got a response for writing the data
256                         // this must come from HTTP
257                         print("Got response from player stats server:\n");
258                         while((s = url_fgets(fh)))
259                                 print("  ", s, "\n");
260                         print("End of response.\n");
261                         url_fclose(fh);
262                         break;
263                 case URL_READY_CLOSED:
264                         // url_fclose has finished
265                         print("Player stats written\n");
266                         playerstats_waitforme = TRUE;
267                         db_close(playerstats_db);
268                         playerstats_db = -1;
269                         break;
270                 case URL_READY_ERROR:
271                 default:
272                         print("Player stats writing failed: ", ftos(status), "\n");
273                         playerstats_waitforme = TRUE;
274                         if(playerstats_db >= 0)
275                         {
276                                 db_close(playerstats_db);
277                                 playerstats_db = -1;
278                         }
279                         break;
280         }
281 }
282
283 //#NO AUTOCVARS START
284 void PlayerStats_Shutdown()
285 {
286         string uri;
287
288         if(playerstats_db < 0)
289                 return;
290
291         uri = autocvar_g_playerstats_uri;
292         if(uri != "")
293         {
294                 playerstats_waitforme = FALSE;
295                 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
296         }
297         else
298         {
299                 playerstats_waitforme = TRUE;
300                 db_close(playerstats_db);
301                 playerstats_db = -1;
302         }
303 }
304 //#NO AUTOCVARS END
305
306 void PlayerStats_Accuracy(entity p)
307 {
308     entity a, w;
309     a = p.accuracy;
310     float i;
311
312     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
313     {
314         w = get_weaponinfo(i);
315
316         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
317         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
318
319         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
320         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
321
322         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
323     }
324 }
325
326 void PlayerStats_AddGlobalInfo(entity p)
327 {
328         if(playerstats_db < 0)
329                 return;
330         if(!p.playerstats_id || playerstats_db < 0)
331                 return;
332         p.playerstats_addedglobalinfo = TRUE;
333
334         // add global info!
335         if(p.alivetime)
336         {
337                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
338                 p.alivetime = 0;
339         }
340
341         db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
342         
343         if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
344                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
345
346     if(teamplay)
347                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
348
349         if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
350                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
351
352         PlayerStats_Accuracy(p);
353
354         strunzone(p.playerstats_id);
355         p.playerstats_id = string_null;
356 }
357
358 void PlayerStats_EndMatch(float finished)
359 {
360         entity p, winner;
361         winner = PlayerScore_Sort(score_dummyfield);
362         FOR_EACH_CLIENT(p) // spectators intentionally not included
363         {
364                 PlayerStats_Accuracy(p);
365                 if(g_arena || g_lms || g_ca)
366                 {
367                         if(p.alivetime <= 0)
368                                 continue;
369                 }
370                 else
371                 {
372                         if(p.classname != "player")
373                                 continue;
374                 }
375                 PlayerScore_PlayerStats(p);
376                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
377                 if(finished)
378                 {
379                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
380                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
381                         PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
382                 }
383         }
384 }