2 string playerstats_last;
4 .float playerstats_addedglobalinfo;
5 float playerstats_requested;
7 void PlayerStats_Init()
11 playerstats_waitforme = TRUE;
12 uri = autocvar_g_playerstats_uri;
15 playerstats_db = db_create();
16 if(playerstats_db >= 0)
17 playerstats_waitforme = FALSE; // must wait for it at match end
19 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
20 PlayerStats_AddEvent(PLAYERSTATS_KILLS);
23 void PlayerStats_AddPlayer(entity e)
25 if(!e.crypto_idfp || playerstats_db < 0)
29 key = sprintf("%s:*", e.crypto_idfp);
32 p = db_get(playerstats_db, key);
37 db_put(playerstats_db, key, playerstats_last);
38 strunzone(playerstats_last);
41 db_put(playerstats_db, key, "#");
42 playerstats_last = strzone(e.crypto_idfp);
46 void PlayerStats_AddEvent(string event_id)
48 if(playerstats_db < 0)
52 key = sprintf("*:%s", event_id);
55 p = db_get(playerstats_db, key);
60 db_put(playerstats_db, key, events_last);
61 strunzone(events_last);
64 db_put(playerstats_db, key, "#");
65 events_last = strzone(event_id);
69 void PlayerStats_Event(entity e, string event_id, float value)
71 if(!e.crypto_idfp || playerstats_db < 0)
76 key = sprintf("%s:%s", e.crypto_idfp, event_id);
77 val = stof(db_get(playerstats_db, key));
79 db_put(playerstats_db, key, ftos(val));
82 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
84 if(playerstats_requested)
85 playerstats_waitforme = TRUE;
89 void PlayerStats_Shutdown()
98 if(playerstats_db < 0)
101 uri = autocvar_g_playerstats_uri;
107 db_dump(playerstats_db, "foo.db");
109 bufstr_set(b, i++, "V 1");
110 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
111 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
112 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
113 bufstr_set(b, i++, sprintf("S %s", cvar_string("hostname")));
114 bufstr_set(b, i++, sprintf("C %d", cvar_purechanges_count));
115 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
117 bufstr_set(b, i++, sprintf("P %s", p));
118 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
120 bufstr_set(b, i++, sprintf("n %s", nn));
121 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
124 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
125 bufstr_set(b, i++, sprintf("e %s %f", e, v));
128 bufstr_set(b, i++, "");
130 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
131 playerstats_requested = TRUE;
133 playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
138 playerstats_waitforme = TRUE;
140 db_close(playerstats_db);
145 void PlayerStats_AddGlobalInfo(entity p)
147 if(playerstats_db < 0)
149 if(!p.crypto_idfp || playerstats_db < 0)
151 p.playerstats_addedglobalinfo = TRUE;
155 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
157 if(p.cvar_cl_allow_uid2name == 1)
158 db_put(playerstats_db, sprintf("%s:_netname", p.crypto_idfp), p.netname);