Skip to content

Commit

Permalink
Check user has inventory item
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsv committed Mar 13, 2017
1 parent 50687c9 commit 5877b33
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
87 changes: 67 additions & 20 deletions src/dbstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ using namespace std;
static int callback(void* NotUsed, int argc, char** argv, char** azColName)
{
int i;
string output = "";
for (i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
//printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
output += (string)azColName[i];
output += " = ";
output += (string)(argv[i] ? argv[i] : "NULL");
output += "\n";
}
printf("\n");
print(output);
return 0;
}

Expand All @@ -28,7 +33,7 @@ int write_recent_user(string name)
return 0;
}

string get_recent_user_db()
string get_recent_user_name()
{
ifstream infile("most-recent-user.txt");

Expand Down Expand Up @@ -86,19 +91,23 @@ int create_user_database(string player_name)
return 0;
}

int add_to_inventory(string item_name)
string get_recent_user_db_filepath()
{
// Add item to database
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
string path = string(result, (count > 0) ? count : 0);
string toRemove = "bin/cosmic-horizon.o";
path.erase(path.find(toRemove), toRemove.length());
string databaseName = path;
databaseName += "save-";
databaseName += get_recent_user_db();
databaseName += ".db";
print(databaseName);
path += "save-";
path += get_recent_user_name();
path += ".db";
return path;
}

int add_to_inventory(string itemName, string got)
{
// Add item to database
string databaseName = get_recent_user_db_filepath();

sqlite3* db;
char* zErrMsg = 0;
Expand All @@ -117,36 +126,74 @@ int add_to_inventory(string item_name)
sqlString = "INSERT INTO INVENTORY ("
"NAME, GOT"
") VALUES ("
"\""
+ item_name + "\", \"true\""
");";
print(sqlString);
"\"" + itemName + "\", \"" + got + "\""
");";
sql = (char*)sqlString.c_str();

// Execute SQL statement
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

if (rc != SQLITE_OK) {
string zErrMsg_str = zErrMsg;
print(zErrMsg_str);
if(zErrMsg_str == "UNIQUE constraint failed: INVENTORY.NAME"){
print(itemName + " already in database");
} else {
print(zErrMsg_str);
}
sqlite3_free(zErrMsg);
} else {
print("Added " + item_name + " to database");
print("Added " + itemName + " to database");
}

sqlite3_close(db);
return 0;
}

int remove_from_inventory(string item_name)
int remove_from_inventory(string itemName)
{
// Remove item from database
return 0;
}

bool do_i_have(string item_name)
bool do_i_have(string itemName)
{
// Check database to see if user has item
// Return true if user has item
sqlite3* db;
char* zErrMsg = 0;
int rc;
//char* sql;

string databaseName = get_recent_user_db_filepath();

// Open database
rc = sqlite3_open(databaseName.c_str(), &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return (0);
}

sqlite3_stmt *stmt;
const char *sql = "SELECT * FROM INVENTORY";
/*int */rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
print("error: ", sqlite3_errmsg(db));
return false;
}
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
string name = reinterpret_cast<const char*>(sqlite3_column_text (stmt, 0));
string got = reinterpret_cast<const char*>(sqlite3_column_text (stmt, 1));
string callbackString = name + (string)"=" + got;
string existsString = itemName + (string)"=" + got;
if (callbackString.find(existsString) != string::npos) {
return true;
} else {
return false;
}
}
if (rc != SQLITE_DONE) {
print("error: ", sqlite3_errmsg(db));
}
sqlite3_finalize(stmt);

sqlite3_close(db);
return false;
}
6 changes: 3 additions & 3 deletions src/dbstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ using namespace std;

int database_test();
int create_user_database(string player_name);
int add_to_inventory(string item_name);
int remove_from_inventory(string item_name);
bool do_i_have(string item_name);
int add_to_inventory(string itemName, string got = "true");
//int remove_from_inventory(string itemName);
bool do_i_have(string itemName);
16 changes: 12 additions & 4 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,26 @@ using namespace std;

int main()
{
print("\n" + get_ascii_title(), "cyan");

print("Name: ", "", false);
string name = "";
name = get_text(true);
print("Hello, " + name + "!\n");
print("Hello, " + name + "!");

create_user_database(name);

add_to_inventory("potato");
add_to_inventory("lmao", "false");
add_to_inventory("meh");

if(do_i_have("magic")){
print("you have a magic");
} else {
print("you do not have a magic");
}

print("\n" + get_ascii_title(), "cyan");

print("Do you want to play Cosmic Horizon? ");
print("\nDo you want to play Cosmic Horizon? ");
bool choice;
choice = yes_or_no();
if (choice) {
Expand Down
6 changes: 3 additions & 3 deletions src/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ string get_ascii_title()
" ██║ ██║ ██║╚════██║██║╚██╔╝██║██║██║ \n"
" ╚██████╗╚██████╔╝███████║██║ ╚═╝ ██║██║╚██████╗\n"
" ╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═════╝\n"
" ██░ ██ ░█████ ██▀███ ██░░███████░ ░█████ ███▄ \n"
" ░██░ ██░░██░ ██░░██ ░ ██░░██░░ ░ ░ ▄▀░░██░ ██░ ██ ▀█ \n"
" ██░ ██ ░█████ ██▀███ ██░░███████░ ░█████ ███▄ █ \n"
" ░██░ ██░░██░ ██░░██ ░ ██░░██░░ ░ ░ ▄▀░░██░ ██░ ██ ▀█ █ \n"
" ░██▀▀██░░██░ ██░░██ ░▄█ ░░██░░ ░ ▄▀░░ ░██░ ██░░██ ▀█ █░\n"
" ░██ ░██ ░██ ██░░██▀▀█▄ ░██░ ▄▀░ ░░██ ██░░██░ ▐██\n"
" ░██ ░██ ░██ ██░░██▀▀█▄ ░██░ ▄▀░ ░░██ ██░░██░ ▐██░\n"
" ░██░░██░░ ████░░░░██░ ░██░░██░░███████░░ ████░░░██░ ░██░\n"
" ░ ░ ░░░░░░ ░░░░░░ ░ ░░ ░░░░░░ ░░░ ░░░░░░ ░░░░░░ ░ ░░ ░ ░\n"
" ░ ░░░ ░ ░ ░ ░░ ░░ ░ ░░ ░ ░░░░ ░ ░ ░ ░ ░ ░░ ░ ░░ ░ ░░\n"
Expand Down

1 comment on commit 5877b33

@jmsv
Copy link
Owner Author

@jmsv jmsv commented on 5877b33 Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2

Please sign in to comment.