diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accounthandler.cpp | 265 | ||||
-rw-r--r-- | src/chatchannelmanager.cpp | 37 | ||||
-rw-r--r-- | src/chathandler.cpp | 240 | ||||
-rw-r--r-- | src/chathandler.h | 5 | ||||
-rw-r--r-- | src/client.cpp | 209 | ||||
-rw-r--r-- | src/defines.h | 355 | ||||
-rw-r--r-- | src/gamehandler.cpp | 62 |
7 files changed, 413 insertions, 760 deletions
diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index d778c86c..7d49c3df 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -164,14 +164,14 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) switch (message.getId()) { - case CMSG_LOGIN: + case PAMSG_LOGIN: { std::string clientVersion = message.readString(); std::string username = message.readString(); std::string password = message.readString(); LOG_INFO(username << " is trying to login.", 1); - result.writeShort(SMSG_LOGIN_RESPONSE); + result.writeShort(APMSG_LOGIN_RESPONSE); if (clientVersion < config.getValue("clientVersion", "0.0.0")) { @@ -181,7 +181,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } if (stringFilter->findDoubleQuotes(username)) { - result.writeByte(LOGIN_INVALID_USERNAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(username << ": has got double quotes in it.", 1); break; } @@ -189,7 +189,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) LOG_INFO("Already logged in as " << computer.getAccount()->getName() << ".", 1); LOG_INFO("Please logout first.", 1); - result.writeByte(LOGIN_ALREADY_LOGGED); + result.writeByte(ERRMSG_FAILURE); break; } if (getClientNumber() >= MAX_CLIENTS ) @@ -204,79 +204,64 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // see if the account exists tmwserv::AccountPtr acc = store.getAccount(username); - if (!acc.get()) { + if (!acc.get() || acc->getPassword() != password) { // account doesn't exist -- send error to client - LOG_INFO(username << ": Account does not exist.", 1); + LOG_INFO(username << ": Account does not exist or the password is invalid.", 1); - result.writeByte(LOGIN_INVALID_USERNAME); - } else if (acc->getPassword() != password) { - // bad password -- send error to client - LOG_INFO("Bad password for " << username, 1); + result.writeByte(ERRMSG_INVALID_ARGUMENT); + break; + } - result.writeByte(LOGIN_INVALID_PASSWORD); - } else { - LOG_INFO("Login OK by " << username, 1); + LOG_INFO("Login OK by " << username, 1); - // Associate account with connection - computer.setAccount(acc); + // Associate account with connection + computer.setAccount(acc); - result.writeByte(LOGIN_OK); + result.writeByte(ERRMSG_OK); - // Return information about available characters - tmwserv::Beings &chars = computer.getAccount()->getCharacters(); - result.writeByte(chars.size()); + // Return information about available characters + tmwserv::Beings &chars = computer.getAccount()->getCharacters(); + result.writeByte(chars.size()); - LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1); - std::string charNames = ""; - for (unsigned int i = 0; i < chars.size(); i++) - { - result.writeString(chars[i]->getName()); - if (i >0) charNames += ", "; - charNames += chars[i]->getName(); - } - charNames += "."; - LOG_INFO(charNames.c_str(), 1); + LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1); + std::string charNames = ""; + for (unsigned int i = 0; i < chars.size(); i++) + { + result.writeString(chars[i]->getName()); + if (i >0) charNames += ", "; + charNames += chars[i]->getName(); } + charNames += "."; + LOG_INFO(charNames.c_str(), 1); } break; - case CMSG_LOGOUT: + case PAMSG_LOGOUT: { - result.writeShort(SMSG_LOGOUT_RESPONSE); + result.writeShort(APMSG_LOGOUT_RESPONSE); if ( computer.getAccount().get() == NULL ) { LOG_INFO("Can't logout. Not even logged in.", 1); - result.writeByte(LOGOUT_UNSUCCESSFULL); + result.writeByte(ERRMSG_NO_LOGIN); } else { - std::string username = computer.getAccount()->getName(); - if ( username == "" ) - { - LOG_INFO("Account without name ? Logged out anyway...", 1); - // computer.unsetCharacter(); Done by unsetAccount(); - computer.unsetAccount(); - result.writeByte(LOGOUT_UNSUCCESSFULL); - } - else - { - LOG_INFO(computer.getAccount()->getName() << " logs out.", 1); - // computer.unsetCharacter(); Done by unsetAccount(); - computer.unsetAccount(); - result.writeByte(LOGOUT_OK); - } + LOG_INFO(computer.getAccount()->getName() << " logs out.", 1); + // computer.unsetCharacter(); Done by unsetAccount(); + computer.unsetAccount(); + result.writeByte(ERRMSG_OK); } } break; - case CMSG_REGISTER: + case PAMSG_REGISTER: { std::string clientVersion = message.readString(); std::string username = message.readString(); std::string password = message.readString(); std::string email = message.readString(); - result.writeShort(SMSG_REGISTER_RESPONSE); + result.writeShort(APMSG_REGISTER_RESPONSE); if (clientVersion < config.getValue("clientVersion", "0.0.0")) { @@ -285,17 +270,10 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) break; } - // Checking if the Name is slang's free. - if (!stringFilter->filterContent(username)) - { - result.writeByte(REGISTER_INVALID_USERNAME); - LOG_INFO(username << ": has got bad words in it.", 1); - break; - } // Checking if there are double quotes in it. if (stringFilter->findDoubleQuotes(username)) { - result.writeByte(REGISTER_INVALID_USERNAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(username << ": has got double quotes in it.", 1); break; } @@ -312,22 +290,28 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } else if ((username.length() < MIN_LOGIN_LENGTH) || (username.length() > MAX_LOGIN_LENGTH)) // Username length { - result.writeByte(REGISTER_INVALID_USERNAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(username << ": Username too short or too long.", 1); } + else if (!stringFilter->filterContent(username)) // Checking if the Name is slang's free. + { + result.writeByte(ERRMSG_INVALID_ARGUMENT); + LOG_INFO(username << ": has got bad words in it.", 1); + break; + } else if ((password.length() < MIN_PASSWORD_LENGTH) || (password.length() > MAX_PASSWORD_LENGTH)) { - result.writeByte(REGISTER_INVALID_PASSWORD); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": Password too short or too long.", 1); } else if (!stringFilter->isEmailValid(email)) { - result.writeByte(REGISTER_INVALID_EMAIL); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": Email Invalid, only a@b.c format is accepted.", 1); } else if (stringFilter->findDoubleQuotes(email)) { - result.writeByte(REGISTER_INVALID_EMAIL); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": has got double quotes in it.", 1); break; } @@ -341,7 +325,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) AccountPtr acc(new Account(username, password, email)); store.addAccount(acc); - result.writeByte(REGISTER_OK); + result.writeByte(ERRMSG_OK); store.flush(); // flush changes LOG_INFO(username << ": Account registered.", 1); @@ -349,17 +333,17 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_UNREGISTER: + case PAMSG_UNREGISTER: { std::string username = message.readString(); std::string password = message.readString(); LOG_INFO(username << " wants to be deleted from our accounts.", 1); - result.writeShort(SMSG_UNREGISTER_RESPONSE); + result.writeShort(APMSG_UNREGISTER_RESPONSE); if (stringFilter->findDoubleQuotes(username)) { - result.writeByte(UNREGISTER_INVALID_USERNAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(username << ": has got double quotes in it.", 1); break; } @@ -367,16 +351,10 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // see if the account exists tmwserv::AccountPtr accPtr = store.getAccount(username); - if (!accPtr.get()) { - // account doesn't exist -- send error to client - LOG_INFO(username << ": Account doesn't exist anyway.", 1); - - result.writeByte(UNREGISTER_INVALID_USERNAME); - } else if (accPtr->getPassword() != password) { - // bad password -- send error to client - LOG_INFO("Won't delete it : Bad password for " << username << ".", 1); + if (!accPtr.get() || accPtr->getPassword() != password) { + LOG_INFO("Account does not exist of bad password for " << username << ".", 1); - result.writeByte(UNREGISTER_INVALID_PASSWORD); + result.writeByte(ERRMSG_INVALID_ARGUMENT); } else { // If the account to delete is the current account we're logged in. @@ -393,17 +371,17 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) LOG_INFO("Farewell " << username << " ...", 1); store.delAccount(username); store.flush(); - result.writeByte(UNREGISTER_OK); + result.writeByte(ERRMSG_OK); } } break; - case CMSG_EMAIL_CHANGE: + case PAMSG_EMAIL_CHANGE: { - result.writeShort(SMSG_EMAIL_CHANGE_RESPONSE); + result.writeShort(APMSG_EMAIL_CHANGE_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(EMAILCHG_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't change your Account's Email.", 1); break; } @@ -411,13 +389,13 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) std::string email = message.readString(); if (!stringFilter->isEmailValid(email)) { - result.writeByte(EMAILCHG_INVALID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": Invalid format, cannot change Email for " << computer.getAccount()->getName(), 1); } else if (stringFilter->findDoubleQuotes(email)) { - result.writeByte(EMAILCHG_INVALID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(email << ": has got double quotes in it.", 1); } else if (store.getSameEmailNumber(email) > 1) // Search if Email already exists, @@ -428,82 +406,75 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) else { computer.getAccount()->setEmail(email); - result.writeByte(EMAILCHG_OK); + result.writeByte(ERRMSG_OK); LOG_INFO(computer.getAccount()->getName() << ": Email changed to: " << email, 1); } } break; - case CMSG_EMAIL_GET: + case PAMSG_EMAIL_GET: { - result.writeShort(SMSG_EMAIL_GET_RESPONSE); + result.writeShort(APMSG_EMAIL_GET_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(EMAILGET_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't get your Account's current Email.", 1); break; } else { - result.writeByte(EMAILGET_OK); + result.writeByte(ERRMSG_OK); result.writeString(computer.getAccount()->getEmail()); } } break; - case CMSG_PASSWORD_CHANGE: + case PAMSG_PASSWORD_CHANGE: { - result.writeShort(SMSG_PASSWORD_CHANGE_RESPONSE); + result.writeShort(APMSG_PASSWORD_CHANGE_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(PASSCHG_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't change your Account's Password.", 1); break; } std::string oldPassword = message.readString(); - std::string password1 = message.readString(); - std::string password2 = message.readString(); - if ( password1.length() < MIN_PASSWORD_LENGTH || - password1.length() > MAX_PASSWORD_LENGTH ) + std::string newPassword = message.readString(); + if ( newPassword.length() < MIN_PASSWORD_LENGTH || + newPassword.length() > MAX_PASSWORD_LENGTH ) { - result.writeByte(PASSCHG_INVALID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(computer.getAccount()->getName() << ": New password too long or too short.", 1); } - else if (stringFilter->findDoubleQuotes(password1)) + else if (stringFilter->findDoubleQuotes(newPassword)) { - result.writeByte(PASSCHG_INVALID); - LOG_INFO(password1 << ": has got double quotes in it.", 1); - } - else if ( password1 != password2 ) - { - result.writeByte(PASSCHG_MISMATCH); - LOG_INFO(computer.getAccount()->getName() << - ": New password mismatched confirmation password.", 1); + result.writeByte(ERRMSG_INVALID_ARGUMENT); + LOG_INFO(newPassword << ": has got double quotes in it.", 1); } else if ( oldPassword != computer.getAccount()->getPassword() ) { - result.writeByte(PASSCHG_MISMATCH); + result.writeByte(ERRMSG_FAILURE); LOG_INFO(computer.getAccount()->getName() << ": Old password is wrong.", 1); } else { - computer.getAccount()->setPassword(password1); - result.writeByte(PASSCHG_OK); + computer.getAccount()->setPassword(newPassword); + result.writeByte(ERRMSG_OK); LOG_INFO(computer.getAccount()->getName() << ": The password was changed.", 1); } } break; - case CMSG_CHAR_CREATE: + case PAMSG_CHAR_CREATE: { - result.writeShort(SMSG_CHAR_CREATE_RESPONSE); + result.writeShort(APMSG_CHAR_CREATE_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(CREATE_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't create a Character.", 1); break; } @@ -522,14 +493,14 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // Checking if the Name is slang's free. if (!stringFilter->filterContent(name)) { - result.writeByte(CREATE_INVALID_NAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(name << ": Character has got bad words in it.", 1); break; } // Checking if the Name has got double quotes. if (stringFilter->findDoubleQuotes(name)) { - result.writeByte(CREATE_INVALID_NAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(name << ": has got double quotes in it.", 1); break; } @@ -543,7 +514,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // Check for character's name length if ((name.length() < MIN_CHARACTER_LENGTH) || (name.length() > MAX_CHARACTER_LENGTH)) { - result.writeByte(CREATE_INVALID_NAME); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO(name << ": Character's name too short or too long.", 1); break; } @@ -660,17 +631,17 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) << computer.getAccount()->getName() << "'s account.", 1); store.flush(); // flush changes - result.writeByte(CREATE_OK); + result.writeByte(ERRMSG_OK); } break; - case CMSG_CHAR_SELECT: + case PAMSG_CHAR_SELECT: { - result.writeShort(SMSG_CHAR_SELECT_RESPONSE); + result.writeShort(APMSG_CHAR_SELECT_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(SELECT_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't select a Character.", 1); break; // not logged in } @@ -678,16 +649,10 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) unsigned char charNum = message.readByte(); tmwserv::Beings &chars = computer.getAccount()->getCharacters(); - if (chars.size() == 0 ) - { - result.writeByte(SELECT_NO_CHARACTERS); - LOG_INFO("Character Selection : Yet no characters created.", 1); - break; - } // Character ID = 0 to Number of Characters - 1. if (charNum >= chars.size()) { // invalid char selection - result.writeByte(SELECT_INVALID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO("Character Selection : Selection out of ID range.", 1); break; } @@ -698,7 +663,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // be loaded in setCharacter(). Not implemented yet for tests purpose... computer.setCharacter(chars[charNum]); tmwserv::BeingPtr selectedChar = computer.getCharacter(); - result.writeByte(SELECT_OK); + result.writeByte(ERRMSG_OK); std::string mapName = store.getMapNameFromId(selectedChar->getMapId()); result.writeString(mapName); result.writeShort(selectedChar->getX()); @@ -709,13 +674,13 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_CHAR_DELETE: + case PAMSG_CHAR_DELETE: { - result.writeShort(SMSG_CHAR_DELETE_RESPONSE); + result.writeShort(APMSG_CHAR_DELETE_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(DELETE_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't delete a Character.", 1); break; // not logged in } @@ -723,17 +688,10 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) unsigned char charNum = message.readByte(); tmwserv::Beings &chars = computer.getAccount()->getCharacters(); - if ( chars.size() == 0 ) - { - result.writeByte(DELETE_NO_MORE_CHARACTERS); - LOG_INFO("Character Deletion : No characters in " << computer.getAccount()->getName() - << "'s account.", 1); - break; - } // Character ID = 0 to Number of Characters - 1. if (charNum >= chars.size()) { // invalid char selection - result.writeByte(DELETE_INVALID_ID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); LOG_INFO("Character Deletion : Selection out of ID range.", 1); break; } @@ -753,36 +711,36 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) computer.getAccount()->delCharacter(deletedCharacter); store.flush(); LOG_INFO(deletedCharacter << ": Character deleted...", 1); - result.writeByte(DELETE_OK); + result.writeByte(ERRMSG_OK); } break; - case CMSG_CHAR_LIST: + case PAMSG_CHAR_LIST: { - result.writeShort(SMSG_CHAR_LIST_RESPONSE); + result.writeShort(APMSG_CHAR_LIST_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(CHAR_LIST_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't list characters.", 1); break; // not logged in } - result.writeByte(CHAR_LIST_OK); + result.writeByte(ERRMSG_OK); // Return information about available characters tmwserv::Beings &chars = computer.getAccount()->getCharacters(); result.writeByte(chars.size()); LOG_INFO(computer.getAccount()->getName() << "'s account has " << chars.size() << " character(s).", 1); - std::string charStats = ""; - std::string mapName = ""; + std::string charStats; + std::string mapName; for (unsigned int i = 0; i < chars.size(); i++) { result.writeByte(i); result.writeString(chars[i]->getName()); - if (i >0) charStats += ", "; + if (i > 0) charStats += ", "; charStats += chars[i]->getName(); result.writeByte(unsigned(short(chars[i]->getGender()))); result.writeByte(chars[i]->getHairStyle()); @@ -805,25 +763,25 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_ENTER_WORLD: + case PAMSG_ENTER_WORLD: { - result.writeShort(SMSG_ENTER_WORLD_RESPONSE); + result.writeShort(APMSG_ENTER_WORLD_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(ENTER_WORLD_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't enter the world.", 1); break; // not logged in } if (computer.getCharacter().get() == NULL) { - result.writeByte(ENTER_WORLD_NO_CHARACTER_SELECTED); + result.writeByte(ERRMSG_NO_CHARACTER_SELECTED); LOG_INFO("No character selected. Can't enter the world.", 2); break; // no character selected } std::string magic_token(32, ' '); for(int i = 0; i < 32; ++i) magic_token[i] = 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); - result.writeByte(ENTER_WORLD_OK); + result.writeByte(ERRMSG_OK); result.writeString("localhost"); result.writeShort(9603); result.writeString(magic_token, 32); @@ -831,25 +789,25 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_ENTER_CHAT: + case PAMSG_ENTER_CHAT: { - result.writeShort(SMSG_ENTER_CHAT_RESPONSE); + result.writeShort(APMSG_ENTER_CHAT_RESPONSE); if (computer.getAccount().get() == NULL) { - result.writeByte(ENTER_CHAT_NOLOGIN); + result.writeByte(ERRMSG_NO_LOGIN); LOG_INFO("Not logged in. Can't enter the chat.", 1); break; // not logged in } if (computer.getCharacter().get() == NULL) { - result.writeByte(ENTER_CHAT_NO_CHARACTER_SELECTED); + result.writeByte(ERRMSG_NO_CHARACTER_SELECTED); LOG_INFO("No character selected. Can't enter the chat.", 2); break; // no character selected } std::string magic_token(32, ' '); for(int i = 0; i < 32; ++i) magic_token[i] = 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); - result.writeByte(ENTER_CHAT_OK); + result.writeByte(ERRMSG_OK); result.writeString("localhost"); result.writeShort(9603); result.writeString(magic_token, 32); @@ -860,8 +818,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) default: LOG_WARN("Invalid message type", 0); - result.writeShort(SMSG_LOGIN_RESPONSE); - result.writeByte(LOGIN_UNKNOWN); + result.writeShort(XXMSG_INVALID); break; } diff --git a/src/chatchannelmanager.cpp b/src/chatchannelmanager.cpp index 9a2af3b9..0d64c311 100644 --- a/src/chatchannelmanager.cpp +++ b/src/chatchannelmanager.cpp @@ -47,16 +47,16 @@ ChatChannelManager::registerPublicChannel(const std::string& channelName, const std::string& channelAnnouncement, const std::string& channelPassword) { short channelId = 1; - for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(), + end = mChatChannels.end(); i != end; ++i) { if ( i->second.getName() == channelName ) return 0; // We seek the highest channelId in the public range - if ( (channelId <= i->first) && (i->first < (signed)MAX_PRIVATE_CHANNELS_RANGE) ) + if (channelId <= i->first && i->first < (signed)MAX_PUBLIC_CHANNELS_RANGE) channelId = i->first + 1; - ++i; } // Too much channels registered - if ( channelId >= (signed)MAX_PRIVATE_CHANNELS_RANGE ) return 0; + if (channelId >= (signed)MAX_PUBLIC_CHANNELS_RANGE) return 0; // Register Channel mChatChannels.insert(std::make_pair(channelId,ChatChannel(channelName, @@ -69,17 +69,17 @@ short ChatChannelManager::registerPrivateChannel(const std::string& channelName, const std::string& channelAnnouncement, const std::string& channelPassword) { - short channelId = MAX_PRIVATE_CHANNELS_RANGE; - for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + short channelId = MAX_PUBLIC_CHANNELS_RANGE; + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(), + end = mChatChannels.end(); i != end; ++i) { if ( i->second.getName() == channelName ) return 0; // We seek the highest channelId in the private range - if ( (channelId <= i->first) && (i->first >= (signed)MAX_PRIVATE_CHANNELS_RANGE) ) + if (channelId <= i->first) channelId = i->first + 1; - ++i; } // Too much channels registered - if ( channelId >= (signed)MAX_PUBLIC_CHANNELS_RANGE ) return 0; + if (channelId >= (signed)MAX_PRIVATE_CHANNELS_RANGE) return 0; // Register Channel mChatChannels.insert(std::make_pair(channelId,ChatChannel(channelName, @@ -89,20 +89,11 @@ ChatChannelManager::registerPrivateChannel(const std::string& channelName, bool ChatChannelManager::removeChannel(short channelId) { - for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) - { - if ( i->first == channelId ) - { - // Removing every user from the channel - i->second.removeEveryUsersFromChannel(); - // Erasing the channel now it's empty - mChatChannels.erase(i); - i++; - return true; - } - ++i; - } - return false; + std::map<short, ChatChannel>::iterator i = mChatChannels.find(channelId); + if (i == mChatChannels.end()) return false; + i->second.removeEveryUsersFromChannel(); + mChatChannels.erase(i); + return true; } diff --git a/src/chathandler.cpp b/src/chathandler.cpp index b1bf23b2..163d6208 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -28,6 +28,7 @@ #include "messagein.h" #include "messageout.h" #include "netcomputer.h" +#include "packet.h" #include "utils/logger.h" #include "utils/stringfilter.h" @@ -74,8 +75,8 @@ void registerChatClient(std::string const &token, std::string const &name, int l computer->accountLevel = (AccountLevels)level; pendingClients.erase(i); MessageOut result; - result.writeShort(SMSG_CHATSRV_CONNECT_RESPONSE); - result.writeByte(CSRV_CONNECT_OK); + result.writeShort(CPMSG_CONNECT_RESPONSE); + result.writeByte(ERRMSG_OK); computer->send(result.getPacket()); } else @@ -127,9 +128,10 @@ void ChatHandler::process() void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) { ChatClient &computer = *static_cast< ChatClient * >(comp); + MessageOut result; if (computer.characterName.empty()) { - if (message.getId() != CMSG_CHATSRV_CONNECT) return; + if (message.getId() != PCMSG_CONNECT) return; std::string magic_token = message.readString(32); ChatPendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) @@ -144,16 +146,15 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) computer.characterName = i->second.character; computer.accountLevel = i->second.level; pendingLogins.erase(i); - MessageOut result; - result.writeShort(SMSG_CHATSRV_CONNECT_RESPONSE); - result.writeByte(CSRV_CONNECT_OK); + result.writeShort(CPMSG_CONNECT_RESPONSE); + result.writeByte(ERRMSG_OK); computer.send(result.getPacket()); return; } switch (message.getId()) { - case CMSG_CHAT: + case PCMSG_CHAT: { // chat to people around area std::string text = message.readString(); @@ -183,7 +184,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_ANNOUNCE: + case PCMSG_ANNOUNCE: { std::string text = message.readString(); // If it's slang's free. @@ -200,7 +201,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_PRIVMSG: + case PCMSG_PRIVMSG: { std::string user = message.readString(); std::string text = message.readString(); @@ -218,10 +219,9 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) // Channels handling // ================= - case CMSG_REGISTER_CHANNEL: + case PCMSG_REGISTER_CHANNEL: { - MessageOut result; - result.writeShort(SMSG_REGISTER_CHANNEL_RESPONSE); + result.writeShort(CPMSG_REGISTER_CHANNEL_RESPONSE); // 0 public, 1 private char channelType = message.readByte(); if (!channelType) @@ -229,9 +229,8 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) if (computer.accountLevel != AL_ADMIN && computer.accountLevel != AL_GM) { - result.writeByte(CHATCNL_CREATE_UNSUFFICIENT_RIGHTS); - computer.send(result.getPacket()); - return; + result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); + break; } } std::string channelName = message.readString(); @@ -242,23 +241,20 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) if (channelName.empty() || channelName.length() > MAX_CHANNEL_NAME || stringFilter->findDoubleQuotes(channelName)) { - result.writeByte(CHATCNL_CREATE_INVALID_NAME); - computer.send(result.getPacket()); - return; + result.writeByte(ERRMSG_INVALID_ARGUMENT); + break; } if (channelAnnouncement.length() > MAX_CHANNEL_ANNOUNCEMENT || stringFilter->findDoubleQuotes(channelAnnouncement)) { - result.writeByte(CHATCNL_CREATE_INVALID_ANNOUNCEMENT); - computer.send(result.getPacket()); - return; + result.writeByte(ERRMSG_INVALID_ARGUMENT); + break; } if (channelPassword.length() > MAX_CHANNEL_PASSWORD || stringFilter->findDoubleQuotes(channelPassword)) { - result.writeByte(CHATCNL_CREATE_INVALID_PASSWORD); - computer.send(result.getPacket()); - return; + result.writeByte(ERRMSG_INVALID_ARGUMENT); + break; } // If it's slang's free. @@ -282,16 +278,14 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) // to update the password and the announcement in it and also to remove it. chatChannelManager->addUserInChannel(computer.characterName, channelId); - result.writeByte(CHATCNL_CREATE_OK); + result.writeByte(ERRMSG_OK); result.writeShort(channelId); - computer.send(result.getPacket()); - return; + break; } else { - result.writeByte(CHATCNL_CREATE_UNKNOWN); - computer.send(result.getPacket()); - return; + result.writeByte(ERRMSG_FAILURE); + break; } } else @@ -301,83 +295,56 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) } break; - case CMSG_UNREGISTER_CHANNEL: + case PCMSG_UNREGISTER_CHANNEL: { - MessageOut result; - result.writeShort(SMSG_UNREGISTER_CHANNEL_RESPONSE); + result.writeShort(CPMSG_UNREGISTER_CHANNEL_RESPONSE); short channelId = message.readShort(); - if (channelId != 0) + if (!chatChannelManager->isChannelRegistered(channelId)) { - // Public channels - if (channelId < (signed)MAX_PUBLIC_CHANNELS_RANGE) + result.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else if (channelId < (signed)MAX_PUBLIC_CHANNELS_RANGE) + { // Public channel + if (computer.accountLevel == AL_ADMIN || computer.accountLevel == AL_GM) { - if (computer.accountLevel != AL_ADMIN && - computer.accountLevel != AL_GM) - { - result.writeByte(CHATCNL_DEL_UNSUFFICIENT_RIGHTS); - } - else - { // if the channel actually exist - if (chatChannelManager->isChannelRegistered(channelId)) - { - // Make every user quit the channel - makeUsersLeaveChannel(channelId); - if (chatChannelManager->removeChannel(channelId)) - result.writeByte(CHATCNL_DEL_OK); - else - result.writeByte(CHATCNL_DEL_UNKNOWN); - } - else - { // Couldn't remove channel because it doesn't exist - result.writeByte(CHATCNL_DEL_INVALID_ID); - } - } - } - else if (channelId < (signed)MAX_PRIVATE_CHANNELS_RANGE) - { // Private channels - if (chatChannelManager->isChannelRegistered(channelId)) - { - // We first see if the user is the admin (first user) of the channel - std::vector< std::string > const &userList = - chatChannelManager->getUserListInChannel(channelId); - std::vector< std::string >::const_iterator i = userList.begin(); - // if it's actually the private channel's admin - if (*i == computer.characterName) - { - // Make every user quit the channel - makeUsersLeaveChannel(channelId); - if (chatChannelManager->removeChannel(channelId)) - result.writeByte(CHATCNL_DEL_OK); - else - result.writeByte(CHATCNL_DEL_UNKNOWN); - } - else - { - result.writeByte(CHATCNL_DEL_UNSUFFICIENT_RIGHTS); - } - } + warnUsersAboutPlayerEventInChat(channelId, "", CHAT_EVENT_LEAVING_PLAYER); + if (chatChannelManager->removeChannel(channelId)) + result.writeByte(ERRMSG_OK); else - { - result.writeByte(CHATCNL_DEL_INVALID_ID); - } + result.writeByte(ERRMSG_FAILURE); } else - { // Id too high or too low - result.writeByte(CHATCNL_DEL_INVALID_ID); + { + result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); } } else - { - result.writeByte(CHATCNL_DEL_INVALID_ID); + { // Private channel + // We first see if the user is the admin (first user) of the channel + std::vector< std::string > const &userList = + chatChannelManager->getUserListInChannel(channelId); + std::vector< std::string >::const_iterator i = userList.begin(); + // if it's actually the private channel's admin + if (*i == computer.characterName) + { + // Make every user quit the channel + warnUsersAboutPlayerEventInChat(channelId, "", CHAT_EVENT_LEAVING_PLAYER); + if (chatChannelManager->removeChannel(channelId)) + result.writeByte(ERRMSG_OK); + else + result.writeByte(ERRMSG_FAILURE); + } + else + { + result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); + } } - computer.send(result.getPacket()); } break; - case CMSG_ENTER_CHANNEL: + case PCMSG_ENTER_CHANNEL: { - MessageOut result; - result.writeShort(SMSG_ENTER_CHANNEL_RESPONSE); + result.writeShort(CPMSG_ENTER_CHANNEL_RESPONSE); short channelId = message.readShort(); std::string givenPassword = message.readString(); if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) @@ -387,14 +354,13 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) { if (channelPassword != givenPassword) { - result.writeByte(CHATCNL_IN_BAD_PASSWORD); - computer.send(result.getPacket()); + result.writeByte(ERRMSG_INVALID_ARGUMENT); break; } } if (chatChannelManager->addUserInChannel(computer.characterName, channelId)) { - result.writeByte(CHATCNL_IN_OK); + result.writeByte(ERRMSG_OK); // The user entered the channel, now give him the announcement string // and the user list. result.writeString(chatChannelManager->getChannelAnnouncement(channelId)); @@ -405,64 +371,65 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) i != i_end; ++i) { result.writeString(*i); } - // Send an SMSG_UPDATE_CHANNEL_RESPONSE to warn other clients a user went + // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went // in the channel. warnUsersAboutPlayerEventInChat(channelId, computer.characterName, - CHATCNL_UPD_NEW_PLAYER); + CHAT_EVENT_NEW_PLAYER); } else { - result.writeByte(CHATCNL_IN_UNKNOWN); + result.writeByte(ERRMSG_FAILURE); } } else { - result.writeByte(CHATCNL_IN_INVALID_ID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); } - computer.send(result.getPacket()); } break; - case CMSG_QUIT_CHANNEL: + case PCMSG_QUIT_CHANNEL: { - MessageOut result; - result.writeShort(SMSG_QUIT_CHANNEL_RESPONSE); + result.writeShort(CPMSG_QUIT_CHANNEL_RESPONSE); short channelId = message.readShort(); if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) { if (chatChannelManager->removeUserFromChannel(computer.characterName, channelId)) { - result.writeByte(CHATCNL_OUT_OK); - // Send an SMSG_UPDATE_CHANNEL_RESPONSE to warn other clients a user left + result.writeByte(ERRMSG_OK); + // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user left // the channel. warnUsersAboutPlayerEventInChat(channelId, computer.characterName, - CHATCNL_UPD_LEAVING_PLAYER); + CHAT_EVENT_LEAVING_PLAYER); } else { - result.writeByte(CHATCNL_OUT_UNKNOWN); + result.writeByte(ERRMSG_FAILURE); } } else { - result.writeByte(CHATCNL_OUT_INVALID_ID); + result.writeByte(ERRMSG_INVALID_ARGUMENT); } - computer.send(result.getPacket()); } break; default: - LOG_INFO("Chat: Invalid message type", 2); + LOG_WARN("Invalid message type", 0); + result.writeShort(XXMSG_INVALID); break; } + + if (result.getPacket()->length > 0) + computer.send(result.getPacket()); } void ChatHandler::handleCommand(ChatClient &computer, std::string const &command) { LOG_INFO("Chat: Received unhandled command: " << command, 2); MessageOut result; - result.writeShort(SMSG_CHAT); - result.writeByte(CHATCMD_UNHANDLED_COMMAND); + result.writeShort(CPMSG_ERROR); + result.writeByte(CHAT_UNHANDLED_COMMAND); computer.send(result.getPacket()); } @@ -470,7 +437,7 @@ void ChatHandler::warnPlayerAboutBadWords(ChatClient &computer) { // We could later count if the player is really often unpolite. MessageOut result; - result.writeShort(SMSG_CHAT); + result.writeShort(CPMSG_ERROR); result.writeByte(CHAT_USING_BAD_WORDS); // The Channel computer.send(result.getPacket()); @@ -485,14 +452,14 @@ void ChatHandler::announce(ChatClient &computer, std::string const &text) { LOG_INFO("ANNOUNCE: " << text, 0); // Send it to every beings. - result.writeShort(SMSG_ANNOUNCEMENT); + result.writeShort(CPMSG_ANNOUNCEMENT); result.writeString(text); sendToEveryone(result); } else { - result.writeShort(SMSG_CHAT); - result.writeByte(CHATCMD_UNSUFFICIENT_RIGHTS); + result.writeShort(CPMSG_ERROR); + result.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); computer.send(result.getPacket()); LOG_INFO(computer.characterName << " couldn't make an announcement due to insufficient rights.", 2); @@ -504,7 +471,7 @@ void ChatHandler::sayToPlayer(ChatClient &computer, std::string const &playerNam MessageOut result; LOG_INFO(computer.characterName << " says to " << playerName << ": " << text, 2); // Send it to the being if the being exists - result.writeShort(SMSG_PRIVMSG); + result.writeShort(CPMSG_PRIVMSG); result.writeString(computer.characterName); result.writeString(text); for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); @@ -522,54 +489,23 @@ void ChatHandler::sayInChannel(ChatClient &computer, short channel, std::string MessageOut result; LOG_INFO(computer.characterName << " says in channel " << channel << ": " << text, 2); // Send it to every beings in channel - result.writeShort(SMSG_CHAT_CNL); + result.writeShort(CPMSG_PUBMSG); result.writeShort(channel); result.writeString(computer.characterName); result.writeString(text); sendInChannel(channel, result); } -void ChatHandler::makeUsersLeaveChannel(short channelId) -{ - MessageOut result; - result.writeShort(SMSG_QUIT_CHANNEL_RESPONSE); - result.writeByte(CHATCNL_OUT_OK); - - std::vector< std::string > const &users = - chatChannelManager->getUserListInChannel(channelId); - for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); - i != i_end; ++i) { - // If the client is in the channel, send it the 'leave now' packet - std::vector< std::string >::const_iterator j_end = users.end(), - j = std::find(users.begin(), j_end, static_cast< ChatClient * >(*i)->characterName); - if (j != j_end) - { - (*i)->send(result.getPacket()); - } - } -} - void ChatHandler::warnUsersAboutPlayerEventInChat(short channelId, std::string const &userName, char eventId) { MessageOut result; - result.writeShort(SMSG_UPDATE_CHANNEL_RESPONSE); + result.writeShort(CPMSG_CHANNEL_EVENT); + result.writeShort(channelId); result.writeByte(eventId); result.writeString(userName); - - std::vector< std::string > const &users = - chatChannelManager->getUserListInChannel(channelId); - for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); - i != i_end; ++i) { - // If the client is in the channel, send it the 'eventId' packet - std::vector< std::string >::const_iterator j_end = users.end(), - j = std::find(users.begin(), j_end, static_cast< ChatClient * >(*i)->characterName); - if (j != j_end) - { - (*i)->send(result.getPacket()); - } - } + sendInChannel(channelId, result); } void ChatHandler::sendInChannel(short channelId, MessageOut &msg) diff --git a/src/chathandler.h b/src/chathandler.h index b5a81375..6f71733b 100644 --- a/src/chathandler.h +++ b/src/chathandler.h @@ -78,11 +78,6 @@ class ChatHandler : public ConnectionHandler void sendInChannel(short channelId, MessageOut &); /** - * Tell a list of user they are leaving a channel. - */ - void makeUsersLeaveChannel(short channelId); - - /** * Tell a list of user about an event in a chatchannel about a player. */ void warnUsersAboutPlayerEventInChat(short channelId, std::string const &userName, char eventId); diff --git a/src/client.cpp b/src/client.cpp index 925f36a2..d90edfbd 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -119,7 +119,7 @@ int main(int argc, char *argv[]) case 1: // Register - msg.writeShort(CMSG_REGISTER); + msg.writeShort(PAMSG_REGISTER); // We send the client version msg.writeString(PACKAGE_VERSION); std::cout << "Account name: "; @@ -135,7 +135,7 @@ int main(int argc, char *argv[]) case 2: // Unregister (deleting an account) - msg.writeShort(CMSG_UNREGISTER); + msg.writeShort(PAMSG_UNREGISTER); std::cout << "Account name: "; std::cin >> line; msg.writeString(line); @@ -146,7 +146,7 @@ int main(int argc, char *argv[]) case 3: // Login - msg.writeShort(CMSG_LOGIN); + msg.writeShort(PAMSG_LOGIN); // We send the client version msg.writeString(PACKAGE_VERSION); std::cout << "Account name: "; @@ -159,27 +159,33 @@ int main(int argc, char *argv[]) case 4: // Logout - msg.writeShort(CMSG_LOGOUT); + msg.writeShort(PAMSG_LOGOUT); std::cout << "Logout" << std::endl; break; case 5: + { // Change Password - msg.writeShort(CMSG_PASSWORD_CHANGE); + msg.writeShort(PAMSG_PASSWORD_CHANGE); std::cout << "Old Password: "; std::cin >> line; msg.writeString(line); std::cout << "New Password: "; std::cin >> line; msg.writeString(line); + std::string line2; std::cout << "Retype new Password: "; - std::cin >> line; - msg.writeString(line); - break; + std::cin >> line2; + if (line != line2) + { + std::cout << "Error: Password mismatch." << std::endl; + goto process_enet; + } + } break; case 6: // Change Email - msg.writeShort(CMSG_EMAIL_CHANGE); + msg.writeShort(PAMSG_EMAIL_CHANGE); std::cout << "New Email: "; std::cin >> line; msg.writeString(line); @@ -187,13 +193,13 @@ int main(int argc, char *argv[]) case 7: // Get current Account's Email value - msg.writeShort(CMSG_EMAIL_GET); + msg.writeShort(PAMSG_EMAIL_GET); break; case 8: { // Create character - msg.writeShort(CMSG_CHAR_CREATE); + msg.writeShort(PAMSG_CHAR_CREATE); std::cout << "Name: "; std::cin >> line; msg.writeString(line); @@ -238,7 +244,7 @@ int main(int argc, char *argv[]) case 9: { // Select character - msg.writeShort(CMSG_CHAR_SELECT); + msg.writeShort(PAMSG_CHAR_SELECT); std::cout << "Character ID: "; std::cin >> line; msg.writeByte(atoi(line)); @@ -247,7 +253,7 @@ int main(int argc, char *argv[]) case 10: { // Delete character - msg.writeShort(CMSG_CHAR_DELETE); + msg.writeShort(PAMSG_CHAR_DELETE); std::cout << "Character ID: "; std::cin >> line; msg.writeByte(atoi(line)); @@ -256,7 +262,7 @@ int main(int argc, char *argv[]) case 11: { // List characters - msg.writeShort(CMSG_CHAR_LIST); + msg.writeShort(PAMSG_CHAR_LIST); } break; case 12: @@ -268,7 +274,7 @@ int main(int argc, char *argv[]) std::cout << "Y: "; std::cin >> y; - msg.writeShort(CMSG_WALK); + msg.writeShort(PGMSG_WALK); msg.writeLong(x); msg.writeLong(y); @@ -278,7 +284,7 @@ int main(int argc, char *argv[]) case 13: { // Chat - msg.writeShort(CMSG_SAY); + msg.writeShort(PGMSG_SAY); std::cout << "Say: "; std::cin.getline(line, 256); line[255] = '\0'; @@ -296,7 +302,7 @@ int main(int argc, char *argv[]) std::cin >> itemId; std::cout << "Slot: "; std::cin >> slot; - msg.writeShort(CMSG_EQUIP); + msg.writeShort(PGMSG_EQUIP); msg.writeLong(itemId); msg.writeByte(slot); @@ -313,12 +319,12 @@ int main(int argc, char *argv[]) case 16: { - msg.writeShort(CMSG_ENTER_WORLD); + msg.writeShort(PAMSG_ENTER_WORLD); } break; case 17: { - msg.writeShort(CMSG_GAMESRV_CONNECT); + msg.writeShort(PGMSG_CONNECT); msg.writeString(token, 32); msgDestination = 1; } break; @@ -392,20 +398,14 @@ void parsePacket(char *data, int recvLength) { MessageIn msg(packet); // (MessageIn frees packet) switch (msg.getId()) { - case SMSG_REGISTER_RESPONSE: + case APMSG_REGISTER_RESPONSE: // Register switch (msg.readByte()) { - case REGISTER_OK: + case ERRMSG_OK: std::cout << "Account registered." << std::endl; break; - case REGISTER_INVALID_USERNAME: - std::cout << "Account registering: Invalid username." << std::endl; - break; - case REGISTER_INVALID_PASSWORD: - std::cout << "Account registering: Invalid password." << std::endl; - break; - case REGISTER_INVALID_EMAIL: - std::cout << "Account registering: Invalid Email." << std::endl; + case ERRMSG_INVALID_ARGUMENT: + std::cout << "Account registering: Invalid username, password, or email." << std::endl; break; case REGISTER_INVALID_VERSION: std::cout << "Account registering: Invalid version." << std::endl; @@ -417,38 +417,30 @@ void parsePacket(char *data, int recvLength) { std::cout << "Account registering: Email already exists." << std::endl; break; default: - case REGISTER_UNKNOWN: std::cout << "Account registering: Unknown error." << std::endl; break; } break; - case SMSG_UNREGISTER_RESPONSE: + case APMSG_UNREGISTER_RESPONSE: // Register switch (msg.readByte()) { - case UNREGISTER_OK: + case ERRMSG_OK: std::cout << "Account unregistered." << std::endl; break; - case UNREGISTER_INVALID_PASSWORD: - std::cout << "Account unregistering: Invalid password." << std::endl; - break; - case UNREGISTER_INVALID_USERNAME: - std::cout << "Account unregistering: Invalid username." << std::endl; - break; - case UNREGISTER_INVALID_UNSUFFICIENT_RIGHTS: - std::cout << "Account unregistering: unsufficient rights." << std::endl; + case ERRMSG_INVALID_ARGUMENT: + std::cout << "Account unregistering: Invalid username or password." << std::endl; break; default: - case UNREGISTER_UNKNOWN: std::cout << "Account unregistering: Unknown error." << std::endl; break; } break; - case SMSG_LOGIN_RESPONSE: + case APMSG_LOGIN_RESPONSE: // Register switch (msg.readByte()) { - case LOGIN_OK: + case ERRMSG_OK: unsigned char charNumber; charNumber = msg.readByte(); std::cout << "Account has " << int(charNumber) << " characters." << std::endl; @@ -458,118 +450,103 @@ void parsePacket(char *data, int recvLength) { } std::cout << "." << std::endl; break; - case LOGIN_INVALID_USERNAME: - std::cout << "Login: Invalid Username." << std::endl; - break; - case LOGIN_INVALID_PASSWORD: - std::cout << "Login: Invalid Password." << std::endl; + case ERRMSG_INVALID_ARGUMENT: + std::cout << "Login: Invalid username or password." << std::endl; break; case LOGIN_INVALID_VERSION: std::cout << "Login: Invalid Version." << std::endl; break; - case LOGIN_ALREADY_LOGGED: + case ERRMSG_FAILURE: std::cout << "Login: Already logged with another account." << std::endl; break; - case LOGIN_SERVER_FULL: - std::cout << "Login: Server has reached maximum of clients." << std::endl; - break; - case LOGIN_ACCOUNT_BANNED: - std::cout << "Login: Your account has been banned." << std::endl; - break; - case LOGIN_ACCOUNT_REVIEW: - std::cout << "TODO:Login: Your account is being reviewed." << std::endl; - break; default: - case LOGIN_UNKNOWN: std::cout << "Login: Unknown error." << std::endl; break; } break; - case SMSG_LOGOUT_RESPONSE: + case APMSG_LOGOUT_RESPONSE: { switch (msg.readByte()) { - case LOGOUT_OK: + case ERRMSG_OK: std::cout << "Logout..." << std::endl; break; - default: - case LOGOUT_UNSUCCESSFULL: - std::cout << "Logout: unsuccessfull." << std::endl; + case ERRMSG_NO_LOGIN: + std::cout << "Logout: Unsuccessful." << std::endl; break; + default: + std::cout << "Logout: Unknown error." << std::endl; } } break; - case SMSG_PASSWORD_CHANGE_RESPONSE: + case APMSG_PASSWORD_CHANGE_RESPONSE: { switch (msg.readByte()) { - case PASSCHG_OK: + case ERRMSG_OK: std::cout << "Password correctly changed." << std::endl; break; - case PASSCHG_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Password change: Not logged in." << std::endl; break; - case PASSCHG_MISMATCH: - std::cout << "Password change: Passwords mismatch." << std::endl; - break; - case PASSCHG_INVALID: + case ERRMSG_INVALID_ARGUMENT: std::cout << "Password change: New password is invalid." << std::endl; break; + case ERRMSG_FAILURE: + std::cout << "Password change: Old password is invalid." << std::endl; + break; default: - case PASSCHG_UNKNOWN: std::cout << "Password change: Unknown error." << std::endl; break; } } break; - case SMSG_EMAIL_CHANGE_RESPONSE: + case APMSG_EMAIL_CHANGE_RESPONSE: { switch (msg.readByte()) { - case EMAILCHG_OK: + case ERRMSG_OK: std::cout << "Email correctly changed." << std::endl; break; - case EMAILCHG_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Email change: Not logged in." << std::endl; break; case EMAILCHG_EXISTS_EMAIL: std::cout << "Email change: Email already exists." << std::endl; break; - case EMAILCHG_INVALID: + case ERRMSG_INVALID_ARGUMENT: std::cout << "Email change: New Email is invalid." << std::endl; break; default: - case EMAILCHG_UNKNOWN: std::cout << "Email change: Unknown error." << std::endl; break; } } break; - case SMSG_EMAIL_GET_RESPONSE: + case APMSG_EMAIL_GET_RESPONSE: { switch (msg.readByte()) { - case EMAILGET_OK: + case ERRMSG_OK: std::cout << "Current Email: " << msg.readString() << std::endl; break; - case EMAILGET_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Get Email: Not logged in." << std::endl; break; default: - case EMAILGET_UNKNOWN: std::cout << "Get Email: Unknown error." << std::endl; break; } } break; - case SMSG_CHAR_CREATE_RESPONSE: + case APMSG_CHAR_CREATE_RESPONSE: { switch (msg.readByte()) { - case CREATE_OK: + case ERRMSG_OK: std::cout << "Character Created successfully." << std::endl; break; case CREATE_EXISTS_NAME: std::cout << "Character Creation: Character's name already exists." << std::endl; break; - case CREATE_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Character Creation: Not logged in." << std::endl; break; case CREATE_TOO_MUCH_CHARACTERS: @@ -584,9 +561,6 @@ void parsePacket(char *data, int recvLength) { case CREATE_INVALID_GENDER: std::cout << "Character Creation: Invalid Gender Value." << std::endl; break; - case CREATE_INVALID_NAME: - std::cout << "Character Creation: Invalid Name." << std::endl; - break; case CREATE_RAW_STATS_EQUAL_TO_ZERO: std::cout << "Character Creation: a Statistic is equal to zero." << std::endl; break; @@ -600,39 +574,34 @@ void parsePacket(char *data, int recvLength) { std::cout << "Character Creation: Statistics too low for level 1." << std::endl; break; default: - case CREATE_UNKNOWN: std::cout << "Character Creation: Unknown error." << std::endl; break; } } break; - case SMSG_CHAR_DELETE_RESPONSE: + case APMSG_CHAR_DELETE_RESPONSE: { switch (msg.readByte()) { - case DELETE_OK: + case ERRMSG_OK: std::cout << "Character deleted." << std::endl; break; - case DELETE_INVALID_ID: + case ERRMSG_INVALID_ARGUMENT: std::cout << "Character Deletion: Character's ID doesn't exist." << std::endl; break; - case DELETE_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Character Deletion: Not logged in." << std::endl; break; - case DELETE_NO_MORE_CHARACTERS: - std::cout << "Character Deletion: No more characters." << std::endl; - break; default: - case DELETE_UNKNOWN: std::cout << "Character Deletion: Unknown error." << std::endl; break; } } break; - case SMSG_CHAR_SELECT_RESPONSE: + case APMSG_CHAR_SELECT_RESPONSE: { switch (msg.readByte()) { - case SELECT_OK: + case ERRMSG_OK: { std::cout << "Character selected successfully."; std::cout << std::endl; @@ -641,31 +610,24 @@ void parsePacket(char *data, int recvLength) { std::cout << (int)msg.readShort() << ", Y:"; std::cout << (int)msg.readShort() << ")" << std::endl; } break; - case SELECT_INVALID: + case ERRMSG_INVALID_ARGUMENT: std::cout << "Character Selection: invalid ID." << std::endl; break; - case SELECT_NOLOGIN: + case ERRMSG_NO_LOGIN: std::cout << "Character Selection: Not logged in." << std::endl; break; - case SELECT_NO_CHARACTERS: - std::cout << "Character Selection: No character to select." << std::endl; - break; - case SELECT_NO_MAPS: - std::cout << "Character Selection: Can't load default map for character." - << std::endl; - break; default: - case SELECT_UNKNOWN: std::cout << "Character Selection: Unknown error." << std::endl; break; } } break; - case SMSG_CHAR_LIST_RESPONSE: + case APMSG_CHAR_LIST_RESPONSE: { switch (msg.readByte()) { - case CHAR_LIST_OK: + case ERRMSG_OK: + { unsigned char charNumber; charNumber = msg.readByte(); std::cout << "Character List:" << std::endl @@ -695,29 +657,25 @@ void parsePacket(char *data, int recvLength) { std::cout << "Current Map: " << msg.readString() << " (X:"; std::cout << int(msg.readShort()) << ", Y:" << int(msg.readShort()) << ")." << std::endl << std::endl; - } break; - case CHAR_LIST_NOLOGIN: - std::cout << "Character List: Not logged in." - << std::endl; - break; + } + } break; default: - case CHAR_LIST_UNKNOWN: std::cout << "Character List: Unknown error." << std::endl; break; } } break; - case SMSG_SAY: + case GPMSG_SAY: { std::string who = msg.readString(); std::cout << who << " says around:" << std::endl << msg.readString() << std::endl; } break; - case SMSG_ENTER_WORLD_RESPONSE: + case APMSG_ENTER_WORLD_RESPONSE: { switch (msg.readByte()) { - case ENTER_WORLD_OK: + case ERRMSG_OK: { std::string server = msg.readString(); enet_address_set_host(&addressGame, server.c_str()); @@ -727,12 +685,23 @@ void parsePacket(char *data, int recvLength) { connected = false; std::cout << "Connecting to " << server << ':' << addressGame.port << std::endl; } break; + case ERRMSG_NO_LOGIN: + std::cout << "Enter world: Not logged in." << std::endl; + break; + case ERRMSG_NO_CHARACTER_SELECTED: + std::cout << "Enter world: No character selected." << std::endl; + break; default: - std::cout << "Enter world failed." << std::endl; + std::cout << "Enter world: Unknown error." << std::endl; + break; } } break; + case XXMSG_INVALID: + std::cout << "The server does not understand our message." << std::endl; + break; default: + std::cout << "Unknown message received. Id: " << msg.getId() << "." << std::endl; break; } // end switch MessageId diff --git a/src/defines.h b/src/defines.h index a37c7e74..a19df556 100644 --- a/src/defines.h +++ b/src/defines.h @@ -103,147 +103,108 @@ const unsigned int MAX_CLIENTS = 1024, /** * Enumerated type for communicated messages + * - PAMSG_*: from client to account server + * - APMSG_*: from account server to client + * - PCMSG_*: from client to chat server + * - CPMSG_*: from chat server to client + * - PGMSG_*: from client to game server + * - GPMSG_*: from game server to client + * Components: B byte, W word, D double word, S variable-size string */ enum { // Login/Register - CMSG_REGISTER = 0x0000, - CMSG_ENCRYPTED_REGISTER = 0x0001, - SMSG_REGISTER_RESPONSE = 0x0002, - CMSG_UNREGISTER = 0x0003, - SMSG_UNREGISTER_RESPONSE = 0x0004, - CMSG_LOGIN = 0x0010, - CMSG_ENCRYPTED_LOGIN = 0x0011, - SMSG_LOGIN_RESPONSE = 0x0012, - CMSG_LOGOUT = 0x0013, - SMSG_LOGOUT_RESPONSE = 0x0014, - CMSG_FORGOT_PASSWORD = 0x0015, - SMSG_FORGOT_PASSWORD_RESPONSE = 0x0016, - CMSG_CHAR_CREATE = 0x0020, - SMSG_CHAR_CREATE_RESPONSE = 0x0021, - 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 = 0x0026, - SMSG_CHAR_SELECT_RESPONSE = 0x0027, - CMSG_EMAIL_CHANGE = 0x0030, - SMSG_EMAIL_CHANGE_RESPONSE = 0x0031, - CMSG_EMAIL_GET = 0x0032, - SMSG_EMAIL_GET_RESPONSE = 0x0033, - CMSG_PASSWORD_CHANGE = 0x0034, - SMSG_PASSWORD_CHANGE_RESPONSE = 0x0035, - CMSG_ENTER_WORLD = 0x0040, - SMSG_ENTER_WORLD_RESPONSE = 0x0041, - CMSG_ENTER_CHAT = 0x0042, - SMSG_ENTER_CHAT_RESPONSE = 0x0043, - CMSG_GAMESRV_CONNECT = 0x0050, - SMSG_GAMESRV_CONNECT_RESPONSE = 0x0051, - SMSG_GAMESRV_DISCONNECT = 0x0052, - CMSG_CHATSRV_CONNECT = 0x0053, - SMSG_CHATSRV_CONNECT_RESPONSE = 0x0054, - SMSG_CHATSRV_DISCONNECT = 0x0055, - - // Objects - SMSG_NEW_OBJECT = 0x0100, - SMSG_REMOVE_OBJECT = 0x0101, - SMSG_CHANGE_OBJECT = 0x0102, - CMSG_PICKUP = 0x0110, - SMSG_PICKUP_RESPONSE = 0x0111, - CMSG_USE_OBJECT = 0x0120, - SMSG_USE_RESPONSE = 0x0121, - - // Beings - SMSG_NEW_BEING = 0x0200, - SMSG_REMOVE_BEING = 0x0201, - SMSG_INVENTORY_UPD = 0x0210, - SMSG_EQUIPMENT_UPD = 0x0220, - SMSG_ATTACK = 0x0230, - SMSG_PATH = 0x0240, - CMSG_TARGET = 0x0250, - CMSG_WALK = 0x0260, - CMSG_START_TRADE = 0x0270, - CMSG_START_TALK = 0x0280, - CMSG_REQ_TRADE = 0x0290, - - // Items - CMSG_USE_ITEM = 0x0300, - CMSG_EQUIP = 0x0301, - SMSG_EQUIP_RESPONSE = 0x0302, + PAMSG_REGISTER = 0x0000, // S version, S username, S password, S email + APMSG_REGISTER_RESPONSE = 0x0002, // B error + PAMSG_UNREGISTER = 0x0003, // - + APMSG_UNREGISTER_RESPONSE = 0x0004, // B error + PAMSG_LOGIN = 0x0010, // S version, S username, S password + APMSG_LOGIN_RESPONSE = 0x0012, // B error + PAMSG_LOGOUT = 0x0013, // - + APMSG_LOGOUT_RESPONSE = 0x0014, // B error + PAMSG_CHAR_CREATE = 0x0020, // S name, B hair style, B hair color, B gender, W*6 stats + APMSG_CHAR_CREATE_RESPONSE = 0x0021, // B error + PAMSG_CHAR_DELETE = 0x0022, // B index + APMSG_CHAR_DELETE_RESPONSE = 0x0023, // B error + PAMSG_CHAR_LIST = 0x0024, // - + APMSG_CHAR_LIST_RESPONSE = 0x0025, // B number, { B index, S name, B gender, B hair style, B hair color, B level, W money, W*6 stats, S mapname, W*2 position }* + PAMSG_CHAR_SELECT = 0x0026, // B index + APMSG_CHAR_SELECT_RESPONSE = 0x0027, // B error, S mapname, W*2 position + PAMSG_EMAIL_CHANGE = 0x0030, // S email + APMSG_EMAIL_CHANGE_RESPONSE = 0x0031, // B error + PAMSG_EMAIL_GET = 0x0032, // - + APMSG_EMAIL_GET_RESPONSE = 0x0033, // B error, S email + PAMSG_PASSWORD_CHANGE = 0x0034, // S old password, S new password + APMSG_PASSWORD_CHANGE_RESPONSE = 0x0035, // B error + PAMSG_ENTER_WORLD = 0x0040, // - + APMSG_ENTER_WORLD_RESPONSE = 0x0041, // B error, S address, W port, B*32 token + PAMSG_ENTER_CHAT = 0x0042, // - + APMSG_ENTER_CHAT_RESPONSE = 0x0043, // B error, S address, W port, B*32 token + PGMSG_CONNECT = 0x0050, // B*32 token + GPMSG_CONNECT_RESPONSE = 0x0051, // B error + PCMSG_CONNECT = 0x0053, // B*32 token + CPMSG_CONNECT_RESPONSE = 0x0054, // B error + + // Game + PGMSG_PICKUP = 0x0110, + GPMSG_PICKUP_RESPONSE = 0x0111, + SMSG_NEW_BEING = 0x0200, + PGMSG_WALK = 0x0260, // L*2 destination + PGMSG_SAY = 0x02A0, // S text + GPMSG_SAY = 0x02A1, // S being, S text + PGMSG_USE_ITEM = 0x0300, // L item id + GPMSG_USE_RESPONSE = 0x0301, // B error + PGMSG_EQUIP = 0x0302, // L item id, B slot + GPMSG_EQUIP_RESPONSE = 0x0303, // B error // Chat - SMSG_SYSTEM = 0x0400, - SMSG_CHAT = 0x0401, - SMSG_ANNOUNCEMENT = 0x0402, - SMSG_PRIVMSG = 0x0403, - SMSG_CHAT_CNL = 0x0404, - SMSG_SAY = 0x0405, - CMSG_CHAT = 0x0410, - CMSG_ANNOUNCE = 0x0411, - CMSG_PRIVMSG = 0x0412, - CMSG_SAY = 0x0413, + CPMSG_ERROR = 0x0401, // B error + CPMSG_ANNOUNCEMENT = 0x0402, // S text + CPMSG_PRIVMSG = 0x0403, // S user, S text + CPMSG_PUBMSG = 0x0404, // W channel, S user, S text + PCMSG_CHAT = 0x0410, // S text, W channel + PCMSG_ANNOUNCE = 0x0411, // S text + PCMSG_PRIVMSG = 0x0412, // S user, S text // -- Channeling - CMSG_REGISTER_CHANNEL = 0x0413, - SMSG_REGISTER_CHANNEL_RESPONSE = 0x0414, - CMSG_UNREGISTER_CHANNEL = 0x0415, - SMSG_UNREGISTER_CHANNEL_RESPONSE = 0x0416, - CMSG_UPDATE_CHANNEL = 0x0417, - SMSG_UPDATE_CHANNEL_RESPONSE = 0x0418, - CMSG_ENTER_CHANNEL = 0x0419, - SMSG_ENTER_CHANNEL_RESPONSE = 0x0420, - CMSG_QUIT_CHANNEL = 0x0421, - SMSG_QUIT_CHANNEL_RESPONSE = 0x0422, - - // Other - SMSG_LOAD_MAP = 0x0500 + PCMSG_REGISTER_CHANNEL = 0x0413, // B pub/priv, S name, S announcement, S password + CPMSG_REGISTER_CHANNEL_RESPONSE = 0x0414, // B error + PCMSG_UNREGISTER_CHANNEL = 0x0415, // W channel + CPMSG_UNREGISTER_CHANNEL_RESPONSE = 0x0416, // B error + CPMSG_CHANNEL_EVENT = 0x0418, // W channel, B event, S user + PCMSG_ENTER_CHANNEL = 0x0419, // W channel, S password + CPMSG_ENTER_CHANNEL_RESPONSE = 0x0420, // B error + PCMSG_QUIT_CHANNEL = 0x0421, // W channel + CPMSG_QUIT_CHANNEL_RESPONSE = 0x0422, // B error - // NOTE: We will need more messages for in-game control (eg. moving a client to a new map/position etc.). Currently the protocol only caters for the bare basics. + XXMSG_INVALID = 0x7FFF }; -// Login return values -enum { - LOGIN_OK = 0, - LOGIN_INVALID_USERNAME, - LOGIN_INVALID_PASSWORD, - LOGIN_INVALID_VERSION, - LOGIN_SERVER_FULL, - LOGIN_ACCOUNT_BANNED, - LOGIN_ACCOUNT_REVIEW, - LOGIN_ALREADY_LOGGED, - LOGIN_UNKNOWN -}; +// Generic return values -// Logout return values enum { - LOGOUT_OK = 0, - LOGOUT_UNSUCCESSFULL + ERRMSG_OK = 0, // everything is fine + ERRMSG_FAILURE, // the action failed + ERRMSG_NO_LOGIN, // the user is not yet logged + ERRMSG_NO_CHARACTER_SELECTED, // the user needs a character + ERRMSG_INSUFFICIENT_RIGHTS, // the user is not privileged + ERRMSG_INVALID_ARGUMENT, // part of the received message was invalid }; -// Account register return values +// Login specific return values enum { - REGISTER_OK = 0, - REGISTER_INVALID_USERNAME, - REGISTER_INVALID_PASSWORD, - REGISTER_INVALID_EMAIL, - REGISTER_INVALID_VERSION, - REGISTER_EXISTS_USERNAME, - REGISTER_EXISTS_EMAIL, - REGISTER_UNKNOWN + LOGIN_INVALID_VERSION = 0x40, // the user is using an incompatible protocol + LOGIN_SERVER_FULL // the server is overloaded }; -// Account deletion return values +// Account register specific return values enum { - UNREGISTER_OK = 0, - UNREGISTER_INVALID_USERNAME, - UNREGISTER_INVALID_PASSWORD, - UNREGISTER_INVALID_UNSUFFICIENT_RIGHTS, - UNREGISTER_UNKNOWN + REGISTER_INVALID_VERSION = 0x40, // the user is using an incompatible protocol + REGISTER_EXISTS_USERNAME, // there already is an account with this username + REGISTER_EXISTS_EMAIL // there already is an account with this email address }; -// Character creation return values +// Character creation specific return values enum { - CREATE_OK = 0, - CREATE_INVALID_NAME, - CREATE_INVALID_HAIRSTYLE, + CREATE_INVALID_HAIRSTYLE = 0x40, CREATE_INVALID_HAIRCOLOR, CREATE_INVALID_GENDER, CREATE_RAW_STATS_TOO_HIGH, @@ -251,141 +212,24 @@ enum { CREATE_RAW_STATS_INVALID_DIFF, CREATE_RAW_STATS_EQUAL_TO_ZERO, CREATE_EXISTS_NAME, - CREATE_TOO_MUCH_CHARACTERS, - CREATE_NOLOGIN, - CREATE_UNKNOWN -}; - -// Character deletion return values -enum { - DELETE_OK = 0, - DELETE_INVALID_ID, - DELETE_NO_MORE_CHARACTERS, - DELETE_NOLOGIN, - DELETE_UNKNOWN -}; - -// Character selection return values -// (When selecting a new one, you deselect the previous.) -enum { - SELECT_OK = 0, - SELECT_INVALID, - SELECT_NO_CHARACTERS, - SELECT_NOLOGIN, - SELECT_NO_MAPS, - SELECT_UNKNOWN -}; - -// Character's list return values -enum { - CHAR_LIST_OK = 0, - CHAR_LIST_NOLOGIN, - CHAR_LIST_UNKNOWN -}; - -// Email change return values -enum { - EMAILCHG_OK = 0, - EMAILCHG_NOLOGIN, - EMAILCHG_INVALID, - EMAILCHG_EXISTS_EMAIL, - EMAILCHG_UNKNOWN -}; - -// Get Email return values -enum { - EMAILGET_OK = 0, - EMAILGET_NOLOGIN, - EMAILGET_UNKNOWN -}; - -// Password change return values -enum { - PASSCHG_OK = 0, - PASSCHG_NOLOGIN, - PASSCHG_INVALID, - PASSCHG_MISMATCH, - PASSCHG_UNKNOWN -}; - -// Enter world return values -enum { - ENTER_WORLD_OK = 0, - ENTER_WORLD_NOLOGIN, - ENTER_WORLD_NO_CHARACTER_SELECTED + CREATE_TOO_MUCH_CHARACTERS }; -// Enter chat return values +// Email change specific return values enum { - ENTER_CHAT_OK = 0, - ENTER_CHAT_NOLOGIN, - ENTER_CHAT_NO_CHARACTER_SELECTED -}; - -// Game server connect values -enum { - GSRV_CONNECT_OK = 0 -}; - -// Chat server connect values -enum { - CSRV_CONNECT_OK = 0 + EMAILCHG_EXISTS_EMAIL = 0x40 }; // Chat errors return values enum { - // CHAT_AROUND = 0, - CHAT_NOLOGIN = 1, - CHAT_NO_CHARACTER_SELECTED, - CHAT_USING_BAD_WORDS, - CHATCMD_UNHANDLED_COMMAND, - CHATCMD_UNSUFFICIENT_RIGHTS, - CHATCMD_UNKNOWN -}; - -// Chat channels creation return values -enum { - CHATCNL_CREATE_OK = 0, - CHATCNL_CREATE_UNSUFFICIENT_RIGHTS, - CHATCNL_CREATE_INVALID_NAME, - CHATCNL_CREATE_INVALID_ANNOUNCEMENT, - CHATCNL_CREATE_INVALID_PASSWORD, - CHATCNL_CREATE_UNKNOWN + CHAT_USING_BAD_WORDS = 0x40, + CHAT_UNHANDLED_COMMAND, }; -// Chat channels update return values +// Chat channels event values enum { - CHATCNL_UPD_OK = 0, - CHATCNL_UPD_UNSUFFICIENT_RIGHTS, - CHATCNL_UPD_NEW_PLAYER, - CHATCNL_UPD_LEAVING_PLAYER, - CHATCNL_UPD_KICKED_PLAYER, // To be implemented. - CHATCNL_UPD_CHANGED_ADMIN, // dito - CHATCNL_UPD_CHANGED_ANNOUNCEMENT, // dito - CHATCNL_UPD_UNKNOWN -}; - -// Chat channels deletion return values -enum { - CHATCNL_DEL_OK = 0, - CHATCNL_DEL_UNSUFFICIENT_RIGHTS, - CHATCNL_DEL_INVALID_ID, - CHATCNL_DEL_UNKNOWN -}; - -// Chat channels entering return values -enum { - CHATCNL_IN_OK = 0, - CHATCNL_IN_INVALID_ID, - CHATCNL_IN_BAD_PASSWORD, - CHATCNL_IN_UNKNOWN -}; - -// Chat channels leaving return values -enum { - CHATCNL_OUT_OK = 0, - CHATCNL_OUT_INVALID_ID, - CHATCNL_OUT_UNKNOWN + CHAT_EVENT_NEW_PLAYER = 0, + CHAT_EVENT_LEAVING_PLAYER }; // Object type enumeration @@ -397,23 +241,4 @@ enum { OBJECT_PLAYER // A normal being }; -// Pickup response enumeration -enum { - PICKUP_OK = 0, - PICKUP_OVERWEIGHT, - PICKUP_FAIL -}; - -// Object use response enumeration -enum { - USE_OK = 0, - USE_FAIL -}; - -// Equip responses -enum { - EQUIP_OK = 0, - EQUIP_FAIL -}; - #endif // _TMWSERV_DEFINES_H_ diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index dc14fd2a..c61a95db 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -32,6 +32,7 @@ #include "netcomputer.h" #include "packet.h" #include "state.h" +#include "utils/logger.h" using tmwserv::BeingPtr; @@ -118,8 +119,8 @@ void registerGameClient(std::string const &token, tmwserv::BeingPtr ch) computer->setCharacter(ch); pendingClients.erase(i); MessageOut result; - result.writeShort(SMSG_GAMESRV_CONNECT_RESPONSE); - result.writeByte(GSRV_CONNECT_OK); + result.writeShort(GPMSG_CONNECT_RESPONSE); + result.writeByte(ERRMSG_OK); computer->send(result.getPacket()); } else @@ -173,7 +174,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) MessageOut result; if (computer.getCharacter().get() == NULL) { - if (message.getId() != CMSG_GAMESRV_CONNECT) return; + if (message.getId() != PGMSG_CONNECT) return; std::string magic_token = message.readString(32); GamePendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) @@ -187,21 +188,21 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) } computer.setCharacter(i->second.character); pendingLogins.erase(i); - result.writeShort(SMSG_GAMESRV_CONNECT_RESPONSE); - result.writeByte(GSRV_CONNECT_OK); + result.writeShort(GPMSG_CONNECT_RESPONSE); + result.writeByte(ERRMSG_OK); computer.send(result.getPacket()); return; } switch (message.getId()) { - case CMSG_SAY: + case PGMSG_SAY: { std::string say = message.readString(); sayAround(computer, say); } break; - case CMSG_PICKUP: + case PGMSG_PICKUP: { // add item to inventory (this is too simplistic atm) unsigned int itemId = message.readLong(); @@ -210,33 +211,27 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) // send feedback computer.getCharacter()->addInventory(itemId); - result.writeShort(SMSG_PICKUP_RESPONSE); - result.writeByte(PICKUP_OK); + result.writeShort(GPMSG_PICKUP_RESPONSE); + result.writeByte(ERRMSG_OK); } break; - case CMSG_USE_ITEM: - case CMSG_USE_OBJECT: + case PGMSG_USE_ITEM: { unsigned int itemId = message.readLong(); - result.writeShort(SMSG_USE_RESPONSE); + result.writeShort(GPMSG_USE_RESPONSE); if (computer.getCharacter()->hasItem(itemId)) { // use item // this should execute a script which will do the appropriate action // (the script will determine if the item is 1 use only) - result.writeByte(USE_OK); + result.writeByte(ERRMSG_OK); } else { - result.writeByte(USE_FAIL); + result.writeByte(ERRMSG_FAILURE); } } break; - case CMSG_TARGET: - { - // nothing at the moment - } break; - - case CMSG_WALK: + case PGMSG_WALK: { long x = message.readLong(); long y = message.readLong(); @@ -248,34 +243,19 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) // no response should be required } break; - case CMSG_START_TRADE: - { - // nothing at the moment - } break; - - case CMSG_START_TALK: - { - // nothing at the moment - } break; - - case CMSG_REQ_TRADE: - { - // nothing at the moment - } break; - - case CMSG_EQUIP: + case PGMSG_EQUIP: { int itemId = message.readLong(); char slot = message.readByte(); - result.writeShort(SMSG_EQUIP_RESPONSE); + result.writeShort(GPMSG_EQUIP_RESPONSE); result.writeByte(computer.getCharacter()->equip(itemId, slot) ? - EQUIP_OK : EQUIP_FAIL); + ERRMSG_OK : ERRMSG_FAILURE); } break; default: - std::cerr << "Warning: GameHandler received message of unkown type" - << " (" << message.getId() << ")" << std::endl; + LOG_WARN("Invalid message type", 0); + result.writeShort(XXMSG_INVALID); break; } @@ -287,7 +267,7 @@ void GameHandler::sayAround(GameClient &computer, std::string const &text) { BeingPtr beingPtr = computer.getCharacter(); MessageOut msg; - msg.writeShort(SMSG_SAY); + msg.writeShort(GPMSG_SAY); msg.writeString(beingPtr->getName()); msg.writeString(text); unsigned speakerMapId = beingPtr->getMapId(); |