GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/partyrecv.cpp Lines: 1 216 0.5 %
Date: 2018-05-19 03:07:18 Branches: 0 234 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
    int offset = 0;
193
    if (msg.getVersion() >= 20170502)
194
    {
195
        partySize = 50;
196
        offset = 28 + 6;
197
    }
198
    else
199
    {
200
        partySize = 46;
201
        offset = 28;
202
    }
203
204
    const int count = (length - offset) / partySize;
205
    if (localPlayer != nullptr &&
206
        Ea::taParty != nullptr)
207
    {
208
        localPlayer->setParty(Ea::taParty);
209
        localPlayer->setPartyName(Ea::taParty->getName());
210
    }
211
212
    for (int i = 0; i < count; i++)
213
    {
214
        const BeingId id = msg.readBeingId("account id");
215
        if (msg.getVersion() >= 20171207)
216
            msg.readBeingId("char id");
217
        std::string nick = msg.readString(24, "nick");
218
        std::string map = msg.readString(16, "map name");
219
        const bool leader = msg.readUInt8("leader") == 0U;
220
        const bool online = msg.readUInt8("online") == 0U;
221
        int level = 0;
222
        if (msg.getVersion() >= 20170502)
223
        {
224
            msg.readInt16("class");
225
            level = msg.readInt16("level");
226
        }
227
228
        if (Ea::taParty != nullptr)
229
        {
230
            bool joined(false);
231
232
            if (isOldParty)
233
            {
234
                if (names.find(nick) == names.end())
235
                {
236
                    NotifyManager::notify(NotifyTypes::PARTY_USER_JOINED,
237
                        nick);
238
                    joined = true;
239
                }
240
            }
241
            PartyMember *const member = Ea::taParty->addMember(id, nick);
242
            if (member != nullptr)
243
            {
244
                if (!joined && (partyTab != nullptr))
245
                {
246
                    if (!names.empty() && ((onlineNames.find(nick)
247
                        == onlineNames.end() && online)
248
                        || (onlineNames.find(nick) != onlineNames.end()
249
                        && !online)))
250
                    {
251
                        partyTab->showOnline(nick, fromBool(online, Online));
252
                    }
253
                }
254
                member->setLeader(leader);
255
                member->setOnline(online);
256
                member->setMap(map);
257
                if (level != 0)
258
                    member->setLevel(level);
259
            }
260
        }
261
    }
262
263
    if (msg.getVersion() >= 20170502 && msg.getUnreadLength() >= 6)
264
    {
265
        msg.readInt8("pickup item share (&1)");
266
        msg.readInt8("get item share (&2)");
267
        msg.readInt32("unknown");
268
    }
269
270
    if (Ea::taParty != nullptr)
271
        Ea::taParty->sort();
272
273
    if ((localPlayer != nullptr) && (Ea::taParty != nullptr))
274
    {
275
        localPlayer->setParty(Ea::taParty);
276
        localPlayer->setPartyName(Ea::taParty->getName());
277
        if (socialWindow != nullptr)
278
            socialWindow->updateParty();
279
    }
280
}
281
282
void PartyRecv::processPartyMessage(Net::MessageIn &msg)
283
{
284
    const int msgLength = msg.readInt16("len") - 8;
285
    if (msgLength <= 0)
286
        return;
287
288
    const BeingId id = msg.readBeingId("id");
289
    std::string chatMsg = msg.readString(msgLength, "message");
290
291
    const size_t pos = chatMsg.find(" : ", 0);
292
    if (pos != std::string::npos)
293
        chatMsg.erase(0, pos + 3);
294
295
    if ((Ea::taParty != nullptr) && (partyTab != nullptr))
296
    {
297
        const PartyMember *const member = Ea::taParty->getMember(id);
298
        if (member != nullptr)
299
        {
300
            partyTab->chatLog(member->getName(), chatMsg);
301
        }
302
        else
303
        {
304
            NotifyManager::notify(NotifyTypes::PARTY_UNKNOWN_USER_MSG,
305
                chatMsg);
306
        }
307
    }
308
}
309
310
void PartyRecv::processPartyInviteResponse(Net::MessageIn &msg)
311
{
312
    if (partyTab == nullptr)
313
        return;
314
315
    const std::string nick = msg.readString(24, "nick");
316
317
    switch (msg.readInt32("result"))
318
    {
319
        case 0:
320
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ALREADY_MEMBER,
321
                nick);
322
            break;
323
        case 1:
324
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_REFUSED, nick);
325
            break;
326
        case 2:
327
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_DONE, nick);
328
            break;
329
        case 3:
330
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_FULL,
331
                nick);
332
            break;
333
        case 4:
334
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_SAME_ACCOUNT,
335
                nick);
336
            break;
337
        case 5:
338
            NotifyManager::notify(
339
                NotifyTypes::PARTY_INVITE_PARTY_BLOCKED_INVITE,
340
                nick);
341
            break;
342
        case 7:
343
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_PARTY_NOT_ONLINE,
344
                nick);
345
            break;
346
        default:
347
            NotifyManager::notify(NotifyTypes::PARTY_INVITE_ERROR, nick);
348
            break;
349
    }
350
}
351
352
void PartyRecv::processPartyItemPickup(Net::MessageIn &msg)
353
{
354
    UNIMPLEMENTEDPACKET;
355
    // +++ probably need add option to show pickup notifications
356
    // in party tab
357
    msg.readBeingId("account id");
358
    msg.readInt16("item id");
359
    msg.readUInt8("identify");
360
    msg.readUInt8("attribute");
361
    msg.readUInt8("refine");
362
    for (int f = 0; f < maxCards; f++)
363
        msg.readUInt16("card");
364
    msg.readInt16("equip location");
365
    msg.readUInt8("item type");
366
    // for color can be used ItemColorManager
367
}
368
369
void PartyRecv::processPartyLeader(Net::MessageIn &msg)
370
{
371
    PartyMember *const oldMember = Ea::taParty->getMember(
372
        msg.readBeingId("old leder id"));
373
    PartyMember *const newMember = Ea::taParty->getMember(
374
        msg.readBeingId("new leder id"));
375
    if (oldMember != nullptr)
376
        oldMember->setLeader(false);
377
    if (newMember != nullptr)
378
        newMember->setLeader(true);
379
}
380
381
void PartyRecv::processPartyInvited(Net::MessageIn &msg)
382
{
383
    if (socialWindow == nullptr)
384
    {
385
        msg.readInt32("party id");
386
        msg.readString(24, "party name");
387
        return;
388
    }
389
    const int id = msg.readInt32("party id");
390
    const std::string partyName = msg.readString(24, "party name");
391
392
    if (socialWindow != nullptr)
393
        socialWindow->showPartyInvite(partyName, std::string(), id);
394
}
395
396
void PartyRecv::processPartyAutoItemSettingsContinue(Net::MessageIn &msg,
397
                                                     const PartyShareT item)
398
{
399
    switch (item)
400
    {
401
        case PartyShare::YES:
402
            if (mShareAutoItems == PartyShare::YES)
403
                break;
404
            mShareAutoItems = PartyShare::YES;
405
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_ON);
406
            break;
407
        case PartyShare::NO:
408
            if (mShareAutoItems == PartyShare::NO)
409
                break;
410
            mShareAutoItems = PartyShare::NO;
411
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_OFF);
412
            break;
413
        case PartyShare::NOT_POSSIBLE:
414
            if (mShareAutoItems == PartyShare::NOT_POSSIBLE)
415
                break;
416
            mShareAutoItems = PartyShare::NOT_POSSIBLE;
417
            NotifyManager::notify(NotifyTypes::PARTY_ITEM_SHARE_ERROR);
418
            break;
419
        default:
420
        case PartyShare::UNKNOWN:
421
            UNIMPLEMENTEDPACKETFIELD(CAST_S32(item));
422
            break;
423
    }
424
}
425
426
4
}  // namespace EAthena