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