GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/partyrecv.cpp Lines: 1 204 0.5 %
Date: 2018-09-20 Branches: 0 242 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  Lloyd Bryant <[email protected]>
4
 *  Copyright (C) 2011-2018  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/eathena/partyrecv.h"
23
24
#include "notifymanager.h"
25
#include "party.h"
26
27
#include "being/localplayer.h"
28
29
#include "enums/resources/notifytypes.h"
30
31
#include "gui/windows/chatwindow.h"
32
#include "gui/windows/socialwindow.h"
33
34
#include "gui/widgets/tabs/chat/partytab.h"
35
36
#include "net/messagein.h"
37
38
#include "net/ea/partyrecv.h"
39
40
#include "utils/checkutils.h"
41
#include "utils/foreach.h"
42
43
#include "debug.h"
44
45
namespace EAthena
46
{
47
48
namespace PartyRecv
49
{
50
    PartyShareT mShareAutoItems = PartyShare::UNKNOWN;
51
}  // namespace PartyRecv
52
53
void PartyRecv::processPartyInvitationStats(Net::MessageIn &msg)
54
{
55
    // +++ for now server allow only switch this option but not using it.
56
    msg.readUInt8("allow party");
57
}
58
59
void PartyRecv::processPartyMemberInfo(Net::MessageIn &msg)
60
{
61
    const BeingId id = msg.readBeingId("account id");
62
    if (msg.getVersion() >= 20171207)
63
        msg.readBeingId("char id");
64
    const bool leader = msg.readInt32("leader") == 0U;
65
    int level = 0;
66
    if (msg.getVersion() >= 20170502)
67
    {
68
        msg.readInt16("class");
69
        level = msg.readInt16("level");
70
    }
71
    const int x = msg.readInt16("x");
72
    const int y = msg.readInt16("y");
73
    const bool online = msg.readInt8("online") == 0U;
74
    msg.readString(24, "party name");
75
    const std::string nick = msg.readString(24, "player name");
76
    const std::string map = msg.readString(16, "map name");
77
    msg.readInt8("pickup item share (&1)");
78
    msg.readInt8("get item share (&2)");
79
80
    if (Ea::taParty == nullptr)
81
        return;
82
83
    PartyMember *const member = Ea::taParty->addMember(id, nick);
84
    if (member != nullptr)
85
    {
86
        if ((partyTab != nullptr) && member->getOnline() != online)
87
            partyTab->showOnline(nick, fromBool(online, Online));
88
        member->setLeader(leader);
89
        member->setOnline(online);
90
        member->setMap(map);
91
        member->setX(x);
92
        member->setY(y);
93
        if (level != 0)
94
            member->setLevel(level);
95
    }
96
}
97
98
void PartyRecv::processPartyMemberJobLevel(Net::MessageIn &msg)
99
{
100
    const BeingId id = msg.readBeingId("account id");
101
    msg.readInt16("class");
102
    const int level = msg.readInt16("level");
103
104
    if (Ea::taParty == nullptr)
105
        return;
106
107
    PartyMember *const member = Ea::taParty->getMember(id);
108
    if (member != nullptr)
109
    {
110
        member->setOnline(true);
111
        if (level != 0)
112
            member->setLevel(level);
113
    }
114
    else
115
    {
116
        reportAlways("processPartyMemberJobLevel: party member not exists.");
117
    }
118
}
119
120
void PartyRecv::processPartySettings(Net::MessageIn &msg)
121
{
122
    if (partyTab == nullptr)
123
    {
124
        if (chatWindow == nullptr)
125
            return;
126
127
        Ea::PartyRecv::createTab();
128
    }
129
130
    const PartyShareT exp = static_cast<PartyShareT>(
131
        msg.readInt32("party exp"));
132
    Ea::PartyRecv::processPartyExpSettingsContinue(msg, exp);
133
    if (msg.getVersion() >= 20090603)
134
    {
135
        const PartyShareT item = static_cast<PartyShareT>(
136
            msg.readInt8("pickup item share (&1)"));
137
        Ea::PartyRecv::processPartyItemSettingsContinue(msg, item);
138
        const PartyShareT autoItem = static_cast<PartyShareT>(
139
            msg.readInt8("get auto item share (&2)"));
140
        processPartyAutoItemSettingsContinue(msg, autoItem);
141
    }
142
}
143
144
void PartyRecv::processPartyInfo(Net::MessageIn &msg)
145
{
146
    bool isOldParty = false;
147
    std::set<std::string> names;
148
    std::set<std::string> onlineNames;
149
    if (Ea::taParty == nullptr)
150
    {
151
        logger->log1("error: party empty in SMSG_PARTY_INFO");
152
        Ea::taParty = Party::getParty(1);
153
    }
154
    if (partyTab == nullptr)
155
        Ea::PartyRecv::createTab();
156
157
    if (Ea::taParty != nullptr)
158
    {
159
        if (Ea::taParty->getNumberOfElements() > 1)
160
        {
161
            isOldParty = true;
162
            Ea::taParty->getNamesSet(names);
163
            const Party::MemberList *const members = Ea::taParty->getMembers();
164
            FOR_EACHP (Party::MemberList::const_iterator, it, members)
165
            {
166
                if ((*it)->getOnline())
167
                    onlineNames.insert((*it)->getName());
168
            }
169
            if (localPlayer != nullptr)
170
                onlineNames.insert(localPlayer->getName());
171
        }
172
    }
173
174
    if (localPlayer == nullptr)
175
        logger->log1("error: localPlayer==0 in SMSG_PARTY_INFO");
176
177
    if (Ea::taParty != nullptr)
178
        Ea::taParty->clearMembers();
179
180
    const int length = msg.readInt16("len");
181
    if (Ea::taParty != nullptr)
182
    {
183
        const std::string name = msg.readString(24, "party name");
184
        Ea::taParty->setName(name);
185
    }
186
    else
187
    {
188
        msg.readString(24, "party name");
189
    }
190
191
    int partySize = 0;
192
    const int offset = 28;
193
    if (msg.getVersion() >= 20171207)
194
    {
195
        partySize = 4 + 4 + 24 + 16 + 1 + 1 + 2 + 2;
196
    }
197
    else if (msg.getVersion() >= 20170502)
198
    {
199
        partySize = 4 + 24 + 16 + 1 + 1 + 2 + 2;
200
    }
201
    else
202
    {
203
        partySize = 4 + 24 + 16 + 1 + 1;
204
    }
205
206
    const int count = (length - offset) / partySize;
207
    if (localPlayer != nullptr &&
208
        Ea::taParty != nullptr)
209
    {
210
        localPlayer->setParty(Ea::taParty);
211
        localPlayer->setPartyName(Ea::taParty->getName());
212
    }
213
214
    for (int i = 0; i < count; i++)
215
    {
216
        const BeingId id = msg.readBeingId("account id");
217
        if (msg.getVersion() >= 20171207)
218
            msg.readBeingId("char id");
219
        std::string nick = msg.readString(24, "nick");
220
        std::string map = msg.readString(16, "map name");
221
        const bool leader = msg.readUInt8("leader") == 0U;
222
        const bool online = msg.readUInt8("online") == 0U;
223
        int level = 0;
224
        if (msg.getVersion() >= 20170502)
225
        {
226
            msg.readInt16("class");
227
            level = msg.readInt16("level");
228
        }
229
230
        if (Ea::taParty != nullptr)
231
        {
232
            bool joined(false);
233
234
            if (isOldParty)
235
            {
236
                if (names.find(nick) == names.end())
237
                {
238
                    NotifyManager::notify(NotifyTypes::PARTY_USER_JOINED,
239
                        nick);
240
                    joined = true;
241
                }
242
            }
243
            PartyMember *const member = Ea::taParty->addMember(id, nick);
244
            if (member != nullptr)
245
            {
246
                if (!joined && (partyTab != nullptr))
247
                {
248
                    if (!names.empty() && ((onlineNames.find(nick)
249
                        == onlineNames.end() && online)
250
                        || (onlineNames.find(nick) != onlineNames.end()
251
                        && !online)))
252
                    {
253
                        partyTab->showOnline(nick, fromBool(online, Online));
254
                    }
255
                }
256
                member->setLeader(leader);
257
                member->setOnline(online);
258
                member->setMap(map);
259
                if (level != 0)
260
                    member->setLevel(level);
261
            }
262
        }
263
    }
264
265
    if (msg.getVersion() >= 20170502 && msg.getUnreadLength() >= 6)
266
    {
267
        msg.readInt8("pickup item share (&1)");
268
        msg.readInt8("get item share (&2)");
269
        msg.readInt32("unknown");
270
    }
271
272
    if (Ea::taParty != nullptr)
273
        Ea::taParty->sort();
274
275
    if ((localPlayer != nullptr) && (Ea::taParty != nullptr))
276
    {
277
        localPlayer->setParty(Ea::taParty);
278
        localPlayer->setPartyName(Ea::taParty->getName());
279
        if (socialWindow != nullptr)
280
            socialWindow->updateParty();
281
    }
282
}
283
284
void PartyRecv::processPartyMessage(Net::MessageIn &msg)
285
{
286
    const int msgLength = msg.readInt16("len") - 8;
287
    if (msgLength <= 0)
288
        return;
289
290
    const BeingId id = msg.readBeingId("id");
291
    std::string chatMsg = msg.readString(msgLength, "message");
292
293
    const size_t pos = chatMsg.find(" : ", 0);
294
    if (pos != std::string::npos)
295
        chatMsg.erase(0, pos + 3);
296
297
    if ((Ea::taParty != nullptr) && (partyTab != nullptr))
298
    {
299
        const PartyMember *const member = Ea::taParty->getMember(id);
300
        if (member != nullptr)
301
        {
302
            partyTab->chatLog(member->getName(), chatMsg);
303
        }
304
        else
305
        {
306
            NotifyManager::notify(NotifyTypes::PARTY_UNKNOWN_USER_MSG,
307
                chatMsg);
308
        }
309
    }
310
}
311
312
void PartyRecv::processPartyInviteResponse(Net::MessageIn &msg)
313
{
314
    if (partyTab == nullptr)
315
        return;
316
317
    const std::string nick = msg.readString(24, "nick");
318
319
    switch (msg.readInt32("result"))
320
    {
321
        case 0:
322
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ALREADY_MEMBER,
323
                nick);
324
            break;
325
        case 1:
326
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_REFUSED, nick);
327
            break;
328
        case 2:
329
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_DONE, nick);
330
            break;
331
        case 3:
332
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_FULL,
333
                nick);
334
            break;
335
        case 4:
336
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_SAME_ACCOUNT,
337
                nick);
338
            break;
339
        case 5:
340
            NotifyManager::notify(
341
                NotifyTypes::PARTY_INVITE_PARTY_BLOCKED_INVITE,
342
                nick);
343
            break;
344
        case 7:
345
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_NOT_ONLINE,
346
                nick);
347
            break;
348
        default:
349
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ERROR, nick);
350
            break;
351
    }
352
}
353
354
void PartyRecv::processPartyItemPickup(Net::MessageIn &msg)
355
{
356
    UNIMPLEMENTEDPACKET;
357
    // +++ probably need add option to show pickup notifications
358
    // in party tab
359
    msg.readBeingId("account id");
360
    msg.readItemId("item id");
361
    msg.readUInt8("identify");
362
    msg.readUInt8("attribute");
363
    msg.readUInt8("refine");
364
    for (int f = 0; f < maxCards; f++)
365
        msg.readItemId("card");
366
    msg.readInt16("equip location");
367
    msg.readUInt8("item type");
368
    // for color can be used ItemColorManager
369
}
370
371
void PartyRecv::processPartyLeader(Net::MessageIn &msg)
372
{
373
    PartyMember *const oldMember = Ea::taParty->getMember(
374
        msg.readBeingId("old leder id"));
375
    PartyMember *const newMember = Ea::taParty->getMember(
376
        msg.readBeingId("new leder id"));
377
    if (oldMember != nullptr)
378
        oldMember->setLeader(false);
379
    if (newMember != nullptr)
380
        newMember->setLeader(true);
381
}
382
383
void PartyRecv::processPartyInvited(Net::MessageIn &msg)
384
{
385
    if (socialWindow == nullptr)
386
    {
387
        msg.readInt32("party id");
388
        msg.readString(24, "party name");
389
        return;
390
    }
391
    const int id = msg.readInt32("party id");
392
    const std::string partyName = msg.readString(24, "party name");
393
394
    if (socialWindow != nullptr)
395
        socialWindow->showPartyInvite(partyName, std::string(), id);
396
}
397
398
void PartyRecv::processPartyAutoItemSettingsContinue(Net::MessageIn &msg,
399
                                                     const PartyShareT item)
400
{
401
    switch (item)
402
    {
403
        case PartyShare::YES:
404
            if (mShareAutoItems == PartyShare::YES)
405
                break;
406
            mShareAutoItems = PartyShare::YES;
407
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_ON);
408
            break;
409
        case PartyShare::NO:
410
            if (mShareAutoItems == PartyShare::NO)
411
                break;
412
            mShareAutoItems = PartyShare::NO;
413
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_OFF);
414
            break;
415
        case PartyShare::NOT_POSSIBLE:
416
            if (mShareAutoItems == PartyShare::NOT_POSSIBLE)
417
                break;
418
            mShareAutoItems = PartyShare::NOT_POSSIBLE;
419
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_ERROR);
420
            break;
421
        default:
422
        case PartyShare::UNKNOWN:
423
            UNIMPLEMENTEDPACKETFIELD(CAST_S32(item));
424
            break;
425
    }
426
}
427
428
2
}  // namespace EAthena