From 12bf548533867a2eb3a1c354b778ef7f9157322a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 26 Aug 2011 16:57:05 +0300 Subject: Basic support for guild bot integration. Not working context menu actions, chat commands, auto complete. --- manaplus.cbp | 4 + src/CMakeLists.txt | 4 + src/Makefile.am | 4 + src/being.cpp | 6 +- src/client.cpp | 16 +++ src/client.h | 2 + src/game.cpp | 24 ++-- src/gui/chatwindow.cpp | 6 +- src/gui/popupmenu.cpp | 77 ++++++++--- src/gui/setup_other.cpp | 6 + src/gui/socialwindow.cpp | 111 +++++++++++++++- src/gui/widgets/guildtab.cpp | 127 ++++++++++++++++++ src/gui/widgets/guildtab.h | 52 ++++++++ src/guild.cpp | 6 +- src/guild.h | 9 +- src/guildmanager.cpp | 309 +++++++++++++++++++++++++++++++++++++++++++ src/guildmanager.h | 81 ++++++++++++ src/net/ea/chathandler.cpp | 7 + src/net/ea/guildhandler.cpp | 1 + src/utils/stringutils.cpp | 54 ++++++++ src/utils/stringutils.h | 8 ++ 21 files changed, 878 insertions(+), 36 deletions(-) create mode 100644 src/gui/widgets/guildtab.cpp create mode 100644 src/gui/widgets/guildtab.h create mode 100644 src/guildmanager.cpp create mode 100644 src/guildmanager.h diff --git a/manaplus.cbp b/manaplus.cbp index 8ed18dbed..d05cccd3b 100644 --- a/manaplus.cbp +++ b/manaplus.cbp @@ -310,6 +310,8 @@ + + @@ -394,6 +396,8 @@ + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 413a72f65..7f59135b8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -137,6 +137,8 @@ SET(SRCS gui/widgets/emoteshortcutcontainer.h gui/widgets/flowcontainer.cpp gui/widgets/flowcontainer.h + gui/widgets/guildtab.cpp + gui/widgets/guildtab.h gui/widgets/horizontcontainer.cpp gui/widgets/horizontcontainer.h gui/widgets/icon.cpp @@ -508,6 +510,8 @@ SET(SRCS guichanfwd.h guild.cpp guild.h + guildmanager.cpp + guildmanager.h imageparticle.cpp imageparticle.h imagesprite.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 57bb4e620..7d0c1634b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -140,6 +140,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/widgets/emoteshortcutcontainer.h \ gui/widgets/flowcontainer.cpp \ gui/widgets/flowcontainer.h \ + gui/widgets/guildtab.cpp \ + gui/widgets/guildtab.h \ gui/widgets/horizontcontainer.cpp \ gui/widgets/horizontcontainer.h \ gui/widgets/icon.cpp \ @@ -515,6 +517,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ guichanfwd.h \ guild.cpp \ guild.h \ + guildmanager.cpp \ + guildmanager.h \ imageparticle.cpp \ imageparticle.h \ imagesprite.cpp \ diff --git a/src/being.cpp b/src/being.cpp index cc88d8f6e..1924a8944 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -871,8 +871,12 @@ void Being::updateGuild() return; } if (guild->getMember(getName())) + { setGuild(guild); - updateColors(); + if (!guild->getName().empty()) + mGuildName = guild->getName(); + updateColors(); + } } void Being::setGuild(Guild *guild) diff --git a/src/client.cpp b/src/client.cpp index 0e871ed91..385af99ad 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -30,6 +30,7 @@ #include "event.h" #include "game.h" #include "guild.h" +#include "guildmanager.h" #include "graphicsvertexes.h" #include "itemshortcut.h" #include "keyboardconfig.h" @@ -793,6 +794,10 @@ int Client::exec() if (mumbleManager) mumbleManager->setServer(mCurrentServer.hostname); + if (!guildManager) + guildManager = new GuildManager(); + guildManager->init(); + if (!mConfigAutoSaved) { mConfigAutoSaved = true; @@ -2180,3 +2185,14 @@ void Client::closeDialogs() NpcDialog::closeAll(); SellDialog::closeAll(); } + +bool Client::isTmw() +{ + if (getServerName() == "server.themanaworld.org" + || Client::getServerName() == "themanaworld.org" + || Client::getServerName() == "81.161.192.4") + { + return true; + } + return false; +} diff --git a/src/client.h b/src/client.h index 986e06cfb..3edf12674 100644 --- a/src/client.h +++ b/src/client.h @@ -260,6 +260,8 @@ public: static void setFramerate(int fpsLimit); + static bool isTmw(); + void optionChanged(const std::string &name); void action(const gcn::ActionEvent &event); diff --git a/src/game.cpp b/src/game.cpp index 82f919976..c933378e6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -31,14 +31,13 @@ #include "client.h" #include "commandhandler.h" #include "configuration.h" +#include "dropshortcut.h" #include "effectmanager.h" -#include "event.h" -#include "spellmanager.h" #include "emoteshortcut.h" +#include "event.h" +#include "guildmanager.h" #include "graphics.h" #include "itemshortcut.h" -#include "dropshortcut.h" -#include "spellshortcut.h" #include "joystick.h" #include "keyboardconfig.h" #include "localplayer.h" @@ -47,6 +46,8 @@ #include "particle.h" #include "playerrelations.h" #include "sound.h" +#include "spellmanager.h" +#include "spellshortcut.h" #include "gui/botcheckerwindow.h" #include "gui/buyselldialog.h" @@ -156,6 +157,7 @@ Particle *particleEngine = NULL; EffectManager *effectManager = NULL; SpellManager *spellManager = NULL; Viewport *viewport = NULL; /**< Viewport on the map. */ +GuildManager *guildManager = NULL; ChatTab *localChatTab = NULL; ChatTab *debugChatTab = NULL; @@ -175,6 +177,8 @@ static void initEngines() commandHandler = new CommandHandler; channelManager = new ChannelManager; effectManager = new EffectManager; + if (!guildManager) + guildManager = new GuildManager; particleEngine = new Particle(NULL); particleEngine->setupEngine(); @@ -393,6 +397,9 @@ Game::Game(): setupWindow->setInGame(true); clearKeysArray(); + if (guildManager && guildManager->getEnableGuildBot()) + guildManager->requestGuildInfo(); + Mana::Event::trigger(CHANNEL_GAME, Mana::Event(EVENT_CONSTRUCTED)); } @@ -411,16 +418,17 @@ Game::~Game() del_0(actorSpriteManager) if (Client::getState() != STATE_CHANGE_MAP) del_0(player_node) + del_0(guildManager) del_0(channelManager) del_0(commandHandler) - del_0(effectManager); + del_0(effectManager) del_0(joystick) del_0(particleEngine) del_0(viewport) del_0(mCurrentMap) - del_0(spellManager); - del_0(spellShortcut); - del_0(mumbleManager); + del_0(spellManager) + del_0(spellShortcut) + del_0(mumbleManager) Being::clearCache(); diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp index 8896ca028..9c75320a6 100644 --- a/src/gui/chatwindow.cpp +++ b/src/gui/chatwindow.cpp @@ -1459,12 +1459,8 @@ void ChatWindow::saveState() std::string ChatWindow::doReplace(const std::string &msg) { - if (Client::getServerName() == "server.themanaworld.org" - || Client::getServerName() == "themanaworld.org" - || Client::getServerName() == "81.161.192.4") - { + if (Client::isTmw()) return msg; - } std::string str = msg; replaceSpecialChars(str); diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp index 61769bc7e..a3041fcf7 100644 --- a/src/gui/popupmenu.cpp +++ b/src/gui/popupmenu.cpp @@ -27,6 +27,7 @@ #include "being.h" #include "dropshortcut.h" #include "guild.h" +#include "guildmanager.h" #include "flooritem.h" #include "graphics.h" #include "item.h" @@ -224,14 +225,22 @@ void PopupMenu::showPopup(int x, int y, Being *being) { mBrowserBox->addRow(strprintf( "@@guild-kick|%s@@", _("Kick from guild"))); - mBrowserBox->addRow(strprintf("@@guild-pos|%s >@@", - _("Change pos in guild"))); + if (guild2->getServerGuild()) + { + mBrowserBox->addRow(strprintf( + "@@guild-pos|%s >@@", + _("Change pos in guild"))); + } } } else { - mBrowserBox->addRow(strprintf( - "@@guild|%s@@", _("Invite to guild"))); + if (guild2->getServerGuild() + || (guildManager && guildManager->havePower())) + { + mBrowserBox->addRow(strprintf( + "@@guild|%s@@", _("Invite to guild"))); + } } } @@ -435,15 +444,26 @@ void PopupMenu::showPlayerPopup(int x, int y, std::string nick) { if (guild2->getMember(mNick)) { - mBrowserBox->addRow(strprintf( - "@@guild-kick|%s@@", _("Kick from guild"))); - mBrowserBox->addRow(strprintf( - "@@guild-pos|%s >@@", _("Change pos in guild"))); + if (guild2->getServerGuild() || (guildManager + && guildManager->havePower())) + { + mBrowserBox->addRow(strprintf( + "@@guild-kick|%s@@", _("Kick from guild"))); + } + if (guild2->getServerGuild()) + { + mBrowserBox->addRow(strprintf( + "@@guild-pos|%s >@@", _("Change pos in guild"))); + } } else { - mBrowserBox->addRow(strprintf( - "@@guild|%s@@", _("Invite to guild"))); + if (guild2->getServerGuild() || (guildManager + && guildManager->havePower())) + { + mBrowserBox->addRow(strprintf( + "@@guild|%s@@", _("Invite to guild"))); + } } } @@ -725,16 +745,27 @@ void PopupMenu::showChatPopup(int x, int y, ChatTab *tab) { if (guild1->getId() == guild2->getId()) { - mBrowserBox->addRow(strprintf( - "@@guild-kick|%s@@", _("Kick from guild"))); - mBrowserBox->addRow(strprintf( - "@@guild-pos|%s >@@", _("Change pos in guild"))); + if (guild2->getServerGuild() || (guildManager + && guildManager->havePower())) + { + mBrowserBox->addRow(strprintf( + "@@guild-kick|%s@@", _("Kick from guild"))); + } + if (guild2->getServerGuild()) + { + mBrowserBox->addRow(strprintf("@@guild-pos|%s >@@", + _("Change pos in guild"))); + } } } else { - mBrowserBox->addRow(strprintf( - "@@guild|%s@@", _("Invite to guild"))); + if (guild2->getServerGuild() || (guildManager + && guildManager->havePower())) + { + mBrowserBox->addRow(strprintf( + "@@guild|%s@@", _("Invite to guild"))); + } } } } @@ -897,7 +928,12 @@ void PopupMenu::handleLink(const std::string &link, { const Guild *guild = player_node->getGuild(); if (guild) - Net::getGuildHandler()->invite(guild->getId(), mNick); + { + if (guild->getServerGuild()) + Net::getGuildHandler()->invite(guild->getId(), mNick); + else if (guildManager) + guildManager->invite(mNick); + } } } else if (link == "nuke" && being) @@ -1263,7 +1299,12 @@ void PopupMenu::handleLink(const std::string &link, { const Guild *guild = player_node->getGuild(); if (guild) - Net::getGuildHandler()->kick(guild->getMember(mNick)); + { + if (guild->getServerGuild()) + Net::getGuildHandler()->kick(guild->getMember(mNick)); + else if (guildManager) + guildManager->kick(mNick); + } } } else if (link == "enable highlight" && mTab) diff --git a/src/gui/setup_other.cpp b/src/gui/setup_other.cpp index b405f5045..05ec9d672 100644 --- a/src/gui/setup_other.cpp +++ b/src/gui/setup_other.cpp @@ -145,6 +145,12 @@ Setup_Other::Setup_Other() this, "logNpcInGuiEvent"); + new SetupItemLabel(_("Bots support"), "", this); + + new SetupItemCheckBox(_("Enable guild bot support and disable native " + "guild support"), "", "enableGuildBot", this, + "enableGuildBotEvent", false); + new SetupItemLabel(_("Other"), "", this); new SetupItemCheckBox(_("Enable server side attack"), "", diff --git a/src/gui/socialwindow.cpp b/src/gui/socialwindow.cpp index e3aa69b30..aa26fa051 100644 --- a/src/gui/socialwindow.cpp +++ b/src/gui/socialwindow.cpp @@ -206,6 +206,109 @@ private: Guild *mGuild; }; +class GuildTab2 : public SocialTab, public gcn::ActionListener +{ +public: + GuildTab2(Guild *guild): + mGuild(guild) + { + setCaption(_("Guild")); + + setTabColor(&Theme::getThemeColor(Theme::GUILD_SOCIAL_TAB)); + + mList = new AvatarListBox(guild); + mScroll = new ScrollArea(mList); + + mScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_AUTO); + mScroll->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS); + } + + ~GuildTab2() + { + delete mList; + mList = 0; + delete mScroll; + mScroll = 0; + } + + void action(const gcn::ActionEvent &event) + { +/* + if (event.getId() == "do invite") + { + std::string name = mInviteDialog->getText(); + Net::getGuildHandler()->invite(mGuild->getId(), name); + + if (localChatTab) + { + localChatTab->chatLog(strprintf( + _("Invited user %s to guild %s."), + name.c_str(), mGuild->getName().c_str()), BY_SERVER); + } + mInviteDialog = 0; + } + else if (event.getId() == "~do invite") + { + mInviteDialog = 0; + } + else if (event.getId() == "yes") + { + Net::getGuildHandler()->leave(mGuild->getId()); + if (localChatTab) + { + localChatTab->chatLog(strprintf(_("Guild %s quit requested."), + mGuild->getName().c_str()), BY_SERVER); + } + mConfirmDialog = 0; + } + else if (event.getId() == "~yes") + { + mConfirmDialog = 0; + } +*/ + } + + void updateList() + { + } + + void updateAvatar(std::string name A_UNUSED) + { + } + + void resetDamage(std::string name A_UNUSED) + { + } + +protected: + void invite() + { +/* + mInviteDialog = new TextDialog(_("Member Invite to Guild"), + strprintf(_("Who would you like to invite to guild %s?"), + mGuild->getName().c_str()), + socialWindow); + mInviteDialog->setActionEventId("do invite"); + mInviteDialog->addActionListener(this); +*/ + } + + void leave() + { +/* + mConfirmDialog = new ConfirmDialog(_("Leave Guild?"), + strprintf(_("Are you sure you want to leave guild %s?"), + mGuild->getName().c_str()), + socialWindow); + + mConfirmDialog->addActionListener(this); +*/ + } + +private: + Guild *mGuild; +}; + class PartyTab : public SocialTab, public gcn::ActionListener { public: @@ -1106,9 +1209,13 @@ bool SocialWindow::addTab(Guild *guild) if (mGuilds.find(guild) != mGuilds.end()) return false; - GuildTab *tab = new GuildTab(guild); - mGuilds[guild] = tab; + SocialTab *tab = 0; + if (guild->getServerGuild()) + tab = new GuildTab(guild); + else + tab = new GuildTab2(guild); + mGuilds[guild] = tab; mTabs->addTab(tab, tab->mScroll); updateButtons(); diff --git a/src/gui/widgets/guildtab.cpp b/src/gui/widgets/guildtab.cpp new file mode 100644 index 000000000..d9f61cb43 --- /dev/null +++ b/src/gui/widgets/guildtab.cpp @@ -0,0 +1,127 @@ +/* + * The ManaPlus Client + * Copyright (C) 2008-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "gui/widgets/guildtab.h" + +#include "chatlogger.h" +#include "commandhandler.h" +#include "guild.h" +#include "guildmanager.h" +#include "localplayer.h" + +#include "gui/theme.h" + +#include "resources/iteminfo.h" +#include "resources/itemdb.h" + +#include "utils/dtor.h" +#include "utils/gettext.h" +#include "utils/stringutils.h" + +#include "debug.h" + +GuildTab::GuildTab() : + ChatTab(_("Guild")) +{ + setTabColor(&Theme::getThemeColor(Theme::GUILD_CHAT_TAB)); +} + +GuildTab::~GuildTab() +{ +} + +bool GuildTab::handleCommand(const std::string &type, const std::string &args) +{ + if (type == "help") + { + if (args == "invite") + { + chatLog(_("Command: /invite ")); + chatLog(_("This command invites to the guild you're in.")); + chatLog(_("If the has spaces in it, enclose it in " + "double quotes (\").")); + } + else if (args == "leave") + { + chatLog(_("Command: /leave")); + chatLog(_("This command causes the player to leave the guild.")); + } + else + return false; + } + else if (type == "invite" && guildManager) + { + guildManager->invite(args); + } + else if (type == "leave" && guildManager) + { + guildManager->leave(); + } + else if (type == "kick" && guildManager) + { + guildManager->kick(args); + } + else if (type == "notice" && guildManager) + { + guildManager->notice(args); + } + else + { + return false; + } + + return false; +} + +void GuildTab::handleInput(const std::string &msg) +{ + if (!guildManager) + return; + + if (chatWindow) + guildManager->chat(chatWindow->doReplace(msg)); + else + guildManager->chat(msg); +} + +void GuildTab::showHelp() +{ + chatLog(_("/help > Display this help.")); + chatLog(_("/invite > Invite a player to your guild")); + chatLog(_("/leave > Leave the guild you are in")); + chatLog(_("/kick > Kick some one from the guild you are in")); +} + +void GuildTab::getAutoCompleteList(std::vector &names) const +{ + if (!guildManager) + return; + + guildManager->getNames(names); + names.push_back("/notice "); +} + +void GuildTab::saveToLogFile(std::string &msg) +{ + if (chatLogger) + chatLogger->log("#Guild", msg); +} diff --git a/src/gui/widgets/guildtab.h b/src/gui/widgets/guildtab.h new file mode 100644 index 000000000..a7f0aa766 --- /dev/null +++ b/src/gui/widgets/guildtab.h @@ -0,0 +1,52 @@ +/* + * The ManaPlus Client + * Copyright (C) 2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef GUI_GUILDTAB_H +#define GUI_GUILDTAB_H + +#include "gui/widgets/chattab.h" + +/** + * A tab for a guild chat channel. + */ +class GuildTab : public ChatTab +{ + public: + GuildTab(); + + ~GuildTab(); + + bool handleCommand(const std::string &type, const std::string &args); + + void showHelp(); + + void saveToLogFile(std::string &msg); + + int getType() const { return ChatTab::TAB_GUILD; } + + protected: + void handleInput(const std::string &msg); + + void getAutoCompleteList(std::vector &names) const; +}; + +#endif diff --git a/src/guild.cpp b/src/guild.cpp index b84602858..e36b68863 100644 --- a/src/guild.cpp +++ b/src/guild.cpp @@ -36,6 +36,9 @@ class SortGuildFunctor if (m1->getPos() != m2->getPos()) return m1->getPos() < m2->getPos(); +// if (m1->getOnline() != m2->getOnline()) +// return m1->getOnline() > m2->getOnline(); + return m1->getName() < m2->getName(); } } guildSorter; @@ -66,7 +69,8 @@ Guild::GuildMap Guild::guilds; Guild::Guild(short id): mId(id), mCanInviteUsers(false), - mEmblemId(0) + mEmblemId(0), + mServerGuild(true) { guilds[id] = this; } diff --git a/src/guild.h b/src/guild.h index 7986eb5d3..62135e9ad 100644 --- a/src/guild.h +++ b/src/guild.h @@ -178,11 +178,17 @@ public: void setEmblemId(int id) { mEmblemId = id; } - int getEmblemId() + int getEmblemId() const { return mEmblemId; } static void clearGuilds(); + void setServerGuild(bool b) + { mServerGuild = b; } + + bool getServerGuild() const + { return mServerGuild; } + private: typedef std::map GuildMap; static GuildMap guilds; @@ -199,6 +205,7 @@ private: bool mCanInviteUsers; int mEmblemId; PositionsMap mPositions; + bool mServerGuild; }; #endif // GUILD_H diff --git a/src/guildmanager.cpp b/src/guildmanager.cpp new file mode 100644 index 000000000..ec9de2506 --- /dev/null +++ b/src/guildmanager.cpp @@ -0,0 +1,309 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "guildmanager.h" + +#include "actorspritemanager.h" +#include "client.h" +#include "configuration.h" +#include "guild.h" +#include "localplayer.h" + +#include "gui/socialwindow.h" + +#include "gui/widgets/guildtab.h" + +#include "net/chathandler.h" +#include "net/net.h" + +#include "utils/stringutils.h" + +#include "debug.h" + + +GuildManager::GuildManager() : + mEnableGuildBot(false), + mGotInfo(false), + mGotName(false), + mHavePower(false), + mTab(0), + mRequest(false) +{ +} + +GuildManager::~GuildManager() +{ + mTab = 0; +} + +void GuildManager::init() +{ + int val = serverConfig.getValue("enableGuildBot", -1); + if (val == -1) + { + if (Client::isTmw()) + val = 1; + else + val = 0; + serverConfig.setValue("enableGuildBot", val); + } + mEnableGuildBot = val; +} + +void GuildManager::send(std::string msg) +{ + Net::getChatHandler()->privateMessage("guild", msg); +} + +void GuildManager::chat(std::string msg) +{ + if (!player_node || !mTab) + return; + + Net::getChatHandler()->privateMessage("guild", msg); + mTab->chatLog(player_node->getName(), msg); +} + +void GuildManager::getNames(std::vector &names) +{ +} + +void GuildManager::requestGuildInfo() +{ + if (mRequest) + return; + + if (!mGotName) + { + send("!info"); + mRequest = true; + } + else if (!mGotInfo) + { + send("!getonlineinfo"); + mRequest = true; + } +} + +void GuildManager::updateList() +{ + Guild *guild = Guild::getGuild(1); + if (guild) + { + logger->log("filling player"); + guild->setServerGuild(false); + std::vector::iterator it = mTempList.begin(); + std::vector::iterator it_end = mTempList.end(); + int i = 0; + while (it != it_end) + { + std::string name = *it; + if (name.size() > 1) + { + int status = atoi(name.substr(name.size() - 1).c_str()); + name = name.substr(0, name.size() - 1); + GuildMember *m = guild->addMember(i, 0, name); + if (m) + { + m->setOnline(status & 1); + m->setGender(GENDER_UNSPECIFIED); + } + } + ++ it; + i ++; + } + guild->sort(); + if (actorSpriteManager) + actorSpriteManager->updatePlayerGuild(); + } + mTempList.clear(); + mGotInfo = true; + if (!mTab) + { + mTab = new GuildTab(); + mTab->loadFromLogFile("#Guild"); + if (player_node) + player_node->addGuild(guild); + } +} + +bool GuildManager::processGuildMessage(std::string msg) +{ + bool res = process(msg); + + if (!mRequest) + requestGuildInfo(); + + return res; +} + +bool GuildManager::process(std::string msg) +{ + if (msg.size() > 4 && msg[0] == '#' && msg[1] == '#') + msg = msg.substr(3); + + Guild *guild = Guild::getGuild(1); + if (!guild) + return false; + + guild->setServerGuild(false); + + if (findCutLast(msg, " is now Offline.")) + { + if (msg.size() < 4) + return false; + if (msg[0] == '#' && msg[1] == '#') + msg = msg.substr(3); + + GuildMember *m = guild->getMember(msg); + if (m) + m->setOnline(false); + mRequest = false; + return true; + } + else if (findCutLast(msg, " is now Online.")) + { + if (msg.size() < 4) + return false; + if (msg[0] == '#' && msg[1] == '#') + msg = msg.substr(3); + GuildMember *m = guild->getMember(msg); + if (m) + m->setOnline(true); + mRequest = false; + return true; + } + else if (findCutFirst(msg, "Welcome to the ")) + { + logger->log("welcome message: %s", msg.c_str()); + int pos = msg.find("! ("); + if (pos == (int)std::string::npos) + return false; + msg = msg.substr(0, pos); + guild->setName(msg); + if (player_node) + player_node->setGuildName(msg); + mGotName = true; + mRequest = false; + return true; + } + else if (findCutFirst(msg, "Player name: ")) + { + int pos = msg.find("Access Level: "); + if (pos == (int)std::string::npos) + return false; + + msg = msg.substr(pos); + if (!findCutFirst(msg, "Access Level: ")) + return false; + + pos = msg.find(", Guild:"); + if (pos == (int)std::string::npos) + return false; + + int level = atoi(msg.substr(0, pos).c_str()); + if (level >= 10) + mHavePower = true; + else + mHavePower = false; + + msg = msg.substr(pos + strlen(", Guild:")); + pos = msg.find(", No. Of Online Players: "); + if (pos == (int)std::string::npos) + return false; + + msg = msg.substr(0, pos); +// logger->log("guild name: %s", msg.c_str()); + + guild->setName(msg); + if (player_node) + player_node->setGuildName(msg); + mGotName = true; + mRequest = false; + return true; + } + else if (findCutFirst(msg, "OL#")) + { + logger->log("OL"); + mTempList.clear(); + splitToStringVector(mTempList, msg, '#'); + if (msg.size() < 1 || msg[msg.size() - 1] != '#') + updateList(); + mRequest = false; + return true; + } + else if (findCutFirst(msg, "oL#")) + { + logger->log("oL"); + splitToStringVector(mTempList, msg, '#'); + if (msg.size() < 1 || msg[msg.size() - 1] != '#') + updateList(); + mRequest = false; + return true; + } + else if (msg == "You are currently not in a guild. For more information " + "or to discuss the possibility of adding you own guild " + "please contact Jero.") + { + mRequest = true; + return true; + } + else + { + if (mTab) + { + std::string::size_type pos = msg.find(": ", 0); + if (pos != std::string::npos) + { + std::string sender_name = ((pos == std::string::npos) + ? "" : msg.substr(0, pos)); + + msg.erase(0, pos + 2); + if (msg.size() > 3 && msg[0] == '#' && msg[1] == '#') + msg.erase(0, 3); + + trim(msg); + mTab->chatLog(sender_name, msg); + } + else + { + mTab->chatLog(msg); + } + return true; + } + } + return false; +} + +void GuildManager::kick(std::string msg) +{ +} + +void GuildManager::invite(std::string msg) +{ +} + +void GuildManager::leave() +{ +} + +void GuildManager::notice(std::string msg) +{ +} diff --git a/src/guildmanager.h b/src/guildmanager.h new file mode 100644 index 000000000..f02368ab6 --- /dev/null +++ b/src/guildmanager.h @@ -0,0 +1,81 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef GUILDMANAGER_H +#define GUILDMANAGER_H + +#include "utils/dtor.h" + +#include +#include +#include + +class GuildTab; + +class GuildManager +{ + public: + GuildManager(); + + ~GuildManager(); + + void init(); + + void chat(std::string msg); + + void send(std::string msg); + + bool processGuildMessage(std::string msg); + + void getNames(std::vector &names); + + void requestGuildInfo(); + + void updateList(); + + bool getEnableGuildBot() + { return mEnableGuildBot; } + + void kick(std::string msg); + + void invite(std::string msg); + + void leave(); + + void notice(std::string msg); + + bool havePower() + { return mHavePower; } + + private: + bool process(std::string msg); + + bool mEnableGuildBot; + bool mGotInfo; + bool mGotName; + bool mHavePower; + std::vector mTempList; + GuildTab *mTab; + bool mRequest; +}; + +extern GuildManager *guildManager; + +#endif // GUILDMANAGER_H diff --git a/src/net/ea/chathandler.cpp b/src/net/ea/chathandler.cpp index c188a2204..e430dfc0f 100644 --- a/src/net/ea/chathandler.cpp +++ b/src/net/ea/chathandler.cpp @@ -25,6 +25,7 @@ #include "actorspritemanager.h" #include "being.h" #include "configuration.h" +#include "guildmanager.h" #include "localplayer.h" #include "playerrelations.h" #include "logger.h" @@ -161,6 +162,12 @@ void ChatHandler::processWhisper(Net::MessageIn &msg) if (nick != "Server") { + if (guildManager && guildManager->getEnableGuildBot() + && nick == "guild" && guildManager->processGuildMessage(chatMsg)) + { + return; + } + if (player_relations.hasPermission( nick, PlayerRelation::WHISPER)) { diff --git a/src/net/ea/guildhandler.cpp b/src/net/ea/guildhandler.cpp index 0210a76ff..2accb1f1d 100644 --- a/src/net/ea/guildhandler.cpp +++ b/src/net/ea/guildhandler.cpp @@ -125,6 +125,7 @@ void GuildHandler::processGuildPositionInfo(Net::MessageIn &msg) if (!guildTab && chatWindow) { guildTab = new GuildTab(); + guildTab->loadFromLogFile("#Guild"); if (player_node) player_node->addGuild(taGuild); memberList(guildId); diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 0243c4315..317d88f2d 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -524,3 +524,57 @@ void deleteCharLeft(std::string &str, unsigned *pos) break; } } + +bool findLast(std::string &str1, std::string str2) +{ + const unsigned s1 = str1.size(); + const unsigned s2 = str2.size(); + if (s1 < s2) + return false; + std::string tmp = str1.substr(s1 - s2); + if (tmp == str2) + return true; + return false; +} + +bool findFirst(std::string &str1, std::string str2) +{ + const unsigned s1 = str1.size(); + const unsigned s2 = str2.size(); + if (s1 < s2) + return false; + std::string tmp = str1.substr(0, s2); + if (tmp == str2) + return true; + return false; +} + +bool findCutLast(std::string &str1, std::string str2) +{ + const unsigned s1 = str1.size(); + const unsigned s2 = str2.size(); + if (s1 < s2) + return false; + std::string tmp = str1.substr(s1 - s2); + if (tmp == str2) + { + str1 = str1.substr(0, s1 - s2); + return true; + } + return false; +} + +bool findCutFirst(std::string &str1, std::string str2) +{ + const unsigned s1 = str1.size(); + const unsigned s2 = str2.size(); + if (s1 < s2) + return false; + std::string tmp = str1.substr(0, s2); + if (tmp == str2) + { + str1 = str1.substr(s2); + return true; + } + return false; +} diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 4fe6369e9..25e038a89 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -193,4 +193,12 @@ std::string stringToHexPath(const std::string &str); void deleteCharLeft(std::string &str, unsigned *pos); +bool findLast(std::string &str1, std::string str2); + +bool findFirst(std::string &str1, std::string str2); + +bool findCutLast(std::string &str1, std::string str2); + +bool findCutFirst(std::string &str1, std::string str2); + #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-70-g09d2