From 4c4d82278f9f60803ba6a317a623aa282d42d3ef Mon Sep 17 00:00:00 2001 From: ioaioa Date: Thu, 9 Jan 2025 21:38:49 +0300 Subject: [PATCH] statswrite ready for test --- src/Makefile | 3 +- src/mod/statswriter.cpp | 81 ++++++++++++++++++++++++++++++----------- src/mod/statswriter.h | 12 +++--- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/Makefile b/src/Makefile index 11c28d8..2bde01a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -71,7 +71,8 @@ SERVER_OBJS= \ mod/cryptomod/polarssl/library/sha2-standalone.o \ mod/cryptomod/polarssl/library/sha4-standalone.o \ mod/banlist-standalone.o \ - mod/authservers-standalone.o + mod/authservers-standalone.o \ + mod/statswriter.o # geoip ifeq ($(USE_GEOIP),true) diff --git a/src/mod/statswriter.cpp b/src/mod/statswriter.cpp index 2797e71..c670a11 100644 --- a/src/mod/statswriter.cpp +++ b/src/mod/statswriter.cpp @@ -63,7 +63,7 @@ namespace remod statsfile->printf(" \"damage\": %d", players[i].damage); statsfile->printf(" \"effectiveness\": %f,", players[i].effectiveness); statsfile->printf(" \"suicides\": %d,", players[i].suicides); - statsfile->printf(" \"guninfo\": [", players[i].suicides); + statsfile->printf(" \"guninfo\": ["); loopj(NUMGUNS) { if (i != 0) @@ -71,23 +71,16 @@ namespace remod statsfile->printf(","); } statsfile->printf("{ \"gun\": %d,", j); - statsfile->printf(" \"shotdamage\": %d,", players[i].guninfo[j].shotdamage); - statsfile->printf(" \"damage\": %d}", players[i].guninfo[j].damage); + statsfile->printf(" \"shotdamage\": %d,", players[i].gi[j].shotdamage); + statsfile->printf(" \"damage\": %d}", players[i].gi[j].damage); } statsfile->printf(" ]}"); } statsfile->printf("]"); } - void writeto(const char *path, stats stats) + void writeto(stream *statsfile, stats stats) { - stream *statsfile = openfile(path, "wb"); - if (!statsfile) - { - conoutf("could not write stats to \"%s\"", path); - return; - } - statsfile->printf("{ \"version\": %d,", STATSFILE_FORMAT_VERSION); write_game(statsfile, stats.game); statsfile->printf(","); @@ -97,27 +90,73 @@ namespace remod statsfile->printf("}"); }; + SVARP(statsdir, "statsdir"); + void write(const char *path) { + static string s; + formatstring(s, "%s/%s", statsdir, path); + + stream *statsfile = openfile(s, "wb"); + if (!statsfile) + { + conoutf("could not write stats to \"%s\"", s); + return; + } + vector players; - vector teams; loopv(server::clients) { + playerinfo player = { + .name = server::clients[i]->name, + .authname = server::clients[i]->authname, + .connect_seconds = (unsigned int) server::clients[i]->connectmillis / 1000, + .team = server::clients[i]->team, + .privilege = server::clients[i]->privilege, + .frags = server::clients[i]->state.frags, + .flags = server::clients[i]->state.flags, + .deaths = server::clients[i]->state.deaths, + .teamkills = server::clients[i]->state.teamkills, + .shotdamage = server::clients[i]->state.shotdamage, + .damage = server::clients[i]->state.damage, + .effectiveness = server::clients[i]->state.effectiveness, + .suicides = server::clients[i]->state.ext.suicides}; + + loopj(NUMGUNS) + { + player.gi[j].shotdamage = server::clients[i]->state.ext.guninfo[j].shotdamage; + player.gi[j].damage = server::clients[i]->state.ext.guninfo[j].damage; + } + + players.add(player); } - struct stats stats = { - players : { + vector scores; - }, - teams : { + server::smode->getteamscores(scores); - }, - game : { - mode : server::gamemode, - mapname : newstring(server::smapname), - }, + vector teams; + loopv(scores) + { + teaminfo teaminfo = { + .team = newstring(scores[i].team), + .score = scores[i].score, + }; + teams.add(teaminfo); + } + + stats stats = { + .players = players, + .teams = teams, + .game = { + .mode = server::gamemode, + .mapname = newstring(server::smapname)}, }; + + writeto(statsfile, stats); } + + COMMANDN(writestats, write, "s"); } // namespace statswriter } // namespace remod diff --git a/src/mod/statswriter.h b/src/mod/statswriter.h index 590dc8d..57dad27 100644 --- a/src/mod/statswriter.h +++ b/src/mod/statswriter.h @@ -7,6 +7,12 @@ namespace remod { namespace statswriter { + struct guninfo + { + int shotdamage; + int damage; + }; + struct playerinfo { const char *name; @@ -17,11 +23,7 @@ namespace remod int frags, flags, deaths, teamkills, shotdamage, damage; float effectiveness; int suicides; - struct - { - int shotdamage; - int damage; - } guninfo[NUMGUNS]; + guninfo gi[NUMGUNS]; }; struct teaminfo {