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