juk3n/fluffypublic Fork
⑂ 52b2642
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 choosing game to run

juk3n committed 2024-06-06T18:36:22+02:00 Browse files
52b2642 parent: c10208d
modified src/main.cpp +57 -17
@@ -3,16 +3,15 @@
33 #include <cstdlib>
44 #include <filesystem>
55 #include <../include/sqlite3.h>
6+#include <map>
67
7-std::string gamePath;
8+std::map<std::string, std::string> games;
89
910 std::string database_initialization_command = "CREATE TABLE GAME("
1011 "GAME_ID INT PRIMARY KEY NOT NULL, "
1112 "GAME_NAME TEXT NOT NULL, "
1213 "GAME_PATH TEXT NOT NULL);";
1314
14-std::string insert_example_game_command = "INSERT INTO GAME (GAME_ID, GAME_NAME, GAME_PATH) VALUES (1, 'Devastating Fog', \"\");";
15-
1615 const char* select_all_games_command = "SELECT * FROM GAME;";
1716
1817 bool debugRun = true;
@@ -26,28 +25,37 @@ void printMessage(std::string message) {
2625 void execute_sql_command(sqlite3* database, std::string command) {
2726 int exit = 0;
2827 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);
3029
3130 if (exit != SQLITE_OK) {
32- printMessage("Error with command: " + command);
31+ printMessage("Error with command: " + command + " " + messageError);
3332 sqlite3_free(messageError);
3433 }
3534 }
3635
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+
3744 void initialize_database(sqlite3* database) {
38- execute_sql_command(database, database_initialization_command.c_str());
39- execute_sql_command(database, insert_example_game_command.c_str());
45+ execute_sql_command(database, database_initialization_command);
46+ addGame(database, "Devastating Fog", "");
47+ addGame(database, "No Pirates Here!", "");
4048 printMessage("Database Initialized");
4149 }
4250
4351 int main() {
44- bool newCreation = not std::filesystem::exists("example_database");
52+ bool newCreation = not std::filesystem::exists("data.db");
4553
4654 char* messageError;
4755 sqlite3* database;
4856 sqlite3_stmt* stmt;
4957 int exit = 0;
50- exit = sqlite3_open("example_database", &database);
58+ exit = sqlite3_open("data.db", &database);
5159
5260 if (exit) {
5361 std::cerr << "Error opening database" << sqlite3_errmsg(database) << std::endl;
@@ -55,8 +63,6 @@ int main() {
5563 }
5664
5765 std::cout << "Opened database successfully!" << std::endl;
58-
59-
6066
6167 if (newCreation) {
6268 initialize_database(database);
@@ -72,15 +78,49 @@ int main() {
7278
7379 while (sqlite3_step(stmt) == SQLITE_ROW) {
7480 int id = sqlite3_column_int(stmt, 0);
75- const unsigned char* game_name = sqlite3_column_text(stmt, 1);
76- const unsigned char* path = sqlite3_column_text(stmt, 2);
77-
78- std::cout << game_name << ": " << path << std::endl;
79- gamePath.append(reinterpret_cast<const char*>(path));
81+ const unsigned char* gameName = sqlite3_column_text(stmt, 1);
82+ const unsigned char* gamePath = sqlite3_column_text(stmt, 2);
83+
84+ std::cout << gameName << ": " << gamePath << std::endl;
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;
8090 }
8191 sqlite3_finalize(stmt);
8292
8393 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+
85125 return 0;
86126 }
\ No newline at end of file
@@ -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