GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/chatutils.cpp Lines: 48 71 67.6 %
Date: 2021-03-17 Branches: 66 118 55.9 %

Line Branch Exec Source
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
29
#include "gui/widgets/tabs/chat/whispertab.h"
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
                {
80
                    if (Net::getNetworkType() == ServerType::TMWATHENA)
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
        }
96
        case ChatTabType::WHISPER:
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:
108
        case ChatTabType::UNKNOWN:
109
        case ChatTabType::INPUT:
110
        case ChatTabType::TRADE:
111
        case ChatTabType::LANG:
112
        case ChatTabType::GM:
113
        case ChatTabType::CHANNEL:
114
            chatHandler->talk(def);
115
            break;
116
    }
117
}
118
119
18
void replaceVars(std::string &str)
120
{
121

18
    if ((localPlayer == nullptr) || (actorManager == nullptr))
122
        return;
123
124
18
    if (str.find("<PLAYER>") != std::string::npos)
125
    {
126
2
        const Being *target = localPlayer->getTarget();
127

3
        if ((target == nullptr) || target->getType() != ActorType::Player)
128
        {
129
1
            target = actorManager->findNearestLivingBeing(
130
1
                localPlayer, 20, ActorType::Player, AllowSort_true);
131
        }
132
2
        if (target != nullptr)
133
4
            replaceAll(str, "<PLAYER>", target->getName());
134
        else
135

7
            replaceAll(str, "<PLAYER>", "");
136
    }
137
18
    if (str.find("<MONSTER>") != std::string::npos)
138
    {
139
2
        const Being *target = localPlayer->getTarget();
140

3
        if ((target == nullptr) || target->getType() != ActorType::Monster)
141
        {
142
1
            target = actorManager->findNearestLivingBeing(
143
1
                localPlayer, 20, ActorType::Monster, AllowSort_true);
144
        }
145
2
        if (target != nullptr)
146
4
            replaceAll(str, "<MONSTER>", target->getName());
147
        else
148

7
            replaceAll(str, "<MONSTER>", "");
149
    }
150
18
    if (str.find("<PEOPLE>") != std::string::npos)
151
    {
152
8
        StringVect names;
153
8
        std::string newStr;
154
4
        actorManager->getPlayerNames(names, NpcNames_false);
155
23
        FOR_EACH (StringVectCIter, it, names)
156
        {
157
9
            if (*it != localPlayer->getName())
158
6
                newStr.append(*it).append(",");
159
        }
160
4
        if (!newStr.empty())
161
        {
162
4
            if (newStr[newStr.size() - 1] == ',')
163
4
                newStr = newStr.substr(0, newStr.size() - 1);
164

8
            replaceAll(str, "<PEOPLE>", newStr);
165
        }
166
        else
167
        {
168

14
            replaceAll(str, "<PEOPLE>", "");
169
        }
170
    }
171
18
    if (str.find("<PARTY>") != std::string::npos)
172
    {
173
14
        StringVect names;
174
14
        std::string newStr;
175
7
        const Party *party = nullptr;
176

14
        if (localPlayer->isInParty() &&
177
            ((party = localPlayer->getParty()) != nullptr))
178
        {
179
6
            party->getNames(names);
180
46
            FOR_EACH (StringVectCIter, it, names)
181
            {
182
48
                if (*it != localPlayer->getName())
183
20
                    newStr.append(*it).append(",");
184
            }
185
6
            if (!newStr.empty())
186
            {
187
10
                if (newStr[newStr.size() - 1] == ',')
188
10
                    newStr = newStr.substr(0, newStr.size() - 1);
189

20
                replaceAll(str, "<PARTY>", newStr);
190
            }
191
            else
192
            {
193

7
                replaceAll(str, "<PARTY>", "");
194
            }
195
        }
196
        else
197
        {
198

7
            replaceAll(str, "<PARTY>", "");
199
        }
200
    }
201
}
202
203
4
std::string textToMe(const std::string &str)
204
{
205
4
    return strprintf("*%s*", str.c_str());
206
2
}