Skip to content

Commit 8de75ee

Browse files
authored
Adds move count to hiscores (#157)
* Adds move count to hiscores Making this available for possible playtime based unlocks. * Review fixes * More review fixes
1 parent 48d0e5e commit 8de75ee

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/hiscore.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ static DbQuery MIGRATE_COMMANDS[] = {
2828
"time DATETIME DEFAULT CURRENT_TIMESTAMP, "
2929
"gold FLOAT, "
3030
"playerLevel INTEGER, "
31-
"dungeonLevel INTEGER)",
31+
"dungeonLevel INTEGER, "
32+
"moves INTEGER DEFAULT 0)",
3233
NULL, NULL},
3334
{"CREATE TABLE IF NOT EXISTS hiscore_artifacts("
3435
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
@@ -42,7 +43,7 @@ static DbQuery MIGRATE_COMMANDS[] = {
4243
static int load_hiscore_cb(void *, int count, char **values, char **colNames);
4344

4445
static DbQuery GET_TOP_10_COMMAND = {"SELECT datetime(time, 'localtime') as "
45-
"time, gold, playerLevel, dungeonLevel "
46+
"time, gold, playerLevel, dungeonLevel, moves "
4647
"FROM hiscore "
4748
"ORDER BY gold DESC "
4849
"LIMIT 10",
@@ -68,6 +69,9 @@ create_tables(void)
6869

6970
db_execute(db, query);
7071
}
72+
73+
// Soft migration: add moves column to existing databases (ignore error if already present)
74+
(void)db_execute_stmnt("ALTER TABLE hiscore ADD COLUMN moves INTEGER DEFAULT 0", db, NULL, NULL);
7175
}
7276

7377
void
@@ -80,18 +84,20 @@ hiscore_init(void)
8084
create_tables();
8185
}
8286

83-
static void
84-
save_hiscore(double gold, int lvl, int dlvl)
87+
static int
88+
save_hiscore(double gold, int lvl, int dlvl, unsigned int moves)
8589
{
86-
const char *query = "INSERT INTO hiscore(gold, playerLevel, dungeonLevel) values (?, ?, ?)";
90+
const char *query = "INSERT INTO hiscore(gold, playerLevel, dungeonLevel, moves) values (?, ?, ?, ?)";
8791
sqlite3_stmt *stmt = db_prepare(db, query);
8892

89-
debug("Saving high score: %dg %dpl %dl", gold, lvl, dlvl);
93+
debug("Saving high score: %dg %dpl %dl %um", gold, lvl, dlvl, moves);
9094
sqlite3_bind_double(stmt, 1, gold);
9195
sqlite3_bind_int(stmt, 2, lvl);
9296
sqlite3_bind_int(stmt, 3, dlvl);
93-
sqlite3_step(stmt);
97+
sqlite3_bind_int64(stmt, 4, (sqlite3_int64)moves);
98+
int rc = sqlite3_step(stmt);
9499
sqlite3_finalize(stmt);
100+
return rc == SQLITE_DONE ? (int)sqlite3_last_insert_rowid(db) : 0;
95101
}
96102

97103
static void
@@ -111,10 +117,9 @@ save_hiscore_artifact(int hid, int aid)
111117
void
112118
hiscore_register(Player *p, unsigned int dungeonLevel)
113119
{
114-
save_hiscore(p->gold, p->stats.lvl, dungeonLevel);
115-
int hiscoreId = (int)sqlite3_last_insert_rowid(db);
116-
if (!hiscoreId) {
117-
error("Failed to retrieve last inserted hiscore id");
120+
int hiscoreId = save_hiscore(p->gold, p->stats.lvl, dungeonLevel, p->stat_data.total_steps);
121+
if (hiscoreId == 0) {
122+
error("Failed to save hiscore");
118123
return;
119124
}
120125

@@ -138,6 +143,10 @@ load_hiscore_cb(void *result, int count, char **values, char **colNames)
138143
score->playerLevel = atoi(values[i]);
139144
else if (strcmp(colNames[i], "dungeonLevel") == 0)
140145
score->dungeonLevel = atoi(values[i]);
146+
else if (strcmp(colNames[i], "moves") == 0) {
147+
unsigned long parsed = values[i] ? strtoul(values[i], NULL, 10) : 0UL;
148+
score->moves = (unsigned int)parsed;
149+
}
141150
}
142151
LinkedList **scores = result;
143152
linkedlist_append(scores, score);

src/hiscore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef struct HiScore {
2626
double gold;
2727
unsigned int playerLevel;
2828
unsigned int dungeonLevel;
29+
unsigned int moves;
2930
LinkedList *artifacts;
3031
} HiScore;
3132

0 commit comments

Comments
 (0)