ManaPlus
chatutils.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "utils/chatutils.h"
23 
24 #include "actormanager.h"
25 #include "party.h"
26 
27 #include "being/localplayer.h"
28 
30 
31 #include "net/chathandler.h"
32 #include "net/clanhandler.h"
33 #include "net/guildhandler.h"
34 #include "net/partyhandler.h"
35 
36 #ifdef TMWA_SUPPORT
37 #include "net/net.h"
38 
39 #include "net/tmwa/guildmanager.h"
40 #endif // TMWA_SUPPORT
41 
42 #include "utils/foreach.h"
43 #include "utils/stringutils.h"
44 
45 #include "debug.h"
46 
47 void outStringNormal(ChatTab *const tab,
48  const std::string &str,
49  const std::string &def)
50 {
51  if (localPlayer == nullptr)
52  return;
53 
54  if (tab == nullptr)
55  {
56  chatHandler->talk(str);
57  return;
58  }
59 
60  switch (tab->getType())
61  {
62  case ChatTabType::CLAN:
63  {
64  clanHandler->chat(str);
65  break;
66  }
67  case ChatTabType::PARTY:
68  {
69  partyHandler->chat(str);
70  break;
71  }
72  case ChatTabType::GUILD:
73  {
74  const Guild *const guild = localPlayer->getGuild();
75  if (guild != nullptr)
76  {
77 #ifdef TMWA_SUPPORT
78  if (guild->getServerGuild())
79  {
81  return;
82  guildHandler->chat(str);
83  }
84  else if (guildManager != nullptr)
85  {
86  guildManager->chat(str);
87  }
88 #else // TMWA_SUPPORT
89 
90  if (guild->getServerGuild())
91  guildHandler->chat(str);
92 #endif // TMWA_SUPPORT
93  }
94  break;
95  }
97  {
98  const WhisperTab *const whisper
99  = static_cast<const WhisperTab *>(tab);
100  tab->chatLog(localPlayer->getName(), str);
101  chatHandler->privateMessage(whisper->getNick(), str);
102  break;
103  }
104  case ChatTabType::DEBUG:
105  case ChatTabType::BATTLE:
106  break;
107  default:
109  case ChatTabType::INPUT:
110  case ChatTabType::TRADE:
111  case ChatTabType::LANG:
112  case ChatTabType::GM:
114  chatHandler->talk(def);
115  break;
116  }
117 }
118 
119 void replaceVars(std::string &str)
120 {
121  if ((localPlayer == nullptr) || (actorManager == nullptr))
122  return;
123 
124  if (str.find("<PLAYER>") != std::string::npos)
125  {
126  const Being *target = localPlayer->getTarget();
127  if ((target == nullptr) || target->getType() != ActorType::Player)
128  {
131  }
132  if (target != nullptr)
133  replaceAll(str, "<PLAYER>", target->getName());
134  else
135  replaceAll(str, "<PLAYER>", "");
136  }
137  if (str.find("<MONSTER>") != std::string::npos)
138  {
139  const Being *target = localPlayer->getTarget();
140  if ((target == nullptr) || target->getType() != ActorType::Monster)
141  {
144  }
145  if (target != nullptr)
146  replaceAll(str, "<MONSTER>", target->getName());
147  else
148  replaceAll(str, "<MONSTER>", "");
149  }
150  if (str.find("<PEOPLE>") != std::string::npos)
151  {
152  StringVect names;
153  std::string newStr;
155  FOR_EACH (StringVectCIter, it, names)
156  {
157  if (*it != localPlayer->getName())
158  newStr.append(*it).append(",");
159  }
160  if (!newStr.empty())
161  {
162  if (newStr[newStr.size() - 1] == ',')
163  newStr = newStr.substr(0, newStr.size() - 1);
164  replaceAll(str, "<PEOPLE>", newStr);
165  }
166  else
167  {
168  replaceAll(str, "<PEOPLE>", "");
169  }
170  }
171  if (str.find("<PARTY>") != std::string::npos)
172  {
173  StringVect names;
174  std::string newStr;
175  const Party *party = nullptr;
176  if (localPlayer->isInParty() &&
177  ((party = localPlayer->getParty()) != nullptr))
178  {
179  party->getNames(names);
180  FOR_EACH (StringVectCIter, it, names)
181  {
182  if (*it != localPlayer->getName())
183  newStr.append(*it).append(",");
184  }
185  if (!newStr.empty())
186  {
187  if (newStr[newStr.size() - 1] == ',')
188  newStr = newStr.substr(0, newStr.size() - 1);
189  replaceAll(str, "<PARTY>", newStr);
190  }
191  else
192  {
193  replaceAll(str, "<PARTY>", "");
194  }
195  }
196  else
197  {
198  replaceAll(str, "<PARTY>", "");
199  }
200  }
201 }
202 
203 std::string textToMe(const std::string &str)
204 {
205  return strprintf("*%s*", str.c_str());
206 }
ActorManager * actorManager
const bool AllowSort_true
Definition: allowsort.h:30
Net::ChatHandler * chatHandler
Definition: net.cpp:86
std::string textToMe(const std::string &str)
Definition: chatutils.cpp:203
void replaceVars(std::string &str)
Definition: chatutils.cpp:119
void outStringNormal(ChatTab *const tab, const std::string &str, const std::string &def)
Definition: chatutils.cpp:47
Net::ClanHandler * clanHandler
Definition: net.cpp:87
Being * findNearestLivingBeing(const int x, const int y, int maxTileDist, const ActorTypeT type, const Being *const excluded) const
void getPlayerNames(StringVect &names, const NpcNames npcNames) const
Definition: being.h:96
Party * getParty() const
Definition: being.h:330
const Guild * getGuild(const std::string &guildName) const
Definition: being.cpp:1258
const std::string & getName() const
Definition: being.h:232
bool isInParty() const
Definition: being.h:321
ActorTypeT getType() const
Definition: being.h:116
ChatTabTypeT getType() const
Definition: chattab.h:145
void chatLog(std::string line, ChatMsgTypeT own, const IgnoreRecord ignoreRecord, const TryRemoveColors tryRemoveColors)
Definition: chattab.cpp:111
void chat(const std::string &msg)
Definition: guild.h:70
Being * getTarget() const
virtual void talk(const std::string &text) const =0
virtual void privateMessage(const std::string &recipient, const std::string &text) const =0
virtual void chat(const std::string &text) const =0
virtual void chat(const std::string &text) const =0
virtual void chat(const std::string &text) const =0
Definition: party.h:63
const std::string & getNick() const
Definition: whispertab.h:37
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Net::GuildHandler * guildHandler
Definition: net.cpp:92
GuildManager * guildManager
LocalPlayer * localPlayer
uint32_t guild
uint32_t party
ServerTypeT getNetworkType()
Definition: net.cpp:189
Net::PartyHandler * partyHandler
Definition: net.cpp:94
const bool NpcNames_false
Definition: npcnames.h:30
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
std::string strprintf(const char *const format,...)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29