| @@ -11,12 +11,13 @@ |
| 11 | #include <filesystem> | 11 | #include <filesystem> |
| 12 | #include <sqlite3.h> | 12 | #include <sqlite3.h> |
| 13 | #include <map> | 13 | #include <map> |
| | 14 | +#include <vector> |
| 14 | | 15 | |
| 15 | #include <game.hpp> | 16 | #include <game.hpp> |
| 16 | | 17 | |
| 17 | using namespace ftxui; | 18 | using namespace ftxui; |
| 18 | | 19 | |
| 19 | -std::map<std::string, std::string> games; | 20 | +std::vector<Game> games; |
| 20 | | 21 | |
| 21 | std::string database_initialization_command = "CREATE TABLE GAME(" | 22 | std::string database_initialization_command = "CREATE TABLE GAME(" |
| 22 | "GAME_ID INT PRIMARY KEY NOT NULL, " | 23 | "GAME_ID INT PRIMARY KEY NOT NULL, " |
| @@ -25,7 +26,7 @@ std::string database_initialization_command = "CREATE TABLE GAME(" |
| 25 | | 26 | |
| 26 | const char* select_all_games_command = "SELECT * FROM GAME;"; | 27 | const char* select_all_games_command = "SELECT * FROM GAME;"; |
| 27 | | 28 | |
| 28 | -bool debugRun = true; | 29 | +bool debugRun = false; |
| 29 | | 30 | |
| 30 | void printMessage(std::string message) { | 31 | void printMessage(std::string message) { |
| 31 | if (debugRun) { | 32 | if (debugRun) { |
| @@ -52,17 +53,17 @@ int calculateFirstGameIdAvailable() { |
| 52 | return availableId; | 53 | return availableId; |
| 53 | } | 54 | } |
| 54 | | 55 | |
| 55 | -void addGame(sqlite3* database, std::string name, std::string path) { | | |
| 56 | - int id = calculateFirstGameIdAvailable(); | | |
| 57 | - addGameToDatabase(database, id, name, path); | | |
| 58 | -} | | |
| 59 | - | | |
| 60 | void addGameToDatabase(sqlite3* database, int id, std::string name, std::string path) { | 56 | void addGameToDatabase(sqlite3* database, int id, std::string name, std::string path) { |
| 61 | std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; | 57 | std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; |
| 62 | command += std::to_string(id) + ", '" + name + "', \"" + path + "\");"; | 58 | command += std::to_string(id) + ", '" + name + "', \"" + path + "\");"; |
| 63 | execute_sql_command(database, command); | 59 | execute_sql_command(database, command); |
| 64 | } | 60 | } |
| 65 | | 61 | |
| | 62 | +void addGame(sqlite3* database, std::string name, std::string path) { |
| | 63 | + int id = calculateFirstGameIdAvailable(); |
| | 64 | + addGameToDatabase(database, id, name, path); |
| | 65 | +} |
| | 66 | + |
| 66 | void removeGame(sqlite3* database, std::string name) { | 67 | void removeGame(sqlite3* database, std::string name) { |
| 67 | std::string command = "DELETE FROM GAME WHERE GAME_NAME="; | 68 | std::string command = "DELETE FROM GAME WHERE GAME_NAME="; |
| 68 | command += "'" + name + "';"; | 69 | command += "'" + name + "';"; |
| @@ -76,9 +77,9 @@ void initialize_database(sqlite3* database) { |
| 76 | | 77 | |
| 77 | void runGame(sqlite3* database, std::string gameName) { | 78 | void runGame(sqlite3* database, std::string gameName) { |
| 78 | std::string pathToRun = ""; | 79 | std::string pathToRun = ""; |
| 79 | - for (const auto& [name, path] : games) { | 80 | + for (auto& game : games) { |
| 80 | - if (name == gameName) { | 81 | + if (gameName == game.getName()) { |
| 81 | - pathToRun = path; | 82 | + pathToRun = game.getPath(); |
| 82 | } | 83 | } |
| 83 | } | 84 | } |
| 84 | system(pathToRun.c_str()); | 85 | system(pathToRun.c_str()); |
| @@ -90,8 +91,8 @@ void handleCommand(int argc, char const *argv[], sqlite3* database) { |
| 90 | case 2: | 91 | case 2: |
| 91 | command = argv[1]; | 92 | command = argv[1]; |
| 92 | if (command == "show") { | 93 | if (command == "show") { |
| 93 | - for (const auto& [game, path] : games) { | 94 | + for (auto& game : games) { |
| 94 | - std::cout << game << ": " << path << std::endl; | 95 | + std::cout << game.getName() << ": " << game.getPath() << std::endl; |
| 95 | } | 96 | } |
| 96 | } | 97 | } |
| 97 | break; | 98 | break; |
| @@ -142,7 +143,7 @@ int main(int argc, char const *argv[]) { |
| 142 | return -1; | 143 | return -1; |
| 143 | } | 144 | } |
| 144 | | 145 | |
| 145 | - std::cout << "Opened database successfully!" << std::endl; | 146 | + printMessage("Opened database successfully!"); |
| 146 | | 147 | |
| 147 | if (newCreation) { | 148 | if (newCreation) { |
| 148 | initialize_database(database); | 149 | initialize_database(database); |
| @@ -161,9 +162,13 @@ int main(int argc, char const *argv[]) { |
| 161 | const unsigned char* gameName = sqlite3_column_text(stmt, 1); | 162 | const unsigned char* gameName = sqlite3_column_text(stmt, 1); |
| 162 | const unsigned char* gamePath = sqlite3_column_text(stmt, 2); | 163 | const unsigned char* gamePath = sqlite3_column_text(stmt, 2); |
| 163 | | 164 | |
| 164 | - std::string name{reinterpret_cast<const char*>(gameName)}; | 165 | + games.push_back( |
| 165 | - std::string path{reinterpret_cast<const char*>(gamePath)}; | 166 | + Game { |
| 166 | - games[name] = path; | 167 | + id, |
| | 168 | + reinterpret_cast<const char*>(gameName), |
| | 169 | + reinterpret_cast<const char*>(gamePath) |
| | 170 | + } |
| | 171 | + ); |
| 167 | } | 172 | } |
| 168 | sqlite3_finalize(stmt); | 173 | sqlite3_finalize(stmt); |
| 169 | | 174 | |
| @@ -191,8 +196,8 @@ int main(int argc, char const *argv[]) { |
| 191 | auto menu_screen = ScreenInteractive::TerminalOutput(); | 196 | auto menu_screen = ScreenInteractive::TerminalOutput(); |
| 192 | | 197 | |
| 193 | std::vector<std::string> localGames = {}; | 198 | std::vector<std::string> localGames = {}; |
| 194 | - for (const auto& [name, path] : games) { | 199 | + for (auto& game : games) { |
| 195 | - localGames.push_back(name); | 200 | + localGames.push_back(game.getName()); |
| 196 | } | 201 | } |
| 197 | localGames.push_back("Exit"); | 202 | localGames.push_back("Exit"); |
| 198 | | 203 | |
| @@ -204,11 +209,10 @@ int main(int argc, char const *argv[]) { |
| 204 | if (choice != exit_id) { | 209 | if (choice != exit_id) { |
| 205 | int i{ 0 }; | 210 | int i{ 0 }; |
| 206 | std::string gameToRun = ""; | 211 | std::string gameToRun = ""; |
| 207 | - for (const auto& [name, path] : games) { | 212 | + for (auto& game : games) { |
| 208 | | 213 | |
| 209 | if (i == choice) { | 214 | if (i == choice) { |
| 210 | - gameToRun = name; | 215 | + gameToRun = game.getName(); |
| 211 | - | | |
| 212 | } | 216 | } |
| 213 | i++; | 217 | i++; |
| 214 | } | 218 | } |