1 #include "weaponstats.qh"
3 #include "../g_world.qh"
5 #include <common/weapons/all.qh>
7 void WeaponStats_Init()
9 weaponstats_buffer = ((autocvar_sv_weaponstats_file != "") ? buf_create() : -1);
12 void WeaponStats_ready(entity fh, entity pass, float status)
14 float i, j, n, ibot, jbot, idx;
19 case URL_READY_CANWRITE:
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"));
25 url_fputs(fh, strcat("#version ", WATERMARK, "\n"));
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)
35 idx = WEAPONSTATS_GETINDEX(i, ibot, j, jbot);
36 v = stov(bufstr_get(weaponstats_buffer, idx));
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));
44 url_fputs(fh, "#end\n\n");
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");
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;
64 LOG_INFO("Weapon stats writing failed: ", ftos(status), "\n");
65 buf_del(weaponstats_buffer);
66 weaponstats_buffer = -1;
71 void WeaponStats_Shutdown()
73 if(weaponstats_buffer < 0)
75 if(autocvar_sv_weaponstats_file != "")
77 url_multi_fopen(autocvar_sv_weaponstats_file, FILE_APPEND, WeaponStats_ready, world);
81 buf_del(weaponstats_buffer);
82 weaponstats_buffer = -1;
86 void WeaponStats_LogItem(float awep, float abot, float vwep, float vbot, vector item)
89 if(weaponstats_buffer < 0)
91 if(awep < WEP_FIRST || vwep < WEP_FIRST)
93 if(awep > WEP_LAST || vwep > WEP_LAST)
95 idx = WEAPONSTATS_GETINDEX(awep,abot,vwep,vbot);
96 bufstr_set(weaponstats_buffer, idx, vtos(stov(bufstr_get(weaponstats_buffer, idx)) + item));
99 void WeaponStats_LogDamage(float awep, float abot, float vwep, float vbot, float damage)
102 error("negative damage?");
103 WeaponStats_LogItem(awep, abot, vwep, vbot, '0 0 1' * damage + '0 1 0');
106 void WeaponStats_LogKill(float awep, float abot, float vwep, float vbot)
108 WeaponStats_LogItem(awep, abot, vwep, vbot, '1 0 0');