juk3n/fluffypublic Fork
⑂ 643c6dd
Commits
⬇ Clone ▾
git clone https://git.rickub.com/juk3n/fluffy.git
git clone ssh://git@rickub.com/juk3n/fluffy.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

added using game class instead of strings

Adrian Duraj committed 2024-06-20T17:10:26+02:00 Browse files
643c6dd parent: 3e8aa86
modified src/main.cpp +25 -21
@@ -11,12 +11,13 @@
1111 #include <filesystem>
1212 #include <sqlite3.h>
1313 #include <map>
14+#include <vector>
1415
1516 #include <game.hpp>
1617
1718 using namespace ftxui;
1819
19-std::map<std::string, std::string> games;
20+std::vector<Game> games;
2021
2122 std::string database_initialization_command = "CREATE TABLE GAME("
2223 "GAME_ID INT PRIMARY KEY NOT NULL, "
@@ -25,7 +26,7 @@ std::string database_initialization_command = "CREATE TABLE GAME("
2526
2627 const char* select_all_games_command = "SELECT * FROM GAME;";
2728
28-bool debugRun = true;
29+bool debugRun = false;
2930
3031 void printMessage(std::string message) {
3132 if (debugRun) {
@@ -52,17 +53,17 @@ int calculateFirstGameIdAvailable() {
5253 return availableId;
5354 }
5455
55-void addGame(sqlite3* database, std::string name, std::string path) {
56- int id = calculateFirstGameIdAvailable();
57- addGameToDatabase(database, id, name, path);
58-}
59-
6056 void addGameToDatabase(sqlite3* database, int id, std::string name, std::string path) {
6157 std::string command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (";
6258 command += std::to_string(id) + ", '" + name + "', \"" + path + "\");";
6359 execute_sql_command(database, command);
6460 }
6561
62+void addGame(sqlite3* database, std::string name, std::string path) {
63+ int id = calculateFirstGameIdAvailable();
64+ addGameToDatabase(database, id, name, path);
65+}
66+
6667 void removeGame(sqlite3* database, std::string name) {
6768 std::string command = "DELETE FROM GAME WHERE GAME_NAME=";
6869 command += "'" + name + "';";
@@ -76,9 +77,9 @@ void initialize_database(sqlite3* database) {
7677
7778 void runGame(sqlite3* database, std::string gameName) {
7879 std::string pathToRun = "";
79- for (const auto& [name, path] : games) {
80- if (name == gameName) {
81- pathToRun = path;
80+ for (auto& game : games) {
81+ if (gameName == game.getName()) {
82+ pathToRun = game.getPath();
8283 }
8384 }
8485 system(pathToRun.c_str());
@@ -90,8 +91,8 @@ void handleCommand(int argc, char const *argv[], sqlite3* database) {
9091 case 2:
9192 command = argv[1];
9293 if (command == "show") {
93- for (const auto& [game, path] : games) {
94- std::cout << game << ": " << path << std::endl;
94+ for (auto& game : games) {
95+ std::cout << game.getName() << ": " << game.getPath() << std::endl;
9596 }
9697 }
9798 break;
@@ -142,7 +143,7 @@ int main(int argc, char const *argv[]) {
142143 return -1;
143144 }
144145
145- std::cout << "Opened database successfully!" << std::endl;
146+ printMessage("Opened database successfully!");
146147
147148 if (newCreation) {
148149 initialize_database(database);
@@ -161,9 +162,13 @@ int main(int argc, char const *argv[]) {
161162 const unsigned char* gameName = sqlite3_column_text(stmt, 1);
162163 const unsigned char* gamePath = sqlite3_column_text(stmt, 2);
163164
164- std::string name{reinterpret_cast<const char*>(gameName)};
165- std::string path{reinterpret_cast<const char*>(gamePath)};
166- games[name] = path;
165+ games.push_back(
166+ Game {
167+ id,
168+ reinterpret_cast<const char*>(gameName),
169+ reinterpret_cast<const char*>(gamePath)
170+ }
171+ );
167172 }
168173 sqlite3_finalize(stmt);
169174
@@ -191,8 +196,8 @@ int main(int argc, char const *argv[]) {
191196 auto menu_screen = ScreenInteractive::TerminalOutput();
192197
193198 std::vector<std::string> localGames = {};
194- for (const auto& [name, path] : games) {
195- localGames.push_back(name);
199+ for (auto& game : games) {
200+ localGames.push_back(game.getName());
196201 }
197202 localGames.push_back("Exit");
198203
@@ -204,11 +209,10 @@ int main(int argc, char const *argv[]) {
204209 if (choice != exit_id) {
205210 int i{ 0 };
206211 std::string gameToRun = "";
207- for (const auto& [name, path] : games) {
212+ for (auto& game : games) {
208213
209214 if (i == choice) {
210- gameToRun = name;
211-
215+ gameToRun = game.getName();
212216 }
213217 i++;
214218 }
@@ -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 }