]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/playerstats.qc
Merge branch 'master' into terencehill/menu_tooltips_2
[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 .string playerstats_id;
7
8 void PlayerStats_Init()
9 {
10         string uri;
11         playerstats_db = -1;
12         playerstats_waitforme = TRUE;
13         uri = autocvar_g_playerstats_uri;
14         if(uri == "")
15                 return;
16         playerstats_db = db_create();
17         if(playerstats_db >= 0)
18                 playerstats_waitforme = FALSE; // must wait for it at match end
19         
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);
25
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);
36 }
37
38 void PlayerStats_AddPlayer(entity e)
39 {
40         if(playerstats_db < 0)
41                 return;
42
43         if(e.crypto_idfp != "")
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));
47         else
48                 e.playerstats_id = strzone(sprintf("player#%d", e.playerid));
49         
50         string key;
51         key = sprintf("%s:*", e.playerstats_id);
52         
53         string p;
54         p = db_get(playerstats_db, key);
55         if(p == "")
56         {
57                 if(playerstats_last)
58                 {
59                         db_put(playerstats_db, key, playerstats_last);
60                         strunzone(playerstats_last);
61                 }
62                 else
63                         db_put(playerstats_db, key, "#");
64                 playerstats_last = strzone(e.playerstats_id);
65         }
66 }
67
68 void PlayerStats_AddEvent(string event_id)
69 {
70         if(playerstats_db < 0)
71                 return;
72         
73         string key;
74         key = sprintf("*:%s", event_id);
75         
76         string p;
77         p = db_get(playerstats_db, key);
78         if(p == "")
79         {
80                 if(events_last)
81                 {
82                         db_put(playerstats_db, key, events_last);
83                         strunzone(events_last);
84                 }
85                 else
86                         db_put(playerstats_db, key, "#");
87                 events_last = strzone(event_id);
88         }
89 }
90
91 void PlayerStats_Event(entity e, string event_id, float value)
92 {
93         if(!e.playerstats_id || playerstats_db < 0)
94                 return;
95         
96         string key;
97         float val;
98         key = sprintf("%s:%s", e.playerstats_id, event_id);
99         val = stof(db_get(playerstats_db, key));
100         val += value;
101         db_put(playerstats_db, key, ftos(val));
102 }
103
104 void PlayerStats_Sent_URI_Get_Callback(float id, float status, string data)
105 {
106         if(playerstats_requested)
107                 playerstats_waitforme = TRUE;
108 }
109
110 //#NO AUTOCVARS START
111 void PlayerStats_Shutdown()
112 {
113         string p, pn;
114         string e, en;
115         string nn;
116         float b;
117         float i;
118         string uri;
119
120         if(playerstats_db < 0)
121                 return;
122
123         uri = autocvar_g_playerstats_uri;
124         if(uri != "")
125         {
126                 b = buf_create();
127                 i = 0;
128
129                 db_dump(playerstats_db, "foo.db");
130
131                 bufstr_set(b, i++, "V 1");
132                 bufstr_set(b, i++, sprintf("T %s.%06d", strftime(FALSE, "%s"), floor(random() * 1000000)));
133                 bufstr_set(b, i++, sprintf("G %s", GetGametype()));
134                 bufstr_set(b, i++, sprintf("M %s", GetMapname()));
135                 bufstr_set(b, i++, sprintf("S %s", cvar_string("hostname")));
136                 bufstr_set(b, i++, sprintf("C %d", cvar_purechanges_count));
137                 for(p = playerstats_last; (pn = db_get(playerstats_db, sprintf("%s:*", p))) != ""; p = pn)
138                 {
139                         bufstr_set(b, i++, sprintf("P %s", p));
140                         nn = db_get(playerstats_db, sprintf("%s:_netname", p));
141                         if(nn != "")
142                                 bufstr_set(b, i++, sprintf("n %s", nn));
143                         for(e = events_last; (en = db_get(playerstats_db, sprintf("*:%s", e))) != ""; e = en)
144                         {
145                                 float v;
146                                 v = stof(db_get(playerstats_db, sprintf("%s:%s", p, e)));
147                                 if(v != 0)
148                                         bufstr_set(b, i++, sprintf("e %s %g", e, v));
149                         }
150                 }
151                 bufstr_set(b, i++, "");
152
153                 if(autocvar_g_playerstats_debug)
154                 {
155                         for(i = 0; i < buf_getsize(b); ++i)
156                                 print(bufstr_get(b, i), "\n");
157                 }
158
159                 if(crypto_uri_postbuf(uri, URI_GET_PLAYERSTATS_SENT, "text/plain", "\n", b, 0))
160                         playerstats_requested = TRUE;
161                 else
162                         playerstats_waitforme = TRUE; // if posting fails, we must continue anyway
163
164                 buf_del(b);
165         }
166         else
167                 playerstats_waitforme = TRUE;
168
169         db_close(playerstats_db);
170         playerstats_db = -1;
171 }
172 //#NO AUTOCVARS END
173
174 void PlayerStats_AddGlobalInfo(entity p)
175 {
176         if(playerstats_db < 0)
177                 return;
178         if(!p.playerstats_id || playerstats_db < 0)
179                 return;
180         p.playerstats_addedglobalinfo = TRUE;
181
182         // add global info!
183         if(p.alivetime)
184                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
185
186         if(p.alivetime)
187                 PlayerStats_Event(p, PLAYERSTATS_ALIVETIME, time - p.alivetime);
188         
189         if(p.cvar_cl_allow_uid2name == 1 || clienttype(p) == CLIENTTYPE_BOT)
190                 db_put(playerstats_db, sprintf("%s:_netname", p.playerstats_id), p.netname);
191
192         if(p.alivetime > 0)
193                 PlayerStats_Event(p, PLAYERSTATS_JOINS, 1);
194
195         strunzone(p.playerstats_id);
196         p.playerstats_id = string_null;
197 }
198
199 void PlayerStats_EndMatch(float finished)
200 {
201         entity p;
202         FOR_EACH_PLAYER(p)
203         {
204                 PlayerScore_PlayerStats(p);
205                 PlayerStats_Event(p, PLAYERSTATS_SCOREBOARD_VALID, 1);
206                 if(finished)
207                 {
208                         PlayerStats_Event(p, PLAYERSTATS_WINS, p.winning);
209                         PlayerStats_Event(p, PLAYERSTATS_MATCHES, 1);
210                 }
211         }
212 }