GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/guild.cpp Lines: 2 161 1.2 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "guild.h"
25
26
#include "actormanager.h"
27
28
#include "being/being.h"
29
30
#include "utils/stringutils.h"
31
32
#include "debug.h"
33
34
static class SortGuildFunctor final
35
{
36
    public:
37
        A_DEFAULT_COPY(SortGuildFunctor)
38
39
        bool operator() (const GuildMember *const m1,
40
                         const GuildMember *const m2) const
41
        {
42
            if ((m1 == nullptr) || (m2 == nullptr))
43
                return false;
44
45
            if (m1->getOnline() != m2->getOnline())
46
            {
47
                return static_cast<int>(m1->getOnline()) >=
48
                    static_cast<int>(m2->getOnline());
49
            }
50
51
            if (m1->getPos() != m2->getPos())
52
                return m1->getPos() >= m2->getPos();
53
54
            if (m1->getName() != m2->getName())
55
            {
56
                std::string s1 = m1->getName();
57
                std::string s2 = m2->getName();
58
                toLower(s1);
59
                toLower(s2);
60
                return s1 < s2;
61
            }
62
            return false;
63
        }
64
} guildSorter;
65
66
GuildMember::GuildMember(Guild *const guild,
67
                         const BeingId accountId,
68
                         const int charId,
69
                         const std::string &name) :
70
    Avatar(name),
71
    mGuild(guild),
72
    mPos(0)
73
{
74
    mId = accountId;
75
    mCharId = charId;
76
}
77
78
GuildMember::GuildMember(Guild *const guild, const std::string &name) :
79
    Avatar(name),
80
    mGuild(guild),
81
    mPos(0)
82
{
83
}
84
85
std::string GuildMember::getAdditionString() const
86
{
87
    if (mGuild == nullptr)
88
        return "";
89
90
    return " - " + mGuild->getPos(mPos);
91
}
92
93
1
Guild::GuildMap Guild::guilds;
94
95
Guild::Guild(const int16_t id) :
96
    mMembers(),
97
    mPositions(),
98
    mName(),
99
    mEmblemId(0),
100
    mId(id),
101
    mCanInviteUsers(false),
102
    mServerGuild(true)
103
{
104
    guilds[id] = this;
105
}
106
107
Guild::~Guild()
108
{
109
    clearMembers();
110
}
111
112
GuildMember *Guild::addMember(const BeingId accountId,
113
                              const int charId,
114
                              const std::string &name)
115
{
116
    GuildMember *m = getMember(accountId, charId);
117
    if (m != nullptr)
118
        return m;
119
120
    m = new GuildMember(this, accountId, charId, name);
121
122
    mMembers.push_back(m);
123
124
    return m;
125
}
126
127
GuildMember *Guild::addMember(const std::string &name)
128
{
129
    GuildMember *m = getMember(name);
130
    if (m != nullptr)
131
        return m;
132
133
    m = new GuildMember(this, name);
134
135
    mMembers.push_back(m);
136
137
    return m;
138
}
139
140
GuildMember *Guild::getMember(const BeingId id) const
141
{
142
    MemberList::const_iterator itr = mMembers.begin();
143
    const MemberList::const_iterator itr_end = mMembers.end();
144
    while (itr != itr_end)
145
    {
146
        if (((*itr) != nullptr) && (*itr)->mId == id)
147
            return (*itr);
148
        ++itr;
149
    }
150
151
    return nullptr;
152
}
153
154
GuildMember *Guild::getMember(const BeingId accountId,
155
                              const int charId) const
156
{
157
    MemberList::const_iterator itr = mMembers.begin();
158
    const MemberList::const_iterator itr_end = mMembers.end();
159
    while (itr != itr_end)
160
    {
161
        if ((*itr) != nullptr &&
162
            (*itr)->mId == accountId &&
163
            (*itr)->mCharId == charId)
164
        {
165
            return (*itr);
166
        }
167
        ++itr;
168
    }
169
170
    return nullptr;
171
}
172
173
GuildMember *Guild::getMemberByCharId(const int charId) const
174
{
175
    MemberList::const_iterator itr = mMembers.begin();
176
    const MemberList::const_iterator itr_end = mMembers.end();
177
    while (itr != itr_end)
178
    {
179
        if ((*itr) != nullptr &&
180
            (*itr)->mCharId == charId)
181
        {
182
            return (*itr);
183
        }
184
        ++itr;
185
    }
186
187
    return nullptr;
188
}
189
190
GuildMember *Guild::getMember(const std::string &name) const
191
{
192
    MemberList::const_iterator itr = mMembers.begin();
193
    const MemberList::const_iterator itr_end = mMembers.end();
194
    while (itr != itr_end)
195
    {
196
        if (((*itr) != nullptr) && (*itr)->getName() == name)
197
            return (*itr);
198
        ++itr;
199
    }
200
201
    return nullptr;
202
}
203
204
void Guild::removeMember(const GuildMember *const member)
205
{
206
    if (member == nullptr)
207
        return;
208
    MemberList::iterator itr = mMembers.begin();
209
    const MemberList::iterator itr_end = mMembers.end();
210
    while (itr != itr_end)
211
    {
212
        if (*itr == nullptr)
213
            continue;
214
        if ((*itr)->mId == member->mId &&
215
            (*itr)->mCharId == member->mCharId &&
216
            (*itr)->getName() == member->getName())
217
        {
218
            GuildMember *m = *itr;
219
            mMembers.erase(itr);
220
            delete m;
221
            return;
222
        }
223
        ++ itr;
224
    }
225
}
226
227
void Guild::removeMember(const BeingId id)
228
{
229
    bool deleted = true;
230
    while (deleted)
231
    {
232
        deleted = false;
233
        MemberList::iterator itr = mMembers.begin();
234
        const MemberList::iterator itr_end = mMembers.end();
235
        while (itr != itr_end)
236
        {
237
            if (((*itr) != nullptr) && (*itr)->mId == id)
238
            {
239
                GuildMember *member = *itr;
240
                mMembers.erase(itr);
241
                delete member;
242
                deleted = true;
243
                break;
244
            }
245
            ++itr;
246
        }
247
    }
248
}
249
250
void Guild::removeMember(const std::string &name)
251
{
252
    bool deleted = true;
253
    while (deleted)
254
    {
255
        deleted = false;
256
        MemberList::iterator itr = mMembers.begin();
257
        const MemberList::iterator itr_end = mMembers.end();
258
        while (itr != itr_end)
259
        {
260
            if (((*itr) != nullptr) && (*itr)->getName() == name)
261
            {
262
                GuildMember *member = *itr;
263
                mMembers.erase(itr);
264
                delete member;
265
                deleted = true;
266
                break;
267
            }
268
            ++itr;
269
        }
270
    }
271
}
272
273
void Guild::removeFromMembers()
274
{
275
    if (actorManager == nullptr)
276
        return;
277
278
    MemberList::const_iterator itr = mMembers.begin();
279
    const MemberList::const_iterator itr_end = mMembers.end();
280
    while (itr != itr_end)
281
    {
282
        if (*itr != nullptr)
283
        {
284
            Being *const b = actorManager->findBeing((*itr)->getID());
285
            if (b != nullptr)
286
                b->removeGuild(mId);
287
        }
288
        ++itr;
289
    }
290
}
291
292
Avatar *Guild::getAvatarAt(const int index)
293
{
294
    return mMembers[index];
295
}
296
297
void Guild::setRights(const int16_t rights)
298
{
299
    // to invite, rights must be greater than 0
300
    if (rights > 0)
301
        mCanInviteUsers = true;
302
}
303
304
bool Guild::isMember(const GuildMember *const member) const
305
{
306
    if (member == nullptr)
307
        return false;
308
309
    if ((member->mGuild != nullptr) && member->mGuild != this)
310
        return false;
311
312
    MemberList::const_iterator itr = mMembers.begin();
313
    const MemberList::const_iterator itr_end = mMembers.end();
314
    while (itr != itr_end)
315
    {
316
        if (((*itr) != nullptr) && (*itr)->mId == member->mId &&
317
            (*itr)->getName() == member->getName())
318
        {
319
            return true;
320
        }
321
        ++itr;
322
    }
323
324
    return false;
325
}
326
327
bool Guild::isMember(const BeingId id) const
328
{
329
    MemberList::const_iterator itr = mMembers.begin();
330
    const MemberList::const_iterator itr_end = mMembers.end();
331
    while (itr != itr_end)
332
    {
333
        if (((*itr) != nullptr) && (*itr)->mId == id)
334
            return true;
335
        ++itr;
336
    }
337
338
    return false;
339
}
340
341
bool Guild::isMember(const std::string &name) const
342
{
343
    MemberList::const_iterator itr = mMembers.begin();
344
    const MemberList::const_iterator itr_end = mMembers.end();
345
    while (itr != itr_end)
346
    {
347
        if (((*itr) != nullptr) && (*itr)->getName() == name)
348
            return true;
349
        ++itr;
350
    }
351
352
    return false;
353
}
354
355
void Guild::getNames(StringVect &names) const
356
{
357
    names.clear();
358
    MemberList::const_iterator it = mMembers.begin();
359
    const MemberList::const_iterator it_end = mMembers.end();
360
361
    while (it != it_end)
362
    {
363
        if (*it != nullptr)
364
            names.push_back((*it)->getName());
365
        ++it;
366
    }
367
}
368
369
void Guild::addPos(const int id, const std::string &name)
370
{
371
    mPositions[id] = name;
372
}
373
374
Guild *Guild::getGuild(const int16_t id)
375
{
376
    const GuildMap::const_iterator it = guilds.find(id);
377
    if (it != guilds.end())
378
        return it->second;
379
380
    Guild *const guild = new Guild(id);
381
    guilds[id] = guild;
382
    return guild;
383
}
384
385
std::string Guild::getPos(const int id) const
386
{
387
    const PositionsMap::const_iterator it = mPositions.find(id);
388
    if (it == mPositions.end())
389
        return "";
390
    return it->second;
391
}
392
393
void Guild::sort()
394
{
395
    std::sort(mMembers.begin(), mMembers.end(), guildSorter);
396
}
397
398
void Guild::clearGuilds()
399
{
400
    GuildMap::iterator it = guilds.begin();
401
    while (it != guilds.end())
402
    {
403
        delete (*it).second;
404
        ++ it;
405
    }
406
    guilds.clear();
407

3
}