2 string playerstats_last;
4 .float playerstats_addedglobalinfo;
5 float playerstats_requested;
6 .string playerstats_id;
8 void PlayerStats_Init()
12 playerstats_waitforme = TRUE;
13 uri = autocvar_g_playerstats_uri;
16 playerstats_db = db_create();
17 if(playerstats_db >= 0)
18 playerstats_waitforme = FALSE; // must wait for it at match end
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);
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);
38 void PlayerStats_AddPlayer(entity e)
40 if(playerstats_db < 0)
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));
48 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
51 key = sprintf("%s:*", e.playerstats_id);
54 p = db_get(playerstats_db, key);
59 db_put(playerstats_db, key, playerstats_last);
60 strunzone(playerstats_last);
63 db_put(playerstats_db, key, "#");
64 playerstats_last = strzone(e.playerstats_id);
68 void PlayerStats_AddTeam(float t)
70 if(playerstats_db < 0)
74 key = sprintf("team#%d:*", t);
77 p = db_get(playerstats_db, key);
82 db_put(playerstats_db, key, playerstats_last);
83 strunzone(playerstats_last);
86 db_put(playerstats_db, key, "#");
87 playerstats_last = strzone(sprintf("team%d", t));
91 void PlayerStats_AddEvent(string event_id)
93 if(playerstats_db < 0)
97 key = sprintf("*:%s", event_id);
100 p = db_get(playerstats_db, key);
105 db_put(playerstats_db, key, events_last);
106 strunzone(events_last);
109 db_put(playerstats_db, key, "#");
110 events_last = strzone(event_id);
114 void PlayerStats_Event(entity e, string event_id, float value)
116 if(!e.playerstats_id || playerstats_db < 0)
121 key = sprintf("%s:%s", e.playerstats_id, event_id);
122 val = stof(db_get(playerstats_db, key));
124 db_put(playerstats_db, key, ftos(val));
127 void PlayerStats_TeamScore(float t, string event_id, float value)
131 key = sprintf("team#%d:%s", t, event_id);
132 val = stof(db_get(playerstats_db, key));
134 db_put(playerstats_db, key, ftos(val));
137 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
139 if(playerstats_requested)
140 playerstats_waitforme = TRUE;
146 A collection of lines of the format <key> SPACE <value> NEWLINE, where
147 <key> is always a single character.
149 The following keys are defined:
151 V: format version (always 1)
152 R: release information on the server
153 T: time at which the game ended
156 S: "hostname" of the server
157 C: number of "unpure" cvar changes
158 P: player ID of an existing player; this also sets the owner for all following "n" and "e" lines (lower case!)
159 n: nickname of the player (optional)
160 e: followed by an event name, a space, and the event count/score
162 alivetime: total playing time of the player
163 wins: number of games won (can only be set if matches is set)
164 matches: number of matches played to the end (not aborted by map switch)
165 joins: number of matches joined (always 1 unless player never played during the match)
166 scoreboardvalid: set to 1 if the player was there at the end of the match
167 total-<scoreboardname>: total score of that scoreboard item
168 scoreboard-<scoreboardname>: end-of-game score of that scoreboard item (can differ in non-team games)
169 achievement-<achievementname>: achievement counters
172 //#NO AUTOCVARS START
173 void PlayerStats_Shutdown()
182 if(playerstats_db < 0)
185 uri = autocvar_g_playerstats_uri;
191 db_dump(playerstats_db, "foo.db");
193 bufstr_set(b, i++, "V 1");
194 bufstr_set(b, i++, sprintf("R %s", WATERMARK()));
195 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
196 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
197 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
198 bufstr_set(b, i++, sprintf("S %s", cvar_string("hostname")));
199 bufstr_set(b, i++, sprintf("C %d", cvar_purechanges_count));
200 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
202 bufstr_set(b, i++, sprintf("P %s", p));
203 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
205 bufstr_set(b, i++, sprintf("n %s", nn));
206 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
209 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
211 bufstr_set(b, i++, sprintf("e %s %g", e, v));
214 bufstr_set(b, i++, "");
216 if(autocvar_g_playerstats_debug)
218 for(i = 0; i < buf_getsize(b); ++i)
219 print(bufstr_get(b, i), "\n");
222 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
223 playerstats_requested = TRUE;
225 playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
230 playerstats_waitforme = TRUE;
232 db_close(playerstats_db);
237 void PlayerStats_AddGlobalInfo(entity p)
239 if(playerstats_db < 0)
241 if(!p.playerstats_id || playerstats_db < 0)
243 p.playerstats_addedglobalinfo = TRUE;
247 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
250 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
252 if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
253 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
256 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
258 strunzone(p.playerstats_id);
259 p.playerstats_id = string_null;
262 void PlayerStats_EndMatch(float finished)
267 PlayerScore_PlayerStats(p);
268 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
271 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
272 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);