GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/guild.cpp Lines: 2 161 1.2 %
Date: 2018-11-12 Branches: 2 184 1.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
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 "guild.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 SortGuildFunctor final
34
{
35
    public:
36
        A_DEFAULT_COPY(SortGuildFunctor)
37
38
        bool operator() (const GuildMember *const m1,
39
                         const GuildMember *const m2) const
40
        {
41
            if ((m1 == nullptr) || (m2 == nullptr))
42
                return false;
43
44
            if (m1->getOnline() != m2->getOnline())
45
            {
46
                return static_cast<int>(m1->getOnline()) >=
47
                    static_cast<int>(m2->getOnline());
48
            }
49
50
            if (m1->getPos() != m2->getPos())
51
                return m1->getPos() >= m2->getPos();
52
53
            if (m1->getName() != m2->getName())
54
            {
55
                std::string s1 = m1->getName();
56
                std::string s2 = m2->getName();
57
                toLower(s1);
58
                toLower(s2);
59
                return s1 < s2;
60
            }
61
            return false;
62
        }
63
} guildSorter;
64
65
GuildMember::GuildMember(Guild *const guild,
66
                         const BeingId accountId,
67
                         const int charId,
68
                         const std::string &name) :
69
    Avatar(name),
70
    mGuild(guild),
71
    mPos(0)
72
{
73
    mId = accountId;
74
    mCharId = charId;
75
}
76
77
GuildMember::GuildMember(Guild *const guild, const std::string &name) :
78
    Avatar(name),
79
    mGuild(guild),
80
    mPos(0)
81
{
82
}
83
84
std::string GuildMember::getAdditionString() const
85
{
86
    if (mGuild == nullptr)
87
        return "";
88
89
    return " - " + mGuild->getPos(mPos);
90
}
91
92
1
Guild::GuildMap Guild::guilds;
93
94
Guild::Guild(const int16_t id) :
95
    mMembers(),
96
    mPositions(),
97
    mName(),
98
    mEmblemId(0),
99
    mId(id),
100
    mCanInviteUsers(false),
101
    mServerGuild(true)
102
{
103
    guilds[id] = this;
104
}
105
106
Guild::~Guild()
107
{
108
    clearMembers();
109
}
110
111
GuildMember *Guild::addMember(const BeingId accountId,
112
                              const int charId,
113
                              const std::string &name)
114
{
115
    GuildMember *m = getMember(accountId, charId);
116
    if (m != nullptr)
117
        return m;
118
119
    m = new GuildMember(this, accountId, charId, name);
120
121
    mMembers.push_back(m);
122
123
    return m;
124
}
125
126
GuildMember *Guild::addMember(const std::string &name)
127
{
128
    GuildMember *m = getMember(name);
129
    if (m != nullptr)
130
        return m;
131
132
    m = new GuildMember(this, name);
133
134
    mMembers.push_back(m);
135
136
    return m;
137
}
138
139
GuildMember *Guild::getMember(const BeingId id) const
140
{
141
    MemberList::const_iterator itr = mMembers.begin();
142
    const MemberList::const_iterator itr_end = mMembers.end();
143
    while (itr != itr_end)
144
    {
145
        if (((*itr) != nullptr) && (*itr)->mId == id)
146
            return (*itr);
147
        ++itr;
148
    }
149
150
    return nullptr;
151
}
152
153
GuildMember *Guild::getMember(const BeingId accountId,
154
                              const int charId) const
155
{
156
    MemberList::const_iterator itr = mMembers.begin();
157
    const MemberList::const_iterator itr_end = mMembers.end();
158
    while (itr != itr_end)
159
    {
160
        if ((*itr) != nullptr &&
161
            (*itr)->mId == accountId &&
162
            (*itr)->mCharId == charId)
163
        {
164
            return (*itr);
165
        }
166
        ++itr;
167
    }
168
169
    return nullptr;
170
}
171
172
GuildMember *Guild::getMemberByCharId(const int charId) const
173
{
174
    MemberList::const_iterator itr = mMembers.begin();
175
    const MemberList::const_iterator itr_end = mMembers.end();
176
    while (itr != itr_end)
177
    {
178
        if ((*itr) != nullptr &&
179
            (*itr)->mCharId == charId)
180
        {
181
            return (*itr);
182
        }
183
        ++itr;
184
    }
185
186
    return nullptr;
187
}
188
189
GuildMember *Guild::getMember(const std::string &name) const
190
{
191
    MemberList::const_iterator itr = mMembers.begin();
192
    const MemberList::const_iterator itr_end = mMembers.end();
193
    while (itr != itr_end)
194
    {
195
        if (((*itr) != nullptr) && (*itr)->getName() == name)
196
            return (*itr);
197
        ++itr;
198
    }
199
200
    return nullptr;
201
}
202
203
void Guild::removeMember(const GuildMember *const member)
204
{
205
    if (member == nullptr)
206
        return;
207
    MemberList::iterator itr = mMembers.begin();
208
    const MemberList::iterator itr_end = mMembers.end();
209
    while (itr != itr_end)
210
    {
211
        if (*itr == nullptr)
212
            continue;
213
        if ((*itr)->mId == member->mId &&
214
            (*itr)->mCharId == member->mCharId &&
215
            (*itr)->getName() == member->getName())
216
        {
217
            GuildMember *m = *itr;
218
            mMembers.erase(itr);
219
            delete m;
220
            return;
221
        }
222
        ++ itr;
223
    }
224
}
225
226
void Guild::removeMember(const BeingId id)
227
{
228
    bool deleted = true;
229
    while (deleted)
230
    {
231
        deleted = false;
232
        MemberList::iterator itr = mMembers.begin();
233
        const MemberList::iterator itr_end = mMembers.end();
234
        while (itr != itr_end)
235
        {
236
            if (((*itr) != nullptr) && (*itr)->mId == id)
237
            {
238
                GuildMember *member = *itr;
239
                mMembers.erase(itr);
240
                delete member;
241
                deleted = true;
242
                break;
243
            }
244
            ++itr;
245
        }
246
    }
247
}
248
249
void Guild::removeMember(const std::string &name)
250
{
251
    bool deleted = true;
252
    while (deleted)
253
    {
254
        deleted = false;
255
        MemberList::iterator itr = mMembers.begin();
256
        const MemberList::iterator itr_end = mMembers.end();
257
        while (itr != itr_end)
258
        {
259
            if (((*itr) != nullptr) && (*itr)->getName() == name)
260
            {
261
                GuildMember *member = *itr;
262
                mMembers.erase(itr);
263
                delete member;
264
                deleted = true;
265
                break;
266
            }
267
            ++itr;
268
        }
269
    }
270
}
271
272
void Guild::removeFromMembers()
273
{
274
    if (actorManager == nullptr)
275
        return;
276
277
    MemberList::const_iterator itr = mMembers.begin();
278
    const MemberList::const_iterator itr_end = mMembers.end();
279
    while (itr != itr_end)
280
    {
281
        if (*itr != nullptr)
282
        {
283
            Being *const b = actorManager->findBeing((*itr)->getID());
284
            if (b != nullptr)
285
                b->removeGuild(mId);
286
        }
287
        ++itr;
288
    }
289
}
290
291
Avatar *Guild::getAvatarAt(const int index)
292
{
293
    return mMembers[index];
294
}
295
296
void Guild::setRights(const int16_t rights)
297
{
298
    // to invite, rights must be greater than 0
299
    if (rights > 0)
300
        mCanInviteUsers = true;
301
}
302
303
bool Guild::isMember(const GuildMember *const member) const
304
{
305
    if (member == nullptr)
306
        return false;
307
308
    if ((member->mGuild != nullptr) && member->mGuild != this)
309
        return false;
310
311
    MemberList::const_iterator itr = mMembers.begin();
312
    const MemberList::const_iterator itr_end = mMembers.end();
313
    while (itr != itr_end)
314
    {
315
        if (((*itr) != nullptr) && (*itr)->mId == member->mId &&
316
            (*itr)->getName() == member->getName())
317
        {
318
            return true;
319
        }
320
        ++itr;
321
    }
322
323
    return false;
324
}
325
326
bool Guild::isMember(const BeingId id) const
327
{
328
    MemberList::const_iterator itr = mMembers.begin();
329
    const MemberList::const_iterator itr_end = mMembers.end();
330
    while (itr != itr_end)
331
    {
332
        if (((*itr) != nullptr) && (*itr)->mId == id)
333
            return true;
334
        ++itr;
335
    }
336
337
    return false;
338
}
339
340
bool Guild::isMember(const std::string &name) const
341
{
342
    MemberList::const_iterator itr = mMembers.begin();
343
    const MemberList::const_iterator itr_end = mMembers.end();
344
    while (itr != itr_end)
345
    {
346
        if (((*itr) != nullptr) && (*itr)->getName() == name)
347
            return true;
348
        ++itr;
349
    }
350
351
    return false;
352
}
353
354
void Guild::getNames(StringVect &names) const
355
{
356
    names.clear();
357
    MemberList::const_iterator it = mMembers.begin();
358
    const MemberList::const_iterator it_end = mMembers.end();
359
360
    while (it != it_end)
361
    {
362
        if (*it != nullptr)
363
            names.push_back((*it)->getName());
364
        ++it;
365
    }
366
}
367
368
void Guild::addPos(const int id, const std::string &name)
369
{
370
    mPositions[id] = name;
371
}
372
373
Guild *Guild::getGuild(const int16_t id)
374
{
375
    const GuildMap::const_iterator it = guilds.find(id);
376
    if (it != guilds.end())
377
        return it->second;
378
379
    Guild *const guild = new Guild(id);
380
    guilds[id] = guild;
381
    return guild;
382
}
383
384
std::string Guild::getPos(const int id) const
385
{
386
    const PositionsMap::const_iterator it = mPositions.find(id);
387
    if (it == mPositions.end())
388
        return "";
389
    return it->second;
390
}
391
392
void Guild::sort()
393
{
394
    std::sort(mMembers.begin(), mMembers.end(), guildSorter);
395
}
396
397
void Guild::clearGuilds()
398
{
399
    GuildMap::iterator it = guilds.begin();
400
    while (it != guilds.end())
401
    {
402
        delete (*it).second;
403
        ++ it;
404
    }
405
    guilds.clear();
406

3
}