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