GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/partyrecv.cpp Lines: 1 143 0.7 %
Date: 2017-11-29 Branches: 0 169 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Lloyd Bryant <[email protected]>
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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 "net/tmwa/partyrecv.h"
23
24
#include "actormanager.h"
25
#include "notifymanager.h"
26
#include "party.h"
27
28
#include "being/localplayer.h"
29
30
#include "enums/resources/notifytypes.h"
31
32
#include "gui/windows/chatwindow.h"
33
#include "gui/windows/socialwindow.h"
34
35
#include "gui/widgets/tabs/chat/partytab.h"
36
37
#include "net/messagein.h"
38
39
#include "net/ea/partyrecv.h"
40
41
#include "utils/foreach.h"
42
43
#include "debug.h"
44
45
namespace TmwAthena
46
{
47
48
void PartyRecv::processPartySettings(Net::MessageIn &msg)
49
{
50
    if (partyTab == nullptr)
51
    {
52
        if (chatWindow == nullptr)
53
            return;
54
55
        Ea::PartyRecv::createTab();
56
    }
57
58
    // These seem to indicate the sharing mode for exp and items
59
    const PartyShareT exp = static_cast<PartyShareT>(
60
        msg.readInt16("share exp"));
61
    const PartyShareT item = static_cast<PartyShareT>(
62
        msg.readInt16("share items"));
63
    Ea::PartyRecv::processPartyExpSettingsContinue(msg, exp);
64
    Ea::PartyRecv::processPartyItemSettingsContinue(msg, item);
65
}
66
67
void PartyRecv::processPartyInfo(Net::MessageIn &msg)
68
{
69
    bool isOldParty = false;
70
    std::set<std::string> names;
71
    std::set<std::string> onlineNames;
72
    if (Ea::taParty == nullptr)
73
    {
74
        logger->log1("error: party empty in SMSG_PARTY_INFO");
75
        Ea::taParty = Party::getParty(1);
76
    }
77
    if (Ea::taParty != nullptr)
78
    {
79
        if (Ea::taParty->getNumberOfElements() > 1)
80
        {
81
            isOldParty = true;
82
            Ea::taParty->getNamesSet(names);
83
            const Party::MemberList *const members = Ea::taParty->getMembers();
84
            FOR_EACHP (Party::MemberList::const_iterator, it, members)
85
            {
86
                if ((*it)->getOnline())
87
                    onlineNames.insert((*it)->getName());
88
            }
89
            if (localPlayer != nullptr)
90
                onlineNames.insert(localPlayer->getName());
91
        }
92
    }
93
94
    if (localPlayer == nullptr)
95
        logger->log1("error: localPlayer==0 in SMSG_PARTY_INFO");
96
97
    if (Ea::taParty != nullptr)
98
        Ea::taParty->clearMembers();
99
100
    const int length = msg.readInt16("len");
101
    if (Ea::taParty != nullptr)
102
        Ea::taParty->setName(msg.readString(24, "party name"));
103
104
    const int count = (length - 28) / 46;
105
    if ((localPlayer != nullptr) && (Ea::taParty != nullptr))
106
    {
107
        localPlayer->setParty(Ea::taParty);
108
        localPlayer->setPartyName(Ea::taParty->getName());
109
    }
110
111
    for (int i = 0; i < count; i++)
112
    {
113
        const BeingId id = msg.readBeingId("id");
114
        std::string nick = msg.readString(24, "nick");
115
        std::string map = msg.readString(16, "map");
116
        const bool leader = msg.readUInt8("leader") == 0U;
117
        const bool online = msg.readUInt8("online") == 0U;
118
119
        if (Ea::taParty != nullptr)
120
        {
121
            bool joined(false);
122
123
            if (isOldParty)
124
            {
125
                if (names.find(nick) == names.end())
126
                {
127
                    NotifyManager::notify(NotifyTypes::PARTY_USER_JOINED,
128
                        nick);
129
                    joined = true;
130
                }
131
            }
132
            PartyMember *const member = Ea::taParty->addMember(id, nick);
133
            if (member != nullptr)
134
            {
135
                if (!joined && (partyTab != nullptr))
136
                {
137
                    if (!names.empty() && ((onlineNames.find(nick)
138
                        == onlineNames.end() && online)
139
                        || (onlineNames.find(nick) != onlineNames.end()
140
                        && !online)))
141
                    {
142
                        partyTab->showOnline(nick, fromBool(online, Online));
143
                    }
144
145
                    member->setLeader(leader);
146
                    member->setOnline(online);
147
                    member->setMap(map);
148
                }
149
                else
150
                {
151
                    member->setLeader(leader);
152
                    member->setOnline(online);
153
                    member->setMap(map);
154
                }
155
            }
156
        }
157
    }
158
159
    if (Ea::taParty != nullptr)
160
        Ea::taParty->sort();
161
162
    if ((localPlayer != nullptr) && (Ea::taParty != nullptr))
163
    {
164
        localPlayer->setParty(Ea::taParty);
165
        localPlayer->setPartyName(Ea::taParty->getName());
166
        if (socialWindow != nullptr)
167
            socialWindow->updateParty();
168
    }
169
}
170
171
void PartyRecv::processPartyMessage(Net::MessageIn &msg)
172
{
173
    const int msgLength = msg.readInt16("len") - 8;
174
    if (msgLength <= 0)
175
        return;
176
177
    const BeingId id = msg.readBeingId("id");
178
    const std::string chatMsg = msg.readString(msgLength, "message");
179
180
    if ((Ea::taParty != nullptr) && (partyTab != nullptr))
181
    {
182
        const PartyMember *const member = Ea::taParty->getMember(id);
183
        if (member != nullptr)
184
        {
185
            partyTab->chatLog(member->getName(), chatMsg);
186
        }
187
        else
188
        {
189
            NotifyManager::notify(NotifyTypes::PARTY_UNKNOWN_USER_MSG,
190
                chatMsg);
191
        }
192
    }
193
}
194
195
void PartyRecv::processPartyInviteResponse(Net::MessageIn &msg)
196
{
197
    if (partyTab == nullptr)
198
        return;
199
200
    const std::string nick = msg.readString(24, "nick");
201
202
    switch (msg.readUInt8("status"))
203
    {
204
        case 0:
205
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ALREADY_MEMBER,
206
                nick);
207
            break;
208
        case 1:
209
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_REFUSED, nick);
210
            break;
211
        case 2:
212
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_DONE, nick);
213
            break;
214
        case 3:
215
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_FULL,
216
                nick);
217
            break;
218
        default:
219
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ERROR, nick);
220
            break;
221
    }
222
}
223
224
void PartyRecv::processPartyInvited(Net::MessageIn &msg)
225
{
226
    const BeingId id = msg.readBeingId("account id");
227
    std::string nick;
228
229
    if (actorManager != nullptr)
230
    {
231
        const Being *const being = actorManager->findBeing(id);
232
        if (being != nullptr)
233
        {
234
            if (being->getType() == ActorType::Player)
235
                nick = being->getName();
236
        }
237
    }
238
239
    if (socialWindow != nullptr)
240
    {
241
        const std::string partyName = msg.readString(24, "party name");
242
        socialWindow->showPartyInvite(partyName, nick, 0);
243
    }
244
    else
245
    {
246
        msg.readString(24, "party name");
247
    }
248
}
249
250
void PartyRecv::processPartyMove(Net::MessageIn &msg)
251
{
252
    const BeingId id = msg.readBeingId("id");
253
    PartyMember *m = nullptr;
254
    if (Ea::taParty != nullptr)
255
        m = Ea::taParty->getMember(id);
256
    if (m != nullptr)
257
    {
258
        msg.readInt32("unused");
259
        m->setX(msg.readInt16("x"));
260
        m->setY(msg.readInt16("y"));
261
        const bool online = msg.readUInt8("online") != 0;
262
        if (m->getOnline() != online)
263
            partyTab->showOnline(m->getName(), fromBool(online, Online));
264
        m->setOnline(online);
265
        msg.readString(24, "party");
266
        msg.readString(24, "nick");
267
        m->setMap(msg.readString(16, "map"));
268
    }
269
    else
270
    {
271
        msg.readInt32("unused");
272
        msg.readInt16("x");
273
        msg.readInt16("y");
274
        msg.readUInt8("online");
275
        msg.readString(24, "party");
276
        msg.readString(24, "nick");
277
        msg.readString(16, "map");
278
    }
279
}
280
281
void PartyRecv::processPartyUpdateHp(Net::MessageIn &msg)
282
{
283
    const BeingId id = msg.readBeingId("id");
284
    const int hp = msg.readInt16("hp");
285
    const int maxhp = msg.readInt16("max hp");
286
    PartyMember *m = nullptr;
287
    if (Ea::taParty != nullptr)
288
        m = Ea::taParty->getMember(id);
289
    if (m != nullptr)
290
    {
291
        m->setHp(hp);
292
        m->setMaxHp(maxhp);
293
    }
294
295
    // The server only sends this when the member is in range, so
296
    // lets make sure they get the party hilight.
297
    if ((actorManager != nullptr) && (Ea::taParty != nullptr))
298
    {
299
        if (Being *const b = actorManager->findBeing(id))
300
            b->setParty(Ea::taParty);
301
    }
302
}
303
304
4
}  // namespace TmwAthena