]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
if server has playerstats, show uid2name dialog too
[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 float playerstats_requested;
7 .string playerstats_id;
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         serverflags |= SERVERFLAG_PLAYERSTATS;  
22
23         PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
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_RANK);
29
30     // accuracy stats
31     entity w;
32     float i;
33     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
34     {
35         w = get_weaponinfo(i);
36
37         PlayerStats_AddEvent(strcat("acc-", w.netname, "-hit"));
38         PlayerStats_AddEvent(strcat("acc-", w.netname, "-fired"));
39
40         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-hit"));
41         PlayerStats_AddEvent(strcat("acc-", w.netname, "-cnt-fired"));
42
43         PlayerStats_AddEvent(strcat("acc-", w.netname, "-frags"));
44     }
45
46         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_3);
47         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_5);
48         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_10);
49         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_15);
50         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_20);
51         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_25);
52         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_KILL_SPREE_30);
53         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_BOTLIKE);
54         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTBLOOD);
55         PlayerStats_AddEvent(PLAYERSTATS_ACHIEVEMENT_FIRSTVICTIM);
56 }
57
58 void PlayerStats_AddPlayer(entity e)
59 {
60         if(playerstats_db < 0)
61                 return;
62         if(e.playerstats_id)
63                 return;
64
65         if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
66                 e.playerstats_id = strzone(e.crypto_idfp);
67         else if(clienttype(e) == CLIENTTYPE_BOT)
68                 e.playerstats_id = strzone(sprintf("bot#%d", e.playerid));
69         else
70                 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
71
72         string key;
73         key = sprintf("%s:*", e.playerstats_id);
74         
75         string p;
76         p = db_get(playerstats_db, key);
77         if(p == "")
78         {
79                 if(playerstats_last)
80                 {
81                         db_put(playerstats_db, key, playerstats_last);
82                         strunzone(playerstats_last);
83                 }
84                 else
85                         db_put(playerstats_db, key, "#");
86                 playerstats_last = strzone(e.playerstats_id);
87         }
88 }
89
90 void PlayerStats_AddTeam(float t) // TODO: doesn't this remain unused?
91 {
92         if(playerstats_db < 0)
93                 return;
94
95         string key;
96         key = sprintf("%d", t);
97         
98         string p;
99         p = db_get(playerstats_db, key);
100         if(p == "")
101         {
102                 if(teamstats_last)
103                 {
104                         db_put(playerstats_db, key, teamstats_last);
105                         strunzone(teamstats_last);
106                 }
107                 else
108                         db_put(playerstats_db, key, "#");
109                 teamstats_last = strzone(key);
110         }
111 }
112
113 void PlayerStats_AddEvent(string event_id)
114 {
115         if(playerstats_db < 0)
116                 return;
117         
118         string key;
119         key = sprintf("*:%s", event_id);
120         
121         string p;
122         p = db_get(playerstats_db, key);
123         if(p == "")
124         {
125                 if(events_last)
126                 {
127                         db_put(playerstats_db, key, events_last);
128                         strunzone(events_last);
129                 }
130                 else
131                         db_put(playerstats_db, key, "#");
132                 events_last = strzone(event_id);
133         }
134 }
135
136 void PlayerStats_Event(entity e, string event_id, float value)
137 {
138         if(!e.playerstats_id || playerstats_db < 0)
139                 return;
140         
141         string key;
142         float val;
143         key = sprintf("%s:%s", e.playerstats_id, event_id);
144         val = stof(db_get(playerstats_db, key));
145         val += value;
146         db_put(playerstats_db, key, ftos(val));
147 }
148
149 void PlayerStats_TeamScore(float t, string event_id, float value) // TODO: doesn't this remain unused?
150 {
151         string key;
152         float val;
153         key = sprintf("team#%d:%s", t, event_id);
154         val = stof(db_get(playerstats_db, key));
155         val += value;
156         db_put(playerstats_db, key, ftos(val));
157 }
158
159 /*
160         format spec:
161
162         A collection of lines of the format <key> SPACE <value> NEWLINE, where
163         <key> is always a single character.
164
165         The following keys are defined:
166
167         V: format version (always 1) - this MUST be the first line!
168         #: comment (MUST be ignored by any parser)
169         R: release information on the server
170         T: time at which the game ended
171         G: game type
172         M: map name
173         S: "hostname" of the server
174         C: number of "unpure" cvar changes
175     W: winning team ID
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                         for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
218                         {
219                                 url_fputs(fh, sprintf("P %s\n", p));
220                                 nn = db_get(playerstats_db, sprintf("%s:_playerid", p));
221                                 if(nn != "")
222                                         url_fputs(fh, sprintf("i %s\n", nn));
223                                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
224                                 if(nn != "")
225                                         url_fputs(fh, sprintf("n %s\n", nn));
226                                 if(teamplay)
227                                 {
228                                         tt = db_get(playerstats_db, sprintf("%s:_team", p));
229                                         url_fputs(fh, sprintf("t %s\n", tt));
230                                 }
231                                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
232                                 {
233                                         float v;
234                                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
235                                         if(v != 0)
236                                                 url_fputs(fh, sprintf("e %s %g\n", e, v));
237                                 }
238                         }
239                         url_fputs(fh, "\n");
240                         url_fclose(fh, PlayerStats_ready, world);
241                         break;
242                 case URL_READY_CANREAD:
243                         // url_fclose is processing, we got a response for writing the data
244                         // this must come from HTTP
245                         print("Got response from player stats server:\n");
246                         while((s = url_fgets(fh)))
247                                 print("  ", s, "\n");
248                         print("End of response.\n");
249                         url_fclose(fh, PlayerStats_ready, world);
250                         break;
251                 case URL_READY_CLOSED:
252                         // url_fclose has finished
253                         print("Player stats written\n");
254                         playerstats_waitforme = TRUE;
255                         db_close(playerstats_db);
256                         playerstats_db = -1;
257                         break;
258                 case URL_READY_ERROR:
259                 default:
260                         print("Player stats writing failed: ", ftos(status), "\n");
261                         playerstats_waitforme = TRUE;
262                         if(playerstats_db >= 0)
263                         {
264                                 db_close(playerstats_db);
265                                 playerstats_db = -1;
266                         }
267                         break;
268         }
269 }
270
271 //#NO AUTOCVARS START
272 void PlayerStats_Shutdown()
273 {
274         string uri;
275
276         if(playerstats_db < 0)
277                 return;
278
279         uri = autocvar_g_playerstats_uri;
280         if(uri != "")
281         {
282                 playerstats_waitforme = FALSE;
283                 url_multi_fopen(uri, FILE_APPEND, PlayerStats_ready, world);
284         }
285         else
286         {
287                 playerstats_waitforme = TRUE;
288                 db_close(playerstats_db);
289                 playerstats_db = -1;
290         }
291 }
292 //#NO AUTOCVARS END
293
294 void PlayerStats_Accuracy(entity p)
295 {
296     entity a, w;
297     a = p.accuracy;
298     float i;
299
300     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
301     {
302         w = get_weaponinfo(i);
303
304         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.(accuracy_hit[i-1]));
305         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.(accuracy_fired[i-1]));
306
307         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.(accuracy_cnt_hit[i-1]));
308         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.(accuracy_cnt_fired[i-1]));
309
310         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.(accuracy_frags[i-1]));
311     }
312 }
313
314 void PlayerStats_AddGlobalInfo(entity p)
315 {
316         if(playerstats_db < 0)
317                 return;
318         if(!p.playerstats_id || playerstats_db < 0)
319                 return;
320         p.playerstats_addedglobalinfo = TRUE;
321
322         // add global info!
323         if(p.alivetime)
324         {
325                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
326                 p.alivetime = 0;
327         }
328
329         db_put(playerstats_db, sprintf("%s:_playerid", p.playerstats_id), ftos(p.playerid));
330         
331         if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
332                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
333
334     if(teamplay)
335                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
336
337         if(stof(db_get(playerstats_db, sprintf("%d:%s", p.playerstats_id, PLAYERSTATS_ALIVETIME))) > 0)
338                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
339
340         PlayerStats_Accuracy(p);
341
342         strunzone(p.playerstats_id);
343         p.playerstats_id = string_null;
344 }
345
346 void PlayerStats_EndMatch(float finished)
347 {
348         entity p, winner;
349     winner = PlayerScore_Sort(score_dummyfield);
350         FOR_EACH_PLAYER(p) // spectators intentionally not included
351         {
352                 PlayerScore_PlayerStats(p);
353                 PlayerStats_Accuracy(p);
354                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
355                 if(finished)
356                 {
357                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
358                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
359                         PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
360                 }
361         }
362 }