]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
fix the spam by adding for loops
[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         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
61         if(e.crypto_idfp != "" && e.cvar_cl_allow_uidtracking == 1)
62                 e.playerstats_id = strzone(e.crypto_idfp);
63         else if(clienttype(e) == CLIENTTYPE_BOT)
64                 e.playerstats_id = strzone(sprintf("bot#%d", e.playerid));
65         else
66                 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
67
68     if(teams_matter)
69
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)
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)
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 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
159 {
160         if(playerstats_requested)
161                 playerstats_waitforme = TRUE;
162 }
163
164 /*
165         format spec:
166
167         A collection of lines of the format <key> SPACE <value> NEWLINE, where
168         <key> is always a single character.
169
170         The following keys are defined:
171
172         V: format version (always 1) - this MUST be the first line!
173         #: comment (MUST be ignored by any parser)
174         R: release information on the server
175         T: time at which the game ended
176         G: game type
177         M: map name
178         S: "hostname" of the server
179         C: number of "unpure" cvar changes
180     W: winning team ID
181         t: team ID; all following "P" lines will be players on this team
182         P: player ID of an existing player; this also sets the owner for all following "n" and "e" lines (lower case!)
183         n: nickname of the player (optional)
184         e: followed by an event name, a space, and the event count/score
185                 event names can be:
186                         alivetime: total playing time of the player
187                         wins: number of games won (can only be set if matches is set)
188                         matches: number of matches played to the end (not aborted by map switch)
189                         joins: number of matches joined (always 1 unless player never played during the match)
190                         scoreboardvalid: set to 1 if the player was there at the end of the match
191                         total-<scoreboardname>: total score of that scoreboard item
192                         scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
193                         achievement-<achievementname>: achievement counters
194             rank <number>: rank of player
195 */
196
197 //#NO AUTOCVARS START
198 void PlayerStats_Shutdown()
199 {
200     string t, tn;
201         string p, pn;
202         string e, en;
203         string nn, tt;
204         float b;
205         float i;
206         string uri;
207
208         if(playerstats_db < 0)
209                 return;
210
211         uri = autocvar_g_playerstats_uri;
212         if(uri != "")
213         {
214                 b = buf_create();
215                 i = 0;
216
217                 db_dump(playerstats_db, "foo.db");
218
219                 bufstr_set(b, i++, "V 1");
220 #ifdef WATERMARK
221                 bufstr_set(b, i++, sprintf("R %s", WATERMARK()));
222 #endif
223                 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
224                 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
225                 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
226                 bufstr_set(b, i++, sprintf("S %s", cvar_string("hostname")));
227                 bufstr_set(b, i++, sprintf("C %d", cvar_purechanges_count));
228         if(teams_matter)
229         {
230             entity winner;
231             winner = PlayerScore_Sort(score_dummyfield);
232             bufstr_set(b, i++, sprintf("W %d", winner.team));
233             for(t = teamstats_last; (tn = db_get(playerstats_db, sprintf("%s", t))) != ""; t = tn)
234             {
235                 bufstr_set(b, i++, strcat("t ", t));
236                 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
237                 {
238                     tt = db_get(playerstats_db, sprintf("%s:_team", p));
239                     if(t != tt)
240                         continue;
241
242                     bufstr_set(b, i++, sprintf("P %s", p));
243                     nn = db_get(playerstats_db, sprintf("%s:_netname", p));
244                     if(nn != "")
245                         bufstr_set(b, i++, sprintf("n %s", nn));
246                     for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
247                     {
248                         float v;
249                         v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
250                         if(v != 0)
251                             bufstr_set(b, i++, sprintf("e %s %g", e, v));
252                     }
253                 }
254                 bufstr_set(b, i++, "");
255             }
256         }
257         else {
258             for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
259             {
260                 bufstr_set(b, i++, sprintf("P %s", p));
261                 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
262                 if(nn != "")
263                     bufstr_set(b, i++, sprintf("n %s", nn));
264                 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
265                 {
266                     float v;
267                     v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
268                     if(v != 0)
269                         bufstr_set(b, i++, sprintf("e %s %g", e, v));
270                 }
271             }
272             bufstr_set(b, i++, "");
273         }
274
275                 if(autocvar_g_playerstats_debug)
276                 {
277                         for(i = 0; i < buf_getsize(b); ++i)
278                                 print(bufstr_get(b, i), "\n");
279                 }
280
281                 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
282                         playerstats_requested = TRUE;
283                 else
284                         playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
285
286                 buf_del(b);
287         }
288         else
289                 playerstats_waitforme = TRUE;
290
291         db_close(playerstats_db);
292         playerstats_db = -1;
293 }
294 //#NO AUTOCVARS END
295
296 void PlayerStats_AddGlobalInfo(entity p)
297 {
298         if(playerstats_db < 0)
299                 return;
300         if(!p.playerstats_id || playerstats_db < 0)
301                 return;
302         p.playerstats_addedglobalinfo = TRUE;
303
304         // add global info!
305         if(p.alivetime)
306                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
307
308         if(p.alivetime)
309                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
310         
311         if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
312                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
313
314     if(teams_matter)
315                 db_put(playerstats_db, sprintf("%s:_team", p.playerstats_id), ftos(p.team));
316
317         if(p.alivetime > 0)
318                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
319
320         strunzone(p.playerstats_id);
321         p.playerstats_id = string_null;
322 }
323
324 void PlayerStats_Accuracy(entity p)
325 {
326     entity a, w;
327     a = p.accuracy;
328     float i;
329
330     for(i = WEP_FIRST; i <= WEP_LAST; ++i)
331     {
332         w = get_weaponinfo(i);
333
334         PlayerStats_Event(p, strcat("acc-", w.netname, "-hit"), a.accuracy_hit[i]);
335         PlayerStats_Event(p, strcat("acc-", w.netname, "-fired"), a.accuracy_fired[i]);
336
337         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-hit"), a.accuracy_cnt_hit[i]);
338         PlayerStats_Event(p, strcat("acc-", w.netname, "-cnt-fired"), a.accuracy_cnt_fired[i]);
339
340         PlayerStats_Event(p, strcat("acc-", w.netname, "-frags"), a.accuracy_frags[i]);
341     }
342 }
343
344 void PlayerStats_EndMatch(float finished)
345 {
346         entity p, winner;
347     winner = PlayerScore_Sort(score_dummyfield);
348         FOR_EACH_PLAYER(p)
349         {
350                 PlayerScore_PlayerStats(p);
351                 PlayerStats_Accuracy(p);
352                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
353                 if(finished)
354                 {
355                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
356                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
357                         PlayerStats_Event(p, PLAYERSTATS_RANK, p.score_dummyfield);
358                 }
359         }
360 }