diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 61 | ||||
-rw-r--r-- | src/dalstorage.cpp | 20 |
2 files changed, 66 insertions, 15 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index df4c1f9f..993d72ac 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -61,6 +61,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { std::string username = message.readString(); std::string password = message.readString(); + std::cout << username << " is trying to login." << std::endl; if (computer.getAccount() != NULL) { result.writeShort(SMSG_LOGIN_ERROR); @@ -73,7 +74,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (!acc) { // account doesn't exist -- send error to client - std::cout << "Account does not exist " << username << std::endl; + std::cout << username << ": Account does not exist." << std::endl; result.writeShort(SMSG_LOGIN_ERROR); result.writeByte(LOGIN_INVALID_USERNAME); @@ -112,14 +113,49 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::string password = message.readString(); std::string email = message.readString(); - AccountPtr acc(new Account(username, password, email)); - store.addAccount(acc); + // checking conditions for having a good account. + // TODO: Test if the email already exists using: + // REGISTER_EXISTS_EMAIL + // The DALstorage will first need to have some getListOf Email function. + std::cout << username << " is trying to register." << std::endl; - result.writeShort(SMSG_REGISTER_RESPONSE); - result.writeByte(REGISTER_OK); + // see if the account exists + Account *accPtr = store.getAccount(username); + if ( accPtr ) // Account already exists. + { + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_EXISTS_USERNAME); + std::cout << username << ": Username already exists." << std::endl; + } + else if ((username.length() < 4) || (username.length() > 16)) // Username length + { + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_INVALID_USERNAME); + std::cout << username << ": Username too short or too long." << std::endl; + } + else if (password.length() < 4) + { + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_INVALID_PASSWORD); + std::cout << email << ": Password too short." << std::endl; + } + else if ((email.find_first_of('@') == std::string::npos) || (email.find_first_of('.') == std::string::npos)) + { + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_INVALID_EMAIL); + std::cout << email << ": Email Invalid (misses @ or .)." << std::endl; + } + else + { + AccountPtr acc(new Account(username, password, email)); + store.addAccount(acc); - std::cout << "Account registered" << std::endl; - store.flush(); // flush changes + result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeByte(REGISTER_OK); + + std::cout << username << ": Account registered." << std::endl; + store.flush(); // flush changes + } } break; @@ -153,12 +189,15 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) case CMSG_CHAR_SELECT: { if (computer.getAccount() == NULL) + { + std::cout << "Not Logged in. Can't select Character." << std::endl; break; // not logged in - + } + unsigned char charNum = message.readByte(); - + tmwserv::Beings &chars = computer.getAccount()->getCharacters(); - + result.writeShort(SMSG_CHAR_SELECT_RESPONSE); if (charNum >= chars.size()) { // invalid char selection @@ -172,7 +211,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) // place in world tmwserv::State &state = tmwserv::State::instance(); state.addBeing(computer.getCharacter(), computer.getCharacter()->getMap()); - + result.writeByte(SELECT_OK); } break; diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index 45468f86..02f69239 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -31,7 +31,6 @@ #include "dalstorage.h" #include "dalstoragesql.h" - namespace tmwserv { @@ -79,7 +78,7 @@ DALStorage::open(void) // open a connection to the database. #if defined (MYSQL_SUPPORT) || defined (POSTGRESQL_SUPPORT) mDb->connect(getName(), getUser(), getPassword()); -#else // SQLITE_SUPPORT +#elif defined (SQLITE_SUPPORT) // create the database file name. std::string dbFile(getName()); dbFile += ".db"; @@ -114,6 +113,10 @@ DALStorage::open(void) // we will stick with strategy2 for the moment as we are focusing // on SQLite. + // FIXME: The tables should be checked/created at startup in order to avoid + // a DbSqlQueryExecFailure assert on sqlite while registering. + // Also, this would initialize connection to the database earlier in memory. + createTable(MAPS_TBL_NAME, SQL_MAPS_TABLE); createTable(ACCOUNTS_TBL_NAME, SQL_ACCOUNTS_TABLE); createTable(CHARACTERS_TBL_NAME, SQL_CHARACTERS_TABLE); @@ -151,7 +154,7 @@ DALStorage::getAccount(const std::string& userName) { // connect to the database (if not connected yet). open(); - +std::cout << "trying " << std::endl; // look for the account in the list first. Accounts::iterator it = std::find_if( @@ -238,8 +241,17 @@ DALStorage::getAccount(const std::string& userName) << toUint(charInfo(i, 8)) << ";"; sql = ss.str(); // should be impossible for this to fail due to db referential integrity + const RecordSet& mapInfo = mDb->execSql(sql); - being.get()->setMap(mapInfo(0, 0)); + + if (!mapInfo.isEmpty()) + { + being.get()->setMap(mapInfo(0, 0)); + } + else + { + // TODO: Set player to default map and one of the default location + } mCharacters.push_back(being); beings.push_back(being); |