Merge branch 'master' into martin-t/mg-solidpen
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / weapons / weaponstats.qc
1 #include "weaponstats.qh"
2
3 #include <server/defs.qh>
4 #include <server/miscfunctions.qh>
5 #include "../g_world.qh"
6
7 #include <common/weapons/_all.qh>
8
9 void WeaponStats_Init()
10 {
11         weaponstats_buffer = ((autocvar_sv_weaponstats_file != "") ? buf_create() : -1);
12 }
13
14 void WeaponStats_ready(entity fh, entity pass, float status)
15 {
16         float i, j, n, ibot, jbot, idx;
17         vector v;
18         string prefix, s;
19         switch(status)
20         {
21                 case URL_READY_CANWRITE:
22                         // we can write
23                         prefix = strcat(autocvar_hostname, "\t", GetGametype(), "_", GetMapname(), "\t");
24                         url_fputs(fh, "#begin statsfile\n");
25                         url_fputs(fh, strcat("#date ", strftime(true, "%a %b %e %H:%M:%S %Z %Y"), "\n"));
26 #ifdef WATERMARK
27                         url_fputs(fh, strcat("#version ", WATERMARK, "\n"));
28 #endif
29                         url_fputs(fh, strcat("#config ", ftos(crc16(false, cvar_purechanges)), "\n"));
30                         url_fputs(fh, strcat("#cvar_purechanges ", ftos(cvar_purechanges_count), "\n"));
31                         n = tokenizebyseparator(cvar_purechanges, "\n");
32                         for(i = 0; i < n; ++i)
33                                 url_fputs(fh, strcat("#cvar_purechange ", argv(i), "\n"));
34                         for(i = WEP_FIRST; i <= WEP_LAST; ++i) for(ibot = 0; ibot <= 1; ++ibot)
35                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j) for(jbot = 0; jbot <= 1; ++jbot)
36                                 {
37                                         idx = WEAPONSTATS_GETINDEX(i, ibot, j, jbot);
38                                         v = stov(bufstr_get(weaponstats_buffer, idx));
39                                         if(v != '0 0 0')
40                                         {
41                                                 //vector is: kills hits damage
42                                                 url_fputs(fh, sprintf("%s%d %d\t%d %d\t", prefix, i, ibot, j, jbot));
43                                                 url_fputs(fh, sprintf("%d %d %g\n", v.x, v.y, v.z));
44                                         }
45                                 }
46                         url_fputs(fh, "#end\n\n");
47                         url_fclose(fh);
48                         break;
49                 case URL_READY_CANREAD:
50                         // url_fclose is processing, we got a response for writing the data
51                         // this must come from HTTP
52                         LOG_INFO("Got response from weapon stats server:");
53                         while((s = url_fgets(fh)))
54                                 LOG_INFO("  ", s);
55                         LOG_INFO("End of response.");
56                         url_fclose(fh);
57                         break;
58                 case URL_READY_CLOSED:
59                         // url_fclose has finished
60                         LOG_INFO("Weapon stats written");
61                         buf_del(weaponstats_buffer);
62                         weaponstats_buffer = -1;
63                         break;
64                 case URL_READY_ERROR:
65                 default:
66                         LOG_INFO("Weapon stats writing failed: ", ftos(status));
67                         buf_del(weaponstats_buffer);
68                         weaponstats_buffer = -1;
69                         break;
70         }
71 }
72
73 void WeaponStats_Shutdown()
74 {
75         if(weaponstats_buffer < 0)
76                 return;
77         if(autocvar_sv_weaponstats_file != "")
78         {
79                 url_multi_fopen(autocvar_sv_weaponstats_file, FILE_APPEND, WeaponStats_ready, NULL);
80         }
81         else
82         {
83                 buf_del(weaponstats_buffer);
84                 weaponstats_buffer = -1;
85         }
86 }
87
88 void WeaponStats_LogItem(float awep, float abot, float vwep, float vbot, vector item)
89 {
90         float idx;
91         if(weaponstats_buffer < 0)
92                 return;
93         if(awep < WEP_FIRST || vwep < WEP_FIRST)
94                 return;
95         if(awep > WEP_LAST || vwep > WEP_LAST)
96                 return;
97         idx = WEAPONSTATS_GETINDEX(awep,abot,vwep,vbot);
98         bufstr_set(weaponstats_buffer, idx, vtos(stov(bufstr_get(weaponstats_buffer, idx)) + item));
99 }
100
101 void WeaponStats_LogDamage(float awep, float abot, float vwep, float vbot, float damage)
102 {
103         if(damage < 0)
104                 error("negative damage?");
105         WeaponStats_LogItem(awep, abot, vwep, vbot, '0 0 1' * damage + '0 1 0');
106 }
107
108 void WeaponStats_LogKill(float awep, float abot, float vwep, float vbot)
109 {
110         WeaponStats_LogItem(awep, abot, vwep, vbot, '1 0 0');
111 }