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