ManaPlus
socialplayerstab.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef GUI_WIDGETS_TABS_SOCIALPLAYERSTAB_H
23 #define GUI_WIDGETS_TABS_SOCIALPLAYERSTAB_H
24 
26 
27 #include "actormanager.h"
28 #include "party.h"
29 
30 #include "being/being.h"
31 
33 
34 #include "utils/gettext.h"
35 #include "utils/delete2.h"
36 #include "utils/stringutils.h"
37 
38 #include "localconsts.h"
39 
40 class Avatar;
41 class Being;
42 
44 {
45  public:
46  SocialPlayersTab(const Widget2 *const widget,
47  const std::string &name,
48  const Opaque showBackground) :
49  SocialTab(widget),
51  {
52  createControls(mBeings, showBackground);
53 
55  setCaption(name);
56  mMenuAction = "players";
57  }
58 
60 
62  {
63  delete2(mList)
66  }
67 
69  {
71  }
72 
73  void updateAvatar(const std::string &name) override final
74  {
75  if (actorManager == nullptr)
76  return;
77 
78  BLOCK_START("SocialPlayersTab::updateAvatar")
79  Avatar *const avatar = findAvatarbyName(name);
80  if (avatar == nullptr)
81  return;
82  if (Party::getParty(1) != nullptr)
83  {
84  const PartyMember *const
85  pm = Party::getParty(1)->getMember(name);
86  if ((pm != nullptr) && pm->getMaxHp() > 0)
87  {
88  avatar->setMaxHp(pm->getMaxHp());
89  avatar->setHp(pm->getHp());
90  }
91  }
92  const Being *const being = actorManager->findBeingByName(
93  name, ActorType::Player);
94  if (being != nullptr)
95  {
96  avatar->setDamageHp(being->getDamageTaken());
97  avatar->setLevel(being->getLevel());
98  avatar->setGender(being->getGender());
99  avatar->setIp(being->getIp());
100  avatar->setPoison(being->getPoison());
101  }
102  BLOCK_END("SocialPlayersTab::updateAvatar")
103  }
104 
105  void resetDamage(const std::string &name) override final
106  {
107  if (actorManager == nullptr)
108  return;
109 
110  Avatar *const avatar = findAvatarbyName(name);
111  if (avatar == nullptr)
112  return;
113  avatar->setDamageHp(0);
114  Being *const being = actorManager->findBeingByName(
115  name, ActorType::Player);
116 
117  if (being != nullptr)
118  being->setDamageTaken(0);
119  }
120 
121  Avatar* findAvatarbyName(const std::string &name)
122  {
123  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
124  Avatar *ava = nullptr;
125  STD_VECTOR<Avatar*>::const_iterator i = avatars->begin();
126  const STD_VECTOR<Avatar*>::const_iterator i_end = avatars->end();
127  while (i != i_end)
128  {
129  ava = (*i);
130  if ((ava != nullptr) && ava->getName() == name)
131  return ava;
132  ++i;
133  }
134  ava = new Avatar(name);
135  ava->setOnline(true);
136  avatars->push_back(ava);
137  return ava;
138  }
139 
141  {
142  STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
143  if (actorManager != nullptr)
144  {
145  StringVect names;
147 
148  STD_VECTOR<Avatar*>::iterator ai = avatars->begin();
149  while (ai != avatars->end())
150  {
151  bool finded = false;
152  const Avatar *const ava = (*ai);
153  if (ava == nullptr)
154  break;
155 
156  StringVectCIter i = names.begin();
157  const StringVectCIter i_end = names.end();
158  while (i != i_end)
159  {
160  if (ava->getName() == (*i) && !(*i).empty())
161  {
162  finded = true;
163  break;
164  }
165  ++i;
166  }
167 
168  if (!finded)
169  {
170  delete *ai;
171  ai = avatars->erase(ai);
172  }
173  else
174  {
175  ++ai;
176  }
177  }
178 
179  StringVectCIter i = names.begin();
180  const StringVectCIter i_end = names.end();
181 
182  while (i != i_end)
183  {
184  if (!(*i).empty())
185  updateAvatar(*i);
186  ++i;
187  }
188  }
189  // TRANSLATORS: social window label
190  mCounterString = strprintf(_("Visible players: %d"),
191  CAST_S32(avatars->size()));
192  updateCounter();
193  }
194 
195  private:
197 };
198 
199 #endif // GUI_WIDGETS_TABS_SOCIALPLAYERSTAB_H
ActorManager * actorManager
#define CAST_S32
Definition: cast.h:30
Being * findBeingByName(const std::string &name, const ActorTypeT type) const
void getPlayerNames(StringVect &names, const NpcNames npcNames) const
bool getPoison() const
Definition: actorsprite.h:169
Definition: avatar.h:41
void setOnline(const bool online)
Definition: avatar.h:87
void setMaxHp(const int maxHp)
Definition: avatar.h:99
void setHp(const int hp)
Definition: avatar.h:93
void setGender(const GenderT g)
Definition: avatar.h:165
void setPoison(const bool b)
Definition: avatar.h:183
void setIp(const std::string &ip)
Definition: avatar.h:177
int getMaxHp() const
Definition: avatar.h:96
void setLevel(const int level)
Definition: avatar.h:117
void setDamageHp(const int damageHp)
Definition: avatar.h:105
int getHp() const
Definition: avatar.h:90
std::string getName() const
Definition: avatar.h:53
Definition: being.h:96
virtual int getLevel() const
Definition: being.h:604
void setDamageTaken(const int damage)
Definition: being.h:596
const std::string & getIp() const
Definition: being.h:743
GenderT getGender() const
Definition: being.h:626
int getDamageTaken() const
Definition: being.h:593
std::vector< Avatar * > * getMembers()
PartyMember * getMember(const BeingId id) const
Definition: party.cpp:99
static Party * getParty(const int16_t id)
Definition: party.cpp:313
SocialPlayersTab(const Widget2 *const widget, const std::string &name, const Opaque showBackground)
BeingsListModel * mBeings
void resetDamage(const std::string &name)
void updateAvatar(const std::string &name)
Avatar * findAvatarbyName(const std::string &name)
void createControls(AvatarListModel *const listModel, const Opaque showBackground)
Definition: socialtab.h:107
ScrollArea * mScroll
Definition: socialtab.h:138
std::string mMenuAction
Definition: socialtab.h:141
AvatarListBox * mList
Definition: socialtab.h:139
void updateCounter() const
Definition: socialtab.h:124
std::string mCounterString
Definition: socialtab.h:140
void setCaption(const std::string &caption)
Definition: tab.cpp:458
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define _(s)
Definition: gettext.h:35
#define override
Definition: localconsts.h:47
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
const bool NpcNames_false
Definition: npcnames.h:30
bool Opaque
Definition: opaque.h:30
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
std::string strprintf(const char *const format,...)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29