-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathplayerlistwidget.cpp
More file actions
208 lines (174 loc) · 5.49 KB
/
playerlistwidget.cpp
File metadata and controls
208 lines (174 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "playerlistwidget.h"
#include "aoapplication.h"
#include "moderation_functions.h"
#include "widgets/moderator_dialog.h"
#include <QListWidgetItem>
#include <QMenu>
PlayerListWidget::PlayerListWidget(AOApplication *ao_app, QWidget *parent)
: QListWidget(parent)
, ao_app(ao_app)
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, &PlayerListWidget::customContextMenuRequested, this, &PlayerListWidget::onCustomContextMenuRequested);
}
PlayerListWidget::~PlayerListWidget()
{}
void PlayerListWidget::registerPlayer(const PlayerRegister &update)
{
switch (update.type)
{
default:
Q_UNREACHABLE();
break;
case PlayerRegister::ADD_PLAYER:
addPlayer(update.id);
break;
case PlayerRegister::REMOVE_PLAYER:
removePlayer(update.id);
break;
}
}
void PlayerListWidget::updatePlayer(const PlayerUpdate &update)
{
PlayerData &player = m_player_map[update.id];
bool update_icon = false;
switch (update.type)
{
default:
Q_UNREACHABLE();
break;
case PlayerUpdate::NAME:
player.name = update.data;
break;
case PlayerUpdate::CHARACTER:
player.character = update.data;
update_icon = true;
break;
case PlayerUpdate::CHARACTER_NAME:
player.character_name = update.data;
break;
case PlayerUpdate::AREA_ID:
player.area_id = update.data.toInt();
break;
}
updatePlayer(player.id, update_icon);
filterPlayerList();
}
void PlayerListWidget::reloadPlayers()
{
for (const PlayerData &player : std::as_const(m_player_map))
{
updatePlayer(player.id, false);
}
}
void PlayerListWidget::setAuthenticated(bool f_state)
{
m_is_authenticated = f_state;
for (const PlayerData &data : std::as_const(m_player_map))
{
updatePlayer(data.id, false);
filterPlayerList();
}
}
void PlayerListWidget::onCustomContextMenuRequested(const QPoint &pos)
{
QListWidgetItem *item = itemAt(pos);
if (item == nullptr)
{
return;
}
int id = item->data(Qt::UserRole).toInt();
QString name = item->text();
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
QAction *report_player_action = menu->addAction("Report Player");
connect(report_player_action, &QAction::triggered, this, [this, id, name] {
auto maybe_reason = call_moderator_support(name);
if (maybe_reason.has_value())
{
ao_app->send_server_packet(AOPacket("ZZ", {maybe_reason.value(), QString::number(id)}));
}
});
if (m_is_authenticated)
{
QAction *kick_player_action = menu->addAction("Kick");
connect(kick_player_action, &QAction::triggered, this, [this, id, name] {
ModeratorDialog *dialog = new ModeratorDialog(id, false, ao_app);
dialog->setWindowTitle(tr("Kick %1").arg(name));
connect(this, &PlayerListWidget::destroyed, dialog, &ModeratorDialog::deleteLater);
active_moderator_menu = {id, dialog};
dialog->show();
});
QAction *ban_player_action = menu->addAction("Ban");
connect(ban_player_action, &QAction::triggered, this, [this, id, name] {
ModeratorDialog *dialog = new ModeratorDialog(id, true, ao_app);
dialog->setWindowTitle(tr("Ban %1").arg(name));
connect(this, &PlayerListWidget::destroyed, dialog, &ModeratorDialog::deleteLater);
active_moderator_menu = {id, dialog};
dialog->show();
});
}
menu->popup(mapToGlobal(pos));
}
void PlayerListWidget::addPlayer(int playerId)
{
m_player_map.insert(playerId, PlayerData{.id = playerId});
QListWidgetItem *item = new QListWidgetItem(this);
item->setData(Qt::UserRole, playerId);
m_item_map.insert(playerId, item);
updatePlayer(playerId, false);
}
void PlayerListWidget::removePlayer(int playerId)
{
if (active_moderator_menu.first == playerId && active_moderator_menu.second)
{
delete active_moderator_menu.second;
Q_EMIT notify("Closed Moderation Dialog : User left the server.");
}
delete takeItem(row(m_item_map.take(playerId)));
m_player_map.remove(playerId);
}
void PlayerListWidget::filterPlayerList()
{
int area_id = m_player_map.value(ao_app->client_id).area_id;
for (QListWidgetItem *item : std::as_const(m_item_map))
{
if (!item)
{
qWarning() << "Trying to filter item that does not exist. This indicates either a broken server-implementation or a bad demo file.";
break;
}
item->setHidden(m_player_map[item->data(Qt::UserRole).toInt()].area_id != area_id && !m_is_authenticated);
}
}
void PlayerListWidget::updatePlayer(int playerId, bool updateIcon)
{
PlayerData &data = m_player_map[playerId];
QListWidgetItem *item = m_item_map[playerId];
if (!item)
{
qWarning() << "No player at ID" << playerId << ". This might indicate a broker server implementation or a bad demo file.";
return;
}
item->setText(formatLabel(data));
if (data.character.isEmpty())
{
item->setToolTip(QString());
return;
}
QString tooltip = data.character;
if (!data.character_name.isEmpty())
{
tooltip = QObject::tr("%1 aka %2").arg(data.character, data.character_name);
}
item->setToolTip(tooltip);
if (updateIcon)
{
item->setIcon(QIcon(ao_app->get_image_suffix(ao_app->get_character_path(data.character, "char_icon"), true)));
}
}
QString PlayerListWidget::formatLabel(const PlayerData &data)
{
QString format = Options::getInstance().playerlistFormatString();
return format.replace("{id}", QString::number(data.id)).replace("{character}", data.character).replace("{displayname}", data.character_name.isEmpty() ? "No Data" : data.character_name).replace("{username}", data.name).simplified();
}