Skip to content

Commit ebec702

Browse files
Merge pull request #68 from liam-middlebrook/bingeboard
Add bingeboard
2 parents e25262e + 6caf7a5 commit ebec702

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

nethack_server/include/nhserver.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,20 @@ extern long db_add_new_game(int uid, const char *filename, const char *role,
126126
const char *race, const char *gend,
127127
const char *align, int mode, const char *plname,
128128
const char *levdesc);
129+
extern void db_add_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
130+
int energy, int max_energy, int attrib_str, int attrib_int,
131+
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
132+
int score);
129133
extern void db_update_game(int gameid, int moves, int depth,
130134
const char *levdesc);
135+
extern void db_update_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
136+
int energy, int max_energy, int attrib_str, int attrib_int,
137+
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
138+
int score);
131139
extern enum getgame_result db_get_game_filename(
132140
int gid, char *filenamebuf, int buflen);
133141
extern void db_delete_game(int uid, int gid);
142+
extern void db_delete_binge_entry(int gid);
134143
extern struct gamefile_info *db_list_games(int completed, int uid, int limit,
135144
int *count);
136145
extern void db_add_topten_entry(int gid, int points, int hp, int maxhp,

nethack_server/src/clientcmd.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ ccmd_create_game(json_t * params)
254254
ri->racenames[race], ri->gendnames[gend],
255255
ri->alignnames[align], mode, name,
256256
player_info.level_desc);
257+
db_add_binge_entry(gameid, player_info.z, player_info.hp,
258+
player_info.hpmax, player_info.gold,
259+
player_info.moves, player_info.en,
260+
player_info.enmax, player_info.st,
261+
player_info.in, player_info.wi, player_info.dx,
262+
player_info.co, player_info.ch, player_info.score);
257263
log_msg("%s has created a new game (%d) as %s", user_info.username,
258264
gameid, name);
259265
j_msg = json_pack("{si}", "return", gameid);
@@ -349,6 +355,7 @@ ccmd_play_game(json_t * params)
349355
"list?",
350356
"yn", 'n') == 'y') {
351357
db_delete_game(user_info.uid, gid);
358+
db_delete_binge_entry(gid);
352359
log_msg("%s has chosen to remove game %d from the database",
353360
user_info.username, gid);
354361
}
@@ -359,6 +366,13 @@ ccmd_play_game(json_t * params)
359366
db_update_game(gid, player_info.moves, player_info.z,
360367
player_info.level_desc);
361368

369+
db_update_binge_entry(gid, player_info.z, player_info.hp,
370+
player_info.hpmax, player_info.gold,
371+
player_info.moves, player_info.en,
372+
player_info.enmax, player_info.st,
373+
player_info.in, player_info.wi, player_info.dx,
374+
player_info.co, player_info.ch, player_info.score);
375+
362376
/* move the finished game to its final resting place */
363377
if (status == GAME_OVER) {
364378
char filename[1024], final_name[1024];
@@ -394,6 +408,13 @@ ccmd_exit_game(json_t * params)
394408
if (status) {
395409
db_update_game(gameid, player_info.moves, player_info.z,
396410
player_info.level_desc);
411+
db_update_binge_entry(gameid, player_info.z, player_info.hp,
412+
player_info.hpmax, player_info.gold,
413+
player_info.moves, player_info.en,
414+
player_info.enmax, player_info.st,
415+
player_info.in, player_info.wi, player_info.dx,
416+
player_info.co, player_info.ch, player_info.score);
417+
397418
log_msg("%s has closed game %d", user_info.username, gameid);
398419
gameid = -1;
399420
close(gamefd);

nethack_server/src/db_pgsql.c

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ static const char SQL_init_games_table[] =
3636
"owner integer NOT NULL REFERENCES users (uid), " "ts timestamp NOT NULL, "
3737
"start_ts timestamp NOT NULL" ");";
3838

39+
static const char SQL_init_binge_table[] =
40+
"CREATE TABLE bingeboard ("
41+
"gid SERIAL PRIMARY KEY REFERENCES games (gid), "
42+
"level integer NOT NULL, "
43+
"hp integer NOT NULL, "
44+
"max_hp integer NOT NULL, "
45+
"gold integer NOT NULL, "
46+
"moves integer NOT NULL, "
47+
"energy integer NOT NULL, "
48+
"max_energy integer NOT NULL, "
49+
"attrib_str integer NOT NULL, "
50+
"attrib_int integer NOT NULL, "
51+
"attrib_wis integer NOT NULL, "
52+
"attrib_dex integer NOT NULL, "
53+
"attrib_con integer NOT NULL, "
54+
"attrib_cha integer NOT NULL, "
55+
"score integer NOT NULL "
56+
");";
57+
3958
static const char SQL_init_topten_table[] =
4059
"CREATE TABLE topten(" "gid integer PRIMARY KEY REFERENCES games (gid), "
4160
"points integer NOT NULL, " "hp integer NOT NULL, "
@@ -81,16 +100,36 @@ static const char SQL_add_game[] =
81100
"VALUES ($1::text, $2::text, $3::text, $4::text, $5::text, "
82101
"$6::integer, 1, 1, $7::integer, $8::text, $9::text, 'now', 'now')";
83102

103+
static const char SQL_add_binge_entry[] =
104+
"INSERT INTO bingeboard ("
105+
"gid, level, hp, max_hp, gold, moves, energy, max_energy, attrib_str, "
106+
"attrib_int, attrib_wis, attrib_dex, attrib_con, attrib_cha, score) "
107+
"VALUES ($1::integer, $2::integer, $3::integer, $4::integer, $5::integer, "
108+
"$6::integer, $7::integer, $8::integer, $9::integer, $10::integer, $11::integer, "
109+
"$12::integer, $13::integer, $14::integer, $15::integer)";
110+
84111
static const char SQL_delete_game[] =
85112
"DELETE FROM games WHERE owner = $1::integer AND gid = $2::integer;";
86113

114+
static const char SQL_delete_binge_entry[] =
115+
"DELETE FROM bingeboard WHERE gid = $1::integer;";
116+
87117
static const char SQL_last_game_id[] = "SELECT currval('games_gid_seq');";
88118

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

124+
static const char SQL_update_binge_entry[] =
125+
"UPDATE bingeboard "
126+
"SET level = $2::integer, hp = $3::integer, max_hp = $4::integer, "
127+
"gold = $5::integer, moves = $6::integer, energy = $7::integer, "
128+
"max_energy = $8::integer, attrib_str = $9::integer, attrib_int = $10::integer, "
129+
"attrib_wis = $11::integer, attrib_dex = $12::integer, attrib_con = $13::integer, "
130+
"attrib_cha = $14::integer, score = $15::integer WHERE gid = $1::integer;";
131+
132+
94133
static const char SQL_set_game_done[] =
95134
"UPDATE games " "SET done = TRUE " "WHERE gid = $1::integer;";
96135

@@ -208,6 +247,7 @@ check_database(void)
208247
*/
209248
if (!check_create_table("users", SQL_init_user_table) ||
210249
!check_create_table("games", SQL_init_games_table) ||
250+
!check_create_table("bingeboard", SQL_init_binge_table) ||
211251
!check_create_table("topten", SQL_init_topten_table))
212252
goto err;
213253

@@ -446,6 +486,60 @@ db_add_new_game(int uid, const char *filename, const char *role,
446486
return gid;
447487
}
448488

489+
void
490+
db_add_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
491+
int energy, int max_energy, int attrib_str, int attrib_int,
492+
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
493+
int score)
494+
{
495+
PGresult *res;
496+
char gid_str[16],
497+
level_str[16],
498+
hp_str[16],
499+
max_hp_str[16],
500+
gold_str[16],
501+
moves_str[16],
502+
energy_str[16],
503+
max_energy_str[16],
504+
attrib_str_str[16],
505+
attrib_int_str[16],
506+
attrib_wis_str[16],
507+
attrib_dex_str[16],
508+
attrib_con_str[16],
509+
attrib_cha_str[16],
510+
score_str[16];
511+
512+
const char *const params[] = {gid_str, level_str, hp_str, max_hp_str,
513+
gold_str, moves_str, energy_str, max_energy_str, attrib_str_str,
514+
attrib_int_str, attrib_wis_str, attrib_dex_str, attrib_con_str,
515+
attrib_cha_str, score_str
516+
};
517+
const int paramFormats[15] = { 0 };
518+
519+
snprintf(gid_str, sizeof(gid_str), "%d", gid);
520+
snprintf(level_str, sizeof(level_str), "%d", level);
521+
snprintf(hp_str, sizeof(hp_str), "%d", hp);
522+
snprintf(max_hp_str, sizeof(max_hp_str), "%d", max_hp);
523+
snprintf(gold_str, sizeof(gold_str), "%d", gold);
524+
snprintf(moves_str, sizeof(moves_str), "%d", moves);
525+
snprintf(energy_str, sizeof(energy_str), "%d", energy);
526+
snprintf(max_energy_str, sizeof(max_energy_str), "%d", max_energy);
527+
snprintf(attrib_str_str, sizeof(attrib_str_str), "%d", attrib_str);
528+
snprintf(attrib_int_str, sizeof(attrib_int_str), "%d", attrib_int);
529+
snprintf(attrib_wis_str, sizeof(attrib_wis_str), "%d", attrib_wis);
530+
snprintf(attrib_dex_str, sizeof(attrib_dex_str), "%d", attrib_dex);
531+
snprintf(attrib_con_str, sizeof(attrib_con_str), "%d", attrib_con);
532+
snprintf(attrib_cha_str, sizeof(attrib_cha_str), "%d", attrib_cha);
533+
snprintf(score_str, sizeof(score_str), "%d", score);
534+
535+
res =
536+
PQexecParams(conn, SQL_add_binge_entry, 15, NULL, params, NULL, paramFormats,
537+
0);
538+
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
539+
log_msg("db_add_binge_entry error while adding (%d): %s", gid, PQerrorMessage(conn));
540+
PQclear(res);
541+
}
542+
}
449543

450544
void
451545
db_update_game(int game, int moves, int depth, const char *levdesc)
@@ -467,6 +561,60 @@ db_update_game(int game, int moves, int depth, const char *levdesc)
467561
PQclear(res);
468562
}
469563

564+
void
565+
db_update_binge_entry(int gid, int level, int hp, int max_hp, int gold, int moves,
566+
int energy, int max_energy, int attrib_str, int attrib_int,
567+
int attrib_wis, int attrib_dex, int attrib_con, int attrib_cha,
568+
int score)
569+
{
570+
PGresult *res;
571+
char gid_str[16],
572+
level_str[16],
573+
hp_str[16],
574+
max_hp_str[16],
575+
gold_str[16],
576+
moves_str[16],
577+
energy_str[16],
578+
max_energy_str[16],
579+
attrib_str_str[16],
580+
attrib_int_str[16],
581+
attrib_wis_str[16],
582+
attrib_dex_str[16],
583+
attrib_con_str[16],
584+
attrib_cha_str[16],
585+
score_str[16];
586+
587+
const char *const params[] = {gid_str, level_str, hp_str, max_hp_str,
588+
gold_str, moves_str, energy_str, max_energy_str, attrib_str_str,
589+
attrib_int_str, attrib_wis_str, attrib_dex_str, attrib_con_str,
590+
attrib_cha_str, score_str
591+
};
592+
const int paramFormats[15] = { 0 };
593+
594+
snprintf(gid_str, sizeof(gid_str), "%d", gid);
595+
snprintf(level_str, sizeof(level_str), "%d", level);
596+
snprintf(hp_str, sizeof(hp_str), "%d", hp);
597+
snprintf(max_hp_str, sizeof(max_hp_str), "%d", max_hp);
598+
snprintf(gold_str, sizeof(gold_str), "%d", gold);
599+
snprintf(moves_str, sizeof(moves_str), "%d", moves);
600+
snprintf(energy_str, sizeof(energy_str), "%d", energy);
601+
snprintf(max_energy_str, sizeof(max_energy_str), "%d", max_energy);
602+
snprintf(attrib_str_str, sizeof(attrib_str_str), "%d", attrib_str);
603+
snprintf(attrib_int_str, sizeof(attrib_int_str), "%d", attrib_int);
604+
snprintf(attrib_wis_str, sizeof(attrib_wis_str), "%d", attrib_wis);
605+
snprintf(attrib_dex_str, sizeof(attrib_dex_str), "%d", attrib_dex);
606+
snprintf(attrib_con_str, sizeof(attrib_con_str), "%d", attrib_con);
607+
snprintf(attrib_cha_str, sizeof(attrib_cha_str), "%d", attrib_cha);
608+
snprintf(score_str, sizeof(score_str), "%d", score);
609+
610+
res =
611+
PQexecParams(conn, SQL_update_binge_entry, 15, NULL, params, NULL, paramFormats,
612+
0);
613+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
614+
log_msg("update_binge_entry error: %s", PQerrorMessage(conn));
615+
PQclear(res);
616+
}
617+
470618
void
471619
db_delete_game(int uid, int gid)
472620
{
@@ -487,6 +635,25 @@ db_delete_game(int uid, int gid)
487635
PQclear(res);
488636
}
489637

638+
void
639+
db_delete_binge_entry(int gid)
640+
{
641+
PGresult *res;
642+
char gid_str[16];
643+
const char *const params[] = { gid_str };
644+
const int paramFormats[] = { 0 };
645+
646+
snprintf(gid_str, sizeof(gid_str), "%d", gid);
647+
648+
res =
649+
PQexecParams(conn, SQL_delete_binge_entry, 1, NULL, params, NULL, paramFormats,
650+
0);
651+
if (PQresultStatus(res) != PGRES_COMMAND_OK)
652+
log_msg("db_delete_binge_entry error: %s", PQerrorMessage(conn));
653+
654+
PQclear(res);
655+
}
656+
490657

491658
static struct gamefile_info *
492659
db_game_name_core(int completed, int uid, int gid, int limit, int *count)

nethack_server/src/winprocs.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ srv_update_status(struct nh_player_info *pi)
282282
}
283283
player_info = *pi;
284284

285+
db_update_binge_entry(gameid, player_info.z, player_info.hp,
286+
player_info.hpmax, player_info.gold,
287+
player_info.moves, player_info.en,
288+
player_info.enmax, player_info.st,
289+
player_info.in, player_info.wi, player_info.dx,
290+
player_info.co, player_info.ch, player_info.score);
291+
285292
add_display_data("update_status", jobj);
286293
}
287294

0 commit comments

Comments
 (0)