| @@ -3,16 +3,15 @@ |
| 3 | #include <cstdlib> | 3 | #include <cstdlib> |
| 4 | #include <filesystem> | 4 | #include <filesystem> |
| 5 | #include <../include/sqlite3.h> | 5 | #include <../include/sqlite3.h> |
| | 6 | +#include <map> |
| 6 | | 7 | |
| 7 | -std::string gamePath; | 8 | +std::map<std::string, std::string> games; |
| 8 | | 9 | |
| 9 | std::string database_initialization_command = "CREATE TABLE GAME(" | 10 | std::string database_initialization_command = "CREATE TABLE GAME(" |
| 10 | "GAME_ID INT PRIMARY KEY NOT NULL, " | 11 | "GAME_ID INT PRIMARY KEY NOT NULL, " |
| 11 | "GAME_NAME TEXT NOT NULL, " | 12 | "GAME_NAME TEXT NOT NULL, " |
| 12 | "GAME_PATH TEXT NOT NULL);"; | 13 | "GAME_PATH TEXT NOT NULL);"; |
| 13 | | 14 | |
| 14 | -std::string insert_example_game_command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (1, 'Devastating Fog', \"\");"; | | |
| 15 | - | | |
| 16 | const char* select_all_games_command = "SELECT * FROM GAME;"; | 15 | const char* select_all_games_command = "SELECT * FROM GAME;"; |
| 17 | | 16 | |
| 18 | bool debugRun = true; | 17 | bool debugRun = true; |
| @@ -26,28 +25,37 @@ void printMessage(std::string message) { |
| 26 | void execute_sql_command(sqlite3* database, std::string command) { | 25 | void execute_sql_command(sqlite3* database, std::string command) { |
| 27 | int exit = 0; | 26 | int exit = 0; |
| 28 | char* messageError; | 27 | char* messageError; |
| 29 | - exit = sqlite3_exec(database, database_initialization_command.c_str(), NULL, 0, &messageError); | 28 | + exit = sqlite3_exec(database, command.c_str(), NULL, 0, &messageError); |
| 30 | | 29 | |
| 31 | if (exit != SQLITE_OK) { | 30 | if (exit != SQLITE_OK) { |
| 32 | - printMessage("Error with command: " + command); | 31 | + printMessage("Error with command: " + command + " " + messageError); |
| 33 | sqlite3_free(messageError); | 32 | sqlite3_free(messageError); |
| 34 | } | 33 | } |
| 35 | } | 34 | } |
| 36 | | 35 | |
| | 36 | +void addGame(sqlite3* database, std::string name, std::string path) { |
| | 37 | + static int val = 1; |
| | 38 | + std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES ("; |
| | 39 | + command += std::to_string(val) + ", '" + name + "', \"" + path + "\");"; |
| | 40 | + val++; |
| | 41 | + execute_sql_command(database, command); |
| | 42 | +} |
| | 43 | + |
| 37 | void initialize_database(sqlite3* database) { | 44 | void initialize_database(sqlite3* database) { |
| 38 | - execute_sql_command(database, database_initialization_command.c_str()); | 45 | + execute_sql_command(database, database_initialization_command); |
| 39 | - execute_sql_command(database, insert_example_game_command.c_str()); | 46 | + addGame(database, "Devastating Fog", ""); |
| | 47 | + addGame(database, "No Pirates Here!", ""); |
| 40 | printMessage("Database Initialized"); | 48 | printMessage("Database Initialized"); |
| 41 | } | 49 | } |
| 42 | | 50 | |
| 43 | int main() { | 51 | int main() { |
| 44 | - bool newCreation = not std::filesystem::exists("example_database"); | 52 | + bool newCreation = not std::filesystem::exists("data.db"); |
| 45 | | 53 | |
| 46 | char* messageError; | 54 | char* messageError; |
| 47 | sqlite3* database; | 55 | sqlite3* database; |
| 48 | sqlite3_stmt* stmt; | 56 | sqlite3_stmt* stmt; |
| 49 | int exit = 0; | 57 | int exit = 0; |
| 50 | - exit = sqlite3_open("example_database", &database); | 58 | + exit = sqlite3_open("data.db", &database); |
| 51 | | 59 | |
| 52 | if (exit) { | 60 | if (exit) { |
| 53 | std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; | 61 | std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl; |
| @@ -55,8 +63,6 @@ int main() { |
| 55 | } | 63 | } |
| 56 | | 64 | |
| 57 | std::cout << "Opened database successfully!" << std::endl; | 65 | std::cout << "Opened database successfully!" << std::endl; |
| 58 | - | | |
| 59 | - | | |
| 60 | | 66 | |
| 61 | if (newCreation) { | 67 | if (newCreation) { |
| 62 | initialize_database(database); | 68 | initialize_database(database); |
| @@ -72,15 +78,49 @@ int main() { |
| 72 | | 78 | |
| 73 | while (sqlite3_step(stmt) == SQLITE_ROW) { | 79 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 74 | int id = sqlite3_column_int(stmt, 0); | 80 | int id = sqlite3_column_int(stmt, 0); |
| 75 | - const unsigned char* game_name = sqlite3_column_text(stmt, 1); | 81 | + const unsigned char* gameName = sqlite3_column_text(stmt, 1); |
| 76 | - const unsigned char* path = sqlite3_column_text(stmt, 2); | 82 | + const unsigned char* gamePath = sqlite3_column_text(stmt, 2); |
| 77 | - | 83 | + |
| 78 | - std::cout << game_name << ": " << path << std::endl; | 84 | + std::cout << gameName << ": " << gamePath << std::endl; |
| 79 | - gamePath.append(reinterpret_cast<const char*>(path)); | 85 | + std::string name{}; |
| | 86 | + std::string path{}; |
| | 87 | + name.append(reinterpret_cast<const char*>(gameName)); |
| | 88 | + path.append(reinterpret_cast<const char*>(gamePath)); |
| | 89 | + games[name] = path; |
| 80 | } | 90 | } |
| 81 | sqlite3_finalize(stmt); | 91 | sqlite3_finalize(stmt); |
| 82 | | 92 | |
| 83 | sqlite3_close(database); | 93 | sqlite3_close(database); |
| 84 | - system(gamePath.c_str()); | 94 | + int choice; |
| | 95 | + do { |
| | 96 | + system("clear"); |
| | 97 | + std::cout << "Welcome to fluffy (0 - exit, 1-x - run a game)" << std::endl; |
| | 98 | + int i{ 1 }; |
| | 99 | + for (const auto& [name, path] : games) { |
| | 100 | + std::cout << i ; |
| | 101 | + std::cout << ": " + name << std::endl; |
| | 102 | + i++; |
| | 103 | + } |
| | 104 | + |
| | 105 | + std::cin >> choice; |
| | 106 | + switch (choice) |
| | 107 | + { |
| | 108 | + case 0: |
| | 109 | + break; |
| | 110 | + default: |
| | 111 | + int i{ 1 }; |
| | 112 | + std::string pathToRun = ""; |
| | 113 | + for (const auto& [name, path] : games) { |
| | 114 | + if (i == choice) { |
| | 115 | + pathToRun = path; |
| | 116 | + |
| | 117 | + } |
| | 118 | + i++; |
| | 119 | + } |
| | 120 | + system(pathToRun.c_str()); |
| | 121 | + break; |
| | 122 | + } |
| | 123 | + } while (choice != 0); |
| | 124 | + |
| 85 | return 0; | 125 | return 0; |
| 86 | } | 126 | } |
| \ No newline at end of file | | \ No newline at end of file |