From e9014d82f334bd2abc9854323149f9a165207942 Mon Sep 17 00:00:00 2001 From: brandiob Date: Fri, 7 Jun 2024 14:54:45 -0400 Subject: [PATCH] code comments --- databaseHandler/DatabaseManager.cpp | 36 +++++------------------------ databaseHandler/DatabaseManager.h | 4 +--- databaseHandler/Leaderboards.cpp | 15 +++++------- databaseHandler/Leaderboards.h | 1 - databaseHandler/MapData.cpp | 33 +++++++++++++++----------- databaseHandler/MapData.h | 1 - databaseHandler/main.cpp | 10 ++++++-- 7 files changed, 41 insertions(+), 59 deletions(-) diff --git a/databaseHandler/DatabaseManager.cpp b/databaseHandler/DatabaseManager.cpp index 036fe2e..c6c9f1e 100644 --- a/databaseHandler/DatabaseManager.cpp +++ b/databaseHandler/DatabaseManager.cpp @@ -8,10 +8,6 @@ DatabaseManager::DatabaseManager() { sqlite3_open(dir, &DB); } -DatabaseManager::~DatabaseManager() { - sqlite3_close(DB); -} - int DatabaseManager::createDB() { sqlite3* DB; int exit = 0; @@ -24,6 +20,8 @@ void DatabaseManager::setDir(const char* directory) { dir = directory; } +//theres a lot that can be shared between leaderboards and mapdata, so use inheritance +//its just the sql query thats different int DatabaseManager::createTable() { sqlite3 *DB; char *messageError; @@ -58,31 +56,7 @@ int DatabaseManager::createTable() { return 0; } -int DatabaseManager::executeSQL(const std::string& sql, std::function bindFunc) { - sqlite3* DB; - sqlite3_stmt* stmt; - - int exit = sqlite3_open(this->dir, &DB); - checkOpenDatabase(exit); - - exit = sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, 0); - checkPrepareStatement(exit); - - // Call the bind function, if provided - if (bindFunc) { - bindFunc(stmt); - } - - if (sqlite3_step(stmt) != SQLITE_DONE) { - std::cerr << "Failed to execute statement: " << sqlite3_errmsg(DB) << std::endl; - } - - sqlite3_finalize(stmt); - sqlite3_close(DB); - - return exit; -} - +//it has to do checks and generate a statement since we're inserting variables void DatabaseManager::executeSQLWithCallback(const std::string& sql, std::function bindFunc, std::function callback) { sqlite3* DB; sqlite3_stmt* stmt; @@ -113,7 +87,7 @@ int DatabaseManager::prepareSQLStatement(const std::string& sql, sqlite3_stmt*& return SQLITE_OK; } - +//some generic checks for all insertData functions, to be inherited void DatabaseManager::insertDataHelper(const std::string& sql, std::function bindFunc) { sqlite3* DB; sqlite3_stmt* stmt; @@ -135,6 +109,7 @@ void DatabaseManager::insertDataHelper(const std::string& sql, std::function bindFunc); - int executeSQL(const std::string& sql, std::function bindFunc = nullptr); virtual int prepareSQLStatement(const std::string& sql, sqlite3_stmt*& stmt); virtual std::string getCreateTableSQL() = 0; //these two generic things check if things are working and display an error message if not diff --git a/databaseHandler/Leaderboards.cpp b/databaseHandler/Leaderboards.cpp index d585dc5..ed2e513 100644 --- a/databaseHandler/Leaderboards.cpp +++ b/databaseHandler/Leaderboards.cpp @@ -4,6 +4,7 @@ #include "Leaderboards.h" +//encapsulation void Leaderboards::setLeaderboardsDir() { DatabaseManager::setDir("../leaderboards.db"); } @@ -12,17 +13,13 @@ Leaderboards::Leaderboards() { setLeaderboardsDir(); sqlite3_open(dir, &DB); DatabaseManager::createDB(); - // Other initialization... -} - -Leaderboards::~Leaderboards() { - // Destructor implementation } int Leaderboards::createTable() { return DatabaseManager::createTable(); } +//sql query, the rest is done in database manager class std::string Leaderboards::getCreateTableSQL() { return "CREATE TABLE IF NOT EXISTS LEADERBOARD(" "ID INTEGER PRIMARY KEY AUTOINCREMENT, " @@ -32,6 +29,7 @@ std::string Leaderboards::getCreateTableSQL() { "TIME INT);"; } +//different classback since leaderboards display differently than songs int Leaderboards::callback(void* NotUsed, int argc, char** argv, char** azColName) { for(int i = 0; i < argc; i++) { std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << "\n"; @@ -45,6 +43,7 @@ void Leaderboards::deleteData(int id) { DatabaseManager::deleteData(id, deleteSql); } +//generic part is in database manager class void Leaderboards::outputData() { sqlite3* DB; char* messageError; @@ -72,10 +71,7 @@ int Leaderboards::getHiscore(const std::string& player, int level) { int exit = sqlite3_open(dir, &DB); - if (exit != SQLITE_OK) { - std::cerr << "Cannot open database: " << sqlite3_errmsg(DB) << std::endl; - return -1; - } + checkOpenDatabase(exit); exit = sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, 0); @@ -97,6 +93,7 @@ int Leaderboards::getHiscore(const std::string& player, int level) { return hiscore; } +//generate a leaderboard based on the time, d = within last day, w week, m month. a is set by default void Leaderboards::genLB(int level, char timeRange) { // Build the SQL query based on the time range std::string sql = "SELECT PLAYER, MAX(SCORE), MIN(TIME) FROM LEADERBOARD WHERE LEVEL = ? "; diff --git a/databaseHandler/Leaderboards.h b/databaseHandler/Leaderboards.h index 57e543e..e84cafc 100644 --- a/databaseHandler/Leaderboards.h +++ b/databaseHandler/Leaderboards.h @@ -10,7 +10,6 @@ class Leaderboards : public DatabaseManager { public: - ~Leaderboards(); Leaderboards(); int createTable() override; std::string getCreateTableSQL() override; diff --git a/databaseHandler/MapData.cpp b/databaseHandler/MapData.cpp index fd55b5c..376e9a0 100644 --- a/databaseHandler/MapData.cpp +++ b/databaseHandler/MapData.cpp @@ -4,10 +4,12 @@ #include "MapData.h" +//encapsulation void MapData::setMapDataDir() { DatabaseManager::setDir("../map-data.db"); } +//this ensures the database is always created MapData::MapData() { setMapDataDir(); sqlite3_open(dir, &DB); @@ -15,10 +17,8 @@ MapData::MapData() { loadCurrentValue(); } -MapData::~MapData() { - // Destructor -} - +//sql query that creates 2 tables. the first one is through createTable as there are reusable portions of the leaderboard table +//the other table is to store the currentvalue int MapData::createTable() { int exit = DatabaseManager::createTable(); @@ -41,12 +41,9 @@ int MapData::createTable() { return exit; } -/* -int MapData::createTable() { - return DatabaseManager::createTable(); -} - */ + +//sql query is used in databasemanager std::string MapData::getCreateTableSQL() { return "CREATE TABLE IF NOT EXISTS MapData(" "ID INTEGER PRIMARY KEY AUTOINCREMENT, " @@ -59,6 +56,7 @@ std::string MapData::getCreateTableSQL() { "Source TEXT NOT NULL);"; } +//insert a song entry void MapData::insertData(const std::string& songTitle, const std::string& songArtist, int length, int bpm, int difficulty, int level, const std::string& source) { // Check for duplicates before inserting a new song @@ -78,6 +76,7 @@ void MapData::insertData(const std::string& songTitle, const std::string& songAr } } +//output all data, helpful for debugging void MapData::outputData() { sqlite3* DB; char* messageError; @@ -97,6 +96,7 @@ void MapData::outputData() { sqlite3_close(DB); } +//used to print the data, outputData selects the data int MapData::callback(void* NotUsed, int argc, char** argv, char** azColName) { for(int i = 0; i < argc; i++) { std::cout << azColName[i] << ": " << (argv[i] ? argv[i] : "NULL") << "\n"; @@ -110,6 +110,7 @@ void MapData::deleteData(int id) { DatabaseManager::deleteData(id, deleteSql); } +//go through next and previous levels in the table void MapData::nextLv() { currentLevelId++; @@ -139,6 +140,8 @@ void MapData::prev10Lv() { prevLv(); } +//display info on the current level, make sure it exists though +//if it doesnt exist display the first level void MapData::displayLevel() { std::string sql = "SELECT * FROM MapData WHERE ID = ?;"; sqlite3_stmt* stmt; @@ -162,6 +165,7 @@ void MapData::displayLevel() { } } +//to make sure an error doesnt occur when sqlite tries to display a level that doesnt exist bool MapData::tryDisplayLevel(int id, const std::string& sql, sqlite3_stmt*& stmt) { if (sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, 0) != SQLITE_OK) { std::cout << "Failed to prepare statement\n"; @@ -194,6 +198,7 @@ bool MapData::tryDisplayLevel(int id, const std::string& sql, sqlite3_stmt*& stm return false; } +//used in next and previous functions int MapData::getMaxId() { DatabaseManager::checkOpenDatabase(sqlite3_open(this->dir, &DB)); std::string sql = "SELECT MAX(ID) FROM MapData;"; @@ -220,6 +225,7 @@ int MapData::getMinId() { return minId; } +//save it to the tables void MapData::saveCurrentValue() { std::string sql = "UPDATE CurrentValue SET Value = ? WHERE ID = 1;"; sqlite3_stmt* stmt; @@ -234,6 +240,7 @@ void MapData::saveCurrentValue() { } } +//has to be gotten before its used void MapData::loadCurrentValue() { std::string sql = "SELECT Value FROM CurrentValue WHERE ID = 1;"; sqlite3_stmt* stmt; @@ -250,6 +257,7 @@ void MapData::loadCurrentValue() { sqlite3_close(DB); } +//a different callback for displayLevel, we dont want it to display certain columns in game int MapData::displayLevelCallback(void* NotUsed, int argc, char** argv, char** azColName) { for (int i = 0; i < argc; i++) { std::string columnName = azColName[i]; @@ -265,16 +273,14 @@ int MapData::getCurrentId() const { return currentLevelId; } +//gets the currentLevel from the table 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; - } + checkOpenDatabase(exit); if (sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, 0) == SQLITE_OK) { sqlite3_bind_int(stmt, 1, currentLevelId); @@ -290,6 +296,7 @@ int MapData::getCurrentLevel() { return level; } +//to make sure that multiple songs for the same level cant be created, as leaderboards are per level bool MapData::isDuplicate(int level) { sqlite3* DB; sqlite3_stmt* stmt; diff --git a/databaseHandler/MapData.h b/databaseHandler/MapData.h index 07027d7..c355dc1 100644 --- a/databaseHandler/MapData.h +++ b/databaseHandler/MapData.h @@ -10,7 +10,6 @@ class MapData : public DatabaseManager { public: MapData(); - ~MapData(); int createTable() override; std::string getCreateTableSQL() override; void insertData(const std::string& songTitle, const std::string& songArtist, diff --git a/databaseHandler/main.cpp b/databaseHandler/main.cpp index 259eba0..f88ced3 100644 --- a/databaseHandler/main.cpp +++ b/databaseHandler/main.cpp @@ -12,14 +12,19 @@ DONE -mapData get song/next/previous DONE -mapData get next x 10 or something DONE -Leaderboards, sort with respect to time, day, week, month DONE -mapData get id, so id parameter can be fed into generateLB -lmao no thanks -mapData changesort **** bonus +unable to -mapData changesort **** bonus done??? -command line support - -search and sort instead of if statements +done?? -search and sort instead of if statements -review and comment code + main: ~200 lines, mapData.cpp: ~310, Leaderboards.cpp: ~145, DatabaseManager: ~165, + mapData.h: ~40, Leaderboards.h: ~25, DatabaseManager.h: ~40 + 200+310+145+165+40+25+40=925 lines im sobbing */ + +//also uses recursion int binarySearch(std::pair> arr[], int l, int r, std::string x) { if (r >= l) { int mid = l + (r - l) / 2; @@ -51,6 +56,7 @@ void handleCommandLineArguments(int argc, char* argv[]) { // Define the command array std::pair> commandArray[] = { + //this uses binary search to search for the commands instead of many if statements {"deleteData", [&lb, &md](int argc, char* argv[]) { if (argc == 4) { std::string className = argv[2];