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