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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include <stdio.h>
#include "PlayerPanel.h"
#include "PlayerContextMenu.h"
#include "PlayerListCompare.h"
#include "DialogAddBan.h"
#include <vgui/ISystem.h>
#include <vgui/ISurface.h>
#include <vgui/ILocalize.h>
#include <vgui/IVGui.h>
#include <vgui/KeyCode.h>
#include <KeyValues.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/ListPanel.h>
#include <vgui_controls/QueryBox.h>
using namespace vgui;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CPlayerPanel::CPlayerPanel(vgui::Panel *parent, const char *name) : vgui::PropertyPage(parent, name)
{
m_pPlayerListPanel = new vgui::ListPanel(this, "Players list");
m_pPlayerListPanel->AddColumnHeader(0, "name", "#Player_Panel_Name", 200, ListPanel::COLUMN_RESIZEWITHWINDOW );
m_pPlayerListPanel->AddColumnHeader(1, "authid", "#Player_Panel_ID", 100);
m_pPlayerListPanel->AddColumnHeader(2, "ping", "#Player_Panel_Ping", 50);
m_pPlayerListPanel->AddColumnHeader(3, "loss", "#Player_Panel_Loss", 50);
m_pPlayerListPanel->AddColumnHeader(4, "frags", "#Player_Panel_Frags", 50);
m_pPlayerListPanel->AddColumnHeader(5, "time", "#Player_Panel_Time", 75);
/*
// TODO: update me!!
m_pPlayerListPanel->SetSortFunc(0, PlayerNameCompare);
m_pPlayerListPanel->SetSortFunc(1, PlayerAuthCompare);
m_pPlayerListPanel->SetSortFunc(2, PlayerPingCompare);
m_pPlayerListPanel->SetSortFunc(3, PlayerLossCompare);
m_pPlayerListPanel->SetSortFunc(4, PlayerFragsCompare);
*/
m_pPlayerListPanel->SetSortFunc(5, PlayerTimeCompare);
m_pPlayerListPanel->SetEmptyListText("#Player_Panel_No_Players");
// Sort by ping time by default
m_pPlayerListPanel->SetSortColumn(0);
m_pKickButton = new Button(this, "Kick", "#Player_Panel_Kick");
m_pBanButton = new Button(this, "Ban", "#Player_Panel_Ban");
m_pPlayerContextMenu = new CPlayerContextMenu(this);
m_pPlayerContextMenu->SetVisible(false);
LoadControlSettings("Admin/PlayerPanel.res", "PLATFORM");
m_pKickButton->SetCommand(new KeyValues("KickPlayer"));
m_pBanButton->SetCommand(new KeyValues("BanPlayer"));
OnItemSelected(); // disable the buttons
m_flUpdateTime = 0.0f;
RemoteServer().AddServerMessageHandler(this, "UpdatePlayers");
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CPlayerPanel::~CPlayerPanel()
{
RemoteServer().RemoveServerDataResponseTarget(this);
}
//-----------------------------------------------------------------------------
// Purpose: Rebuilds player info
//-----------------------------------------------------------------------------
void CPlayerPanel::OnResetData()
{
RemoteServer().RequestValue(this, "playerlist");
// update once every minute
m_flUpdateTime = (float)system()->GetFrameTime() + (60 * 1.0f);
}
//-----------------------------------------------------------------------------
// Purpose: Checks to see if we should update player info
//-----------------------------------------------------------------------------
void CPlayerPanel::OnThink()
{
if (m_flUpdateTime < vgui::system()->GetFrameTime())
{
OnResetData();
}
}
//-----------------------------------------------------------------------------
// Purpose: Refreshes page if user hits F5
//-----------------------------------------------------------------------------
void CPlayerPanel::OnKeyCodeTyped(vgui::KeyCode code)
{
if (code == KEY_F5)
{
OnResetData();
}
else
{
BaseClass::OnKeyCodeTyped(code);
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns data on the player who is currently selected in the list
//-----------------------------------------------------------------------------
KeyValues *CPlayerPanel::GetSelected()
{
return m_pPlayerListPanel->GetItem(m_pPlayerListPanel->GetSelectedItem(0));
}
static const char *FormatSeconds( int seconds )
{
static char string[64];
int hours = 0;
int minutes = seconds / 60;
if ( minutes > 0 )
{
seconds -= (minutes * 60);
hours = minutes / 60;
if ( hours > 0 )
{
minutes -= (hours * 60);
}
}
if ( hours > 0 )
{
Q_snprintf( string, sizeof(string), "%2i:%02i:%02i", hours, minutes, seconds );
}
else
{
Q_snprintf( string, sizeof(string), "%02i:%02i", minutes, seconds );
}
return string;
}
//-----------------------------------------------------------------------------
// Purpose: called when the server has returned a requested value
//-----------------------------------------------------------------------------
void CPlayerPanel::OnServerDataResponse(const char *value, const char *response)
{
if (!stricmp(value, "UpdatePlayers"))
{
// server has indicated a change, force an update
m_flUpdateTime = 0.0f;
}
else if (!stricmp(value, "playerlist"))
{
// new list of players
m_pPlayerListPanel->DeleteAllItems();
// parse response
const char *parse = response;
while (parse && *parse)
{
// first should be the size of the player name
char name[64];
name[0] = '\0';
char authID[64];
authID[0] = '\0';
char netAdr[32];
netAdr[0] = '\0';
int ping = 0, packetLoss = 0, frags = 0, connectTime = 0;
ivgui()->DPrintf2("orig: %s\n", parse);
// parse out the name, which is quoted
Assert(*parse == '\"');
if (*parse != '\"')
break;
++parse; // move past start quote
int pos = 0;
while (*parse && *parse != '\"')
{
name[pos++] = *parse;
parse++;
}
name[pos] = 0;
parse++; // move past end quote
if (6 != sscanf(parse, " %s %s %d %d %d %d\n", authID, netAdr, &ping, &packetLoss, &frags, &connectTime))
break;
const char *timeStr = FormatSeconds(connectTime);
ivgui()->DPrintf2("pars: \"%s\" %s %s %d %d %d %s\n", name, authID, netAdr, ping, packetLoss, frags, timeStr);
// add to list
KeyValues *player = new KeyValues("Player");
player->SetString("name", name);
player->SetString("authID", authID);
player->SetString("netAdr", netAdr);
player->SetInt("ping", ping);
player->SetInt("loss", packetLoss);
player->SetInt("frags", frags);
player->SetString("time", timeStr);
m_pPlayerListPanel->AddItem(player, 0, false, false);
// move to next line
parse = strchr(parse, '\n');
if (parse)
{
parse++;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Buttom command handlers
//-----------------------------------------------------------------------------
void CPlayerPanel::OnCommand(const char *command)
{
BaseClass::OnCommand(command);
}
//-----------------------------------------------------------------------------
// Purpose: Handles the UI for kicking a player from the server
//-----------------------------------------------------------------------------
void CPlayerPanel::OnKickButtonPressed()
{
if (m_pPlayerListPanel->GetSelectedItemsCount() < 1)
return;
// open a message box to ask the user if they want to follow through on this
QueryBox *box;
if (m_pPlayerListPanel->GetSelectedItemsCount() > 1)
{
box = new QueryBox("#Kick_Multiple_Players_Title", "#Kick_Multiple_Players_Question");
}
else
{
// show the player name in the message box if only one player is being booted
KeyValues *kv = m_pPlayerListPanel->GetItem(m_pPlayerListPanel->GetSelectedItem(0));
Assert(kv != NULL);
if (!kv)
return;
wchar_t playerName[64];
g_pVGuiLocalize->ConvertANSIToUnicode( kv->GetString("name"), playerName, sizeof(playerName) );
wchar_t msg[512];
g_pVGuiLocalize->ConstructString( msg, sizeof(msg), g_pVGuiLocalize->Find("Kick_Single_Player_Question"), 1, playerName );
box = new QueryBox(g_pVGuiLocalize->Find("#Kick_Single_Player_Title"), msg);
}
box->AddActionSignalTarget(this);
box->SetOKCommand(new KeyValues("KickSelectedPlayers"));
box->ShowWindow();
}
//-----------------------------------------------------------------------------
// Purpose: Prompts a user to be banned
//-----------------------------------------------------------------------------
void CPlayerPanel::OnBanButtonPressed()
{
if (m_pPlayerListPanel->GetSelectedItemsCount() != 1)
return;
// open a message box to ask the user if they want to follow through on this
KeyValues *kv = m_pPlayerListPanel->GetItem(m_pPlayerListPanel->GetSelectedItem(0));
Assert(kv != NULL);
if (!kv)
return;
const char *player = kv->GetString("name");
const char *authid = kv->GetString("authid");
const char *netAdr = kv->GetString("netAdr");
char buf[64];
if ( !strcmp( authid, "UNKNOWN" ) )
{
int s1, s2, s3, s4;
if (4 == sscanf(netAdr, "%d.%d.%d.%d", &s1, &s2, &s3, &s4))
{
Q_snprintf( buf, sizeof(buf), "%d.%d.%d.%d", s1, s2, s3, s4 );
authid = buf;
}
}
CDialogAddBan *box = new CDialogAddBan(this);
box->AddActionSignalTarget(this);
box->Activate("addban", player, authid);
}
//-----------------------------------------------------------------------------
// Purpose: Kicks all the players currently selected
//-----------------------------------------------------------------------------
void CPlayerPanel::KickSelectedPlayers()
{
for (int i = 0; i < m_pPlayerListPanel->GetSelectedItemsCount(); i++)
{
// get the player info
int row = m_pPlayerListPanel->GetSelectedItem(i);
KeyValues *pl = m_pPlayerListPanel->GetItem(row);
if (!pl)
continue;
// kick 'em
char cmd[512];
_snprintf(cmd, sizeof(cmd), "kick \"%s\"", pl->GetString("name"));
RemoteServer().SendCommand(cmd);
}
m_pPlayerListPanel->ClearSelectedItems();
m_pKickButton->SetEnabled(false);
m_pBanButton->SetEnabled(false);
OnResetData();
}
//-----------------------------------------------------------------------------
// Purpose: Adds a new ban
//-----------------------------------------------------------------------------
void CPlayerPanel::AddBanByID(const char *id, const char *newtime)
{
Assert(id && *id);
if (!id || !*id)
return;
// if the newtime string is not valid, then set it to 0 (permanent ban)
if (!newtime || atof(newtime) < 0.001)
{
newtime = "0";
}
const char *banCmd = "banid";
const char *saveCmd = "writeip";
int s1, s2, s3, s4;
if (4 == sscanf(id, "%d.%d.%d.%d", &s1, &s2, &s3, &s4))
{
banCmd = "addip";
saveCmd = "writeid";
}
// send down the ban command
char cmd[512];
_snprintf(cmd, sizeof(cmd) -1, "%s %s %s\n", banCmd, newtime, id);
RemoteServer().SendCommand(cmd);
// force the file to update
RemoteServer().SendCommand(saveCmd);
// refresh
OnResetData();
}
//-----------------------------------------------------------------------------
// Purpose: opens context menu (user right clicked on a server)
//-----------------------------------------------------------------------------
void CPlayerPanel::OnOpenContextMenu(int itemID)
{
/* CODE DISABLED UNTIL VERIFIED AS WORKING
// show the player menus only if the cursor is over it
if (IsCursorOver() && m_pPlayerListPanel->GetNumSelectedRows())
{
// get the server
unsigned int playerID = m_pPlayerListPanel->GetDataItem(m_pPlayerListPanel->GetSelectedRow(0))->userData;
// activate context menu
m_pPlayerContextMenu->ShowMenu(this, playerID);
}
*/
}
//-----------------------------------------------------------------------------
// Purpose: called when an item on the list panel is selected.
//-----------------------------------------------------------------------------
void CPlayerPanel::OnItemSelected()
{
bool state = true;
if ( m_pPlayerListPanel->GetSelectedItemsCount() == 0 )
{
state = false;
}
m_pKickButton->SetEnabled(state);
m_pBanButton->SetEnabled(state);
if ( m_pPlayerListPanel->GetSelectedItemsCount() > 1 )
{
m_pBanButton->SetEnabled(false);
}
}
|