summaryrefslogtreecommitdiff
path: root/src/storage.cpp
diff options
context:
space:
mode:
authorAaron Marks <nymacro@gmail.com>2005-05-29 12:06:58 +0000
committerAaron Marks <nymacro@gmail.com>2005-05-29 12:06:58 +0000
commit7a8b2b11e989e474dceb30330852174d877b1601 (patch)
treedc0e63111c2633bcd9a787f359eb4222fc806225 /src/storage.cpp
parent6d34ea19c35627c1705b81f4c89d8557e13bf4da (diff)
downloadmanaserv-7a8b2b11e989e474dceb30330852174d877b1601.tar.gz
manaserv-7a8b2b11e989e474dceb30330852174d877b1601.tar.bz2
manaserv-7a8b2b11e989e474dceb30330852174d877b1601.tar.xz
manaserv-7a8b2b11e989e474dceb30330852174d877b1601.zip
Added getAccount & getCharacter to storage class. Cleaned up storage code.
Added test in main.cpp.
Diffstat (limited to 'src/storage.cpp')
-rw-r--r--src/storage.cpp153
1 files changed, 109 insertions, 44 deletions
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
}
-*/