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