summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/accounthandler.cpp78
-rw-r--r--src/client.cpp18
-rw-r--r--src/dalstorage.cpp43
-rw-r--r--src/dalstorage.h7
-rw-r--r--src/defines.h9
-rw-r--r--src/main.cpp1
-rw-r--r--src/netcomputer.cpp23
-rw-r--r--src/netcomputer.h11
-rw-r--r--src/storage.h7
9 files changed, 172 insertions, 25 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp
index 0ad9c1ba..4fe91e38 100644
--- a/src/accounthandler.cpp
+++ b/src/accounthandler.cpp
@@ -26,7 +26,6 @@
#include "storage.h"
#include "account.h"
#include "messageout.h"
-#include "state.h"
#include "configuration.h"
#include <iostream>
#include <cctype>
@@ -102,15 +101,13 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
std::cout << username << "'s account has " << chars.size() << " character(s)." << std::endl;
- for (unsigned int i = 0; i < chars.size(); i++) {
+ for (unsigned int i = 0; i < chars.size(); i++)
+ {
result.writeString(chars[i]->getName());
- std::cout << chars[i]->getName() << ", ";
- result.writeByte(chars[i]->getLevel());
- result.writeByte(chars[i]->getMoney());
- //result.writeString(chars[i]->getRawStatistics(),
- // sizeof(tmwserv::RawStatistics));
+ if (i >0) std::cout << ", ";
+ std::cout << chars[i]->getName();
}
- std::cout << std::endl;
+ std::cout << "." << std::endl;
}
}
break;
@@ -129,16 +126,16 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
if ( username == "" )
{
std::cout << "Account without name ? Logged out anyway..." << std::endl;
- computer.setCharacter(NULL);
- computer.setAccount(NULL);
+ // computer.unsetCharacter(); Done by unsetAccount();
+ computer.unsetAccount();
result.writeShort(SMSG_LOGOUT_ERROR);
result.writeByte(LOGOUT_UNKNOWN);
}
else
{
std::cout << computer.getAccount()->getName() << " logs out." << std::endl;
- computer.setCharacter(NULL);
- computer.setAccount(NULL);
+ // computer.unsetCharacter(); Done by unsetAccount();
+ computer.unsetAccount();
result.writeShort(SMSG_LOGOUT_CONFIRM);
result.writeByte(LOGOUT_OK);
}
@@ -252,8 +249,8 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
if (computer.getAccount()->getName() == username )
{
- computer.setCharacter(NULL);
- computer.setAccount(NULL);
+ // computer.unsetCharacter(); Done by unsetAccount();
+ computer.unsetAccount();
}
}
// delete account and associated characters
@@ -286,6 +283,15 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
}
std::string name = message.readString();
+ // Check if the character's name already exists
+ if (store.doesCharacterNameExists(name))
+ {
+ result.writeShort(SMSG_CHAR_CREATE_RESPONSE);
+ result.writeByte(CREATE_EXISTS_NAME);
+ std::cout << name << ": Character's name already exists." << std::endl;
+ break;
+ }
+ // Check for character's name length
if ((name.length() < MIN_CHARACTER_LENGTH) || (name.length() > MAX_CHARACTER_LENGTH))
{
result.writeShort(SMSG_CHAR_CREATE_RESPONSE);
@@ -342,10 +348,6 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
// set character
computer.setCharacter(chars[charNum].get());
- // place in world
- tmwserv::State &state = tmwserv::State::instance();
- state.addBeing(computer.getCharacter(), computer.getCharacter()->getMap());
-
result.writeByte(SELECT_OK);
std::cout << "Selected Character " << int(charNum)
<< " : " <<
@@ -388,7 +390,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
{
if ( computer.getCharacter()->getName() == chars[charNum].get()->getName() )
{
- computer.setCharacter(NULL);
+ computer.unsetCharacter();
}
}
@@ -401,6 +403,44 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message)
}
break;
+ case CMSG_CHAR_LIST:
+ {
+ if (computer.getAccount() == NULL)
+ {
+ result.writeShort(SMSG_CHAR_LIST_RESPONSE);
+ result.writeByte(CHAR_LIST_NOLOGIN);
+ std::cout << "Not logged in. Can't list characters." << std::endl;
+ break; // not logged in
+ }
+
+ result.writeShort(SMSG_CHAR_LIST_RESPONSE);
+ result.writeByte(CHAR_LIST_OK);
+ // Return information about available characters
+ tmwserv::Beings &chars = computer.getAccount()->getCharacters();
+ result.writeByte(chars.size());
+
+ std::cout << computer.getAccount()->getName() << "'s account has "
+ << chars.size() << " character(s)." << std::endl;
+
+ for (unsigned int i = 0; i < chars.size(); i++)
+ {
+ result.writeString(chars[i]->getName());
+ if (i >0) std::cout << ", ";
+ std::cout << chars[i]->getName();
+ result.writeByte(unsigned(short(chars[i]->getGender())));
+ result.writeByte(chars[i]->getLevel());
+ result.writeByte(chars[i]->getMoney());
+ result.writeByte(chars[i]->getStrength());
+ result.writeByte(chars[i]->getAgility());
+ result.writeByte(chars[i]->getVitality());
+ result.writeByte(chars[i]->getIntelligence());
+ result.writeByte(chars[i]->getDexterity());
+ result.writeByte(chars[i]->getLuck());
+ }
+ std::cout << "." << std::endl;
+ }
+ break;
+
default:
std::cout << "Invalid message type" << std::endl;
result.writeShort(SMSG_LOGIN_ERROR);
diff --git a/src/client.cpp b/src/client.cpp
index 8bbcce4e..b7f59658 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -56,9 +56,10 @@ int main(int argc, char *argv[])
printf ("6) Create character\n");
printf ("7) Character selection\n");
printf ("8) Delete Character\n");
- printf ("9) Move character\n");
- printf ("10) Equip item\n");
- printf ("11) Ruby expression\n");
+ printf ("9) List Characters\n");
+ printf ("10) Move Character\n");
+ printf ("11) Equip Item\n");
+ printf ("12) Ruby Expression\n");
printf ("Choose your option: ");
std::cin >> answer;
@@ -145,6 +146,13 @@ int main(int argc, char *argv[])
case 9:
{
+ // List characters
+ msg.writeShort(CMSG_CHAR_LIST);
+ std::cout <<"Character List:" << std::endl;
+ } break;
+
+ case 10:
+ {
// Move character
long x, y;
std::cout << "X: ";
@@ -159,7 +167,7 @@ int main(int argc, char *argv[])
responseRequired = false;
} break;
- case 10:
+ case 11:
{
// Equip
unsigned int itemId;
@@ -173,7 +181,7 @@ int main(int argc, char *argv[])
msg.writeByte(slot);
} break;
- case 11:
+ case 12:
{
std::cout << "Expr: ";
std::cin >> line;
diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp
index 5d45ba56..1a9a5141 100644
--- a/src/dalstorage.cpp
+++ b/src/dalstorage.cpp
@@ -447,6 +447,49 @@ DALStorage::doesEmailAlreadyExists(std::string email)
return false;
}
+/**
+ * Tells if the character's name already exists
+ * @return true if character's name exists.
+ */
+bool
+DALStorage::doesCharacterNameExists(std::string name)
+{
+ // If not opened already
+ open();
+
+ try {
+ std::string sql("select count(name) from ");
+ sql += CHARACTERS_TBL_NAME;
+ sql += " where name = '";
+ sql += name;
+ sql += "';";
+ const dal::RecordSet& accountInfo = mDb->execSql(sql);
+
+ // if the account is empty then
+ // we have no choice but to return false.
+ if (accountInfo.isEmpty()) {
+ return false;
+ }
+
+ std::stringstream ssStream(accountInfo(0,0));
+ int iReturn = -1;
+ ssStream >> iReturn;
+ if ( iReturn > 0 )
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ catch (const dal::DbSqlQueryExecFailure& e) {
+ // TODO: throw an exception.
+ LOG_ERROR("SQL query failure: " << e.what())
+ }
+
+ return false;
+}
/**
* Save changes to the database permanently.
diff --git a/src/dalstorage.h b/src/dalstorage.h
index 88495307..6e3eb7fb 100644
--- a/src/dalstorage.h
+++ b/src/dalstorage.h
@@ -107,6 +107,13 @@ class DALStorage: public Storage
doesEmailAlreadyExists(std::string email);
/**
+ * Tells if the character's name already exists
+ * @return true if character's name exists.
+ */
+ bool
+ doesCharacterNameExists(std::string name);
+
+ /**
* Save changes to the database permanently.
*
* @exception tmwserv::dal::DbSqlQueryExecFailure.
diff --git a/src/defines.h b/src/defines.h
index a302796d..954380ba 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -101,6 +101,7 @@ enum {
CMSG_CHAR_DELETE = 0x0022,
SMSG_CHAR_DELETE_RESPONSE = 0x0023,
CMSG_CHAR_LIST = 0x0024, // this is required after char creation
+ SMSG_CHAR_LIST_RESPONSE = 0x0025,
CMSG_CHAR_SELECT = 0x0030,
SMSG_CHAR_SELECT_RESPONSE = 0x0031,
@@ -190,7 +191,7 @@ enum {
CREATE_INVALID_NAME,
CREATE_INVALID_HAIR,
CREATE_INVALID_SEX,
- CREATE_EXISTS_USERNAME,
+ CREATE_EXISTS_NAME,
CREATE_TOO_MUCH_CHARACTERS,
CREATE_NOLOGIN
};
@@ -212,6 +213,12 @@ enum {
SELECT_NOLOGIN
};
+// Character's list return values
+enum {
+ CHAR_LIST_OK = 0,
+ CHAR_LIST_NOLOGIN
+};
+
// Object type enumeration
enum {
OBJECT_ITEM = 0,
diff --git a/src/main.cpp b/src/main.cpp
index 3c2406f6..419c00e2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -233,6 +233,7 @@ int main(int argc, char *argv[])
connectionHandler.registerHandler(CMSG_CHAR_CREATE, accountHandler);
connectionHandler.registerHandler(CMSG_CHAR_SELECT, accountHandler);
connectionHandler.registerHandler(CMSG_CHAR_DELETE, accountHandler);
+ connectionHandler.registerHandler(CMSG_CHAR_LIST, accountHandler);
connectionHandler.registerHandler(CMSG_SAY, chatHandler);
connectionHandler.registerHandler(CMSG_ANNOUNCE, chatHandler);
diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp
index 663d74a8..ff1c93a1 100644
--- a/src/netcomputer.cpp
+++ b/src/netcomputer.cpp
@@ -22,6 +22,7 @@
*/
#include "netcomputer.h"
+#include "state.h"
#include <cstdlib>
#include <iostream>
@@ -55,5 +56,27 @@ void NetComputer::setAccount(tmwserv::Account *acc)
void NetComputer::setCharacter(tmwserv::Being *ch)
{
+ tmwserv::State &state = tmwserv::State::instance();
+ if (character != NULL)
+ {
+ // Remove being from the world : This is buggy for now.
+ //state.removeBeing(character);
+ }
character = ch;
+ state.addBeing(character, character->getMap());
}
+
+void NetComputer::unsetAccount()
+{
+ unsetCharacter();
+ account = NULL;
+}
+
+void NetComputer::unsetCharacter()
+{
+ // remove being from world
+ //tmwserv::State &state = tmwserv::State::instance();
+ //state.removeBeing(character);
+ character = NULL;
+}
+
diff --git a/src/netcomputer.h b/src/netcomputer.h
index 766cf94a..8dace0d8 100644
--- a/src/netcomputer.h
+++ b/src/netcomputer.h
@@ -84,6 +84,11 @@ class NetComputer
void setAccount(tmwserv::Account *acc);
/**
+ * Unset the account associated with the connection
+ */
+ void unsetAccount();
+
+ /**
* Get account associated with the connection
*/
tmwserv::Account *getAccount() { return account; }
@@ -94,6 +99,12 @@ class NetComputer
void setCharacter(tmwserv::Being *ch);
/**
+ * Deselect the character associated with connection
+ * and remove it from the world
+ */
+ void unsetCharacter();
+
+ /**
* Get character associated with the connection
*/
tmwserv::Being *getCharacter() { return character; }
diff --git a/src/storage.h b/src/storage.h
index ac3ad88b..3ee59e70 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -295,6 +295,13 @@ class Storage
bool doesEmailAlreadyExists(std::string email) = 0;
/**
+ * Tells if the character's name already exists
+ * @return true if character's name exists.
+ */
+ virtual
+ bool doesCharacterNameExists(std::string name) = 0;
+
+ /**
* Saves the changes permanently.
*/
virtual void