Skip to content

Commit

Permalink
First version of statswriter
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyahuyasa committed Jan 8, 2025
1 parent 59658a8 commit 3b71b1d
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 51 deletions.
155 changes: 104 additions & 51 deletions src/mod/statswriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,118 @@
* JSON stats writer
*/

#include "statswriter.h"
#include "remod.h"
#include "tools.h"
#include "fpsgame.h"

#define STATSFILE_FORMAT_VERSION 1

EXTENSION(STATSWRITER);

namespace remod {
namespace statswriter {
struct playerinfo {
const char *name;
const char *authname;
unsigned int connect_seconds;
const char *team;
int privilege;
int frags, flags, deaths, teamkills, shotdamage, damage;
float effectiveness;
int suicides;
struct {
int shotdamage;
int damage;
} guninfo[NUMGUNS];
};
struct teaminfo {
const char *team;
int score;
};
struct gameinfo {
int mode;
const char *mapname;
};
struct stats {
vector<playerinfo> players;
vector<teaminfo> teams;
struct gameinfo game;
};
namespace remod
{
namespace statswriter
{

void write_game(stream *statsfile, gameinfo game)
{
statsfile->printf("\"game\":{");
statsfile->printf(" \"mode\": %d,", game.mode);
statsfile->printf(" \"map\": \"%s\"}", game.mapname);
}

void write_teams(stream *statsfile, vector<teaminfo> teams)
{
statsfile->printf("\"teams\":[");
loopv(teams)
{
if (i != 0)
{
statsfile->printf(",");
}
statsfile->printf("{\"team\": \"%s\",", teams[i].team);
statsfile->printf(" \"score\": \"%d\"}", teams[i].score);
}
statsfile->printf("]");
}

void write_players(stream *statsfile, vector<playerinfo> players)
{
statsfile->printf("\"players\":[");
loopv(players)
{
if (i != 0)
{
statsfile->printf(",");
}
statsfile->printf("{\"name\": \"%s\",", players[i].name);
statsfile->printf(" \"authname\": \"%s\",", players[i].authname);
statsfile->printf(" \"connect_seconds\": %d,", players[i].connect_seconds);
statsfile->printf(" \"team\": \"%s\",", players[i].team);
statsfile->printf(" \"privilege\": %d,", players[i].privilege);
statsfile->printf(" \"frags\": %d,", players[i].frags);
statsfile->printf(" \"flags\": %d,", players[i].flags);
statsfile->printf(" \"deaths\": %d,", players[i].deaths);
statsfile->printf(" \"teamkills\": %d,", players[i].teamkills);
statsfile->printf(" \"shotdamage\": %d,", players[i].shotdamage);
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);
loopj(NUMGUNS)
{
if (i != 0)
{
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(" ]}");
}
statsfile->printf("]");
}

void writeto(const char *path, stats stats)
{
stream *statsfile = openfile(path, "wb");
if (!statsfile)
{
conoutf("could not write stats to \"%s\"", path);
return;
}

void write_game(stream *statsfile, gameinfo game) {
statsfile->printf("\"game\":{\"mode\": %d, \"map\": \"%s\"}", game.mode,
game.mapname);
}
statsfile->printf("{ \"version\": %d,", STATSFILE_FORMAT_VERSION);
write_game(statsfile, stats.game);
statsfile->printf(",");
write_teams(statsfile, stats.teams);
statsfile->printf(",");
write_players(statsfile, stats.players);
statsfile->printf("}");
};

void write_teams(stream *statsfile, gameinfo game) {
// TODO
}
void write(const char *path)
{
vector<playerinfo> players;
vector<teaminfo> teams;

void write_players(stream *statsfile, gameinfo game) {
// TODO
}
loopv(server::clients)
{
}

void write(const char *path, stats stats) {
stream *statsfile = openfile(path, "wb");
if (!statsfile) {
conoutf("could not write stats to \"%s\"", path);
return;
}
struct stats stats = {
players : {

statsfile->printf("{");
write_game(statsfile, stats.game);
statsfile->printf("}");
};
},
teams : {

} // namespace statswriter
},
game : {
mode : server::gamemode,
mapname : newstring(server::smapname),
},
};
}
} // namespace statswriter
} // namespace remod
44 changes: 44 additions & 0 deletions src/mod/statswriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef __STATSWRITER_H__
#define __STATSWRITER_H__

#include "game.h"

namespace remod
{
namespace statswriter
{
struct playerinfo
{
const char *name;
const char *authname;
unsigned int connect_seconds;
const char *team;
int privilege;
int frags, flags, deaths, teamkills, shotdamage, damage;
float effectiveness;
int suicides;
struct
{
int shotdamage;
int damage;
} guninfo[NUMGUNS];
};
struct teaminfo
{
const char *team;
int score;
};
struct gameinfo
{
int mode;
const char *mapname;
};
struct stats
{
vector<playerinfo> players;
vector<teaminfo> teams;
struct gameinfo game;
};
} // namespace statswriter
} // namespace remod
#endif

0 comments on commit 3b71b1d

Please sign in to comment.