add stats command
This commit is contained in:
parent
5722589b56
commit
bf987535ac
3 changed files with 57 additions and 6 deletions
|
@ -329,7 +329,7 @@ static void ParseCreateGame(Session *session, char *buf)
|
|||
CreateGame(session, description, map, players, ip, port, password);
|
||||
|
||||
DebugPrint("%s created a game\n" _C_ session->UserData.Name);
|
||||
DBAddGame(description, map, players_int);
|
||||
DBAddGame(session->Game->ID, description, map, players_int);
|
||||
Send(session, "CREATEGAME_OK\n");
|
||||
}
|
||||
|
||||
|
@ -476,6 +476,30 @@ static void ParseEndGame(Session *session, char *buf)
|
|||
Send(session, "ENDGAME_OK\n");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
** Parse STATS
|
||||
*/
|
||||
static void ParseStats(Session *session, char *buf)
|
||||
{
|
||||
char *result;
|
||||
int start_time = 0;
|
||||
char resultbuf[20] = {'\0'};
|
||||
|
||||
while (*buf == ' ') ++buf;
|
||||
if (*buf) {
|
||||
Parse1Arg(buf, &result);
|
||||
start_time = atoi(result);
|
||||
}
|
||||
|
||||
DebugPrint("%s requested stats\n" _C_ session->UserData.Name);
|
||||
char* reply = (char*)calloc(sizeof(char), strlen("GAMES SINCE 12345678901234567890: 12345678901234567890\n") + 1);
|
||||
DBStats(resultbuf, start_time);
|
||||
sprintf(reply, "GAMES SINCE %d: %s\n", start_time, resultbuf);
|
||||
Send(session, reply);
|
||||
Send(session, "STATS_OK\n");
|
||||
}
|
||||
|
||||
/**
|
||||
** Parse MSG
|
||||
*/
|
||||
|
@ -525,6 +549,8 @@ static void ParseBuffer(Session *session)
|
|||
ParsePartGame(session, buf + 8);
|
||||
} else if (!strncmp(buf, "ENDGAME ", 8)) {
|
||||
ParseEndGame(session, buf + 8);
|
||||
} else if (!strncmp(buf, "STATS ", 6)) {
|
||||
ParseStats(session, buf + 6);
|
||||
} else if (!strncmp(buf, "MSG ", 4)) {
|
||||
ParseMsg(session, buf + 4);
|
||||
} else {
|
||||
|
|
|
@ -62,7 +62,7 @@ static sqlite3 *DB;
|
|||
"id INTEGER PRIMARY KEY," \
|
||||
"date INTEGER," \
|
||||
"gamename TEXT," \
|
||||
"mapname TEXT" \
|
||||
"mapname TEXT," \
|
||||
"slots INTEGER" \
|
||||
");"
|
||||
|
||||
|
@ -261,15 +261,15 @@ int DBUpdateLoginDate(char *username)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int DBAddGame(char *description, char *mapname, int numplayers)
|
||||
int DBAddGame(int id, char *description, char *mapname, int numplayers)
|
||||
{
|
||||
char buf[1024];
|
||||
int t;
|
||||
char *errmsg;
|
||||
|
||||
t = (int)time(0);
|
||||
sprintf(buf, "INSERT INTO games VALUES(%d, '%s', '%s', %d);",
|
||||
t, description, mapname, numplayers);
|
||||
sprintf(buf, "INSERT INTO games VALUES(%d, %d, '%s', '%s', %d);",
|
||||
id, t, description, mapname, numplayers);
|
||||
if (sqlite3_exec(DB, buf, NULL, NULL, &errmsg) != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", errmsg);
|
||||
sqlite3_free(errmsg);
|
||||
|
@ -277,3 +277,27 @@ int DBAddGame(char *description, char *mapname, int numplayers)
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int DBStatsCallback(void *resultbuf, int argc, char **argv, char **colname)
|
||||
{
|
||||
Assert(argc == 1);
|
||||
strcpy((char *)resultbuf, argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int DBStats(char* resultbuf, int start_time)
|
||||
{
|
||||
char buf[1024];
|
||||
int t;
|
||||
char *errmsg;
|
||||
|
||||
t = (int)time(0);
|
||||
sprintf(buf, "SELECT COUNT(id) FROM games WHERE date > %d;", start_time);
|
||||
if (sqlite3_exec(DB, buf, DBStatsCallback, resultbuf, &errmsg) != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", errmsg);
|
||||
sqlite3_free(errmsg);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,8 @@ extern void DBQuit(void);
|
|||
extern int DBFindUser(char *username, char *password);
|
||||
extern int DBAddUser(char *username, char *password);
|
||||
extern int DBUpdateLoginDate(char *username);
|
||||
extern int DBAddGame(char *description, char *mapname, int numplayers);
|
||||
extern int DBAddGame(int id, char *description, char *mapname, int numplayers);
|
||||
extern int DBStats(char *results, int resultlen);
|
||||
|
||||
//@}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue