GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/socialplayerstab.h Lines: 21 81 25.9 %
Date: 2021-03-17 Branches: 8 72 11.1 %

Line Branch Exec Source
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
25
#include "gui/widgets/tabs/socialtab.h"
26
27
#include "actormanager.h"
28
#include "party.h"
29
30
#include "being/being.h"
31
32
#include "gui/models/beingslistmodel.h"
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
43
class SocialPlayersTab final : public SocialTab
44
{
45
    public:
46
1
        SocialPlayersTab(const Widget2 *const widget,
47
                         const std::string &name,
48
1
                         const Opaque showBackground) :
49
            SocialTab(widget),
50
2
            mBeings(new BeingsListModel)
51
        {
52
1
            createControls(mBeings, showBackground);
53
54
1
            getPlayersAvatars();
55
1
            setCaption(name);
56
2
            mMenuAction = "players";
57
1
        }
58
59
        A_DELETE_COPY(SocialPlayersTab)
60
61
2
        ~SocialPlayersTab() override final
62
2
        {
63
1
            delete2(mList)
64
1
            delete2(mScroll)
65
2
            delete2(mBeings)
66
2
        }
67
68
        void updateList() override final
69
        {
70
            getPlayersAvatars();
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
140
1
        void getPlayersAvatars()
141
        {
142
2
            STD_VECTOR<Avatar*> *const avatars = mBeings->getMembers();
143
1
            if (actorManager != nullptr)
144
            {
145
                StringVect names;
146
                actorManager->getPlayerNames(names, NpcNames_false);
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
2
            mCounterString = strprintf(_("Visible players: %d"),
191
2
                CAST_S32(avatars->size()));
192
2
            updateCounter();
193
1
        }
194
195
    private:
196
        BeingsListModel *mBeings;
197
};
198
199
#endif  // GUI_WIDGETS_TABS_SOCIALPLAYERSTAB_H