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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "DialogGameInfo.h"
#include "Info.h"
#include "IRunGameEngine.h"
#include "IGameList.h"
#include "TrackerProtocol.h"
#include "serverpage.h"
#include "ServerList.h"
#include "DialogServerPassword.h"
#include <VGUI_Controls.h>
#include <VGUI_ISystem.h>
#include <VGUI_ISurface.h>
#include <VGUI_IVGui.h>
#include <VGUI_KeyValues.h>
#include <VGUI_Label.h>
#include <VGUI_TextEntry.h>
#include <VGUI_Button.h>
#include <VGUI_ToggleButton.h>
#include <VGUI_RadioButton.h>
#include <stdio.h>
using namespace vgui;
static const long RETRY_TIME = 10000; // refresh server every 10 seconds
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CDialogGameInfo::CDialogGameInfo(IGameList *gameList, unsigned int serverID, int serverIP, int serverPort) : Frame(NULL, "DialogGameInfo"), m_Servers(this)
{
MakePopup();
SetBounds(0, 0, 512, 384);
m_bConnecting = false;
m_bServerFull = false;
m_bShowAutoRetryToggle = false;
m_bServerNotResponding = false;
m_bShowingExtendedOptions = false;
m_szPassword[0] = 0;
m_iServerID = serverID;
m_pConnectButton = new Button(this, "Connect", "&Join Game");
m_pCloseButton = new Button(this, "Close", "&Close");
m_pRefreshButton = new Button(this, "Refresh", "&Refresh");
m_pInfoLabel = new Label(this, "InfoLabel", "");
m_pAutoRetry = new ToggleButton(this, "AutoRetry", "&Auto-Retry");
m_pAutoRetry->AddActionSignalTarget(this);
m_pAutoRetryAlert = new RadioButton(this, "AutoRetryAlert", "A&lert me when a player slot is available on server.");
m_pAutoRetryJoin = new RadioButton(this, "AutoRetryJoin", "J&oin the server as soon as a player slot is available.");
m_pAutoRetryAlert->SetSelected(true);
m_pConnectButton->SetCommand(new KeyValues("Connect"));
m_pCloseButton->SetCommand(new KeyValues("Close"));
m_pRefreshButton->SetCommand(new KeyValues("Refresh"));
m_iRequestRetry = 0;
SetSizeable(false);
if (gameList)
{
// we already have the game info, fill it in
serveritem_t &server = gameList->GetServer(serverID);
m_iServerID = m_Servers.AddNewServer(server);
}
else
{
// create a new server to watch
serveritem_t server;
memset(&server, 0, sizeof(server));
*((int *)server.ip) = serverIP;
server.port = serverPort;
m_iServerID = m_Servers.AddNewServer(server);
}
// refresh immediately
RequestInfo();
// let us be ticked every frame
ivgui()->AddTickSignal(this->GetVPanel());
LoadControlSettings("Admin\\DialogGameInfo.res");
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CDialogGameInfo::~CDialogGameInfo()
{
}
//-----------------------------------------------------------------------------
// Purpose: Activates the dialog
//-----------------------------------------------------------------------------
void CDialogGameInfo::Run(const char *titleName)
{
char buf[512];
if (titleName)
{
sprintf(buf, "Game Info - %s", titleName);
}
else
{
strcpy(buf, "Game Info");
}
SetTitle(buf, true);
// get the info from the user
RequestInfo();
RequestFocus();
}
//-----------------------------------------------------------------------------
// Purpose: Changes which server to watch
//-----------------------------------------------------------------------------
void CDialogGameInfo::ChangeGame(int serverIP, int serverPort)
{
// check to see if it's the same game
serveritem_t &server = m_Servers.GetServer(m_iServerID);
if (*(int *)server.ip == serverIP && server.port == serverPort)
{
return;
}
// change the server
m_Servers.Clear();
// create a new server to watch
serveritem_t newServer;
memset(&newServer, 0, sizeof(newServer));
*((int *)newServer.ip) = serverIP;
newServer.port = serverPort;
m_iServerID = m_Servers.AddNewServer(newServer);
// start refresh immediately
RequestInfo();
}
//-----------------------------------------------------------------------------
// Purpose: Relayouts the data
//-----------------------------------------------------------------------------
void CDialogGameInfo::PerformLayout()
{
BaseClass::PerformLayout();
// get the server we're watching
serveritem_t &server = m_Servers.GetServer(m_iServerID);
SetControlText("ServerText", server.name);
SetControlText("GameText", server.gameDescription);
SetControlText("MapText", server.map);
char buf[128];
if (server.maxPlayers > 0)
{
sprintf(buf, "%d / %d", server.players, server.maxPlayers);
}
else
{
buf[0] = 0;
}
SetControlText("PlayersText", buf);
if (server.ip[0] && server.port)
{
char buf[64];
sprintf(buf, "%d.%d.%d.%d:%d", server.ip[0], server.ip[1], server.ip[2], server.ip[3], server.port);
SetControlText("ServerIPText", buf);
m_pConnectButton->SetEnabled(true);
}
else
{
SetControlText("ServerIPText", "");
m_pConnectButton->SetEnabled(false);
}
sprintf(buf, "%d", server.ping);
SetControlText("PingText", buf);
// set the info text
if (m_pAutoRetry->IsSelected())
{
if (server.players < server.maxPlayers)
{
m_pInfoLabel->SetText("Press 'Join Game' to connect to the server.");
}
else if (m_pAutoRetryJoin->IsSelected())
{
m_pInfoLabel->SetText("You will join the server as soon as a player slot is free.");
}
else
{
m_pInfoLabel->SetText("You will be alerted as soon player slot is free on the server.");
}
}
else if (m_bServerFull)
{
m_pInfoLabel->SetText("Could not connect - server is full.");
}
else if (m_bServerNotResponding)
{
char text[100];
_snprintf(text,100,"Server is not responding.%d.%d.%d.%d:%d", server.ip[0], server.ip[1], server.ip[2], server.ip[3], server.port);
m_pInfoLabel->SetText(text);
}
else
{
// clear the status
m_pInfoLabel->SetText("");
}
// auto-retry layout
m_pAutoRetry->SetVisible(m_bShowAutoRetryToggle);
if (m_pAutoRetry->IsSelected())
{
m_pAutoRetryAlert->SetVisible(true);
m_pAutoRetryJoin->SetVisible(true);
}
else
{
m_pAutoRetryAlert->SetVisible(false);
m_pAutoRetryJoin->SetVisible(false);
}
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Sets up the current scheme colors
//-----------------------------------------------------------------------------
void CDialogGameInfo::ApplySchemeSettings(vgui::IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
// force the label to get it's scheme settings
m_pInfoLabel->InvalidateLayout(true);
// override them
m_pInfoLabel->SetFgColor(GetSchemeColor("BrightControlText"));
}
//-----------------------------------------------------------------------------
// Purpose: Forces the game info dialog to try and connect
//-----------------------------------------------------------------------------
void CDialogGameInfo::Connect()
{
OnConnect();
}
//-----------------------------------------------------------------------------
// Purpose: Connects the user to this game
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnConnect()
{
// flag that we are attempting connection
m_bConnecting = true;
// reset state
m_bServerFull = false;
m_bServerNotResponding = false;
InvalidateLayout();
// need to refresh server before attempting to connect, to make sure there is enough room on the server
RequestInfo();
}
//-----------------------------------------------------------------------------
// Purpose: Handles Refresh button press, starts a re-ping of the server
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnRefresh()
{
// re-ask the server for the game info
RequestInfo();
}
//-----------------------------------------------------------------------------
// Purpose: Deletes the dialog when it's closed
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnClose()
{
BaseClass::OnClose();
MarkForDeletion();
}
//-----------------------------------------------------------------------------
// Purpose: Forces the whole dialog to redraw when the auto-retry button is toggled
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnButtonToggled(Panel *panel)
{
if (panel == m_pAutoRetry)
{
ShowAutoRetryOptions(m_pAutoRetry->IsSelected());
}
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Sets whether the extended auto-retry options are visible or not
// Input : state -
//-----------------------------------------------------------------------------
void CDialogGameInfo::ShowAutoRetryOptions(bool state)
{
// we need to extend the dialog
int growSize = 60;
if (!state)
{
growSize = -growSize;
}
// alter the dialog size accordingly
int wide, tall;
GetSize(wide, tall);
tall += growSize;
SetSize(wide, tall);
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Sets the text of a control by name
//-----------------------------------------------------------------------------
void CDialogGameInfo::SetControlText(const char *textEntryName, const char *text)
{
TextEntry *entry = dynamic_cast<TextEntry *>(FindChildByName(textEntryName));
if (entry)
{
entry->SetText(text);
}
}
//-----------------------------------------------------------------------------
// Purpose: Requests the right info from the server
//-----------------------------------------------------------------------------
void CDialogGameInfo::RequestInfo()
{
// reset the time at which we auto-refresh
m_iRequestRetry = system()->GetTimeMillis() + RETRY_TIME;
if (!m_Servers.IsRefreshing())
{
m_Servers.AddServerToRefreshList(m_iServerID);
m_Servers.StartRefresh();
}
}
//-----------------------------------------------------------------------------
// Purpose: Called every frame, handles resending network messages
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnTick()
{
// check to see if we should perform an auto-refresh
if (m_iRequestRetry && m_iRequestRetry < system()->GetTimeMillis())
{
// reask
RequestInfo();
}
m_Servers.RunFrame();
}
//-----------------------------------------------------------------------------
// Purpose: called when the server has successfully responded
//-----------------------------------------------------------------------------
void CDialogGameInfo::ServerResponded(serveritem_t &server)
{
if (m_bConnecting)
{
ConnectToServer();
}
else if (m_pAutoRetry->IsSelected())
{
// auto-retry is enabled, see if we can join
if (server.players < server.maxPlayers)
{
// there is a slot free, we can join
// make the sound
surface()->PlaySound("Servers\\game_ready.wav");
// flash this window
FlashWindow();
// if it's set, connect right away
if (m_pAutoRetryJoin->IsSelected())
{
ConnectToServer();
}
}
}
m_bServerNotResponding = false;
InvalidateLayout();
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: called when a server response has timed out
//-----------------------------------------------------------------------------
void CDialogGameInfo::ServerFailedToRespond(serveritem_t &server)
{
// the server didn't respond, mark that in the UI
// only mark if we haven't ever received a response
if (!server.hadSuccessfulResponse)
{
m_bServerNotResponding = true;
}
InvalidateLayout();
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Connects to the server
//-----------------------------------------------------------------------------
void CDialogGameInfo::ConnectToServer()
{
m_bConnecting = false;
serveritem_t &server = m_Servers.GetServer(m_iServerID);
// check to see if we need a password
if (server.password && !m_szPassword[0])
{
CDialogServerPassword *box = new CDialogServerPassword();
box->AddActionSignalTarget(this);
box->Activate(server.name, server.serverID);
return;
}
// check the player count
if (server.players >= server.maxPlayers)
{
// mark why we cannot connect
m_bServerFull = true;
// give them access to auto-retry options
m_bShowAutoRetryToggle = true;
InvalidateLayout();
return;
}
// tell the engine to connect
char buf[64];
sprintf(buf, "%d.%d.%d.%d:%d", server.ip[0], server.ip[1], server.ip[2], server.ip[3], server.port);
const char *gameDir = server.gameDir;
if (g_pRunGameEngine->IsRunning())
{
char command[256];
// set the server password, if any
if (m_szPassword[0])
{
sprintf(command, "password \"%s\"\n", m_szPassword);
g_pRunGameEngine->AddTextCommand(command);
}
// send engine command to change servers
sprintf(command, "connect %s\n", buf);
g_pRunGameEngine->AddTextCommand(command);
}
else
{
char command[256];
// sprintf(command, " -game %s +connect %s", gameDir, buf);
sprintf(command, " +connect %s", buf);
if (m_szPassword[0])
{
strcat(command, " +password \"");
strcat(command, m_szPassword);
strcat(command, "\"");\
}
g_pRunGameEngine->RunEngine(gameDir, command);
}
// close this dialog
PostMessage(this, new KeyValues("Close"));
}
//-----------------------------------------------------------------------------
// Purpose: called when the current refresh list is complete
//-----------------------------------------------------------------------------
void CDialogGameInfo::RefreshComplete()
{
}
//-----------------------------------------------------------------------------
// Purpose: handles response from the get password dialog
//-----------------------------------------------------------------------------
void CDialogGameInfo::OnJoinServerWithPassword(const char *password)
{
// copy out the password
v_strncpy(m_szPassword, password, sizeof(m_szPassword));
// retry connecting to the server again
OnConnect();
}
//-----------------------------------------------------------------------------
// Purpose: Message map
//-----------------------------------------------------------------------------
MessageMapItem_t CDialogGameInfo::m_MessageMap[] =
{
MAP_MESSAGE( CDialogGameInfo, "Refresh", OnRefresh ),
MAP_MESSAGE( CDialogGameInfo, "Connect", OnConnect ),
MAP_MESSAGE_PTR( CDialogGameInfo, "ButtonToggled", OnButtonToggled, "panel" ),
MAP_MESSAGE_PTR( CDialogGameInfo, "RadioButtonChecked", OnButtonToggled, "panel" ),
MAP_MESSAGE_CONSTCHARPTR( CDialogGameInfo, "JoinServerWithPassword", OnJoinServerWithPassword, "password" ),
};
IMPLEMENT_PANELMAP( CDialogGameInfo, Frame );
|