Xonotic
weaponstats.qc
Go to the documentation of this file.
1 #include "weaponstats.qh"
2 
3 #include <common/stats.qh>
4 #include <common/weapons/_all.qh>
5 #include <common/weapons/_all.qh>
6 #include <server/intermission.qh>
7 #include <server/world.qh>
8 
10 {
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"));
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 
74 {
75  if(weaponstats_buffer < 0)
76  return;
78  {
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 }
const float URL_READY_CANREAD
Definition: urllib.qh:17
const float URL_READY_CLOSED
Definition: urllib.qh:15
void WeaponStats_ready(entity fh, entity pass, float status)
Definition: weaponstats.qc:14
const int WEP_FIRST
Definition: all.qh:304
void WeaponStats_LogKill(float awep, float abot, float vwep, float vbot)
Definition: weaponstats.qc:108
string GetMapname()
Definition: intermission.qc:18
void WeaponStats_LogItem(float awep, float abot, float vwep, float vbot, vector item)
Definition: weaponstats.qc:88
const float URL_READY_CANWRITE
Definition: urllib.qh:16
entity() spawn
void WeaponStats_Shutdown()
Definition: weaponstats.qc:73
const float FILE_APPEND
Definition: csprogsdefs.qc:232
#define WEP_LAST
Definition: all.qh:305
void WeaponStats_Init()
Definition: weaponstats.qc:9
#define buf_create
Definition: dpextensions.qh:63
float cvar_purechanges_count
Definition: world.qh:43
string autocvar_hostname
Definition: client.qh:49
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
ERASEABLE void url_fputs(entity e, string s)
Definition: urllib.qc:312
ERASEABLE void url_fclose(entity e)
Definition: urllib.qc:207
#define NULL
Definition: post.qh:17
#define LOG_INFO(...)
Definition: log.qh:70
string cvar_purechanges
Definition: world.qh:42
#define WEAPONSTATS_GETINDEX(awep, abot, vwep, vbot)
Definition: weaponstats.qh:14
string autocvar_sv_weaponstats_file
Definition: weaponstats.qh:3
vector(float skel, float bonenum) _skel_get_boneabs_hidden
vector v
Definition: ent_cs.qc:116
float weaponstats_buffer
Definition: weaponstats.qh:5
void WeaponStats_LogDamage(float awep, float abot, float vwep, float vbot, float damage)
Definition: weaponstats.qc:101
string GetGametype()
Definition: intermission.qc:13
#define tokenizebyseparator
Definition: dpextensions.qh:21
const float URL_READY_ERROR
Definition: urllib.qh:14
ERASEABLE void url_multi_fopen(string url, int mode, url_ready_func rdy, entity pass)
Definition: urllib.qc:364
#define pass(name, colormin, colormax)
ERASEABLE string url_fgets(entity e)
Definition: urllib.qc:287