GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/chatutils.cpp Lines: 47 72 65.3 %
Date: 2017-11-29 Branches: 66 123 53.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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 "const/gui/chat.h"
29
30
#include "gui/widgets/tabs/chat/whispertab.h"
31
32
#include "net/chathandler.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, GENERAL_CHANNEL);
57
        return;
58
    }
59
60
    switch (tab->getType())
61
    {
62
        case ChatTabType::PARTY:
63
        {
64
            partyHandler->chat(str);
65
            break;
66
        }
67
        case ChatTabType::GUILD:
68
        {
69
            const Guild *const guild = localPlayer->getGuild();
70
            if (guild != nullptr)
71
            {
72
#ifdef TMWA_SUPPORT
73
                if (guild->getServerGuild())
74
                {
75
                    if (Net::getNetworkType() == ServerType::TMWATHENA)
76
                        return;
77
                    guildHandler->chat(str);
78
                }
79
                else if (guildManager != nullptr)
80
                {
81
                    guildManager->chat(str);
82
                }
83
#else  // TMWA_SUPPORT
84
85
                if (guild->getServerGuild())
86
                    guildHandler->chat(str);
87
#endif  // TMWA_SUPPORT
88
            }
89
            break;
90
        }
91
        case ChatTabType::WHISPER:
92
        {
93
            const WhisperTab *const whisper
94
                = static_cast<const WhisperTab *>(tab);
95
            tab->chatLog(localPlayer->getName(), str);
96
            chatHandler->privateMessage(whisper->getNick(), str);
97
            break;
98
        }
99
        case ChatTabType::DEBUG:
100
        case ChatTabType::BATTLE:
101
            break;
102
        default:
103
        case ChatTabType::UNKNOWN:
104
        case ChatTabType::INPUT:
105
        case ChatTabType::TRADE:
106
        case ChatTabType::LANG:
107
        case ChatTabType::GM:
108
        case ChatTabType::CHANNEL:
109
            chatHandler->talk(def, GENERAL_CHANNEL);
110
            break;
111
    }
112
}
113
114
36
void replaceVars(std::string &str)
115
{
116

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

6
        if ((target == nullptr) || target->getType() != ActorType::Player)
123
        {
124
2
            target = actorManager->findNearestLivingBeing(
125
                localPlayer, 20, ActorType::Player, AllowSort_true);
126
        }
127
4
        if (target != nullptr)
128

8
            replaceAll(str, "<PLAYER>", target->getName());
129
        else
130

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

6
        if ((target == nullptr) || target->getType() != ActorType::Monster)
136
        {
137
2
            target = actorManager->findNearestLivingBeing(
138
                localPlayer, 20, ActorType::Monster, AllowSort_true);
139
        }
140
4
        if (target != nullptr)
141

8
            replaceAll(str, "<MONSTER>", target->getName());
142
        else
143

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

16
            replaceAll(str, "<PEOPLE>", newStr);
160
        }
161
        else
162
        {
163

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

40
                replaceAll(str, "<PARTY>", newStr);
185
            }
186
            else
187
            {
188

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

16
            replaceAll(str, "<PARTY>", "");
194
        }
195
    }
196
}
197
198
8
std::string textToMe(const std::string &str)
199
{
200
8
    return strprintf("*%s*", str.c_str());
201

6
}