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