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 #ifndef GUILD_H
25 #define GUILD_H
26 
27 #include "gui/models/avatarlistmodel.h"
28 
29 #include "utils/cast.h"
30 #include "utils/dtor.h"
31 #include "utils/stringvector.h"
32 
33 #include <map>
34 
35 class Guild;
36 
37 typedef std::map<unsigned, std::string> PositionsMap;
38 
39 class GuildMember final : public Avatar
40 {
41     public:
A_DELETE_COPY(GuildMember)42         A_DELETE_COPY(GuildMember)
43 
44         const Guild *getGuild() const noexcept2 A_WARN_UNUSED
45         { return mGuild; }
46 
getPos()47         int getPos() const noexcept2 A_WARN_UNUSED
48         { return mPos; }
49 
setPos(const int pos)50         void setPos(const int pos)
51         { mPos = pos; }
52 
53         std::string getAdditionString() const override final A_WARN_UNUSED;
54 
55     protected:
56         friend class Guild;
57 
58         GuildMember(Guild *const guild,
59                     const BeingId accountId,
60                     const int charId,
61                     const std::string &name);
62 
63         GuildMember(Guild *const guild, const std::string &name);
64 
65         Guild *mGuild;
66         int mPos;
67 };
68 
69 class Guild final : public AvatarListModel
70 {
71     public:
72         A_DELETE_COPY(Guild)
73 
74         ~Guild() override final;
75 
76         /**
77          * Set the guild's name.
78          */
setName(const std::string & name)79         void setName(const std::string &name)
80         { mName = name; }
81 
82         /**
83          * Adds member to the list.
84          */
85         GuildMember *addMember(const BeingId accountId,
86                                const int charId,
87                                const std::string &name);
88 
89         /**
90          * Adds member to the list.
91          */
92         GuildMember *addMember(const std::string &name);
93 
94         /**
95          * Find a member by ID.
96          *
97          * @return the member with the given ID, or NULL if they don't exist.
98          */
99         GuildMember *getMember(const BeingId id) const;
100 
101         /**
102          * Find a member by account ID and char ID.
103          *
104          * @return the member with the given ID, or NULL if they don't exist.
105          */
106         GuildMember *getMember(const BeingId accountId,
107                                const int charId)
108                                const A_WARN_UNUSED;
109 
110         /**
111          * Find a member by char ID.
112          *
113          * @return the member with the given ID, or NULL if they don't exist.
114          */
115         GuildMember *getMemberByCharId(const int charId) const A_WARN_UNUSED;
116 
117         /**
118          * Find a member by name.
119          *
120          * @return the member with the given name, or NULL if they don't exist.
121          */
122         GuildMember *getMember(const std::string &name) const A_WARN_UNUSED;
123 
124         /**
125          * Get the name of the guild.
126          * @return returns name of the guild
127          */
getName()128         const std::string &getName() const noexcept2 A_WARN_UNUSED
129         { return mName; }
130 
131         /**
132          * Get the id of the guild.
133          * @return Returns the id of the guild
134          */
getId()135         int16_t getId() const noexcept2 A_WARN_UNUSED
136         { return mId; }
137 
138         /**
139          * Removes a member from the guild.
140          */
141         void removeMember(const GuildMember *const member);
142 
143         /**
144          * Removes a member from the guild by account id.
145          */
146         void removeMember(const BeingId id);
147 
148         /**
149          * Removes a member from the guild.
150          */
151         void removeMember(const std::string &name);
152 
153         void removeFromMembers();
154 
clearMembers()155         void clearMembers()
156         { delete_all(mMembers); mMembers.clear(); }
157 
158         /**
159          * Get size of members list.
160          * @return Returns the number of members in the guild.
161          */
getNumberOfElements()162         int getNumberOfElements() override final A_WARN_UNUSED
163         { return CAST_S32(mMembers.size()); }
164 
165         Avatar *getAvatarAt(const int index) override final A_WARN_UNUSED;
166 
167         /**
168          * Get whether user can invite users to this guild.
169          * @return Returns true if user can invite users
170          */
getInviteRights()171         bool getInviteRights() const noexcept2 A_WARN_UNUSED
172         { return mCanInviteUsers; }
173 
174         void setRights(const int16_t rights);
175 
176         bool isMember(const GuildMember *const member) const A_WARN_UNUSED;
177 
178         bool isMember(const BeingId id) const A_WARN_UNUSED;
179 
180         bool isMember(const std::string &name) const A_WARN_UNUSED;
181 
182         void getNames(StringVect &names) const;
183 
184         void addPos(const int id, const std::string &name);
185 
186         void sort();
187 
188         std::string getPos(const int id) const A_WARN_UNUSED;
189 
190         static Guild *getGuild(const int16_t id) A_WARN_UNUSED;
191 
getPositions()192         const PositionsMap &getPositions() const noexcept2 A_WARN_UNUSED
193         { return mPositions; }
194 
setEmblemId(const int id)195         void setEmblemId(const int id)
196         { mEmblemId = id; }
197 
getEmblemId()198         int getEmblemId() const noexcept2 A_WARN_UNUSED
199         { return mEmblemId; }
200 
201         static void clearGuilds();
202 
setServerGuild(const bool b)203         void setServerGuild(const bool b)
204         { mServerGuild = b; }
205 
getServerGuild()206         bool getServerGuild() const noexcept2 A_WARN_UNUSED
207         { return mServerGuild; }
208 
209         typedef STD_VECTOR<GuildMember*> MemberList;
210 
getMembers()211         const MemberList *getMembers() const RETURNS_NONNULL A_WARN_UNUSED
212         { return &mMembers; }
213 
214     private:
215         typedef std::map<int, Guild*> GuildMap;
216         static GuildMap guilds;
217 
218         /**
219          * Constructor with guild id passed to it.
220          */
221         explicit Guild(const int16_t id);
222 
223         MemberList mMembers;
224         PositionsMap mPositions;
225         std::string mName;
226         int mEmblemId;
227         int16_t mId;
228         bool mCanInviteUsers;
229         bool mServerGuild;
230 };
231 
232 #endif  // GUILD_H
233