Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bingeboard #68

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions nethack_server/include/nhserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,20 @@ extern long db_add_new_game(int uid, const char *filename, const char *role,
const char *race, const char *gend,
const char *align, int mode, const char *plname,
const char *levdesc);
extern void db_add_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
int energy, int max_energy, int attrib_str, int attrib_int,
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
int score);
extern void db_update_game(int gameid, int moves, int depth,
const char *levdesc);
extern void db_update_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
int energy, int max_energy, int attrib_str, int attrib_int,
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
int score);
extern enum getgame_result db_get_game_filename(
int gid, char *filenamebuf, int buflen);
extern void db_delete_game(int uid, int gid);
extern void db_delete_binge_entry(int gid);
extern struct gamefile_info *db_list_games(int completed, int uid, int limit,
int *count);
extern void db_add_topten_entry(int gid, int points, int hp, int maxhp,
Expand Down
21 changes: 21 additions & 0 deletions nethack_server/src/clientcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ ccmd_create_game(json_t * params)
ri->racenames[race], ri->gendnames[gend],
ri->alignnames[align], mode, name,
player_info.level_desc);
db_add_binge_entry(gameid, player_info.z, player_info.hp,
player_info.hpmax, player_info.gold,
player_info.moves, player_info.en,
player_info.enmax, player_info.st,
player_info.in, player_info.wi, player_info.dx,
player_info.co, player_info.ch, player_info.score);
log_msg("%s has created a new game (%d) as %s", user_info.username,
gameid, name);
j_msg = json_pack("{si}", "return", gameid);
Expand Down Expand Up @@ -349,6 +355,7 @@ ccmd_play_game(json_t * params)
"list?",
"yn", 'n') == 'y') {
db_delete_game(user_info.uid, gid);
db_delete_binge_entry(gid);
log_msg("%s has chosen to remove game %d from the database",
user_info.username, gid);
}
Expand All @@ -359,6 +366,13 @@ ccmd_play_game(json_t * params)
db_update_game(gid, player_info.moves, player_info.z,
player_info.level_desc);

db_update_binge_entry(gid, player_info.z, player_info.hp,
player_info.hpmax, player_info.gold,
player_info.moves, player_info.en,
player_info.enmax, player_info.st,
player_info.in, player_info.wi, player_info.dx,
player_info.co, player_info.ch, player_info.score);

/* move the finished game to its final resting place */
if (status == GAME_OVER) {
char filename[1024], final_name[1024];
Expand Down Expand Up @@ -394,6 +408,13 @@ ccmd_exit_game(json_t * params)
if (status) {
db_update_game(gameid, player_info.moves, player_info.z,
player_info.level_desc);
db_update_binge_entry(gameid, player_info.z, player_info.hp,
player_info.hpmax, player_info.gold,
player_info.moves, player_info.en,
player_info.enmax, player_info.st,
player_info.in, player_info.wi, player_info.dx,
player_info.co, player_info.ch, player_info.score);

log_msg("%s has closed game %d", user_info.username, gameid);
gameid = -1;
close(gamefd);
Expand Down
167 changes: 167 additions & 0 deletions nethack_server/src/db_pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ static const char SQL_init_games_table[] =
"owner integer NOT NULL REFERENCES users (uid), " "ts timestamp NOT NULL, "
"start_ts timestamp NOT NULL" ");";

static const char SQL_init_binge_table[] =
"CREATE TABLE bingeboard ("
"gid SERIAL PRIMARY KEY REFERENCES games (gid), "
"level integer NOT NULL, "
"hp integer NOT NULL, "
"max_hp integer NOT NULL, "
"gold integer NOT NULL, "
"moves integer NOT NULL, "
"energy integer NOT NULL, "
"max_energy integer NOT NULL, "
"attrib_str integer NOT NULL, "
"attrib_int integer NOT NULL, "
"attrib_wis integer NOT NULL, "
"attrib_dex integer NOT NULL, "
"attrib_con integer NOT NULL, "
"attrib_cha integer NOT NULL, "
"score integer NOT NULL "
");";

static const char SQL_init_topten_table[] =
"CREATE TABLE topten(" "gid integer PRIMARY KEY REFERENCES games (gid), "
"points integer NOT NULL, " "hp integer NOT NULL, "
Expand Down Expand Up @@ -81,16 +100,36 @@ static const char SQL_add_game[] =
"VALUES ($1::text, $2::text, $3::text, $4::text, $5::text, "
"$6::integer, 1, 1, $7::integer, $8::text, $9::text, 'now', 'now')";

static const char SQL_add_binge_entry[] =
"INSERT INTO bingeboard ("
"gid, level, hp, max_hp, gold, moves, energy, max_energy, attrib_str, "
"attrib_int, attrib_wis, attrib_dex, attrib_con, attrib_cha, score) "
"VALUES ($1::integer, $2::integer, $3::integer, $4::integer, $5::integer, "
"$6::integer, $7::integer, $8::integer, $9::integer, $10::integer, $11::integer, "
"$12::integer, $13::integer, $14::integer, $15::integer)";

static const char SQL_delete_game[] =
"DELETE FROM games WHERE owner = $1::integer AND gid = $2::integer;";

static const char SQL_delete_binge_entry[] =
"DELETE FROM bingeboard WHERE gid = $1::integer;";

static const char SQL_last_game_id[] = "SELECT currval('games_gid_seq');";

static const char SQL_update_game[] =
"UPDATE games "
"SET ts = 'now', moves = $2::integer, depth = $3::integer, level_desc = "
"$4::text WHERE gid = $1::integer;";

static const char SQL_update_binge_entry[] =
"UPDATE bingeboard "
"SET level = $2::integer, hp = $3::integer, max_hp = $4::integer, "
"gold = $5::integer, moves = $6::integer, energy = $7::integer, "
"max_energy = $8::integer, attrib_str = $9::integer, attrib_int = $10::integer, "
"attrib_wis = $11::integer, attrib_dex = $12::integer, attrib_con = $13::integer, "
"attrib_cha = $14::integer, score = $15::integer WHERE gid = $1::integer;";


static const char SQL_set_game_done[] =
"UPDATE games " "SET done = TRUE " "WHERE gid = $1::integer;";

Expand Down Expand Up @@ -208,6 +247,7 @@ check_database(void)
*/
if (!check_create_table("users", SQL_init_user_table) ||
!check_create_table("games", SQL_init_games_table) ||
!check_create_table("bingeboard", SQL_init_binge_table) ||
!check_create_table("topten", SQL_init_topten_table))
goto err;

Expand Down Expand Up @@ -446,6 +486,60 @@ db_add_new_game(int uid, const char *filename, const char *role,
return gid;
}

void
db_add_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
int energy, int max_energy, int attrib_str, int attrib_int,
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
int score)
{
PGresult *res;
char gid_str[16],
level_str[16],
hp_str[16],
max_hp_str[16],
gold_str[16],
moves_str[16],
energy_str[16],
max_energy_str[16],
attrib_str_str[16],
attrib_int_str[16],
attrib_wis_str[16],
attrib_dex_str[16],
attrib_con_str[16],
attrib_cha_str[16],
score_str[16];

const char *const params[] = {gid_str, level_str, hp_str, max_hp_str,
gold_str, moves_str, energy_str, max_energy_str, attrib_str_str,
attrib_int_str, attrib_wis_str, attrib_dex_str, attrib_con_str,
attrib_cha_str, score_str
};
const int paramFormats[15] = { 0 };

snprintf(gid_str, sizeof(gid_str), "%d", gid);
snprintf(level_str, sizeof(level_str), "%d", level);
snprintf(hp_str, sizeof(hp_str), "%d", hp);
snprintf(max_hp_str, sizeof(max_hp_str), "%d", max_hp);
snprintf(gold_str, sizeof(gold_str), "%d", gold);
snprintf(moves_str, sizeof(moves_str), "%d", moves);
snprintf(energy_str, sizeof(energy_str), "%d", energy);
snprintf(max_energy_str, sizeof(max_energy_str), "%d", max_energy);
snprintf(attrib_str_str, sizeof(attrib_str_str), "%d", attrib_str);
snprintf(attrib_int_str, sizeof(attrib_int_str), "%d", attrib_int);
snprintf(attrib_wis_str, sizeof(attrib_wis_str), "%d", attrib_wis);
snprintf(attrib_dex_str, sizeof(attrib_dex_str), "%d", attrib_dex);
snprintf(attrib_con_str, sizeof(attrib_con_str), "%d", attrib_con);
snprintf(attrib_cha_str, sizeof(attrib_cha_str), "%d", attrib_cha);
snprintf(score_str, sizeof(score_str), "%d", score);

res =
PQexecParams(conn, SQL_add_binge_entry, 15, NULL, params, NULL, paramFormats,
0);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
log_msg("db_add_binge_entry error while adding (%d): %s", gid, PQerrorMessage(conn));
PQclear(res);
}
}

void
db_update_game(int game, int moves, int depth, const char *levdesc)
Expand All @@ -467,6 +561,60 @@ db_update_game(int game, int moves, int depth, const char *levdesc)
PQclear(res);
}

void
db_update_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
int energy, int max_energy, int attrib_str, int attrib_int,
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
int score)
{
PGresult *res;
char gid_str[16],
level_str[16],
hp_str[16],
max_hp_str[16],
gold_str[16],
moves_str[16],
energy_str[16],
max_energy_str[16],
attrib_str_str[16],
attrib_int_str[16],
attrib_wis_str[16],
attrib_dex_str[16],
attrib_con_str[16],
attrib_cha_str[16],
score_str[16];

const char *const params[] = {gid_str, level_str, hp_str, max_hp_str,
gold_str, moves_str, energy_str, max_energy_str, attrib_str_str,
attrib_int_str, attrib_wis_str, attrib_dex_str, attrib_con_str,
attrib_cha_str, score_str
};
const int paramFormats[15] = { 0 };

snprintf(gid_str, sizeof(gid_str), "%d", gid);
snprintf(level_str, sizeof(level_str), "%d", level);
snprintf(hp_str, sizeof(hp_str), "%d", hp);
snprintf(max_hp_str, sizeof(max_hp_str), "%d", max_hp);
snprintf(gold_str, sizeof(gold_str), "%d", gold);
snprintf(moves_str, sizeof(moves_str), "%d", moves);
snprintf(energy_str, sizeof(energy_str), "%d", energy);
snprintf(max_energy_str, sizeof(max_energy_str), "%d", max_energy);
snprintf(attrib_str_str, sizeof(attrib_str_str), "%d", attrib_str);
snprintf(attrib_int_str, sizeof(attrib_int_str), "%d", attrib_int);
snprintf(attrib_wis_str, sizeof(attrib_wis_str), "%d", attrib_wis);
snprintf(attrib_dex_str, sizeof(attrib_dex_str), "%d", attrib_dex);
snprintf(attrib_con_str, sizeof(attrib_con_str), "%d", attrib_con);
snprintf(attrib_cha_str, sizeof(attrib_cha_str), "%d", attrib_cha);
snprintf(score_str, sizeof(score_str), "%d", score);

res =
PQexecParams(conn, SQL_update_binge_entry, 15, NULL, params, NULL, paramFormats,
0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
log_msg("update_binge_entry error: %s", PQerrorMessage(conn));
PQclear(res);
}

void
db_delete_game(int uid, int gid)
{
Expand All @@ -487,6 +635,25 @@ db_delete_game(int uid, int gid)
PQclear(res);
}

void
db_delete_binge_entry(int gid)
{
PGresult *res;
char gid_str[16];
const char *const params[] = { gid_str };
const int paramFormats[] = { 0 };

snprintf(gid_str, sizeof(gid_str), "%d", gid);

res =
PQexecParams(conn, SQL_delete_binge_entry, 1, NULL, params, NULL, paramFormats,
0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
log_msg("db_delete_binge_entry error: %s", PQerrorMessage(conn));

PQclear(res);
}


static struct gamefile_info *
db_game_name_core(int completed, int uid, int gid, int limit, int *count)
Expand Down
7 changes: 7 additions & 0 deletions nethack_server/src/winprocs.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ srv_update_status(struct nh_player_info *pi)
}
player_info = *pi;

db_update_binge_entry(gameid, player_info.z, player_info.hp,
player_info.hpmax, player_info.gold,
player_info.moves, player_info.en,
player_info.enmax, player_info.st,
player_info.in, player_info.wi, player_info.dx,
player_info.co, player_info.ch, player_info.score);

add_display_data("update_status", jobj);
}

Expand Down