]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
Merge branch 'master' of git://de.git.xonotic.org/xonotic/xonotic-data.pk3dir
[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         D: duration of the match
188         P: player ID of an existing player; this also sets the owner for all following "n", "e" and "t" lines (lower case!)
189         n: nickname of the player (optional)
190         t: team ID
191         e: followed by an event name, a space, and the event count/score
192                 event names can be:
193                         alivetime: total playing time of the player
194                         wins: number of games won (can only be set if matches is set)
195                         matches: number of matches played to the end (not aborted by map switch)
196                         joins: number of matches joined (always 1 unless player never played during the match)
197                         scoreboardvalid: set to 1 if the player was there at the end of the match
198                         total-<scoreboardname>: total score of that scoreboard item
199                         scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
200                         achievement-<achievementname>: achievement counters
201                         rank <number>: rank of player
202                         acc-<weapon netname>-hit: total damage dealt
203                         acc-<weapon netname>-fired: total damage that all fired projectiles *could* have dealt
204                         acc-<weapon netname>-cnt-hit: amount of shots that actually hit
205                         acc-<weapon netname>-cnt-fired: amount of fired shots
206                         acc-<weapon netname>-frags: amount of frags dealt by weapon
207 */
208
209 void PlayerStats_ready(entity fh, entity pass, float status)
210 {
211         string p, pn;
212         string e, en;
213         string nn, tt;
214         string s;
215
216         switch(status)
217         {
218                 case URL_READY_CANWRITE:
219                         url_fputs(fh, "V 1\n");
220 #ifdef WATERMARK
221                         url_fputs(fh, sprintf("R %s\n", WATERMARK()));
222 #endif
223                         url_fputs(fh, sprintf("T %s.%06d\n", strftime(FALSE, "%s"), floor(random() * 1000000)));
224                         url_fputs(fh, sprintf("G %s\n", GetGametype()));
225                         url_fputs(fh, sprintf("M %s\n", GetMapname()));
226                         url_fputs(fh, sprintf("I %s\n", matchid));
227                         url_fputs(fh, sprintf("S %s\n", cvar_string("hostname")));
228                         url_fputs(fh, sprintf("C %d\n", cvar_purechanges_count));
229                         url_fputs(fh, sprintf("U %d\n", cvar("port")));
230                         url_fputs(fh, sprintf("D %f\n", max(0, time - game_starttime)));
231                         for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
232                         {
233                                 url_fputs(fh, sprintf("P %s\n", p));
234                                 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
235                                 if(nn != "")
236                                         url_fputs(fh, sprintf("i %s\n", nn));
237                                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
238                                 if(nn != "")
239                                         url_fputs(fh, sprintf("n %s\n", nn));
240                                 if(teamplay)
241                                 {
242                                         tt = db_get(playerstats_db, sprintf("%s:_team", p));
243                                         url_fputs(fh, sprintf("t %s\n", tt));
244                                 }
245                                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
246                                 {
247                                         float v;
248                                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
249                                         if(v != 0)
250                                                 url_fputs(fh, sprintf("e %s %g\n", e, v));
251                                 }
252                         }
253                         url_fputs(fh, "\n");
254                         url_fclose(fh);
255                         break;
256                 case URL_READY_CANREAD:
257                         // url_fclose is processing, we got a response for writing the data
258                         // this must come from HTTP
259                         print("Got response from player stats server:\n");
260                         while((s = url_fgets(fh)))
261                                 print("  ", s, "\n");
262                         print("End of response.\n");
263                         url_fclose(fh);
264                         break;
265                 case URL_READY_CLOSED:
266                         // url_fclose has finished
267                         print("Player stats written\n");
268                         playerstats_waitforme = TRUE;
269                         db_close(playerstats_db);
270                         playerstats_db = -1;
271                         break;
272                 case URL_READY_ERROR:
273                 default:
274                         print("Player stats writing failed: ", ftos(status), "\n");
275                         playerstats_waitforme = TRUE;
276                         if(playerstats_db >= 0)
277                         {
278                                 db_close(playerstats_db);
279                                 playerstats_db = -1;
280                         }
281                         break;
282         }
283 }
284
285 //#NO AUTOCVARS START
286 void PlayerStats_Shutdown()
287 {
288         string uri;
289
290         if(playerstats_db < 0)
291                 return;
292
293         uri = autocvar_g_playerstats_uri;
294         if(uri != "")
295         {
296                 playerstats_waitforme = FALSE;
297                 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
298         }
299         else
300         {
301                 playerstats_waitforme = TRUE;
302                 db_close(playerstats_db);
303                 playerstats_db = -1;
304         }
305 }
306 //#NO AUTOCVARS END
307
308 void PlayerStats_Accuracy(entity p)
309 {
310     entity a, w;
311     a = p.accuracy;
312     float i;
313
314     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
315     {
316         w = get_weaponinfo(i);
317
318         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
319         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
320
321         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
322         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
323
324         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
325     }
326 }
327
328 void PlayerStats_AddGlobalInfo(entity p)
329 {
330         if(playerstats_db < 0)
331                 return;
332         if(!p.playerstats_id || playerstats_db < 0)
333                 return;
334         p.playerstats_addedglobalinfo = TRUE;
335
336         // add global info!
337         if(p.alivetime)
338         {
339                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
340                 p.alivetime = 0;
341         }
342
343         db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
344         
345         if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
346                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
347
348     if(teamplay)
349                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
350
351         if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
352                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
353
354         PlayerStats_Accuracy(p);
355
356         strunzone(p.playerstats_id);
357         p.playerstats_id = string_null;
358 }
359
360 void PlayerStats_EndMatch(float finished)
361 {
362         entity p, winner;
363         winner = PlayerScore_Sort(score_dummyfield);
364         FOR_EACH_CLIENT(p) // spectators intentionally not included
365         {
366                 PlayerStats_Accuracy(p);
367                 if(g_arena || g_lms || g_ca)
368                 {
369                         if(p.alivetime <= 0)
370                                 continue;
371                 }
372                 else
373                 {
374                         if(p.classname != "player")
375                                 continue;
376                 }
377                 PlayerScore_PlayerStats(p);
378                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
379                 if(finished)
380                 {
381                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
382                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
383                         PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
384                 }
385         }
386 }