From 2293b1c5ef0bb7378140bf73f1fef03a4504bdd2 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Thu, 12 Aug 2010 19:43:51 -0600 Subject: Replace most of Net::NpcHandler with events Reviewed-by: Chuck Miller --- src/being.cpp | 7 +-- src/gui/npcdialog.cpp | 40 +++++++++++------ src/gui/npcpostdialog.cpp | 10 ++--- src/net/manaserv/npchandler.cpp | 97 +++++++++++++++++++---------------------- src/net/manaserv/npchandler.h | 20 +++------ src/net/npchandler.h | 15 ------- src/net/tmwa/npchandler.cpp | 88 ++++++++++++++++++------------------- src/net/tmwa/npchandler.h | 21 +++------ 8 files changed, 134 insertions(+), 164 deletions(-) (limited to 'src') diff --git a/src/being.cpp b/src/being.cpp index 9be5986c..9c56dec3 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -26,7 +26,7 @@ #include "client.h" #include "configuration.h" #include "effectmanager.h" -#include "event.h" +#include "eventmanager.h" #include "graphics.h" #include "guild.h" #include "localplayer.h" @@ -48,7 +48,6 @@ #include "net/charhandler.h" #include "net/gamehandler.h" #include "net/net.h" -#include "net/npchandler.h" #include "net/playerhandler.h" #include "resources/beinginfo.h" @@ -1199,7 +1198,9 @@ bool Being::canTalk() void Being::talkTo() { - Net::getNpcHandler()->talk(mId); + Mana::Event event("doTalk"); + event.setInt("npcId", mId); + Mana::EventManager::trigger("NPC", event); } void Being::event(const std::string &channel, const Mana::Event &event) diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index d76e8418..a14e88eb 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -22,6 +22,7 @@ #include "gui/npcdialog.h" #include "configuration.h" +#include "eventmanager.h" #include "listener.h" #include "playerinfo.h" @@ -36,9 +37,6 @@ #include "gui/widgets/textbox.h" #include "gui/widgets/textfield.h" -#include "net/net.h" -#include "net/npchandler.h" - #include "utils/gettext.h" #include "utils/stringutils.h" @@ -49,6 +47,13 @@ #define CAPTION_CLOSE _("Close") #define CAPTION_SUBMIT _("Submit") +#define NpcEvent(name) Mana::Event event(name);\ +event.setInt("npcId", mNpcId); + +#define FireEvent(event) Mana::EventManager::trigger("NPC", event); + +#define FireEvent2(name) NpcEvent(name) FireEvent(event) + typedef std::map NpcDialogs; class NpcEventListener : public Mana::Listener @@ -217,27 +222,32 @@ void NpcDialog::action(const gcn::ActionEvent &event) if (mInputState == NPC_INPUT_LIST) { - int choice = 0; int selectedIndex = mItemList->getSelected(); if (selectedIndex >= (int) mItems.size() || selectedIndex < 0) - { return; - } - choice = selectedIndex + 1; + printText = mItems[selectedIndex]; - Net::getNpcHandler()->listInput(mNpcId, choice); + NpcEvent("doMenu") + event.setInt("choice", selectedIndex + 1); + FireEvent(event); } else if (mInputState == NPC_INPUT_STRING) { printText = mTextField->getText(); - Net::getNpcHandler()->stringInput(mNpcId, printText); + + NpcEvent("doStringInput") + event.setString("value", printText); + FireEvent(event); } else if (mInputState == NPC_INPUT_INTEGER) { printText = strprintf("%d", mIntField->getValue()); - Net::getNpcHandler()->integerInput(mNpcId, mIntField->getValue()); + + NpcEvent("doIntegerInput") + event.setInt("value", mIntField->getValue()); + FireEvent(event); } // addText will auto remove the input layout addText(strprintf("\n> \"%s\"\n", printText.c_str()), false); @@ -275,12 +285,12 @@ void NpcDialog::action(const gcn::ActionEvent &event) void NpcDialog::nextDialog() { - Net::getNpcHandler()->nextDialog(mNpcId); + FireEvent2("doNext") } void NpcDialog::closeDialog() { - Net::getNpcHandler()->closeDialog(mNpcId); + FireEvent2("doClose") close(); } @@ -550,7 +560,8 @@ void NpcEventListener::event(const std::string &channel, if (!dialog) { - Net::getNpcHandler()->nextDialog(id); + int mNpcId = id; + FireEvent2("doNext") return; } @@ -563,7 +574,8 @@ void NpcEventListener::event(const std::string &channel, if (!dialog) { - Net::getNpcHandler()->closeDialog(id); + int mNpcId = id; + FireEvent2("doClose") return; } diff --git a/src/gui/npcpostdialog.cpp b/src/gui/npcpostdialog.cpp index 6bc79017..c35f77a3 100644 --- a/src/gui/npcpostdialog.cpp +++ b/src/gui/npcpostdialog.cpp @@ -30,9 +30,6 @@ #include "gui/widgets/textfield.h" #include "gui/widgets/scrollarea.h" -#include "net/net.h" -#include "net/npchandler.h" - #include "utils/gettext.h" NpcPostDialog::DialogList NpcPostDialog::instances; @@ -100,8 +97,11 @@ void NpcPostDialog::action(const gcn::ActionEvent &event) } else { - Net::getNpcHandler()->sendLetter(mNpcId, mSender->getText(), - mText->getText()); + Mana::Event event("doSendLetter"); + event.setInt("npcId", mNpcId); + event.setString("recipient", mSender->getText()); + event.setString("text", mText->getText()); + Mana::EventManager::trigger("NPC", event); } setVisible(false); } diff --git a/src/net/manaserv/npchandler.cpp b/src/net/manaserv/npchandler.cpp index 09dccffe..9f63c837 100644 --- a/src/net/manaserv/npchandler.cpp +++ b/src/net/manaserv/npchandler.cpp @@ -51,6 +51,8 @@ NpcHandler::NpcHandler() }; handledMessages = _messages; npcHandler = this; + + Mana::EventManager::bind(this, "NPC"); } void NpcHandler::handleMessage(Net::MessageIn &msg) @@ -127,58 +129,51 @@ void NpcHandler::handleMessage(Net::MessageIn &msg) delete event; } -void NpcHandler::talk(int npcId) -{ - MessageOut msg(PGMSG_NPC_TALK); - msg.writeInt16(npcId); - gameServerConnection->send(msg); -} - -void NpcHandler::nextDialog(int npcId) -{ - MessageOut msg(PGMSG_NPC_TALK_NEXT); - msg.writeInt16(npcId); - gameServerConnection->send(msg); -} - -void NpcHandler::closeDialog(int npcId) -{ - MessageOut msg(PGMSG_NPC_TALK_NEXT); - msg.writeInt16(npcId); - gameServerConnection->send(msg); -} - -void NpcHandler::listInput(int npcId, int value) -{ - MessageOut msg(PGMSG_NPC_SELECT); - msg.writeInt16(npcId); - msg.writeInt8(value); - gameServerConnection->send(msg); -} - -void NpcHandler::integerInput(int npcId, int value) -{ - MessageOut msg(PGMSG_NPC_NUMBER); - msg.writeInt16(npcId); - msg.writeInt32(value); - gameServerConnection->send(msg); -} - -void NpcHandler::stringInput(int npcId, const std::string &value) -{ - MessageOut msg(PGMSG_NPC_STRING); - msg.writeInt16(npcId); - msg.writeString(value); - gameServerConnection->send(msg); -} - -void NpcHandler::sendLetter(int npcId, const std::string &recipient, - const std::string &text) +void NpcHandler::event(const std::string &channel, const Mana::Event &event) { - MessageOut msg(PGMSG_NPC_POST_SEND); - msg.writeString(recipient); - msg.writeString(text); - gameServerConnection->send(msg); + if (channel == "NPC") + { + if (event.getName() == "doTalk") + { + MessageOut msg(PGMSG_NPC_TALK); + msg.writeInt16(event.getInt("npcId")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doNext" || event.getName() == "doClose") + { + MessageOut msg(PGMSG_NPC_TALK_NEXT); + msg.writeInt16(event.getInt("npcId")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doMenu") + { + MessageOut msg(PGMSG_NPC_SELECT); + msg.writeInt16(event.getInt("npcId")); + msg.writeInt8(event.getInt("choice")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doIntegerInput") + { + MessageOut msg(PGMSG_NPC_NUMBER); + msg.writeInt16(event.getInt("npcId")); + msg.writeInt32(event.getInt("value")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doStringInput") + { + MessageOut msg(PGMSG_NPC_STRING); + msg.writeInt16(event.getInt("npcId")); + msg.writeString(event.getString("value")); + gameServerConnection->send(msg); + } + else if (event.getName() == "doSendLetter") + { + MessageOut msg(PGMSG_NPC_POST_SEND); + msg.writeString(event.getString("recipient")); + msg.writeString(event.getString("text")); + gameServerConnection->send(msg); + } + } } void NpcHandler::startShopping(int beingId) diff --git a/src/net/manaserv/npchandler.h b/src/net/manaserv/npchandler.h index d66c1a48..14e8a50a 100644 --- a/src/net/manaserv/npchandler.h +++ b/src/net/manaserv/npchandler.h @@ -22,6 +22,8 @@ #ifndef NET_MANASERV_NPCHANDLER_H #define NET_MANASERV_NPCHANDLER_H +#include "listener.h" + #include "net/npchandler.h" #include "net/manaserv/messagehandler.h" @@ -30,27 +32,15 @@ namespace ManaServ { -class NpcHandler : public MessageHandler, public Net::NpcHandler +class NpcHandler : public MessageHandler, public Net::NpcHandler, + public Mana::Listener { public: NpcHandler(); void handleMessage(Net::MessageIn &msg); - void talk(int npcId); - - void nextDialog(int npcId); - - void closeDialog(int npcId); - - void listInput(int npcId, int value); - - void integerInput(int npcId, int value); - - void stringInput(int npcId, const std::string &value); - - void sendLetter(int npcId, const std::string &recipient, - const std::string &text); + void event(const std::string &channel, const Mana::Event &event); void startShopping(int beingId); diff --git a/src/net/npchandler.h b/src/net/npchandler.h index 2a9cfab5..fb8ab7ec 100644 --- a/src/net/npchandler.h +++ b/src/net/npchandler.h @@ -31,21 +31,6 @@ class NpcHandler public: virtual ~NpcHandler() {} - virtual void talk(int npcId) = 0; - - virtual void nextDialog(int npcId) = 0; - - virtual void closeDialog(int npcId) = 0; - - virtual void listInput(int npcId, int value) = 0; - - virtual void integerInput(int npcId, int value) = 0; - - virtual void stringInput(int npcId, const std::string &value) = 0; - - virtual void sendLetter(int npcId, const std::string &recipient, - const std::string &text) = 0; - virtual void startShopping(int beingId) = 0; virtual void buy(int beingId) = 0; diff --git a/src/net/tmwa/npchandler.cpp b/src/net/tmwa/npchandler.cpp index 485a8ff7..b61dc520 100644 --- a/src/net/tmwa/npchandler.cpp +++ b/src/net/tmwa/npchandler.cpp @@ -68,6 +68,8 @@ NpcHandler::NpcHandler() }; handledMessages = _messages; npcHandler = this; + + Mana::EventManager::bind(this, "NPC"); } void NpcHandler::handleMessage(Net::MessageIn &msg) @@ -131,52 +133,48 @@ void NpcHandler::handleMessage(Net::MessageIn &msg) player_node->setAction(Being::STAND); } -void NpcHandler::talk(int npcId) -{ - MessageOut outMsg(CMSG_NPC_TALK); - outMsg.writeInt32(npcId); - outMsg.writeInt8(0); // Unused -} - -void NpcHandler::nextDialog(int npcId) -{ - MessageOut outMsg(CMSG_NPC_NEXT_REQUEST); - outMsg.writeInt32(npcId); -} - -void NpcHandler::closeDialog(int npcId) -{ - MessageOut outMsg(CMSG_NPC_CLOSE); - outMsg.writeInt32(npcId); -} - -void NpcHandler::listInput(int npcId, int value) -{ - MessageOut outMsg(CMSG_NPC_LIST_CHOICE); - outMsg.writeInt32(npcId); - outMsg.writeInt8(value); -} - -void NpcHandler::integerInput(int npcId, int value) -{ - MessageOut outMsg(CMSG_NPC_INT_RESPONSE); - outMsg.writeInt32(npcId); - outMsg.writeInt32(value); -} - -void NpcHandler::stringInput(int npcId, const std::string &value) +void NpcHandler::event(const std::string &channel, const Mana::Event &event) { - MessageOut outMsg(CMSG_NPC_STR_RESPONSE); - outMsg.writeInt16(value.length() + 9); - outMsg.writeInt32(npcId); - outMsg.writeString(value, value.length()); - outMsg.writeInt8(0); // Prevent problems with string reading -} - -void NpcHandler::sendLetter(int npcId, const std::string &recipient, - const std::string &text) -{ - // TODO + if (channel == "NPC") + { + if (event.getName() == "doTalk") + { + MessageOut outMsg(CMSG_NPC_TALK); + outMsg.writeInt32(event.getInt("npcId")); + outMsg.writeInt8(0); // Unused + } + else if (event.getName() == "doNext") + { + MessageOut outMsg(CMSG_NPC_NEXT_REQUEST); + outMsg.writeInt32(event.getInt("npcId")); + } + else if (event.getName() == "doClose") + { + MessageOut outMsg(CMSG_NPC_CLOSE); + outMsg.writeInt32(event.getInt("npcId")); + } + else if (event.getName() == "doMenu") + { + MessageOut outMsg(CMSG_NPC_LIST_CHOICE); + outMsg.writeInt32(event.getInt("npcId")); + outMsg.writeInt8(event.getInt("choice")); + } + else if (event.getName() == "doIntegerInput") + { + MessageOut outMsg(CMSG_NPC_INT_RESPONSE); + outMsg.writeInt32(event.getInt("npcId")); + outMsg.writeInt32(event.getInt("value")); + } + else if (event.getName() == "doStringInput") + { + const std::string &value = event.getString("value"); + MessageOut outMsg(CMSG_NPC_STR_RESPONSE); + outMsg.writeInt16(value.length() + 9); + outMsg.writeInt32(event.getInt("npcId")); + outMsg.writeString(value, value.length()); + outMsg.writeInt8(0); // Prevent problems with string reading + } + } } void NpcHandler::startShopping(int beingId) diff --git a/src/net/tmwa/npchandler.h b/src/net/tmwa/npchandler.h index 515f62df..93c9f6ec 100644 --- a/src/net/tmwa/npchandler.h +++ b/src/net/tmwa/npchandler.h @@ -22,7 +22,8 @@ #ifndef NET_TA_NPCHANDLER_H #define NET_TA_NPCHANDLER_H -#include "net/net.h" +#include "listener.h" + #include "net/npchandler.h" #include "net/tmwa/messagehandler.h" @@ -31,27 +32,15 @@ namespace TmwAthena { -class NpcHandler : public MessageHandler, public Net::NpcHandler +class NpcHandler : public MessageHandler, public Net::NpcHandler, + public Mana::Listener { public: NpcHandler(); void handleMessage(Net::MessageIn &msg); - void talk(int npcId); - - void nextDialog(int npcId); - - void closeDialog(int npcId); - - void listInput(int npcId, int value); - - void integerInput(int npcId, int value); - - void stringInput(int npcId, const std::string &value); - - void sendLetter(int npcId, const std::string &recipient, - const std::string &text); + void event(const std::string &channel, const Mana::Event &event); void startShopping(int beingId); -- cgit v1.2.3-70-g09d2