2 string playerstats_last;
4 .float playerstats_addedglobalinfo;
5 float playerstats_requested;
7 void PlayerStats_Init()
10 playerstats_sent = TRUE;
11 uri = cvar_string("g_playerstats_uri");
14 playerstats_db = db_create();
15 if(playerstats_db >= 0)
16 playerstats_sent = FALSE; // must wait for it at match end
18 PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
19 PlayerStats_AddEvent(PLAYERSTATS_KILLS);
22 void PlayerStats_AddPlayer(entity e)
24 if(!e.crypto_idfp || playerstats_db < 0)
28 key = sprintf("%s:*", e.crypto_idfp);
31 p = db_get(playerstats_db, key);
36 db_put(playerstats_db, key, playerstats_last);
37 strunzone(playerstats_last);
40 db_put(playerstats_db, key, "#");
41 playerstats_last = strzone(e.crypto_idfp);
45 void PlayerStats_AddEvent(string event_id)
47 if(playerstats_db < 0)
51 key = sprintf("*:%s", event_id);
54 p = db_get(playerstats_db, key);
59 db_put(playerstats_db, key, events_last);
60 strunzone(events_last);
63 db_put(playerstats_db, key, "#");
64 events_last = strzone(event_id);
68 void PlayerStats_Event(entity e, string event_id, float value)
70 if(!e.crypto_idfp || playerstats_db < 0)
75 key = sprintf("%s:%s", e.crypto_idfp, event_id);
76 val = stof(db_get(playerstats_db, key));
78 db_put(playerstats_db, key, ftos(val));
81 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
83 if(playerstats_requested)
84 playerstats_sent = TRUE;
87 void PlayerStats_Shutdown()
96 if(playerstats_db < 0)
99 uri = cvar_string("g_playerstats_uri");
105 db_dump(playerstats_db, "foo.db");
107 bufstr_set(b, i++, "V 1");
108 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
109 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
110 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
111 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
113 bufstr_set(b, i++, sprintf("P %s", p));
114 nn = db_get(playerstats_db, sprintf("%s:_netname", p));
116 bufstr_set(b, i++, sprintf("n %s", nn));
117 for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
120 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
121 bufstr_set(b, i++, sprintf("e %s %f", e, v));
124 bufstr_set(b, i++, "");
126 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
127 playerstats_requested = TRUE;
129 playerstats_sent = TRUE; // if posting fails, we must continue anyway
134 playerstats_sent = TRUE;
136 db_close(playerstats_db);
140 void PlayerStats_AddGlobalInfo(entity p)
142 if(playerstats_db < 0)
144 if(!p.crypto_idfp || playerstats_db < 0)
146 p.playerstats_addedglobalinfo = TRUE;
150 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
152 if(p.cvar_cl_allow_uid2name)
153 db_put(playerstats_db, sprintf("%s:_netname", p.crypto_idfp), p.netname);