diff options
author | Yohann Ferreira <bertram@cegetel.net> | 2006-03-05 01:14:15 +0000 |
---|---|---|
committer | Yohann Ferreira <bertram@cegetel.net> | 2006-03-05 01:14:15 +0000 |
commit | ee55fb86718729e1ce445b9f674bf8287981cbaf (patch) | |
tree | 3053a880f816f176d316e21f61924a219eb545b4 /src | |
parent | dca3ef9431e83d60ed305c8a301081d2d004f849 (diff) | |
download | manaserv-ee55fb86718729e1ce445b9f674bf8287981cbaf.tar.gz manaserv-ee55fb86718729e1ce445b9f674bf8287981cbaf.tar.bz2 manaserv-ee55fb86718729e1ce445b9f674bf8287981cbaf.tar.xz manaserv-ee55fb86718729e1ce445b9f674bf8287981cbaf.zip |
Added the server code to enter and leave a channel. Some minor fix to the state code, and completed the TODO a little for later coding.
Diffstat (limited to 'src')
-rw-r--r-- | src/chathandler.cpp | 78 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 30 | ||||
-rw-r--r-- | src/connectionhandler.h | 6 | ||||
-rw-r--r-- | src/defines.h | 10 | ||||
-rw-r--r-- | src/state.cpp | 2 |
5 files changed, 121 insertions, 5 deletions
diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 5f57aeb6..009a8b3b 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -287,7 +287,85 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) computer.send(result.getPacket()); } break; + case CMSG_ENTER_CHANNEL: + { + MessageOut result; + result.writeShort(SMSG_ENTER_CHANNEL_RESPONSE); + short channelId = message.readShort(); + std::string givenPassword = message.readString(); + if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) + { + std::string channelPassword = chatChannelManager->getChannelPassword(channelId); + if (channelPassword != "None") + { + if (channelPassword != givenPassword) + { + result.writeByte(CHATCNL_IN_BAD_PASSWORD); + computer.send(result.getPacket()); + break; + } + } + if (chatChannelManager->addUserInChannel(computer.getCharacter(), channelId)) + { + result.writeByte(CHATCNL_IN_OK); + // The user entered the channel, now give him the announcement string + // and the user list. + result.writeString(chatChannelManager->getChannelAnnouncement(channelId)); + std::vector<tmwserv::BeingPtr> userList = + chatChannelManager->getUserListInChannel(channelId); + result.writeShort(userList.size()); + for (std::vector<tmwserv::BeingPtr>::iterator i = userList.begin(); i != userList.end(); + ++i) + { + result.writeString(i->get()->getName()); + } + // Send an SMSG_UPDATE_CHANNEL_RESPONSE to warn other clients a user went + // in the channel. + connectionHandler->warnUsersAboutPlayerEventInChat(channelId, + computer.getCharacter()->getName(), + CHATCNL_UPD_NEW_PLAYER); + } + else + { + result.writeByte(CHATCNL_IN_UNKNOWN); + } + } + else + { + result.writeByte(CHATCNL_IN_INVALID_ID); + } + computer.send(result.getPacket()); + } + break; + case CMSG_QUIT_CHANNEL: + { + MessageOut result; + result.writeShort(SMSG_QUIT_CHANNEL_RESPONSE); + short channelId = message.readShort(); + if (channelId != 0 && chatChannelManager->isChannelRegistered(channelId)) + { + if (chatChannelManager->removeUserFromChannel(computer.getCharacter(), channelId)) + { + result.writeByte(CHATCNL_OUT_OK); + // Send an SMSG_UPDATE_CHANNEL_RESPONSE to warn other clients a user left + // the channel. + connectionHandler->warnUsersAboutPlayerEventInChat(channelId, + computer.getCharacter()->getName(), + CHATCNL_UPD_LEAVING_PLAYER); + } + else + { + result.writeByte(CHATCNL_OUT_UNKNOWN); + } + } + else + { + result.writeByte(CHATCNL_OUT_INVALID_ID); + } + computer.send(result.getPacket()); + } + break; default: LOG_INFO("Chat: Invalid message type", 2) diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index c7a3d49b..a336a842 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -323,10 +323,11 @@ void ConnectionHandler::makeUsersLeaveChannel(const short channelId) MessageOut result; result.writeShort(SMSG_QUIT_CHANNEL_RESPONSE); result.writeByte(CHATCNL_OUT_OK); + + const std::vector<tmwserv::BeingPtr> beingList = + chatChannelManager->getUserListInChannel(channelId); for (NetComputers::iterator i = clients.begin(); i != clients.end();i++) { - const std::vector<tmwserv::BeingPtr> beingList = - chatChannelManager->getUserListInChannel(channelId); // If the being is in the channel, send it the 'leave now' packet for (std::vector<tmwserv::BeingPtr>::const_iterator j = beingList.begin(); j != beingList.end(); j++) @@ -338,3 +339,28 @@ void ConnectionHandler::makeUsersLeaveChannel(const short channelId) } } } + +void ConnectionHandler::warnUsersAboutPlayerEventInChat(const short channelId, + const std::string& userName, + const char eventId) +{ + MessageOut result; + result.writeShort(SMSG_UPDATE_CHANNEL_RESPONSE); + result.writeByte(eventId); + result.writeString(userName); + + const std::vector<tmwserv::BeingPtr> beingList = + chatChannelManager->getUserListInChannel(channelId); + for (NetComputers::iterator i = clients.begin(); i != clients.end();i++) + { + // If the being is in the channel, send it the 'eventId' packet + for (std::vector<tmwserv::BeingPtr>::const_iterator j = beingList.begin(); + j != beingList.end(); j++) + { + if ((*i)->getCharacter().get() == (*j).get() ) + { + (*i)->send(result.getPacket()); + } + } + } +} diff --git a/src/connectionhandler.h b/src/connectionhandler.h index e89d9f74..c00abfc6 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -138,6 +138,12 @@ class ConnectionHandler */ void makeUsersLeaveChannel(const short channelId); + /** + * tells a list of user about an event in a chatchannel about a player. + */ + void warnUsersAboutPlayerEventInChat(const short channelId, + const std::string& userName, + const char eventId); private: std::map<unsigned int, MessageHandler*> handlers; diff --git a/src/defines.h b/src/defines.h index 3914dbe8..c4d00bb5 100644 --- a/src/defines.h +++ b/src/defines.h @@ -321,6 +321,11 @@ enum { 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 }; @@ -335,7 +340,7 @@ enum { // Chat channels entering return values enum { CHATCNL_IN_OK = 0, - CHATCNL_IN_UNSUFFICIENT_RIGHTS, + CHATCNL_IN_INVALID_ID, CHATCNL_IN_BAD_PASSWORD, CHATCNL_IN_UNKNOWN }; @@ -343,13 +348,14 @@ enum { // Chat channels leaving return values enum { CHATCNL_OUT_OK = 0, + CHATCNL_OUT_INVALID_ID, CHATCNL_OUT_UNKNOWN }; // Object type enumeration enum { OBJECT_ITEM = 0, // A simple item - OBJECT_ACTOR, // An item that toggle map/quest actions (doors, switchs, ...) and to speak (map panels). + OBJECT_ACTOR, // An item that toggle map/quest actions (doors, switchs, ...) and can speak (map panels). OBJECT_NPC, // Non-Playable-Character is an actor capable of movement and maybe actions OBJECT_MONSTER, // A monster (moving actor with AI. Should be able to toggle map/quest actions, too) OBJECT_PLAYER // A normal being diff --git a/src/state.cpp b/src/state.cpp index 75dae62e..a708911b 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -74,7 +74,7 @@ void State::update(ConnectionHandler &connectionHandler) MessageOut msg; msg.writeShort(SMSG_NEW_BEING); // msg.writeLong(OBJECT_PLAYER); // type - msg.writeLong((int)b2->get()); // id + msg.writeString(b2->get()->getName()); // Name msg.writeLong(b2->get()->getX());// x msg.writeLong(b2->get()->getY());// y |