summaryrefslogtreecommitdiff
path: root/src/guild.cpp
blob: 5e6b5cc2e029ab110d49efc5a178793b271ce5f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "guild.h"

#include "actorspritemanager.h"

#include "debug.h"

static class SortGuildFunctor final
{
    public:
        bool operator() (const GuildMember *const m1,
                         const GuildMember *const m2) const
        {
            if (!m1 || !m2)
                return false;

            if (m1->getOnline() != m2->getOnline())
                return m1->getOnline() > m2->getOnline();

            if (m1->getPos() != m2->getPos())
                return m1->getPos() > m2->getPos();

            if (m1->getName() != m2->getName())
            {
                std::string s1 = m1->getName();
                std::string s2 = m2->getName();
                toLower(s1);
                toLower(s2);
                return s1 < s2;
            }
            return false;
        }
} guildSorter;

GuildMember::GuildMember(Guild *const guild, const int accountId,
                         const int charId, const std::string &name):
        Avatar(name), mGuild(guild), mPos(0)
{
    mId = accountId;
    mCharId = charId;
}

GuildMember::GuildMember(Guild *const guild, const std::string &name):
        Avatar(name), mGuild(guild), mPos(0)
{
}

std::string GuildMember::getAdditionString() const
{
    if (!mGuild)
        return "";

    return " - " + mGuild->getPos(mPos);
}

Guild::GuildMap Guild::guilds;

Guild::Guild(const short id):
    mMembers(),
    mName(),
    mId(id),
    mCanInviteUsers(false),
    mEmblemId(0),
    mPositions(),
    mServerGuild(true)
{
    guilds[id] = this;
}

Guild::~Guild()
{
    clearMembers();
}

GuildMember *Guild::addMember(const int accountId, const int charId,
                              const std::string &name)
{
    GuildMember *m;
    if ((m = getMember(accountId, charId)))
        return m;

    m = new GuildMember(this, accountId, charId, name);

    mMembers.push_back(m);

    return m;
}

GuildMember *Guild::addMember(const std::string &name)
{
    GuildMember *m;
    if ((m = getMember(name)))
        return m;

    m = new GuildMember(this, name);

    mMembers.push_back(m);

    return m;
}

GuildMember *Guild::getMember(const int id) const
{
    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->mId == id)
            return (*itr);
        ++itr;
    }

    return nullptr;
}

GuildMember *Guild::getMember(const int accountId, const int charId) const
{
    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->mId == accountId && (*itr)->mCharId == charId)
            return (*itr);
        ++itr;
    }

    return nullptr;
}

GuildMember *Guild::getMember(const std::string &name) const
{
    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->getName() == name)
            return (*itr);
        ++itr;
    }

    return nullptr;
}

void Guild::removeMember(const GuildMember *const member)
{
    MemberList::iterator itr = mMembers.begin();
    const MemberList::iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if (!*itr)
            continue;
        if ((*itr)->mId == member->mId &&
            (*itr)->mCharId == member->mCharId &&
            (*itr)->getName() == member->getName())
        {
            GuildMember *m = *itr;
            mMembers.erase(itr);
            delete m;
            return;
        }
        ++ itr;
    }
}

void Guild::removeMember(const int id)
{
    bool deleted = true;
    while (deleted)
    {
        deleted = false;
        MemberList::iterator itr = mMembers.begin();
        const MemberList::iterator itr_end = mMembers.end();
        while (itr != itr_end)
        {
            if ((*itr) && (*itr)->mId == id)
            {
                GuildMember *member = *itr;
                mMembers.erase(itr);
                delete member;
                deleted = true;
                break;
            }
            ++itr;
        }
    }
}

void Guild::removeMember(const std::string &name)
{
    bool deleted = true;
    while (deleted)
    {
        deleted = false;
        MemberList::iterator itr = mMembers.begin();
        const MemberList::iterator itr_end = mMembers.end();
        while (itr != itr_end)
        {
            if ((*itr) && (*itr)->getName() == name)
            {
                GuildMember *member = *itr;
                mMembers.erase(itr);
                delete member;
                deleted = true;
                break;
            }
            ++itr;
        }
    }
}

void Guild::removeFromMembers()
{
    if (!actorSpriteManager)
        return;

    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if (*itr)
        {
            Being *const b = actorSpriteManager->findBeing((*itr)->getID());
            if (b)
                b->removeGuild(mId);
        }
        ++itr;
    }
}

Avatar *Guild::getAvatarAt(const int index)
{
    return mMembers[index];
}

void Guild::setRights(const short rights)
{
    // to invite, rights must be greater than 0
    if (rights > 0)
        mCanInviteUsers = true;
}

bool Guild::isMember(const GuildMember *const member) const
{
    if (!member)
        return false;

    if (member->mGuild && member->mGuild != this)
        return false;

    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->mId == member->mId &&
            (*itr)->getName() == member->getName())
        {
            return true;
        }
        ++itr;
    }

    return false;
}

bool Guild::isMember(const int id) const
{
    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->mId == id)
            return true;
        ++itr;
    }

    return false;
}

bool Guild::isMember(const std::string &name) const
{
    MemberList::const_iterator itr = mMembers.begin();
    const MemberList::const_iterator itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr) && (*itr)->getName() == name)
            return true;
        ++itr;
    }

    return false;
}

void Guild::getNames(StringVect &names) const
{
    names.clear();
    MemberList::const_iterator it = mMembers.begin();
    const MemberList::const_iterator it_end = mMembers.end();

    while (it != it_end)
    {
        if (*it)
            names.push_back((*it)->getName());
        ++it;
    }
}

void Guild::addPos(const int id, const std::string &name)
{
    mPositions[id] = name;
}

Guild *Guild::getGuild(const short id)
{
    const GuildMap::const_iterator it = guilds.find(id);
    if (it != guilds.end())
        return it->second;

    Guild *const guild = new Guild(id);
    guilds[id] = guild;
    return guild;
}

std::string Guild::getPos(const int id) const
{
    const PositionsMap::const_iterator it = mPositions.find(id);
    if (it == mPositions.end())
        return "";
    else
        return it->second;
}

void Guild::sort()
{
    std::sort(mMembers.begin(), mMembers.end(), guildSorter);
}

void Guild::clearGuilds()
{
    GuildMap::iterator it = guilds.begin();
    while (it != guilds.end())
    {
        delete (*it).second;
        ++ it;
    }
    guilds.clear();
}