Skip to content

Commit

Permalink
bug squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
brandiob committed Jun 5, 2024
1 parent 4ccf294 commit 19d0a94
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
5 changes: 1 addition & 4 deletions databaseHandler/DatabaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ int DatabaseManager::createTable() {
std::string sql = getCreateTableSQL();
try {
int exit = sqlite3_open(this->dir, &DB); // Open the database
if (exit != SQLITE_OK) {
std::cerr << "Cannot open database: " << sqlite3_errmsg(DB) << std::endl;
return exit;
}
checkOpenDatabase(exit);

exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messageError);
if (exit != SQLITE_OK) {
Expand Down
34 changes: 31 additions & 3 deletions databaseHandler/MapData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ void MapData::displayLevel() {
bool idExists = false;

if (currentLevelId < getMinId() || currentLevelId > getMaxId()){
currentLevelId = getMinId();
}
currentLevelId = getMinId(); }

// Try IDs from currentLevelId to max ID
for (int id = currentLevelId; id <= getMaxId() && !idExists; id++) {
Expand Down Expand Up @@ -246,12 +245,41 @@ void MapData::loadCurrentValue() {
}

int MapData::displayLevelCallback(void* NotUsed, int argc, char** argv, char** azColName) {
for(int i = 0; i < argc; i++) {
for (int i = 0; i < argc; i++) {
std::string columnName = azColName[i];
if (columnName != "ID" && columnName != "Source" && columnName != "Level") {
std::cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << "\n";
}
}
std::cout << "\n";
return 0;
}

int MapData::getCurrentId() const {
return currentLevelId;
}

int MapData::getCurrentLevel() {
std::string sql = "SELECT Level FROM MapData WHERE ID = ?;";
sqlite3_stmt* stmt;
int level = -1; // Initialize level to -1 to indicate an error if no level is found

int exit = sqlite3_open(this->dir, &DB);
if (exit != SQLITE_OK) {
std::cout << "Cannot open database: " << sqlite3_errmsg(DB) << std::endl;
return level;
}

if (sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, 0) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, currentLevelId);
if (sqlite3_step(stmt) == SQLITE_ROW) {
level = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
} else {
std::cout << "Failed to prepare statement: " << sqlite3_errmsg(DB) << std::endl;
}

sqlite3_close(DB);
return level;
}
2 changes: 2 additions & 0 deletions databaseHandler/MapData.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class MapData : public DatabaseManager {
void setMapDataDir();
void nextLv();
void prevLv();
int getCurrentId() const;
int getCurrentLevel();
void displayLevel();
int getMaxId();
int getMinId();
Expand Down
34 changes: 8 additions & 26 deletions databaseHandler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,15 @@ DONE -mapData get next x 10 or something
int main() {
// Create a MapData object
MapData mapData;
// Test MapData methods
mapData.createTable();
// Test getCurrentLevel method
int currentLevel = mapData.getCurrentLevel();
std::cout << "Current level: " << currentLevel << std::endl;

// Insert a test record
mapData.insertData("Test Song", "Test Artist", 200, 120, 5, 1, "Test Source");
// Create a Leaderboards object
Leaderboards leaderboards;

mapData.outputData();
// Create a leaderboard for the current level
leaderboards.genLB(currentLevel); // Pass currentLevel directly to genLB

// Display the current level
std::cout << "Displaying current level:\n";
mapData.displayLevel();

// Go to the next level and display it
std::cout << "Going to next level:\n";
mapData.nextLv();

// Go to the next 10 levels and display it
std::cout << "Going to next 10 levels:\n";
mapData.next10Lv();

// Go to the previous level and display it
std::cout << "Going to next 10 levels again:\n";
mapData.next10Lv();

// Go to the previous 10 levels and display it
std::cout << "Going to prev 10 levels:\n";
mapData.prev10Lv();

return 0;
// Rest of your code...
}

0 comments on commit 19d0a94

Please sign in to comment.