1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2010  The Mana Developers
4  *  Copyright (C) 2011-2019  The ManaPlus Developers
5  *  Copyright (C) 2019-2021  Andrei Karas
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 #ifndef PARTY_H
24 #define PARTY_H
25 
26 #include "gui/models/avatarlistmodel.h"
27 
28 #include "utils/cast.h"
29 #include "utils/dtor.h"
30 #include "utils/stringvector.h"
31 
32 #include <map>
33 #include <set>
34 
35 class Party;
36 
37 class PartyMember final : public Avatar
38 {
39     public:
A_DELETE_COPY(PartyMember)40         A_DELETE_COPY(PartyMember)
41 
42         const Party *getParty() const noexcept2 A_WARN_UNUSED
43         { return mParty; }
44 
getLeader()45         bool getLeader() const noexcept2 A_WARN_UNUSED
46         { return mLeader; }
47 
setLeader(const bool leader)48         void setLeader(const bool leader)
49         { mLeader = leader; setDisplayBold(leader); }
50 
51     protected:
52         friend class Party;
53 
54         PartyMember(Party *const party,
55                     const BeingId id,
56                     const std::string &name);
57 
58         Party *mParty;
59         bool mLeader;
60 };
61 
62 class Party final : public AvatarListModel
63 {
64     public:
A_DELETE_COPY(Party)65         A_DELETE_COPY(Party)
66 
67         /**
68          * Set the party's name.
69          */
70         void setName(const std::string &name)
71         { mName = name; }
72 
73         /**
74          * Adds member to the list.
75          */
76         PartyMember *addMember(const BeingId id,
77                                const std::string &name);
78 
79         /**
80          * Find a member by ID.
81          *
82          * @return the member with the given ID, or NULL if they don't exist.
83          */
84         PartyMember *getMember(const BeingId id) const A_WARN_UNUSED;
85 
86         /**
87          * Find a member by name.
88          *
89          * @return the member with the given name, or NULL if they don't exist.
90          */
91         PartyMember *getMember(const std::string &name) const A_WARN_UNUSED;
92 
93         /**
94          * Get the name of the party.
95          * @return returns name of the party
96          */
getName()97         const std::string &getName() const noexcept2 A_WARN_UNUSED
98         { return mName; }
99 
100         /**
101          * Get the id of the party.
102          * @return Returns the id of the party
103          */
getId()104         int16_t getId() const noexcept2 A_WARN_UNUSED
105         { return mId; }
106 
107         /**
108          * Removes a member from the party.
109          */
110         void removeMember(const PartyMember *const member);
111 
112         /**
113          * Removes a member from the party.
114          */
115         void removeMember(const BeingId id);
116 
117         /**
118          * Removes a member from the party.
119          */
120         void removeMember(const std::string &name);
121 
clearMembers()122         void clearMembers()
123         { delete_all(mMembers); mMembers.clear(); }
124 
125         void removeFromMembers();
126 
127         /**
128          * Get size of members list.
129          * @return Returns the number of members in the party.
130          */
getNumberOfElements()131         int getNumberOfElements() override final A_WARN_UNUSED
132         { return CAST_S32(mMembers.size()); }
133 
134         Avatar *getAvatarAt(const int index) override final A_WARN_UNUSED;
135 
136         /**
137          * Get whether user can invite users to this party.
138          * @return Returns true if user can invite users
139          */
getInviteRights()140         bool getInviteRights() const noexcept2 A_WARN_UNUSED
141         { return mCanInviteUsers; }
142 
143         void setRights(const int16_t rights);
144 
145         bool isMember(const PartyMember *const member) const A_WARN_UNUSED;
146 
147         bool isMember(const BeingId id) const A_WARN_UNUSED;
148 
149         bool isMember(const std::string &name) const A_WARN_UNUSED;
150 
151         void getNames(StringVect &names) const;
152 
153         void getNamesSet(std::set<std::string> &names) const;
154 
155         void sort();
156 
157         typedef STD_VECTOR<PartyMember*> MemberList;
158 
getMembers()159         const MemberList *getMembers() const RETURNS_NONNULL A_WARN_UNUSED
160         { return &mMembers; }
161 
162         static Party *getParty(const int16_t id) A_WARN_UNUSED;
163 
164         static void clearParties();
165 
166     private:
167         typedef std::map<int, Party*> PartyMap;
168         static PartyMap parties;
169 
170         /**
171          * Constructor with party id passed to it.
172          */
173         explicit Party(const int16_t id);
174 
175         ~Party() override final;
176 
177         MemberList mMembers;
178         std::string mName;
179         int16_t mId;
180         bool mCanInviteUsers;
181 };
182 
183 #endif  // PARTY_H
184