GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/guild.cpp Lines: 2 156 1.3 %
Date: 2017-11-29 Branches: 2 154 1.3 %

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-2017  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
2
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::getMember(const std::string &name) 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) && (*itr)->getName() == name)
179
            return (*itr);
180
        ++itr;
181
    }
182
183
    return nullptr;
184
}
185
186
void Guild::removeMember(const GuildMember *const member)
187
{
188
    if (member == nullptr)
189
        return;
190
    MemberList::iterator itr = mMembers.begin();
191
    const MemberList::iterator itr_end = mMembers.end();
192
    while (itr != itr_end)
193
    {
194
        if (*itr == nullptr)
195
            continue;
196
        if ((*itr)->mId == member->mId &&
197
            (*itr)->mCharId == member->mCharId &&
198
            (*itr)->getName() == member->getName())
199
        {
200
            GuildMember *m = *itr;
201
            mMembers.erase(itr);
202
            delete m;
203
            return;
204
        }
205
        ++ itr;
206
    }
207
}
208
209
void Guild::removeMember(const BeingId id)
210
{
211
    bool deleted = true;
212
    while (deleted)
213
    {
214
        deleted = false;
215
        MemberList::iterator itr = mMembers.begin();
216
        const MemberList::iterator itr_end = mMembers.end();
217
        while (itr != itr_end)
218
        {
219
            if (((*itr) != nullptr) && (*itr)->mId == id)
220
            {
221
                GuildMember *member = *itr;
222
                mMembers.erase(itr);
223
                delete member;
224
                deleted = true;
225
                break;
226
            }
227
            ++itr;
228
        }
229
    }
230
}
231
232
void Guild::removeMember(const std::string &name)
233
{
234
    bool deleted = true;
235
    while (deleted)
236
    {
237
        deleted = false;
238
        MemberList::iterator itr = mMembers.begin();
239
        const MemberList::iterator itr_end = mMembers.end();
240
        while (itr != itr_end)
241
        {
242
            if (((*itr) != nullptr) && (*itr)->getName() == name)
243
            {
244
                GuildMember *member = *itr;
245
                mMembers.erase(itr);
246
                delete member;
247
                deleted = true;
248
                break;
249
            }
250
            ++itr;
251
        }
252
    }
253
}
254
255
void Guild::removeFromMembers()
256
{
257
    if (actorManager == nullptr)
258
        return;
259
260
    MemberList::const_iterator itr = mMembers.begin();
261
    const MemberList::const_iterator itr_end = mMembers.end();
262
    while (itr != itr_end)
263
    {
264
        if (*itr != nullptr)
265
        {
266
            Being *const b = actorManager->findBeing((*itr)->getID());
267
            if (b != nullptr)
268
                b->removeGuild(mId);
269
        }
270
        ++itr;
271
    }
272
}
273
274
Avatar *Guild::getAvatarAt(const int index)
275
{
276
    return mMembers[index];
277
}
278
279
void Guild::setRights(const int16_t rights)
280
{
281
    // to invite, rights must be greater than 0
282
    if (rights > 0)
283
        mCanInviteUsers = true;
284
}
285
286
bool Guild::isMember(const GuildMember *const member) const
287
{
288
    if (member == nullptr)
289
        return false;
290
291
    if ((member->mGuild != nullptr) && member->mGuild != this)
292
        return false;
293
294
    MemberList::const_iterator itr = mMembers.begin();
295
    const MemberList::const_iterator itr_end = mMembers.end();
296
    while (itr != itr_end)
297
    {
298
        if (((*itr) != nullptr) && (*itr)->mId == member->mId &&
299
            (*itr)->getName() == member->getName())
300
        {
301
            return true;
302
        }
303
        ++itr;
304
    }
305
306
    return false;
307
}
308
309
bool Guild::isMember(const BeingId id) const
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 == id)
316
            return true;
317
        ++itr;
318
    }
319
320
    return false;
321
}
322
323
bool Guild::isMember(const std::string &name) const
324
{
325
    MemberList::const_iterator itr = mMembers.begin();
326
    const MemberList::const_iterator itr_end = mMembers.end();
327
    while (itr != itr_end)
328
    {
329
        if (((*itr) != nullptr) && (*itr)->getName() == name)
330
            return true;
331
        ++itr;
332
    }
333
334
    return false;
335
}
336
337
void Guild::getNames(StringVect &names) const
338
{
339
    names.clear();
340
    MemberList::const_iterator it = mMembers.begin();
341
    const MemberList::const_iterator it_end = mMembers.end();
342
343
    while (it != it_end)
344
    {
345
        if (*it != nullptr)
346
            names.push_back((*it)->getName());
347
        ++it;
348
    }
349
}
350
351
void Guild::addPos(const int id, const std::string &name)
352
{
353
    mPositions[id] = name;
354
}
355
356
Guild *Guild::getGuild(const int16_t id)
357
{
358
    const GuildMap::const_iterator it = guilds.find(id);
359
    if (it != guilds.end())
360
        return it->second;
361
362
    Guild *const guild = new Guild(id);
363
    guilds[id] = guild;
364
    return guild;
365
}
366
367
std::string Guild::getPos(const int id) const
368
{
369
    const PositionsMap::const_iterator it = mPositions.find(id);
370
    if (it == mPositions.end())
371
        return "";
372
    return it->second;
373
}
374
375
void Guild::sort()
376
{
377
    std::sort(mMembers.begin(), mMembers.end(), guildSorter);
378
}
379
380
void Guild::clearGuilds()
381
{
382
    GuildMap::iterator it = guilds.begin();
383
    while (it != guilds.end())
384
    {
385
        delete (*it).second;
386
        ++ it;
387
    }
388
    guilds.clear();
389

6
}