Skip to content

Commit

Permalink
code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brandiob committed Jun 7, 2024
1 parent de13d5c commit e9014d8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 59 deletions.
36 changes: 6 additions & 30 deletions databaseHandler/DatabaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ DatabaseManager::DatabaseManager() {
sqlite3_open(dir, &DB);
}

DatabaseManager::~DatabaseManager() {
sqlite3_close(DB);
}

int DatabaseManager::createDB() {
sqlite3* DB;
int exit = 0;
Expand All @@ -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;
Expand Down Expand Up @@ -58,31 +56,7 @@ int DatabaseManager::createTable() {
return 0;
}

int DatabaseManager::executeSQL(const std::string& sql, std::function<void(sqlite3_stmt*)> 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<void(sqlite3_stmt*)> bindFunc, std::function<void(sqlite3_stmt*)> callback) {
sqlite3* DB;
sqlite3_stmt* stmt;
Expand Down Expand Up @@ -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<void(sqlite3_stmt*)> bindFunc) {
sqlite3* DB;
sqlite3_stmt* stmt;
Expand All @@ -135,6 +109,7 @@ void DatabaseManager::insertDataHelper(const std::string& sql, std::function<voi
sqlite3_close(DB);
}

//we delete rows based on a unique id
void DatabaseManager::deleteData(int id, const std::string& sql) {
sqlite3* DB;
sqlite3_stmt* stmt;
Expand All @@ -155,6 +130,7 @@ void DatabaseManager::deleteData(int id, const std::string& sql) {
sqlite3_close(DB);
}

//the generic checks
void DatabaseManager::checkOpenDatabase(int exit) {
if (exit != SQLITE_OK) {
std::cerr << "Cannot open database: " << sqlite3_errmsg(DB) << std::endl;
Expand Down
4 changes: 1 addition & 3 deletions databaseHandler/DatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
class DatabaseManager {
public:
DatabaseManager();
~DatabaseManager();
static int createDB(); //can be statis since it doesnt need to do polymorphism
static int createDB(); //can be static since it doesnt need to do polymorphism
//all the virtual functions have a generic part and then are overridden with a specific part
virtual int createTable();
//callback had to be static so cant be here
virtual void deleteData(int id, const std::string& sql);
//outputdata does not have enough generic parts for this
void insertDataHelper(const std::string &sql, std::function<void(sqlite3_stmt *)> bindFunc);
int executeSQL(const std::string& sql, std::function<void(sqlite3_stmt*)> 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
Expand Down
15 changes: 6 additions & 9 deletions databaseHandler/Leaderboards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "Leaderboards.h"

//encapsulation
void Leaderboards::setLeaderboardsDir() {
DatabaseManager::setDir("../leaderboards.db");
}
Expand All @@ -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, "
Expand All @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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 = ? ";
Expand Down
1 change: 0 additions & 1 deletion databaseHandler/Leaderboards.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class Leaderboards : public DatabaseManager {
public:

~Leaderboards();
Leaderboards();
int createTable() override;
std::string getCreateTableSQL() override;
Expand Down
33 changes: 20 additions & 13 deletions databaseHandler/MapData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

#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);
DatabaseManager::createDB();
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();

Expand All @@ -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, "
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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++;
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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;";
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion databaseHandler/MapData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions databaseHandler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::function<void(int, char**)>> arr[], int l, int r, std::string x) {
if (r >= l) {
int mid = l + (r - l) / 2;
Expand Down Expand Up @@ -51,6 +56,7 @@ void handleCommandLineArguments(int argc, char* argv[]) {

// Define the command array
std::pair<std::string, std::function<void(int, char**)>> 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];
Expand Down

0 comments on commit e9014d8

Please sign in to comment.