GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/party.cpp Lines: 54 141 38.3 %
Date: 2017-11-29 Branches: 23 132 17.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2010  The Mana Developers
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 "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
            if (p1->getLeader())
43
                return true;
44
            if (p2->getLeader())
45
                return false;
46
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
12
                         const std::string &name) :
62
    Avatar(name),
63
    mParty(party),
64
12
    mLeader(false)
65
{
66
12
    mId = id;
67
}
68
69
2
Party::PartyMap Party::parties;
70
71
4
Party::Party(const int16_t id) :
72
    mMembers(),
73
    mName(),
74
    mId(id),
75
16
    mCanInviteUsers(false)
76
{
77
4
    parties[id] = this;
78
4
}
79
80
20
Party::~Party()
81
{
82
4
    clearMembers();
83
8
}
84
85
12
PartyMember *Party::addMember(const BeingId id,
86
                              const std::string &name)
87
{
88
12
    PartyMember *m = getMember(id);
89
12
    if (m != nullptr)
90
        return m;
91
92
24
    m = new PartyMember(this, id, name);
93
94
12
    mMembers.push_back(m);
95
96
12
    return m;
97
}
98
99
PartyMember *Party::getMember(const BeingId id) const
100
{
101
24
    MemberList::const_iterator itr = mMembers.begin();
102
24
    const MemberList::const_iterator itr_end = mMembers.end();
103

24
    while (itr != itr_end)
104
    {
105

12
        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
4
void Party::removeMember(const BeingId id)
156
{
157
4
    bool deleted = true;
158
8
    while (deleted)
159
    {
160
8
        deleted = false;
161
16
        MemberList::iterator itr = mMembers.begin();
162
16
        const MemberList::iterator itr_end = mMembers.end();
163
18
        while (itr != itr_end)
164
        {
165
14
            PartyMember *const member = *itr;
166

14
            if ((member != nullptr) && member->mId == id)
167
            {
168
12
                mMembers.erase(itr);
169
                delete member;
170
4
                deleted = true;
171
                break;
172
            }
173
            ++itr;
174
        }
175
    }
176
4
}
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
12
void Party::getNames(StringVect &names) const
286
{
287
12
    names.clear();
288
24
    MemberList::const_iterator it = mMembers.begin();
289
24
    const MemberList::const_iterator it_end = mMembers.end();
290
44
    while (it != it_end)
291
    {
292
32
        const PartyMember *const m = *it;
293
32
        if (m != nullptr)
294
128
            names.push_back(m->getName());
295
        ++it;
296
    }
297
12
}
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
4
Party *Party::getParty(const int16_t id)
314
{
315
12
    const PartyMap::const_iterator it = parties.find(id);
316
4
    if (it != parties.end())
317
        return it->second;
318
4
    Party *const party = new Party(id);
319
4
    parties[id] = party;
320
4
    return party;
321
}
322
323
void Party::sort()
324
{
325
    std::sort(mMembers.begin(), mMembers.end(), partySorter);
326
}
327
328
2
void Party::clearParties()
329
{
330
2
    PartyMap::iterator it = parties.begin();
331
6
    while (it != parties.end())
332
    {
333
4
        delete (*it).second;
334
        ++ it;
335
    }
336
2
    parties.clear();
337

8
}