GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/chatutils.cpp Lines: 47 75 62.7 %
Date: 2018-05-24 20:11:55 Branches: 64 120 53.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2018  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 "utils/chatutils.h"
22
23
#include "actormanager.h"
24
#include "party.h"
25
26
#include "being/localplayer.h"
27
28
#include "gui/widgets/tabs/chat/whispertab.h"
29
30
#include "net/chathandler.h"
31
#include "net/clanhandler.h"
32
#include "net/guildhandler.h"
33
#include "net/partyhandler.h"
34
35
#ifdef TMWA_SUPPORT
36
#include "net/net.h"
37
38
#include "net/tmwa/guildmanager.h"
39
#endif  // TMWA_SUPPORT
40
41
#include "utils/foreach.h"
42
#include "utils/stringutils.h"
43
44
#include "debug.h"
45
46
void outStringNormal(ChatTab *const tab,
47
                     const std::string &str,
48
                     const std::string &def)
49
{
50
    if (localPlayer == nullptr)
51
        return;
52
53
    if (tab == nullptr)
54
    {
55
        chatHandler->talk(str);
56
        return;
57
    }
58
59
    switch (tab->getType())
60
    {
61
        case ChatTabType::CLAN:
62
        {
63
            clanHandler->chat(str);
64
            break;
65
        }
66
        case ChatTabType::PARTY:
67
        {
68
            partyHandler->chat(str);
69
            break;
70
        }
71
        case ChatTabType::GUILD:
72
        {
73
            const Guild *const guild = localPlayer->getGuild();
74
            if (guild != nullptr)
75
            {
76
#ifdef TMWA_SUPPORT
77
                if (guild->getServerGuild())
78
                {
79
                    if (Net::getNetworkType() == ServerType::TMWATHENA)
80
                        return;
81
                    guildHandler->chat(str);
82
                }
83
                else if (guildManager != nullptr)
84
                {
85
                    guildManager->chat(str);
86
                }
87
#else  // TMWA_SUPPORT
88
89
                if (guild->getServerGuild())
90
                    guildHandler->chat(str);
91
#endif  // TMWA_SUPPORT
92
            }
93
            break;
94
        }
95
        case ChatTabType::WHISPER:
96
        {
97
            const WhisperTab *const whisper
98
                = static_cast<const WhisperTab *>(tab);
99
            tab->chatLog(localPlayer->getName(), str);
100
            chatHandler->privateMessage(whisper->getNick(), str);
101
            break;
102
        }
103
        case ChatTabType::DEBUG:
104
        case ChatTabType::BATTLE:
105
            break;
106
        default:
107
        case ChatTabType::UNKNOWN:
108
        case ChatTabType::INPUT:
109
        case ChatTabType::TRADE:
110
        case ChatTabType::LANG:
111
        case ChatTabType::GM:
112
        case ChatTabType::CHANNEL:
113
            chatHandler->talk(def);
114
            break;
115
    }
116
}
117
118
36
void replaceVars(std::string &str)
119
{
120

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

6
        if ((target == nullptr) || target->getType() != ActorType::Player)
127
        {
128
2
            target = actorManager->findNearestLivingBeing(
129
                localPlayer, 20, ActorType::Player, AllowSort_true);
130
        }
131
4
        if (target != nullptr)
132

8
            replaceAll(str, "<PLAYER>", target->getName());
133
        else
134

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

6
        if ((target == nullptr) || target->getType() != ActorType::Monster)
140
        {
141
2
            target = actorManager->findNearestLivingBeing(
142
                localPlayer, 20, ActorType::Monster, AllowSort_true);
143
        }
144
4
        if (target != nullptr)
145

8
            replaceAll(str, "<MONSTER>", target->getName());
146
        else
147

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

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

28
            replaceAll(str, "<PEOPLE>", "");
168
        }
169
    }
170
36
    if (str.find("<PARTY>") != std::string::npos)
171
    {
172
28
        StringVect names;
173
28
        std::string newStr;
174
14
        const Party *party = nullptr;
175
40
        if (localPlayer->isInParty() &&
176
12
            ((party = localPlayer->getParty()) != nullptr))
177
        {
178
12
            party->getNames(names);
179
92
            FOR_EACH (StringVectCIter, it, names)
180
            {
181
64
                if (*it != localPlayer->getName())
182
40
                    newStr.append(*it).append(",");
183
            }
184
12
            if (!newStr.empty())
185
            {
186
20
                if (newStr[newStr.size() - 1] == ',')
187
20
                    newStr = newStr.substr(0, newStr.size() - 1);
188

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

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

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