]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
Merge remote branch 'origin/master' into fruitiex/balance
[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
7 void PlayerStats_Init()
8 {
9         string uri;
10         playerstats_db = -1;
11         playerstats_waitforme = TRUE;
12         uri = cvar_string("g_playerstats_uri");
13         if(uri == "")
14                 return;
15         playerstats_db = db_create();
16         if(playerstats_db >= 0)
17                 playerstats_waitforme = FALSE; // must wait for it at match end
18         
19         PlayerStats_AddEvent(PLAYERSTATS_ALIVETIME);
20         PlayerStats_AddEvent(PLAYERSTATS_KILLS);
21 }
22
23 void PlayerStats_AddPlayer(entity e)
24 {
25         if(!e.crypto_idfp || playerstats_db < 0)
26                 return;
27         
28         string key;
29         key = sprintf("%s:*", e.crypto_idfp);
30         
31         string p;
32         p = db_get(playerstats_db, key);
33         if(p == "")
34         {
35                 if(playerstats_last)
36                 {
37                         db_put(playerstats_db, key, playerstats_last);
38                         strunzone(playerstats_last);
39                 }
40                 else
41                         db_put(playerstats_db, key, "#");
42                 playerstats_last = strzone(e.crypto_idfp);
43         }
44 }
45
46 void PlayerStats_AddEvent(string event_id)
47 {
48         if(playerstats_db < 0)
49                 return;
50         
51         string key;
52         key = sprintf("*:%s", event_id);
53         
54         string p;
55         p = db_get(playerstats_db, key);
56         if(p == "")
57         {
58                 if(events_last)
59                 {
60                         db_put(playerstats_db, key, events_last);
61                         strunzone(events_last);
62                 }
63                 else
64                         db_put(playerstats_db, key, "#");
65                 events_last = strzone(event_id);
66         }
67 }
68
69 void PlayerStats_Event(entity e, string event_id, float value)
70 {
71         if(!e.crypto_idfp || playerstats_db < 0)
72                 return;
73         
74         string key;
75         float val;
76         key = sprintf("%s:%s", e.crypto_idfp, event_id);
77         val = stof(db_get(playerstats_db, key));
78         val += value;
79         db_put(playerstats_db, key, ftos(val));
80 }
81
82 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
83 {
84         if(playerstats_requested)
85                 playerstats_waitforme = TRUE;
86 }
87
88 void PlayerStats_Shutdown()
89 {
90         string p, pn;
91         string e, en;
92         string nn;
93         float b;
94         float i;
95         string uri;
96
97         if(playerstats_db < 0)
98                 return;
99
100         uri = cvar_string("g_playerstats_uri");
101         if(uri != "")
102         {
103                 b = buf_create();
104                 i = 0;
105
106                 db_dump(playerstats_db, "foo.db");
107
108                 bufstr_set(b, i++, "V 1");
109                 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
110                 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
111                 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
112                 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
113                 {
114                         bufstr_set(b, i++, sprintf("P %s", p));
115                         nn = db_get(playerstats_db, sprintf("%s:_netname", p));
116                         if(nn != "")
117                                 bufstr_set(b, i++, sprintf("n %s", nn));
118                         for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
119                         {
120                                 float v;
121                                 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
122                                 bufstr_set(b, i++, sprintf("e %s %f", e, v));
123                         }
124                 }
125                 bufstr_set(b, i++, "");
126
127                 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
128                         playerstats_requested = TRUE;
129                 else
130                         playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
131
132                 buf_del(b);
133         }
134         else
135                 playerstats_waitforme = TRUE;
136
137         db_close(playerstats_db);
138         playerstats_db = -1;
139 }
140
141 void PlayerStats_AddGlobalInfo(entity p)
142 {
143         if(playerstats_db < 0)
144                 return;
145         if(!p.crypto_idfp || playerstats_db < 0)
146                 return;
147         p.playerstats_addedglobalinfo = TRUE;
148
149         // add global info!
150         if(p.alivetime)
151                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
152         
153         if(p.cvar_cl_allow_uid2name)
154                 db_put(playerstats_db, sprintf("%s:_netname", p.crypto_idfp), p.netname);
155 }