summaryrefslogtreecommitdiff
path: root/game/shared/motd.cpp
blob: d9a245bc3f610f399f42efd9319d7dda689c59cd (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: motd: Handles a list of message of the day entries
//
//=============================================================================

#include "cbase.h"
#include "motd.h"
#include "schemainitutils.h"
#include "rtime.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

using namespace GCSDK;

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
CMOTDEntryDefinition::CMOTDEntryDefinition( void )
{
	m_pKVMOTD = NULL;
	m_PostTime = 0;
	m_ChangedTime = 0;
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
bool CMOTDEntryDefinition::BInitFromKV( KeyValues *pKVMOTD, CUtlVector<CUtlString> *pVecErrors )
{
	m_pKVMOTD = pKVMOTD->MakeCopy();

	const char *pszTime = m_pKVMOTD->GetString( "post_time", NULL );
	m_PostTime = (pszTime && pszTime[0]) ? CRTime::RTime32FromString(pszTime) : 0;

	pszTime = m_pKVMOTD->GetString( "last_changed_time", NULL );
	m_ChangedTime = (pszTime && pszTime[0]) ? CRTime::RTime32FromString(pszTime) : 0;

	return SCHEMA_INIT_SUCCESS();
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
const char *CMOTDEntryDefinition::GetTitle( ELanguage eLang ) 
{ 
	if ( m_pKVMOTD )
	{
		// See if we have a localised block for the specified language.
		const char *pszLanguage = GetLanguageShortName( eLang );
		if ( pszLanguage && pszLanguage[0] )
		{
			const char *pszText = m_pKVMOTD->GetString( CFmtStr( "title_%s", pszLanguage ), NULL );
			if ( pszText && pszText[0] )
				return pszText;
		}

		// Fall back to english
		return m_pKVMOTD->GetString( "title_english", "No Title" );
	}

	return "No Title";
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
const char *CMOTDEntryDefinition::GetText( ELanguage eLang ) 
{ 
	if ( m_pKVMOTD )
	{
		// See if we have a localised block for the specified language.
		const char *pszLanguage = GetLanguageShortName( eLang );
		if ( pszLanguage && pszLanguage[0] )
		{
			const char *pszText = m_pKVMOTD->GetString( CFmtStr( "text_%s", pszLanguage ), NULL );
			if ( pszText && pszText[0] )
				return pszText;
		}

		// Fall back to english
		return m_pKVMOTD->GetString( "text_english", "No text" );
	}

	return "No text";
}

//-----------------------------------------------------------------------------
// 
//-----------------------------------------------------------------------------
const char *CMOTDEntryDefinition::GetHeaderTitle( ELanguage eLang )
{ 
	if ( m_pKVMOTD )
	{
		// See if we have a localised block for the specified language.
		const char *pszLanguage = GetLanguageShortName( eLang );
		if ( pszLanguage && pszLanguage[0] )
		{
			const char *pszText = m_pKVMOTD->GetString( CFmtStr( "header_%s", pszLanguage ), NULL );
			if ( pszText && pszText[0] )
				return pszText;
		}

		// Fall back to english
		return m_pKVMOTD->GetString( "header_english", "News" );
	}

	return "News";
}

// Sort by ID
int	MOTDEntriesListLess( const CMOTDEntryDefinition *pLhs, const CMOTDEntryDefinition *pRhs )
{
	// This is stupid, sort by the KeyID instead
	return ( pLhs->GetNameInt() > pRhs->GetNameInt() );
}

//-----------------------------------------------------------------------------
// Purpose:	Initializes the loot lists section of the schema
//-----------------------------------------------------------------------------
bool CMOTDManager::BInitMOTDEntries( KeyValues *pKVMOTDEntries, CUtlVector<CUtlString> *pVecErrors )
{
	m_vecMOTDEntries.RemoveAll();

	RTime32 iPrevTime = 0;

	if ( NULL != pKVMOTDEntries )
	{
		FOR_EACH_TRUE_SUBKEY( pKVMOTDEntries, pKVEntry )
		{
			const char *listName = pKVEntry->GetName();

			SCHEMA_INIT_CHECK( listName != NULL, "All MOTD entries must have titles." );

			int idx = m_vecMOTDEntries.AddToTail();
			SCHEMA_INIT_SUBSTEP( m_vecMOTDEntries[idx].BInitFromKV( pKVEntry, pVecErrors ) );

			// Make sure the dates all move forward
			SCHEMA_INIT_CHECK( m_vecMOTDEntries[idx].GetPostTime() > iPrevTime , "MOTD entry '%s' occurs prior to the previous entry.", m_vecMOTDEntries[idx].GetName() );
			iPrevTime = m_vecMOTDEntries[idx].GetPostTime();
		}
	}

	// Then sort all the MOTDs in order of their changed times, so we can easily send them
	m_vecMOTDEntries.Sort( MOTDEntriesListLess );

	return SCHEMA_INIT_SUCCESS();
}

//-----------------------------------------------------------------------------
// Purpose:	Returns the number of MOTD entries we've got after the specified time
//-----------------------------------------------------------------------------
int CMOTDManager::GetNumMOTDAfter( RTime32 iTime )
{
	FOR_EACH_VEC( m_vecMOTDEntries, i )
	{
		if ( m_vecMOTDEntries[i].GetChangedTime() > iTime )
		{
			// We've hit the first MOTD entry after this time. All following posts are assumed after.
			return (m_vecMOTDEntries.Count() - i);
		}
	}

	return 0;
}

//-----------------------------------------------------------------------------
// Remove all unused MOTD: Save memory and whatever
//-----------------------------------------------------------------------------
void CMOTDManager::PurgeUnusedMOTDEntries( KeyValues *pKVMOTDEntries ) 
{
	// Find the latest entry name and remove all others
	int iLargest = -1;
	FOR_EACH_VEC_BACK( m_vecMOTDEntries, i )
	{
		int iMOTDindex = m_vecMOTDEntries[i].GetNameInt();
		if ( iMOTDindex > iLargest )
		{
			iLargest = iMOTDindex;
		}
	}

	FOR_EACH_VEC_BACK( m_vecMOTDEntries, i )
	{
		int iMOTDindex = m_vecMOTDEntries[i].GetNameInt();
		if ( iMOTDindex < iLargest )
		{
			if ( pKVMOTDEntries )
			{
				KeyValues *pKey = pKVMOTDEntries->FindKey( m_vecMOTDEntries[i].GetName() );
				if ( pKey )
				{
					pKVMOTDEntries->RemoveSubKey( pKey );
				}
			}
			m_vecMOTDEntries.Remove( i );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose:	Returns the definition for the next blog post after the specified time
//-----------------------------------------------------------------------------
CMOTDEntryDefinition *CMOTDManager::GetNextMOTDAfter( RTime32 iTime )
{
	FOR_EACH_VEC( m_vecMOTDEntries, i )
	{
		if ( m_vecMOTDEntries[i].GetChangedTime() > iTime )
			return &m_vecMOTDEntries[i];
	}

	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose:	
//-----------------------------------------------------------------------------
CMOTDEntryDefinition *CMOTDManager::GetMOTDByIndex( int iIndex )
{
	if ( iIndex < 0 || iIndex > m_vecMOTDEntries.Count() )
		return NULL;
	return &m_vecMOTDEntries[iIndex];
}


#ifdef GC_DLL

//-----------------------------------------------------------------------------
// Handle MOTD requests job.
//-----------------------------------------------------------------------------
class CGCMOTDRequest : public CGCGameBaseJob
{
public:
	CGCMOTDRequest( CGCGameBase *pGC ) : CGCGameBaseJob( pGC ) { }
	bool BYieldingRunJobFromMsg( GCSDK::IMsgNetPacket *pNetPacket );
};


//-----------------------------------------------------------------------------
// Purpose: Responds to requests from the client for the current MOTD list
//-----------------------------------------------------------------------------
bool CGCMOTDRequest::BYieldingRunJobFromMsg( IMsgNetPacket *pNetPacket )
{
	CGCMsg< MsgGCMOTDRequest_t > msg( pNetPacket );
	ELanguage eLang = (ELanguage)msg.Body().m_eLanguage;
	RTime32 iMOTDTime = msg.Body().m_nLastMOTDRequest;

	// Send the response to the client
	GCSDK::CGCMsg<MsgGCMOTDRequestResponse_t> msg_response( k_EMsgGCMOTDRequestResponse );

	int iEntries = 0;
	CMOTDEntryDefinition *pMOTD = m_pGCGameBase->GetMOTDManager().GetNextMOTDAfter( iMOTDTime );
	while ( pMOTD )
	{
		// Stuff this MOTD into the message.
		msg_response.AddStrData( pMOTD->GetName() );
		msg_response.AddUintData( pMOTD->GetPostTime() );
		msg_response.AddStrData( pMOTD->GetTitle( eLang ) );
		msg_response.AddStrData( pMOTD->GetText( eLang ) );
		msg_response.AddStrData( pMOTD->GetURL() );
		msg_response.AddStrData( pMOTD->GetImage() );
		msg_response.AddIntData( pMOTD->GetHeaderType() );
		msg_response.AddStrData( pMOTD->GetHeaderTitle( eLang ) );
		msg_response.AddStrData( pMOTD->GetHeaderIcon() );
		iEntries++;

		// Move on to the next message.
		iMOTDTime = pMOTD->GetChangedTime();
		pMOTD = m_pGCGameBase->GetMOTDManager().GetNextMOTDAfter( iMOTDTime );
	} 
	msg_response.Body().m_nEntries = iEntries;

	GGCEcon()->BSendGCMsgToClient( msg.Hdr().m_ulSteamID, msg_response );

	return true;
}

GC_REG_JOB( CGCGameBase, CGCMOTDRequest, "CGCMOTDRequest", k_EMsgGCMOTDRequest, k_EServerTypeGC );

#endif // GC_DLL