ManaPlus
notifymanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-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 "notifymanager.h"
23 
24 #include "soundmanager.h"
25 
26 #include "being/localplayer.h"
27 
28 #include "net/guildhandler.h"
29 #include "net/partyhandler.h"
30 
31 #ifdef TMWA_SUPPORT
32 #include "net/tmwa/guildmanager.h"
33 #endif // TMWA_SUPPORT
34 
36 
37 #include "resources/db/sounddb.h"
38 
39 #include "utils/stringutils.h"
40 
41 #include "debug.h"
42 
43 namespace NotifyManager
44 {
45  static ChatTab *getGuildTab()
46  {
47  const Guild *const guild = localPlayer->getGuild();
48  if (guild != nullptr)
49  {
50 #ifdef TMWA_SUPPORT
51  if (guild->getServerGuild())
52  return guildHandler->getTab();
53  else if (guildManager != nullptr)
54  return guildManager->getTab();
55 #else // TMWA_SUPPORT
56  return guildHandler->getTab();
57 #endif // TMWA_SUPPORT
58  }
59  return nullptr;
60  }
61 
62  static void chatLog(ChatTab *const tab, const std::string &str)
63  {
64  if (str.empty())
65  return;
66  if (tab != nullptr)
67  {
68  tab->chatLog(str,
72  }
73  else if (debugChatTab != nullptr)
74  {
75  debugChatTab->chatLog(str,
79  }
80  }
81 
82  void notify(const unsigned int message)
83  {
84  if (message >= NotifyTypes::TYPE_END ||
85  localChatTab == nullptr)
86  {
87  return;
88  }
89  const NotificationInfo &info = notifications[message];
90  if (*info.text == 0)
91  {
92  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
93  return;
94  }
95 
96  switch (info.flags)
97  {
98  case NotifyFlags::EMPTY:
99  localChatTab->chatLog(gettext(info.text),
103  break;
104 
105  case NotifyFlags::GUILD:
106  {
107  if (localPlayer == nullptr)
108  return;
109  ChatTab *const tab = getGuildTab();
110  chatLog(tab, gettext(info.text));
111  break;
112  }
113 
114  case NotifyFlags::PARTY:
115  {
116  ChatTab *const tab = partyHandler->getTab();
117  chatLog(tab, gettext(info.text));
118  break;
119  }
120 
121  case NotifyFlags::SPEECH:
122  {
123  if (localPlayer != nullptr)
124  localPlayer->setSpeech(gettext(info.text));
125  break;
126  }
127 
128  case NotifyFlags::INT:
129  case NotifyFlags::STRING:
132  default:
133  break;
134  }
135  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
136  }
137 
138  void notify(const unsigned int message, const int num)
139  {
140  if (message >= NotifyTypes::TYPE_END ||
141  localChatTab == nullptr)
142  {
143  return;
144  }
145  const NotificationInfo &info = notifications[message];
146  if (info.flags == NotifyFlags::INT &&
147  *info.text != 0)
148  {
150  strprintf(gettext(info.text), num),
154  }
155  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
156  }
157 
158  void notify(const unsigned int message, const std::string &str)
159  {
160  if (message >= NotifyTypes::TYPE_END ||
161  localChatTab == nullptr)
162  {
163  return;
164  }
165  const NotificationInfo &info = notifications[message];
166  if (*info.text == 0)
167  {
168  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
169  return;
170  }
171  switch (info.flags)
172  {
173  case NotifyFlags::STRING:
174  {
176  strprintf(gettext(info.text), str.c_str()),
180  break;
181  }
183  {
184  ChatTab *const tab = getGuildTab();
185  chatLog(tab, strprintf(gettext(info.text), str.c_str()));
186  break;
187  }
189  {
190  ChatTab *const tab = partyHandler->getTab();
191  chatLog(tab, strprintf(gettext(info.text), str.c_str()));
192  break;
193  }
194  case NotifyFlags::EMPTY:
195  case NotifyFlags::INT:
196  case NotifyFlags::GUILD:
197  case NotifyFlags::PARTY:
198  case NotifyFlags::SPEECH:
199  default:
200  break;
201  }
202  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
203  }
204 
205  int getIndexBySound(const std::string &sound)
206  {
207  for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
208  {
209  if (notifications[f].sound == sound)
210  return f;
211  }
212  return 0;
213  }
214 } // namespace NotifyManager
ChatTab * debugChatTab
Definition: chattab.cpp:63
ChatTab * localChatTab
Definition: chattab.cpp:62
const Guild * getGuild(const std::string &guildName) const
Definition: being.cpp:1258
void setSpeech(const std::string &text)
Definition: being.cpp:572
void chatLog(std::string line, ChatMsgTypeT own, const IgnoreRecord ignoreRecord, const TryRemoveColors tryRemoveColors)
Definition: chattab.cpp:111
ChatTab * getTab()
Definition: guild.h:70
virtual ChatTab * getTab() const =0
virtual ChatTab * getTab() const =0
void playSfx(const std::string &path, const int x, const int y) const
Net::GuildHandler * guildHandler
Definition: net.cpp:92
GuildManager * guildManager
const bool IgnoreRecord_false
Definition: ignorerecord.h:30
LocalPlayer * localPlayer
uint32_t guild
bool info(InputEvent &event)
Definition: commands.cpp:57
static ChatTab * getGuildTab()
int getIndexBySound(const std::string &sound)
void notify(const unsigned int message)
static void chatLog(ChatTab *const tab, const std::string &str)
static const NotificationInfo notifications[NotifyTypes::TYPE_END]
Definition: notifications.h:35
std::string & getSound(const int id)
Definition: sounddb.cpp:95
Net::PartyHandler * partyHandler
Definition: net.cpp:94
SoundManager soundManager
std::string strprintf(const char *const format,...)
const bool TryRemoveColors_true