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