GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/tabs/socialplayerstab.h Lines: 21 81 25.9 %
Date: 2017-11-29 Branches: 8 66 12.1 %

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