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