GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/party.cpp Lines: 55 144 38.2 %
Date: 2018-07-14 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-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 "party.h"
23
24
#include "actormanager.h"
25
26
#include "being/being.h"
27
28
#include "utils/stringutils.h"
29
30
#include "debug.h"
31
32
static class SortPartyFunctor final
33
{
34
    public:
35
        A_DEFAULT_COPY(SortPartyFunctor)
36
37
        bool operator() (const PartyMember *const p1,
38
                         const PartyMember *const p2) const
39
        {
40
            if (p1 == nullptr || p2 == nullptr)
41
                return false;
42
            const int leader1 = CAST_S32(p1->getLeader());
43
            const int leader2 = CAST_S32(p2->getLeader());
44
            if (leader1 != leader2)
45
                return leader1 >= leader2;
46
            if (p1->getName() != p2->getName())
47
            {
48
                std::string s1 = p1->getName();
49
                std::string s2 = p2->getName();
50
                toLower(s1);
51
                toLower(s2);
52
                return s1 < s2;
53
            }
54
            return false;
55
        }
56
} partySorter;
57
58
PartyMember::PartyMember(Party *const party,
59
                         const BeingId id,
60
6
                         const std::string &name) :
61
    Avatar(name),
62
    mParty(party),
63
6
    mLeader(false)
64
{
65
6
    mId = id;
66
}
67
68
1
Party::PartyMap Party::parties;
69
70
2
Party::Party(const int16_t id) :
71
    mMembers(),
72
    mName(),
73
    mId(id),
74
8
    mCanInviteUsers(false)
75
{
76
2
    parties[id] = this;
77
2
}
78
79
10
Party::~Party()
80
{
81
2
    clearMembers();
82
4
}
83
84
6
PartyMember *Party::addMember(const BeingId id,
85
                              const std::string &name)
86
{
87
6
    PartyMember *m = getMember(id);
88
6
    if (m != nullptr)
89
        return m;
90
91
12
    m = new PartyMember(this, id, name);
92
93
6
    mMembers.push_back(m);
94
95
6
    return m;
96
}
97
98
PartyMember *Party::getMember(const BeingId id) const
99
{
100
12
    MemberList::const_iterator itr = mMembers.begin();
101
12
    const MemberList::const_iterator itr_end = mMembers.end();
102

12
    while (itr != itr_end)
103
    {
104

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

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

4
}