diff --git a/databaseHandler/DatabaseManager.cpp b/databaseHandler/DatabaseManager.cpp index 8847660..219a68d 100644 --- a/databaseHandler/DatabaseManager.cpp +++ b/databaseHandler/DatabaseManager.cpp @@ -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) { diff --git a/databaseHandler/MapData.cpp b/databaseHandler/MapData.cpp index 93d1956..c271970 100644 --- a/databaseHandler/MapData.cpp +++ b/databaseHandler/MapData.cpp @@ -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++) { @@ -246,7 +245,7 @@ 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"; @@ -254,4 +253,33 @@ int MapData::displayLevelCallback(void* NotUsed, int argc, char** argv, char** a } 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; } \ No newline at end of file diff --git a/databaseHandler/MapData.h b/databaseHandler/MapData.h index 8899a56..6d818a5 100644 --- a/databaseHandler/MapData.h +++ b/databaseHandler/MapData.h @@ -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(); diff --git a/databaseHandler/main.cpp b/databaseHandler/main.cpp index abd4ec3..b0a8e4f 100644 --- a/databaseHandler/main.cpp +++ b/databaseHandler/main.cpp @@ -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... } \ No newline at end of file