summaryrefslogtreecommitdiff
path: root/src/chat-server/guild.cpp
blob: 47ace46e7020878cdf065026502c89cd50f339b4 (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
/*
 *  The Mana Server
 *  Copyright (C) 2004-2010  The Mana World Development Team
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server 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.
 *
 *  The Mana Server 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 The Mana Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "guild.h"

#include "chat-server/guildmanager.h"

#include "common/defines.h"

#include <algorithm>

Guild::Guild(const std::string &name) :
    mName(name)
{
}

Guild::~Guild()
{
}

void Guild::addMember(int playerId, int permissions)
{
    // create new guild member
    GuildMember *member = new GuildMember;
    member->mId = playerId;
    member->mPermissions = permissions;

    // add new guild member to guild
    mMembers.push_back(member);

    mInvited.remove(playerId);
}

void Guild::removeMember(int playerId)
{
    if (getOwner() == playerId)
    {
        // if the leader is leaving, assign next member as leader
        for (std::list<GuildMember*>::iterator it = mMembers.begin(),
             it_end = mMembers.end(); it != it_end; ++it)
        {
            GuildMember *member = *it;
            if (member->mId != playerId)
            {
                setOwner(member->mId);
                break;
            }
        }
    }
    GuildMember *member = getMember(playerId);
    if (member)
        mMembers.remove(member);
}

int Guild::getOwner() const
{
    std::list<GuildMember*>::const_iterator itr = mMembers.begin();
    std::list<GuildMember*>::const_iterator itr_end = mMembers.end();

    while (itr != itr_end)
    {
        if ((*itr)->mPermissions == GAL_OWNER)
            return (*itr)->mId;
        ++itr;
    }

    return 0;
}

void Guild::setOwner(int playerId)
{
    guildManager->setUserRights(this, playerId, GAL_OWNER);
}

bool Guild::checkInvited(int playerId) const
{
    return std::find(mInvited.begin(), mInvited.end(), playerId) != mInvited.end();
}

void Guild::addInvited(int playerId)
{
    mInvited.push_back(playerId);
}

void Guild::removeInvited(int playerId)
{
    mInvited.remove(playerId);
}

bool Guild::checkInGuild(int playerId) const
{
    return getMember(playerId) != 0;
}

GuildMember *Guild::getMember(int playerId) const
{
    std::list<GuildMember*>::const_iterator itr = mMembers.begin(),
                                            itr_end = mMembers.end();
    while (itr != itr_end)
    {
        if ((*itr)->mId == playerId)
            return (*itr);
        ++itr;
    }

    return 0;
}

bool Guild::canInvite(int playerId) const
{
    // Guild members with permissions above NONE can invite
    // Check that guild members permissions are not NONE
    GuildMember *member = getMember(playerId);
    if (member->mPermissions & GAL_INVITE)
        return true;
    return false;
}

int Guild::getUserPermissions(int playerId) const
{
    GuildMember *member = getMember(playerId);
    return member ? member->mPermissions : 0;
}

void Guild::setUserPermissions(int playerId, int level)
{
    GuildMember *member = getMember(playerId);
    member->mPermissions = level;
}