ManaPlus
party.cpp
Go to the documentation of this file.
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 #include "party.h"
24 
25 #include "actormanager.h"
26 
27 #include "being/being.h"
28 
29 #include "utils/stringutils.h"
30 
31 #include "debug.h"
32 
33 static class SortPartyFunctor final
34 {
35  public:
37 
38  bool operator() (const PartyMember *const p1,
39  const PartyMember *const p2) const
40  {
41  if (p1 == nullptr || p2 == nullptr)
42  return false;
43  const int leader1 = CAST_S32(p1->getLeader());
44  const int leader2 = CAST_S32(p2->getLeader());
45  if (leader1 != leader2)
46  return leader1 >= leader2;
47  if (p1->getName() != p2->getName())
48  {
49  std::string s1 = p1->getName();
50  std::string s2 = p2->getName();
51  toLower(s1);
52  toLower(s2);
53  return s1 < s2;
54  }
55  return false;
56  }
58 
60  const BeingId id,
61  const std::string &name) :
62  Avatar(name),
63  mParty(party),
64  mLeader(false)
65 {
66  mId = id;
67 }
68 
70 
71 Party::Party(const int16_t id) :
72  mMembers(),
73  mName(),
74  mId(id),
75  mCanInviteUsers(false)
76 {
77  parties[id] = this;
78 }
79 
81 {
82  clearMembers();
83 }
84 
86  const std::string &name)
87 {
88  PartyMember *m = getMember(id);
89  if (m != nullptr)
90  return m;
91 
92  m = new PartyMember(this, id, name);
93 
94  mMembers.push_back(m);
95 
96  return m;
97 }
98 
100 {
101  MemberList::const_iterator itr = mMembers.begin();
102  const MemberList::const_iterator itr_end = mMembers.end();
103  while (itr != itr_end)
104  {
105  if ((*itr)->mId == id)
106  return (*itr);
107  ++itr;
108  }
109 
110  return nullptr;
111 }
112 
113 PartyMember *Party::getMember(const std::string &name) const
114 {
115  MemberList::const_iterator itr = mMembers.begin();
116  const MemberList::const_iterator itr_end = mMembers.end();
117  while (itr != itr_end)
118  {
119  if (((*itr) != nullptr) && (*itr)->getName() == name)
120  return (*itr);
121 
122  ++itr;
123  }
124 
125  return nullptr;
126 }
127 
128 void Party::removeMember(const PartyMember *const member)
129 {
130  if (member == nullptr)
131  return;
132 
133  bool deleted = true;
134  while (deleted)
135  {
136  deleted = false;
137  MemberList::iterator itr = mMembers.begin();
138  const MemberList::iterator itr_end = mMembers.end();
139  while (itr != itr_end)
140  {
141  PartyMember *const m = *itr;
142  if ((m != nullptr) && m->mId == member->mId
143  && m->getName() == member->getName())
144  {
145  mMembers.erase(itr);
146  delete m;
147  deleted = true;
148  break;
149  }
150  ++itr;
151  }
152  }
153 }
154 
156 {
157  bool deleted = true;
158  while (deleted)
159  {
160  deleted = false;
161  MemberList::iterator itr = mMembers.begin();
162  const MemberList::iterator itr_end = mMembers.end();
163  while (itr != itr_end)
164  {
165  PartyMember *const member = *itr;
166  if ((member != nullptr) && member->mId == id)
167  {
168  mMembers.erase(itr);
169  delete member;
170  deleted = true;
171  break;
172  }
173  ++itr;
174  }
175  }
176 }
177 
178 void Party::removeMember(const std::string &name)
179 {
180  bool deleted = true;
181  while (deleted)
182  {
183  deleted = false;
184  MemberList::iterator itr = mMembers.begin();
185  const MemberList::iterator itr_end = mMembers.end();
186  while (itr != itr_end)
187  {
188  PartyMember *const member = *itr;
189  if ((member != nullptr) && member->getName() == name)
190  {
191  mMembers.erase(itr);
192  delete member;
193  deleted = true;
194  break;
195  }
196  ++itr;
197  }
198  }
199 }
200 
202 {
203  if (actorManager == nullptr)
204  return;
205 
206  MemberList::const_iterator itr = mMembers.begin();
207  const MemberList::const_iterator itr_end = mMembers.end();
208 
209  while (itr != itr_end)
210  {
211  Being *const b = actorManager->findBeing((*itr)->getID());
212  if (b != nullptr)
213  b->setParty(nullptr);
214  ++itr;
215  }
216 }
217 
218 Avatar *Party::getAvatarAt(const int index)
219 {
220  return mMembers[index];
221 }
222 
223 void Party::setRights(const int16_t rights)
224 {
225  // to invite, rights must be greater than 0
226  if (rights > 0)
227  mCanInviteUsers = true;
228 }
229 
230 bool Party::isMember(const PartyMember *const member) const
231 {
232  if (member == nullptr)
233  return false;
234 
235  if ((member->mParty != nullptr) && member->mParty != this)
236  return false;
237 
238  MemberList::const_iterator itr = mMembers.begin();
239  const MemberList::const_iterator itr_end = mMembers.end();
240  while (itr != itr_end)
241  {
242  const PartyMember *const m = *itr;
243  if (m != nullptr &&
244  m->mId == member->mId &&
245  m->getName() == member->getName())
246  {
247  return true;
248  }
249  ++itr;
250  }
251 
252  return false;
253 }
254 
255 bool Party::isMember(const BeingId id) const
256 {
257  MemberList::const_iterator itr = mMembers.begin();
258  const MemberList::const_iterator itr_end = mMembers.end();
259  while (itr != itr_end)
260  {
261  const PartyMember *const m = *itr;
262  if ((m != nullptr) && m->mId == id)
263  return true;
264  ++itr;
265  }
266 
267  return false;
268 }
269 
270 bool Party::isMember(const std::string &name) const
271 {
272  MemberList::const_iterator itr = mMembers.begin();
273  const MemberList::const_iterator itr_end = mMembers.end();
274  while (itr != itr_end)
275  {
276  const PartyMember *const m = *itr;
277  if ((m != nullptr) && m->getName() == name)
278  return true;
279  ++itr;
280  }
281 
282  return false;
283 }
284 
285 void Party::getNames(StringVect &names) const
286 {
287  names.clear();
288  MemberList::const_iterator it = mMembers.begin();
289  const MemberList::const_iterator it_end = mMembers.end();
290  while (it != it_end)
291  {
292  const PartyMember *const m = *it;
293  if (m != nullptr)
294  names.push_back(m->getName());
295  ++it;
296  }
297 }
298 
299 void Party::getNamesSet(std::set<std::string> &names) const
300 {
301  names.clear();
302  MemberList::const_iterator it = mMembers.begin();
303  const MemberList::const_iterator it_end = mMembers.end();
304  while (it != it_end)
305  {
306  const PartyMember *const m = *it;
307  if (m != nullptr)
308  names.insert(m->getName());
309  ++it;
310  }
311 }
312 
313 Party *Party::getParty(const int16_t id)
314 {
315  const PartyMap::const_iterator it = parties.find(id);
316  if (it != parties.end())
317  return it->second;
318  Party *const party = new Party(id);
319  parties[id] = party;
320  return party;
321 }
322 
324 {
325  std::sort(mMembers.begin(), mMembers.end(), partySorter);
326 }
327 
329 {
330  PartyMap::iterator it = parties.begin();
331  while (it != parties.end())
332  {
333  delete (*it).second;
334  ++ it;
335  }
336  parties.clear();
337 }
ActorManager * actorManager
int BeingId
Definition: beingid.h:30
#define CAST_S32
Definition: cast.h:30
Being * findBeing(const BeingId id) const
Definition: avatar.h:41
BeingId mId
Definition: avatar.h:187
std::string getName() const
Definition: avatar.h:53
Definition: being.h:96
void setParty(Party *const party)
Definition: being.cpp:1307
PartyMember(Party *const party, const BeingId id, const std::string &name)
Definition: party.cpp:59
Party * mParty
Definition: party.h:58
Definition: party.h:63
static PartyMap parties
Definition: party.h:168
bool isMember(const PartyMember *const member) const
Definition: party.cpp:230
std::map< int, Party * > PartyMap
Definition: party.h:167
void setRights(const int16_t rights)
Definition: party.cpp:223
Avatar * getAvatarAt(const int index)
Definition: party.cpp:218
Party(const int16_t id)
Definition: party.cpp:71
void removeFromMembers()
Definition: party.cpp:201
~Party()
Definition: party.cpp:80
void removeMember(const PartyMember *const member)
Definition: party.cpp:128
PartyMember * addMember(const BeingId id, const std::string &name)
Definition: party.cpp:85
PartyMember * getMember(const BeingId id) const
Definition: party.cpp:99
void getNamesSet(std::set< std::string > &names) const
Definition: party.cpp:299
static Party * getParty(const int16_t id)
Definition: party.cpp:313
void getNames(StringVect &names) const
Definition: party.cpp:285
MemberList mMembers
Definition: party.h:177
void clearMembers()
Definition: party.h:122
void sort()
Definition: party.cpp:323
bool mCanInviteUsers
Definition: party.h:180
static void clearParties()
Definition: party.cpp:328
#define final
Definition: localconsts.h:46
#define A_DEFAULT_COPY(func)
Definition: localconsts.h:41
uint32_t party
std::string toLower(std::string const &s)
static class SortPartyFunctor partySorter
std::vector< std::string > StringVect
Definition: stringvector.h:29