GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/party.cpp Lines: 55 144 38.2 %
Date: 2021-03-17 Branches: 23 140 16.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2019-2021  Andrei Karas
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "party.h"
24
25
#include "actormanager.h"
26
27
#include "being/being.h"
28
29
#include "utils/stringutils.h"
30
31
#include "debug.h"
32
33
static class SortPartyFunctor final
34
{
35
    public:
36
        A_DEFAULT_COPY(SortPartyFunctor)
37
38
        bool operator() (const PartyMember *const p1,
39
                         const PartyMember *const p2) const
40
        {
41
            if (p1 == nullptr || p2 == nullptr)
42
                return false;
43
            const int leader1 = CAST_S32(p1->getLeader());
44
            const int leader2 = CAST_S32(p2->getLeader());
45
            if (leader1 != leader2)
46
                return leader1 >= leader2;
47
            if (p1->getName() != p2->getName())
48
            {
49
                std::string s1 = p1->getName();
50
                std::string s2 = p2->getName();
51
                toLower(s1);
52
                toLower(s2);
53
                return s1 < s2;
54
            }
55
            return false;
56
        }
57
} partySorter;
58
59
PartyMember::PartyMember(Party *const party,
60
                         const BeingId id,
61
6
                         const std::string &name) :
62
    Avatar(name),
63
    mParty(party),
64
6
    mLeader(false)
65
{
66
6
    mId = id;
67
}
68
69
1
Party::PartyMap Party::parties;
70
71
2
Party::Party(const int16_t id) :
72
    mMembers(),
73
    mName(),
74
    mId(id),
75
8
    mCanInviteUsers(false)
76
{
77
2
    parties[id] = this;
78
2
}
79
80
10
Party::~Party()
81
{
82
2
    clearMembers();
83
4
}
84
85
6
PartyMember *Party::addMember(const BeingId id,
86
                              const std::string &name)
87
{
88
6
    PartyMember *m = getMember(id);
89
6
    if (m != nullptr)
90
        return m;
91
92
12
    m = new PartyMember(this, id, name);
93
94
6
    mMembers.push_back(m);
95
96
6
    return m;
97
}
98
99
PartyMember *Party::getMember(const BeingId id) const
100
{
101
12
    MemberList::const_iterator itr = mMembers.begin();
102
12
    const MemberList::const_iterator itr_end = mMembers.end();
103

12
    while (itr != itr_end)
104
    {
105

6
        if ((*itr)->mId == id)
106
            return (*itr);
107
        ++itr;
108
    }
109
110
    return nullptr;
111
}
112
113
PartyMember *Party::getMember(const std::string &name) const
114
{
115
    MemberList::const_iterator itr = mMembers.begin();
116
    const MemberList::const_iterator itr_end = mMembers.end();
117
    while (itr != itr_end)
118
    {
119
        if (((*itr) != nullptr) && (*itr)->getName() == name)
120
            return (*itr);
121
122
        ++itr;
123
    }
124
125
    return nullptr;
126
}
127
128
void Party::removeMember(const PartyMember *const member)
129
{
130
    if (member == nullptr)
131
        return;
132
133
    bool deleted = true;
134
    while (deleted)
135
    {
136
        deleted = false;
137
        MemberList::iterator itr = mMembers.begin();
138
        const MemberList::iterator itr_end = mMembers.end();
139
        while (itr != itr_end)
140
        {
141
            PartyMember *const m = *itr;
142
            if ((m != nullptr) && m->mId == member->mId
143
                && m->getName() == member->getName())
144
            {
145
                mMembers.erase(itr);
146
                delete m;
147
                deleted = true;
148
                break;
149
            }
150
            ++itr;
151
        }
152
    }
153
}
154
155
2
void Party::removeMember(const BeingId id)
156
{
157
2
    bool deleted = true;
158
6
    while (deleted)
159
    {
160
4
        deleted = false;
161
8
        MemberList::iterator itr = mMembers.begin();
162
8
        const MemberList::iterator itr_end = mMembers.end();
163
9
        while (itr != itr_end)
164
        {
165
7
            PartyMember *const member = *itr;
166

7
            if ((member != nullptr) && member->mId == id)
167
            {
168
4
                mMembers.erase(itr);
169
                delete member;
170
2
                deleted = true;
171
2
                break;
172
            }
173
            ++itr;
174
        }
175
    }
176
2
}
177
178
void Party::removeMember(const std::string &name)
179
{
180
    bool deleted = true;
181
    while (deleted)
182
    {
183
        deleted = false;
184
        MemberList::iterator itr = mMembers.begin();
185
        const MemberList::iterator itr_end = mMembers.end();
186
        while (itr != itr_end)
187
        {
188
            PartyMember *const member = *itr;
189
            if ((member != nullptr) && member->getName() == name)
190
            {
191
                mMembers.erase(itr);
192
                delete member;
193
                deleted = true;
194
                break;
195
            }
196
            ++itr;
197
        }
198
    }
199
}
200
201
void Party::removeFromMembers()
202
{
203
    if (actorManager == nullptr)
204
        return;
205
206
    MemberList::const_iterator itr = mMembers.begin();
207
    const MemberList::const_iterator itr_end = mMembers.end();
208
209
    while (itr != itr_end)
210
    {
211
        Being *const b = actorManager->findBeing((*itr)->getID());
212
        if (b != nullptr)
213
            b->setParty(nullptr);
214
        ++itr;
215
    }
216
}
217
218
Avatar *Party::getAvatarAt(const int index)
219
{
220
    return mMembers[index];
221
}
222
223
void Party::setRights(const int16_t rights)
224
{
225
    // to invite, rights must be greater than 0
226
    if (rights > 0)
227
        mCanInviteUsers = true;
228
}
229
230
bool Party::isMember(const PartyMember *const member) const
231
{
232
    if (member == nullptr)
233
        return false;
234
235
    if ((member->mParty != nullptr) && member->mParty != this)
236
        return false;
237
238
    MemberList::const_iterator itr = mMembers.begin();
239
    const MemberList::const_iterator itr_end = mMembers.end();
240
    while (itr != itr_end)
241
    {
242
        const PartyMember *const m = *itr;
243
        if (m != nullptr &&
244
            m->mId == member->mId &&
245
            m->getName() == member->getName())
246
        {
247
            return true;
248
        }
249
        ++itr;
250
    }
251
252
    return false;
253
}
254
255
bool Party::isMember(const BeingId id) const
256
{
257
    MemberList::const_iterator itr = mMembers.begin();
258
    const MemberList::const_iterator itr_end = mMembers.end();
259
    while (itr != itr_end)
260
    {
261
        const PartyMember *const m = *itr;
262
        if ((m != nullptr) && m->mId == id)
263
            return true;
264
        ++itr;
265
    }
266
267
    return false;
268
}
269
270
bool Party::isMember(const std::string &name) const
271
{
272
    MemberList::const_iterator itr = mMembers.begin();
273
    const MemberList::const_iterator itr_end = mMembers.end();
274
    while (itr != itr_end)
275
    {
276
        const PartyMember *const m = *itr;
277
        if ((m != nullptr) && m->getName() == name)
278
            return true;
279
        ++itr;
280
    }
281
282
    return false;
283
}
284
285
6
void Party::getNames(StringVect &names) const
286
{
287
6
    names.clear();
288
12
    MemberList::const_iterator it = mMembers.begin();
289
12
    const MemberList::const_iterator it_end = mMembers.end();
290
22
    while (it != it_end)
291
    {
292
16
        const PartyMember *const m = *it;
293
16
        if (m != nullptr)
294
64
            names.push_back(m->getName());
295
        ++it;
296
    }
297
6
}
298
299
void Party::getNamesSet(std::set<std::string> &names) const
300
{
301
    names.clear();
302
    MemberList::const_iterator it = mMembers.begin();
303
    const MemberList::const_iterator it_end = mMembers.end();
304
    while (it != it_end)
305
    {
306
        const PartyMember *const m = *it;
307
        if (m != nullptr)
308
            names.insert(m->getName());
309
        ++it;
310
    }
311
}
312
313
2
Party *Party::getParty(const int16_t id)
314
{
315
6
    const PartyMap::const_iterator it = parties.find(id);
316
2
    if (it != parties.end())
317
        return it->second;
318
2
    Party *const party = new Party(id);
319
2
    parties[id] = party;
320
2
    return party;
321
}
322
323
void Party::sort()
324
{
325
    std::sort(mMembers.begin(), mMembers.end(), partySorter);
326
}
327
328
1
void Party::clearParties()
329
{
330
1
    PartyMap::iterator it = parties.begin();
331
3
    while (it != parties.end())
332
    {
333
2
        delete (*it).second;
334
        ++ it;
335
    }
336
1
    parties.clear();
337

4
}