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