summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/account.h2
-rw-r--r--src/main.cpp24
-rw-r--r--src/storage.cpp153
-rw-r--r--src/storage.h18
4 files changed, 145 insertions, 52 deletions
diff --git a/src/account.h b/src/account.h
index 87f781b4..564da11a 100644
--- a/src/account.h
+++ b/src/account.h
@@ -42,9 +42,9 @@ class Account
//Player data
Being player[ACC_MAX_CHARS];
- Account() { };
public:
+ Account() { };
Account(const std::string &aName, const std::string aPassword,
const std::string &email, Being aPlayer[ACC_MAX_CHARS]);
~Account();
diff --git a/src/main.cpp b/src/main.cpp
index 13d310bf..b67fa4d5 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -198,6 +198,28 @@ int main(int argc, char *argv[])
std::cout << "Number of accounts on server: " << store.accountCount() << std::endl;
+ // Test the database retrieval code
+ std::cout << "Attempting to retrieve account with username 'nym'" << std::endl;
+ Account* acc = store.getAccount("nym");
+ if (acc)
+ {
+ std::cout << "Account name: " << acc->getName() << std::endl
+ << "Account email: " << acc->getEmail() << std::endl;
+
+ std::cout << "Attempting to get character of 'nym'" << std::endl;
+ Being* character = store.getCharacter("nym");
+ if (character)
+ {
+ std::cout << "Character name: " << character->getName() << std::endl;
+ } else
+ {
+ std::cout << "No characters found" << std::endl;
+ }
+ } else
+ {
+ std::cout << "Account 'nym' not found" << std::endl;
+ }
+
SDL_Event event;
while (running)
@@ -211,7 +233,7 @@ int main(int argc, char *argv[])
// Print world time at 10 second intervals to show we're alive
if (worldTime % 100 == 0) {
- printf("World time: %d\n", worldTime);
+ //printf("World time: %d\n", worldTime);
logger->log("World time: %d", worldTime);
}
diff --git a/src/storage.cpp b/src/storage.cpp
index 9d071671..0467cb84 100644
--- a/src/storage.cpp
+++ b/src/storage.cpp
@@ -24,6 +24,48 @@
#include <iostream>
#include <sstream>
+const char sqlAccountTable[] =
+"create table tmw_accounts ("
+ //username
+ "user varchar(32) unique primary key not null,"
+ //password hash
+ "password varchar(32) not null,"
+ //email address
+ "email varchar(128) not null"
+ //account type (should we have "admin" table? or should we have account
+ //type here?)
+// "type int not null"
+")";
+
+const char sqlCharacterTable[] =
+"create table tmw_characters ("
+ //character name
+ "name varchar(32) unique primary key not null,"
+ //user name
+ "user varchar(32) not null,"
+ //player information
+ "gender int not null,"
+ "level int not null,"
+ "money int not null,"
+ //coordinates
+ "x int not null,"
+ "y int not null,"
+ //map name
+ "map text not null,"
+ //statistics
+ "strength int not null,"
+ "agility int not null,"
+ "vitality int not null,"
+ "intelligence int not null,"
+ "dexterity int not null,"
+ "luck int not null,"
+ //player equipment
+// "inventory blob not null," // TODO: blob bad
+// "equipment blob not null," // TODO: blob bad
+ //table relationship
+ "foreign key(user) references tmw_accounts(user)"
+")";
+
Storage::Storage()
{
#ifdef SQLITE_SUPPORT
@@ -55,6 +97,13 @@ Storage::~Storage()
std::cout << "Database: couldn't close tmw.db" << std::endl;
}
#endif
+
+ // Clean up managed accounts & characters
+ for (unsigned int i = 0; i < accounts.size(); i++)
+ delete accounts[i];
+ for (unsigned int i = 0; i < characters.size(); i++)
+ delete characters[i];
+
}
/**
@@ -74,16 +123,7 @@ void Storage::create_tables_if_necessary()
sqlite.Begin();
// Accounts table
- if (sqlite.DirectStatement("create table tmw_accounts ("
- //username
- " user varchar(32) unique primary key not null,"
- //password hash
- " password varchar(32) not null,"
- //email address
- " email varchar(128) not null,"
- //account type
- " type int not null"
- ")")) {
+ if (sqlite.DirectStatement(sqlAccountTable)) {
std::cout << "Database: table tmw_accounts created" << std::endl;
}
else {
@@ -91,33 +131,7 @@ void Storage::create_tables_if_necessary()
}
// Characters table
- if (sqlite.DirectStatement("create table tmw_characters ("
- //character name
- " name varchar(32) unique primary key not null,"
- //user name
- " user varchar(32) not null,"
- //player information
- " gender int not null,"
- " level int not null,"
- " money int not null,"
- //coordinates
- " x int not null,"
- " y int not null,"
- //map name
- " map text not null,"
- //statistics
- " strength int not null,"
- " agility int not null,"
- " vitality int not null,"
- " intelligence int not null,"
- " dexterity int not null,"
- " luck int not null,"
- //player equipment
- " inventory blob not null," // TODO: blob bad
- " equipment blob not null," // TODO: blob bad
- //table relationship
- " foreign key(user) references tmw_accounts(user)"
- ")")) {
+ if (sqlite.DirectStatement(sqlCharacterTable)) {
std::cout << "Database: table tmw_characters created" << std::endl;
}
else {
@@ -125,10 +139,11 @@ void Storage::create_tables_if_necessary()
}
//populate table for the hell of it ;)
- sqlite.DirectStatement("insert into tmw_accounts values ('nym', 'tHiSiSHaShEd', 'nym@test', 0)");
- sqlite.DirectStatement("insert into tmw_accounts values ('Bjorn', 'tHiSiSHaShEd', 'bjorn@test', 0)");
- sqlite.DirectStatement("insert into tmw_accounts values ('Usiu', 'tHiSiSHaShEd', 'usiu@test', 0)");
- sqlite.DirectStatement("insert into tmw_accounts values ('ElvenProgrammer', 'tHiSiSHaShEd', 'elven@test', 0)");
+ sqlite.DirectStatement("insert into tmw_accounts values ('nym', 'tHiSiSHaShEd', 'nym@test')");
+ sqlite.DirectStatement("insert into tmw_accounts values ('Bjorn', 'tHiSiSHaShEd', 'bjorn@test')");
+ sqlite.DirectStatement("insert into tmw_accounts values ('Usiu', 'tHiSiSHaShEd', 'usiu@test')");
+ sqlite.DirectStatement("insert into tmw_accounts values ('ElvenProgrammer', 'tHiSiSHaShEd', 'elven@test')");
+ sqlite.DirectStatement("insert into tmw_characters values ('Nym the Great', 'nym', 0, 99, 1000000, 0, 0, 'main.map', 1, 2, 3, 4, 5, 6)");
sqlite.Commit();
#endif
@@ -156,10 +171,60 @@ unsigned int Storage::accountCount()
#endif
}
-/*
-Account& getAccount(const std::string &username)
+Account* Storage::getAccount(const std::string &username)
+{
+#ifdef SQLITE_SUPPORT
+ //give caller a initialized AccountData structure
+ SQLiteWrapper::ResultTable r;
+
+ //make sure account isn't loaded already
+ for (unsigned int i = 0; i < accounts.size(); i++)
+ if (accounts[i]->getName() == username)
+ return accounts[i];
+
+ std::string selectStatement = "select * from tmw_accounts where user = \""
+ + username + "\"";
+ if (!sqlite.SelectStmt(selectStatement, r))
+ {
+ return NULL;
+ }
+
+ Account *acc = new Account;
+ acc->setName(r.records_[0].fields_[0]);
+ acc->setPassword(r.records_[0].fields_[1]);
+ acc->setEmail(r.records_[0].fields_[2]);
+ accounts.push_back(acc);
+
+ return acc;
+#else
+ return NULL;
+#endif
+}
+
+Being* Storage::getCharacter(const std::string &username)
{
+#ifdef SQLITE_SUPPORT
//give caller a initialized AccountData structure
+ SQLiteWrapper::ResultTable r;
+
+ std::string selectStatement = "select * from tmw_characters where user = \""
+ + username + "\"";
+ if (!sqlite.SelectStmt(selectStatement, r))
+ return NULL;
+ if (r.records_.size() == 0)
+ return NULL;
+
+ //make sure account isn't loaded already
+ for (unsigned int i = 0; i < characters.size(); i++)
+ if (characters[i]->getName() == r.records_[0].fields_[0])
+ return characters[i];
+
+ Being *acc = new Being(r.records_[0].fields_[0], 1, 1, 1, 1, 1, 1, 1, 1);
+ characters.push_back(acc);
+
+ return acc;
+#else
+ return NULL;
+#endif
}
-*/
diff --git a/src/storage.h b/src/storage.h
index e6fec7b4..5cf96eb4 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -35,9 +35,15 @@
* Storage is the resource manager
*/
class Storage {
- private:
//make storage singleton
Storage(const Storage &n) { }
+
+#ifdef SQLITE_SUPPORT
+ SQLiteWrapper sqlite; /**< Database */
+#endif
+ std::vector<Account*> accounts; /**< Loaded account data */
+ std::vector<Being*> characters; /**< Loaded account data */
+
public:
/**
* Constructor.
@@ -67,12 +73,12 @@ class Storage {
/**
* Get account & associated data
*/
- //Account& getAccount(const std::string &username);
+ Account* getAccount(const std::string &username);
- private:
-#ifdef SQLITE_SUPPORT
- SQLiteWrapper sqlite; /**< Database */
-#endif
+ /**
+ * Get character of username
+ */
+ Being* getCharacter(const std::string &username);
};
#endif /* STORAGE_H */