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_SCOREBOARD_VALID);
26 void PlayerStats_AddPlayer(entity e)
28 if(playerstats_db < 0)
31 if(e.crypto_idfp != "")
32 e.playerstats_id = strzone(e.crypto_idfp);
33 else if(clienttype(e) == CLIENTTYPE_BOT)
34 e.playerstats_id = strzone(sprintf("bot#%d", e.playerid));
36 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
39 key = sprintf("%s:*", e.playerstats_id);
42 p = db_get(playerstats_db, key);
47 db_put(playerstats_db, key, playerstats_last);
48 strunzone(playerstats_last);
51 db_put(playerstats_db, key, "#");
52 playerstats_last = strzone(e.playerstats_id);
56 void PlayerStats_AddEvent(string event_id)
58 if(playerstats_db < 0)
62 key = sprintf("*:%s", event_id);
65 p = db_get(playerstats_db, key);
70 db_put(playerstats_db, key, events_last);
71 strunzone(events_last);
74 db_put(playerstats_db, key, "#");
75 events_last = strzone(event_id);
79 void PlayerStats_Event(entity e, string event_id, float value)
81 if(!e.playerstats_id || playerstats_db < 0)
86 key = sprintf("%s:%s", e.playerstats_id, event_id);
87 val = stof(db_get(playerstats_db, key));
89 db_put(playerstats_db, key, ftos(val));
92 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
94 if(playerstats_requested)
95 playerstats_waitforme = TRUE;
99 void PlayerStats_Shutdown()
108 if(playerstats_db < 0)
111 uri = autocvar_g_playerstats_uri;
117 db_dump(playerstats_db, "foo.db");
119 bufstr_set(b, i++, "V 1");
120 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
121 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
122 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
123 bufstr_set(b, i++, sprintf("S %s", cvar_string("hostname")));
124 bufstr_set(b, i++, sprintf("C %d", cvar_purechanges_count));
125 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
127 bufstr_set(b, i++, sprintf("P %s", p));
128 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
130 bufstr_set(b, i++, sprintf("n %s", nn));
131 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
134 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
135 bufstr_set(b, i++, sprintf("e %s %g", e, v));
138 bufstr_set(b, i++, "");
140 if(autocvar_g_playerstats_debug)
142 for(i = 0; i < buf_getsize(b); ++i)
143 print(bufstr_get(b, i), "\n");
146 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
147 playerstats_requested = TRUE;
149 playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
154 playerstats_waitforme = TRUE;
156 db_close(playerstats_db);
161 void PlayerStats_AddGlobalInfo(entity p)
163 if(playerstats_db < 0)
165 if(!p.playerstats_id || playerstats_db < 0)
167 p.playerstats_addedglobalinfo = TRUE;
171 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
174 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
176 if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
177 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
179 strunzone(p.playerstats_id);
180 p.playerstats_id = string_null;
183 void PlayerStats_EndMatch(float finished)
188 PlayerScore_PlayerStats(p);
189 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
192 PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
193 PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);