ManaPlus
notifymanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "notifymanager.h"
22 
23 #include "soundmanager.h"
24 
25 #include "being/localplayer.h"
26 
27 #include "net/guildhandler.h"
28 #include "net/partyhandler.h"
29 
30 #ifdef TMWA_SUPPORT
31 #include "net/tmwa/guildmanager.h"
32 #endif // TMWA_SUPPORT
33 
35 
36 #include "resources/db/sounddb.h"
37 
38 #include "utils/stringutils.h"
39 
40 #include "debug.h"
41 
42 namespace NotifyManager
43 {
44  static ChatTab *getGuildTab()
45  {
46  const Guild *const guild = localPlayer->getGuild();
47  if (guild != nullptr)
48  {
49 #ifdef TMWA_SUPPORT
50  if (guild->getServerGuild())
51  return guildHandler->getTab();
52  else if (guildManager != nullptr)
53  return guildManager->getTab();
54 #else // TMWA_SUPPORT
55  return guildHandler->getTab();
56 #endif // TMWA_SUPPORT
57  }
58  return nullptr;
59  }
60 
61  static void chatLog(ChatTab *const tab, const std::string &str)
62  {
63  if (str.empty())
64  return;
65  if (tab != nullptr)
66  {
67  tab->chatLog(str,
71  }
72  else if (debugChatTab != nullptr)
73  {
74  debugChatTab->chatLog(str,
78  }
79  }
80 
81  void notify(const unsigned int message)
82  {
83  if (message >= NotifyTypes::TYPE_END ||
84  localChatTab == nullptr)
85  {
86  return;
87  }
88  const NotificationInfo &info = notifications[message];
89  if (*info.text == 0)
90  {
91  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
92  return;
93  }
94 
95  switch (info.flags)
96  {
97  case NotifyFlags::EMPTY:
98  localChatTab->chatLog(gettext(info.text),
102  break;
103 
104  case NotifyFlags::GUILD:
105  {
106  if (localPlayer == nullptr)
107  return;
108  ChatTab *const tab = getGuildTab();
109  chatLog(tab, gettext(info.text));
110  break;
111  }
112 
113  case NotifyFlags::PARTY:
114  {
115  ChatTab *const tab = partyHandler->getTab();
116  chatLog(tab, gettext(info.text));
117  break;
118  }
119 
120  case NotifyFlags::SPEECH:
121  {
122  if (localPlayer != nullptr)
123  localPlayer->setSpeech(gettext(info.text));
124  break;
125  }
126 
127  case NotifyFlags::INT:
128  case NotifyFlags::STRING:
131  default:
132  break;
133  }
134  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
135  }
136 
137  void notify(const unsigned int message, const int num)
138  {
139  if (message >= NotifyTypes::TYPE_END ||
140  localChatTab == nullptr)
141  {
142  return;
143  }
144  const NotificationInfo &info = notifications[message];
145  if (info.flags == NotifyFlags::INT &&
146  *info.text != 0)
147  {
149  strprintf(gettext(info.text), num),
153  }
154  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
155  }
156 
157  void notify(const unsigned int message, const std::string &str)
158  {
159  if (message >= NotifyTypes::TYPE_END ||
160  localChatTab == nullptr)
161  {
162  return;
163  }
164  const NotificationInfo &info = notifications[message];
165  if (*info.text == 0)
166  {
167  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
168  return;
169  }
170  switch (info.flags)
171  {
172  case NotifyFlags::STRING:
173  {
175  strprintf(gettext(info.text), str.c_str()),
179  break;
180  }
182  {
183  ChatTab *const tab = getGuildTab();
184  chatLog(tab, strprintf(gettext(info.text), str.c_str()));
185  break;
186  }
188  {
189  ChatTab *const tab = partyHandler->getTab();
190  chatLog(tab, strprintf(gettext(info.text), str.c_str()));
191  break;
192  }
193  case NotifyFlags::EMPTY:
194  case NotifyFlags::INT:
195  case NotifyFlags::GUILD:
196  case NotifyFlags::PARTY:
197  case NotifyFlags::SPEECH:
198  default:
199  break;
200  }
201  soundManager.playSfx(SoundDB::getSound(message), 0, 0);
202  }
203 
204  int getIndexBySound(const std::string &sound)
205  {
206  for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
207  {
208  if (notifications[f].sound == sound)
209  return f;
210  }
211  return 0;
212  }
213 } // namespace NotifyManager
ChatTab
Definition: chattab.h:61
guildHandler
Net::GuildHandler * guildHandler
Definition: net.cpp:91
NotifyFlags::STRING
@ STRING
Definition: notifyflags.h:30
notifymanager.h
SoundDB::getSound
std::string & getSound(const int id)
Definition: sounddb.cpp:94
ChatTab::chatLog
void chatLog(std::string line, ChatMsgTypeT own, const IgnoreRecord ignoreRecord, const TryRemoveColors tryRemoveColors)
Definition: chattab.cpp:110
NotifyManager::notifications
static const NotificationInfo notifications[NotifyTypes::TYPE_END]
Definition: notifications.h:34
Being::getGuild
const Guild * getGuild(const std::string &guildName) const
Definition: being.cpp:1257
NotifyManager
Definition: notifymanager.cpp:42
NotificationInfo
Definition: notificationinfo.h:28
NotifyManager::notify
void notify(const unsigned int message)
Definition: notifymanager.cpp:81
partyHandler
Net::PartyHandler * partyHandler
Definition: net.cpp:93
IgnoreRecord_false
const bool IgnoreRecord_false
Definition: ignorerecord.h:29
NotifyFlags::INT
@ INT
Definition: notifyflags.h:29
Net::GuildHandler::getTab
virtual ChatTab * getTab() const =0
Being::setSpeech
void setSpeech(const std::string &text)
Definition: being.cpp:571
TryRemoveColors_true
const bool TryRemoveColors_true
Definition: tryremovecolors.h:29
Net::PartyHandler::getTab
virtual ChatTab * getTab() const =0
NotifyFlags::EMPTY
@ EMPTY
Definition: notifyflags.h:28
sounddb.h
NotifyFlags::PARTY_STRING
@ PARTY_STRING
Definition: notifyflags.h:34
NotifyManager::getIndexBySound
int getIndexBySound(const std::string &sound)
Definition: notifymanager.cpp:204
Actions::info
bool info(InputEvent &event)
Definition: commands.cpp:56
ChatMsgType::BY_SERVER
@ BY_SERVER
Definition: chatmsgtype.h:34
localplayer.h
localPlayer
LocalPlayer * localPlayer
Definition: localplayer.cpp:108
strprintf
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
guildmanager.h
GuildManager::getTab
ChatTab * getTab()
Definition: guildmanager.cpp:544
NotifyFlags::PARTY
@ PARTY
Definition: notifyflags.h:33
NotifyFlags::SPEECH
@ SPEECH
Definition: notifyflags.h:35
NotifyManager::getGuildTab
static ChatTab * getGuildTab()
Definition: notifymanager.cpp:44
Guild
Definition: guild.h:68
NotifyManager::chatLog
static void chatLog(ChatTab *const tab, const std::string &str)
Definition: notifymanager.cpp:61
soundmanager.h
debugChatTab
ChatTab * debugChatTab
Definition: chattab.cpp:62
stringutils.h
guild
uint32_t guild
Definition: maptypeproperty2.h:5
notifications.h
guildManager
GuildManager * guildManager
Definition: guildmanager.cpp:47
guildhandler.h
NotifyTypes::TYPE_END
@ TYPE_END
Definition: notifytypes.h:253
partyhandler.h
SoundManager::playSfx
void playSfx(const std::string &path, const int x, const int y) const
Definition: soundmanager.cpp:462
soundManager
SoundManager soundManager
Definition: soundmanager.cpp:54
localChatTab
ChatTab * localChatTab
Definition: chattab.cpp:61
debug.h
NotifyFlags::GUILD
@ GUILD
Definition: notifyflags.h:31
NotifyFlags::GUILD_STRING
@ GUILD_STRING
Definition: notifyflags.h:32