Skip to content

Commit

Permalink
Add to database
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsv committed Mar 13, 2017
1 parent ec88792 commit 50687c9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Cosmic Horizon stuff
most-recent-user.txt
compile.sh

# Compiled Object files
*.slo
*.lo
Expand Down
Binary file removed save-elliot.db
Binary file not shown.
Binary file removed save-ellioy.db
Binary file not shown.
80 changes: 78 additions & 2 deletions src/dbstore.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "misc.h"
#include "print.h"
#include <fstream>
#include <iostream>
#include <limits.h>
#include <sqlite3.h>
#include <sstream>
#include <string>
#include <unistd.h>
using namespace std;

static int callback(void* NotUsed, int argc, char** argv, char** azColName)
Expand All @@ -15,6 +19,30 @@ static int callback(void* NotUsed, int argc, char** argv, char** azColName)
return 0;
}

int write_recent_user(string name)
{
ofstream file;
file.open("most-recent-user.txt");
file << name;
file.close();
return 0;
}

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

string text = "";
if (infile.good()) {
string sLine;
getline(infile, sLine);
text = sLine;
}
infile.close();

return text;
}

int create_user_database(string player_name)
{
sqlite3* db;
Expand All @@ -33,8 +61,7 @@ int create_user_database(string player_name)
}
// Create SQL statement
sql = (char*)"CREATE TABLE INVENTORY("
"ID INT NOT NULL PRIMARY KEY,"
"NAME TEXT NOT NULL,"
"NAME TEXT NOT NULL PRIMARY KEY,"
"GOT BOOLEAN NOT NULL"
");";

Expand All @@ -50,6 +77,9 @@ int create_user_database(string player_name)
print(zErrMsg_str);
sqlite3_free(zErrMsg);
}
} else {
print("Created database " + databaseName);
write_recent_user(player_name);
}

sqlite3_close(db);
Expand All @@ -59,6 +89,52 @@ int create_user_database(string player_name)
int add_to_inventory(string item_name)
{
// 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);

sqlite3* db;
char* zErrMsg = 0;
int rc;
char* sql;

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

// Create SQL statement
string sqlString = "";
sqlString = "INSERT INTO INVENTORY ("
"NAME, GOT"
") VALUES ("
"\""
+ item_name + "\", \"true\""
");";
print(sqlString);
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);
sqlite3_free(zErrMsg);
} else {
print("Added " + item_name + " to database");
}

sqlite3_close(db);
return 0;
}

Expand Down
3 changes: 3 additions & 0 deletions src/dbstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +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);
6 changes: 4 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ int main()
{
print("Name: ", "", false);
string name = "";
name = get_text();
std::cout << "Hello, " << name << "!\n";
name = get_text(true);
print("Hello, " + name + "!\n");

create_user_database(name);

add_to_inventory("potato");

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

print("Do you want to play Cosmic Horizon? ");
Expand Down
9 changes: 7 additions & 2 deletions src/userin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ bool valid_name(string test_name)
return true;
}

bool valid_text(string text)
bool valid_text(string text, bool name)
{
if (text.length() < 1) {
return false;
}
if (name) {
if (!valid_name(text)) {
return false;
}
}
return true;
}

Expand All @@ -37,7 +42,7 @@ string get_text(bool name)
bool validIn = false;
while (!validIn) {
getline(cin, textIn);
if (!valid_text(textIn)) {
if (!valid_text(textIn, name)) {
print("Invalid input - please try again", "red");
} else {
validIn = true;
Expand Down
2 changes: 1 addition & 1 deletion src/userin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
using namespace std;

bool valid_name(string test_name);
string get_text(bool name = true);
string get_text(bool name = false);
bool yes_or_no();
int int_option(int max_digit);

1 comment on commit 50687c9

@jmsv
Copy link
Owner Author

@jmsv jmsv commented on 50687c9 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.