summaryrefslogtreecommitdiff
path: root/tracker/AdminServer/serverinfopanel.cpp
blob: c384d55aaadb93f756306bd53268b5abe9c08eae (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "ServerInfoPanel.h"
#include "MapCycleEditDialog.h"

#include <vgui/ISystem.h>

#include <ctype.h>
#include <stdio.h>

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CServerInfoPanel::CServerInfoPanel(vgui::Panel *parent, const char *name) : CVarListPropertyPage(parent, name)
{
	LoadControlSettings("Admin/GamePanelInfo.res", "PLATFORM");
	LoadVarList("Admin/MainServerConfig.vdf");
	m_iLastUptimeDisplayed = 0;
	m_flUpdateTime = 0.0f;
	m_bMapListRetrieved = false;
	RemoteServer().AddServerMessageHandler(this, "UpdatePlayers");
	RemoteServer().AddServerMessageHandler(this, "UpdateMap");
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CServerInfoPanel::~CServerInfoPanel()
{
	RemoteServer().RemoveServerDataResponseTarget(this);
}

//-----------------------------------------------------------------------------
// Purpose: gets the current hostname
//-----------------------------------------------------------------------------
const char *CServerInfoPanel::GetHostname()
{
	return GetVarString("hostname");
}

//-----------------------------------------------------------------------------
// Purpose: Refreshes page if necessary
//-----------------------------------------------------------------------------
void CServerInfoPanel::OnThink()
{
	if (m_flUpdateTime < vgui::system()->GetFrameTime())
	{
		OnResetData();
	}

	// check uptime count
	int time = m_iLastUptimeReceived + (int)(vgui::system()->GetFrameTime() - m_flLastUptimeReceiveTime);
	if (time != m_iLastUptimeDisplayed)
	{
		m_iLastUptimeDisplayed = time;
		char timeText[64];
		_snprintf(timeText, sizeof(timeText), "%0.1i:%0.2i:%0.2i:%0.2i", (time / 3600) / 24, (time / 3600), (time / 60) % 60, time % 60);
		SetControlString("UpTimeText", timeText);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Data is be reloaded from document into controls.
//-----------------------------------------------------------------------------
void CServerInfoPanel::OnResetData()
{
	// only ask for maps list once
	if (!m_bMapListRetrieved)
	{
		RemoteServer().RequestValue(this, "maplist");
		m_bMapListRetrieved = true;
	}

	// refresh our vars
	RefreshVarList();

	// ask for the constant data
	RemoteServer().RequestValue(this, "playercount");
	RemoteServer().RequestValue(this, "maxplayers");
	RemoteServer().RequestValue(this, "gamedescription");
	RemoteServer().RequestValue(this, "uptime");
	RemoteServer().RequestValue(this, "ipaddress");

	// update once every minute
	m_flUpdateTime = (float)system()->GetFrameTime() + (60 * 1.0f);
}

//-----------------------------------------------------------------------------
// Purpose: handles responses from the server
//-----------------------------------------------------------------------------
void CServerInfoPanel::OnServerDataResponse(const char *value, const char *response)
{
	if (!stricmp(value, "playercount"))
	{
		m_iPlayerCount = atoi(response);
	}
	else if (!stricmp(value, "maxplayers"))
	{
		m_iMaxPlayers = atoi(response);

		// update control
		char buf[128];
		buf[0] = 0;
		if (m_iMaxPlayers > 0)
		{
			sprintf(buf, "%d / %d", m_iPlayerCount, m_iMaxPlayers);
		}
		SetControlString("PlayersText", buf);
	}
	else if (!stricmp(value, "gamedescription"))
	{
		SetControlString("GameText", response);
	}
	else if (!stricmp(value, "hostname"))
	{
		PostActionSignal(new KeyValues("UpdateTitle"));
	}
	else if (!stricmp(value, "UpdateMap") || !stricmp(value, "UpdatePlayers"))
	{
		// server has indicated a change, force an update
		m_flUpdateTime = 0.0f;
	}
	else if (!stricmp(value, "maplist"))
	{
		SetCustomStringList("map", response);
		// save off maplist for use in mapcycle editing
		ParseIntoMapList(response, m_AvailableMaps);
		// don't chain through, not in varlist
		return;
	}
	else if (!stricmp(value, "uptime"))
	{
		// record uptime for extrapolation
		m_iLastUptimeReceived = atoi(response);
		m_flLastUptimeReceiveTime = (float)system()->GetFrameTime();
	}
	else if (!stricmp(value, "ipaddress"))
	{
		SetControlString("ServerIPText", response);
	}
	else if (!stricmp(value, "mapcycle"))
	{
		ParseIntoMapList(response, m_MapCycle);
		UpdateMapCycleValue();
		// don't chain through, we set the value ourself
		return;
	}

	// always chain through, in case this variable is in the list as well
	BaseClass::OnServerDataResponse(value, response);

	// post update
	if (!stricmp(value, "map"))
	{
		// map has changed, update map cycle view
		UpdateMapCycleValue();
	}
}

//-----------------------------------------------------------------------------
// Purpose: special editing of map cycle list
//-----------------------------------------------------------------------------
void CServerInfoPanel::OnEditVariable(KeyValues *rule)
{
	if (!stricmp(rule->GetName(), "mapcycle"))
	{
		CMapCycleEditDialog *dlg = new CMapCycleEditDialog(this, "MapCycleEditDialog");
		dlg->Activate(this, m_AvailableMaps, m_MapCycle);
	}
	else
	{
		BaseClass::OnEditVariable(rule);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Updates the value shown in the mapcycle field
//-----------------------------------------------------------------------------
void CServerInfoPanel::UpdateMapCycleValue()
{
	// look at current map
	CUtlSymbol currentMap = GetVarString("map");
	if (!currentMap.IsValid())
		return;

	// find it in the map cycle list
	int listPoint = -1;
	for (int i = 0; i < m_MapCycle.Count(); i++)
	{
		if (!stricmp(m_MapCycle[i].String(), currentMap.String()))
		{
			listPoint = i;
		}
	}

	// take out the next 2 maps and make them the value
	char nextMaps[512];
	nextMaps[0] = 0;
	bool needComma = false;
	for (int i = 0; i < 2; i++)
	{
		int point = listPoint + i + 1;
		if (point >= m_MapCycle.Count())
		{
			point -= m_MapCycle.Count();
		}

		if (m_MapCycle.IsValidIndex(point))
		{
			if (needComma)
			{
				strcat(nextMaps, ", ");
			}
			strcat(nextMaps, m_MapCycle[point].String());
			needComma = true;
		}
	}

	// add some elipses to show there is more maps
	if (needComma)
	{
		strcat(nextMaps, ", ");
	}
	strcat(nextMaps, "...");

	// show in varlist
	SetVarString("mapcycle", nextMaps);
}

//-----------------------------------------------------------------------------
// Purpose: Seperates out a newline-seperated map list into an array
//-----------------------------------------------------------------------------
void CServerInfoPanel::ParseIntoMapList(const char *maplist, CUtlVector<CUtlSymbol> &mapArray)
{
	mapArray.RemoveAll();

	const char *parse = maplist;
	while (*parse)
	{
		// newline-seperated map list
		//!! this should be done with a more standard tokenizer
		if (isspace(*parse))
		{
			parse++;
			continue;
		}

		// pull out the map name
		const char *end = strstr(parse, "\n");
		const char *end2 = strstr(parse, "\r");
		if (end && end2 && end2 < end)
		{
			end = end2;
		}
		if (!end)
			break;

		char customString[64];
		int nameSize = end - parse;
		if (nameSize >= sizeof(customString))
		{
			nameSize = sizeof(customString) - 1;
		}

		// copy in the name
		strncpy(customString, parse, nameSize);
		customString[nameSize] = 0;
		parse = end;

		// add to the list string that aren't comments
		if (nameSize > 0 && !(customString[0] == '/' && customString[1] == '/'))
		{
			int i = mapArray.AddToTail();
			mapArray[i] = customString;
		}
	}
}