Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84d0df7

Browse files
committed
Added 5275: ([Request] a function to set the ASE player score)
1 parent 99a8062 commit 84d0df7

File tree

10 files changed

+180
-12
lines changed

10 files changed

+180
-12
lines changed

MTA10_Server/mods/deathmatch/logic/ASE.cpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ ASE::ASE ( CMainConfig* pMainConfig, CPlayerManager* pPlayerManager, unsigned sh
3333
m_pMainConfig = pMainConfig;
3434
m_pPlayerManager = pPlayerManager;
3535

36+
m_uiFullLastPlayerCount = 0;
37+
m_llFullLastTime = 0;
38+
m_lFullMinInterval = 10 * 1000; // Update full query cache after 10 seconds
39+
40+
m_uiLightLastPlayerCount = 0;
41+
m_llLightLastTime = 0;
42+
m_lLightMinInterval = 10 * 1000; // Update light query cache after 10 seconds
43+
3644
m_strGameType = "MTA:SA";
3745
m_strMapName = "None";
3846
if ( szServerIP )
@@ -107,17 +115,17 @@ void ASE::DoPulse ( void )
107115
{
108116
case 's':
109117
{ // ASE protocol query
110-
strReply = QueryFull ();
118+
strReply = QueryFullCached ();
111119
break;
112120
}
113121
case 'b':
114122
{ // Our own lighter query for ingame browser
115-
strReply = QueryLight ();
123+
strReply = QueryLightCached ();
116124
break;
117125
}
118126
case 'r':
119127
{ // Our own lighter query for ingame browser - Release version only
120-
strReply = QueryLight ();
128+
strReply = QueryLightCached ();
121129
break;
122130
}
123131
case 'v':
@@ -143,6 +151,22 @@ void ASE::DoPulse ( void )
143151
}
144152

145153

154+
// Protect against a flood of server queries.
155+
// Send cached version unless player count has changed, or last re-cache is older than m_lFullMinInterval
156+
const std::string& ASE::QueryFullCached ( void )
157+
{
158+
long long llTime = GetTickCount64_ ();
159+
unsigned int uiPlayerCount = m_pPlayerManager->CountJoined ();
160+
if ( uiPlayerCount != m_uiFullLastPlayerCount || llTime - m_llFullLastTime > m_lFullMinInterval || m_strFullCached == "" )
161+
{
162+
m_strFullCached = QueryFull ();
163+
m_llFullLastTime = llTime;
164+
m_uiFullLastPlayerCount = uiPlayerCount;
165+
}
166+
return m_strFullCached;
167+
}
168+
169+
146170
std::string ASE::QueryFull ( void )
147171
{
148172
std::stringstream reply;
@@ -225,8 +249,10 @@ std::string ASE::QueryFull ( void )
225249
reply << ( unsigned char ) 1;
226250
// skin (skip)
227251
reply << ( unsigned char ) 1;
228-
// score (skip)
229-
reply << ( unsigned char ) 1;
252+
// score
253+
const std::string& strScore = pPlayer->GetAnnounceValue ( "Score" );
254+
reply << ( unsigned char ) ( strScore.length () + 1 );
255+
reply << strScore.c_str ();
230256
// ping
231257
_snprintf ( szTemp, 255, "%u", pPlayer->GetPing () );
232258
reply << ( unsigned char ) ( strlen ( szTemp ) + 1 );
@@ -240,6 +266,22 @@ std::string ASE::QueryFull ( void )
240266
}
241267

242268

269+
// Protect against a flood of server queries.
270+
// Send cached version unless player count has changed, or last re-cache is older than m_lLightMinInterval
271+
const std::string& ASE::QueryLightCached ( void )
272+
{
273+
long long llTime = GetTickCount64_ ();
274+
unsigned int uiPlayerCount = m_pPlayerManager->CountJoined ();
275+
if ( uiPlayerCount != m_uiLightLastPlayerCount || llTime - m_llLightLastTime > m_lLightMinInterval || m_strLightCached == "" )
276+
{
277+
m_strLightCached = QueryLight ();
278+
m_llLightLastTime = llTime;
279+
m_uiLightLastPlayerCount = uiPlayerCount;
280+
}
281+
return m_strLightCached;
282+
}
283+
284+
243285
std::string ASE::QueryLight ( void )
244286
{
245287
std::stringstream reply;

MTA10_Server/mods/deathmatch/logic/ASE.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class ASE
5050

5151
static ASE* GetInstance ( void ) { return _instance; }
5252

53+
const std::string& QueryFullCached ( void );
5354
std::string QueryFull ( void );
55+
const std::string& QueryLightCached ( void );
5456
std::string QueryLight ( void );
5557

5658
CLanBroadcast* InitLan ( void );
@@ -91,6 +93,18 @@ class ASE
9193
bool m_bLan;
9294
unsigned short m_usPort;
9395

96+
// Full query cache
97+
unsigned int m_uiFullLastPlayerCount;
98+
long long m_llFullLastTime;
99+
long m_lFullMinInterval;
100+
std::string m_strFullCached;
101+
102+
// Light query cache
103+
unsigned int m_uiLightLastPlayerCount;
104+
long long m_llLightLastTime;
105+
long m_lLightMinInterval;
106+
std::string m_strLightCached;
107+
94108
protected:
95109
void GetStatusVals();
96110

MTA10_Server/mods/deathmatch/logic/CPlayer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,20 @@ void CPlayer::ClearSyncTimes ( void )
589589
// Clear the list so we won't try accessing bad data later
590590
m_SyncTimes.clear ();
591591
}
592+
593+
594+
// Note: The return value must be consumed before m_AnnounceValues is next modified
595+
const std::string& CPlayer::GetAnnounceValue ( const string& strKey ) const
596+
{
597+
std::map < string, string > ::const_iterator it = m_AnnounceValues.find ( strKey );
598+
if ( it != m_AnnounceValues.end () )
599+
return it->second;
600+
static std::string strDefault;
601+
return strDefault;
602+
}
603+
604+
605+
void CPlayer::SetAnnounceValue ( const string& strKey, const string& strValue )
606+
{
607+
m_AnnounceValues [ strKey ] = strValue;
608+
}

MTA10_Server/mods/deathmatch/logic/CPlayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ class CPlayer : public CPed, public CClient
204204
void NotifyReceivedSync ( void ) { m_ulLastReceivedSyncTime = GetTickCount (); }
205205
unsigned long GetTicksSinceLastReceivedSync ( void ) const { return GetTickCount () - m_ulLastReceivedSyncTime; }
206206

207+
const std::string& GetAnnounceValue ( const std::string& strKey ) const;
208+
void SetAnnounceValue ( const std::string& strKey, const std::string& strValue );
209+
207210
private:
208211
void WriteCameraModePacket ( void );
209212
void WriteCameraPositionPacket ( void );
@@ -285,6 +288,8 @@ class CPlayer : public CPed, public CClient
285288
unsigned int m_uiPuresyncPackets;
286289

287290
unsigned long m_ulLastReceivedSyncTime;
291+
292+
std::map < std::string, std::string > m_AnnounceValues;
288293
};
289294

290295
#endif

MTA10_Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7305,6 +7305,32 @@ bool CStaticFunctionDefinitions::RemoveRuleValue ( const char* szKey )
73057305
}
73067306

73077307

7308+
bool CStaticFunctionDefinitions::GetPlayerAnnounceValue ( CElement* pElement, const std::string& strKey, std::string& strOutValue )
7309+
{
7310+
if ( IS_PLAYER ( pElement ) )
7311+
{
7312+
CPlayer* pPlayer = static_cast < CPlayer* > ( pElement );
7313+
7314+
strOutValue = pPlayer->GetAnnounceValue ( strKey );
7315+
return true;
7316+
}
7317+
return false;
7318+
}
7319+
7320+
7321+
bool CStaticFunctionDefinitions::SetPlayerAnnounceValue ( CElement* pElement, const std::string& strKey, const std::string& strValue )
7322+
{
7323+
if ( IS_PLAYER ( pElement ) )
7324+
{
7325+
CPlayer* pPlayer = static_cast < CPlayer* > ( pElement );
7326+
7327+
pPlayer->SetAnnounceValue ( strKey, strValue );
7328+
return true;
7329+
}
7330+
return false;
7331+
}
7332+
7333+
73087334
void CStaticFunctionDefinitions::ExecuteSQLCreateTable ( const std::string& strTable, const std::string& strDefinition )
73097335
{
73107336
m_pRegistry->CreateTable ( strTable, strDefinition );

MTA10_Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ class CStaticFunctionDefinitions
472472
static const char* GetRuleValue ( const char* szKey );
473473
static bool SetRuleValue ( const char* szKey, const char* szValue );
474474
static bool RemoveRuleValue ( const char* szKey );
475+
static bool GetPlayerAnnounceValue ( CElement* pElement, const std::string& strKey, std::string& strOutValue );
476+
static bool SetPlayerAnnounceValue ( CElement* pElement, const std::string& strKey, const std::string& strValue );
475477

476478
// Registry funcs
477479
static const std::string& SQLGetLastError ( void );

MTA10_Server/mods/deathmatch/logic/lua/CLuaFunctionDefinitions.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9721,6 +9721,53 @@ int CLuaFunctionDefinitions::RemoveRuleValue ( lua_State* luaVM )
97219721
}
97229722

97239723

9724+
int CLuaFunctionDefinitions::GetPlayerAnnounceValue ( lua_State* luaVM )
9725+
{
9726+
if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA &&
9727+
lua_type ( luaVM, 2 ) == LUA_TSTRING )
9728+
{
9729+
CElement* pElement = lua_toelement ( luaVM, 1 );
9730+
std::string strKey = lua_tostring ( luaVM, 2 );
9731+
std::string strValue;
9732+
9733+
if ( CStaticFunctionDefinitions::GetPlayerAnnounceValue ( pElement, strKey, strValue ) )
9734+
{
9735+
lua_pushstring ( luaVM, strValue.c_str () );
9736+
return 1;
9737+
}
9738+
}
9739+
else
9740+
m_pScriptDebugging->LogBadType ( luaVM, "getPlayerAnnounceValue" );
9741+
9742+
lua_pushboolean ( luaVM, false );
9743+
return 1;
9744+
}
9745+
9746+
9747+
int CLuaFunctionDefinitions::SetPlayerAnnounceValue ( lua_State* luaVM )
9748+
{
9749+
if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA &&
9750+
lua_type ( luaVM, 2 ) == LUA_TSTRING &&
9751+
lua_type ( luaVM, 3 ) == LUA_TSTRING )
9752+
{
9753+
CElement* pElement = lua_toelement ( luaVM, 1 );
9754+
std::string strKey = lua_tostring ( luaVM, 2 );
9755+
std::string strValue = lua_tostring ( luaVM, 3 );
9756+
9757+
if ( CStaticFunctionDefinitions::SetPlayerAnnounceValue ( pElement, strKey, strValue ) )
9758+
{
9759+
lua_pushboolean ( luaVM, true );
9760+
return 1;
9761+
}
9762+
}
9763+
else
9764+
m_pScriptDebugging->LogBadType ( luaVM, "setPlayerAnnounceValue" );
9765+
9766+
lua_pushboolean ( luaVM, false );
9767+
return 1;
9768+
}
9769+
9770+
97249771
int CLuaFunctionDefinitions::ExecuteSQLCreateTable ( lua_State* luaVM )
97259772
{
97269773
if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TSTRING )

MTA10_Server/mods/deathmatch/logic/lua/CLuaFunctionDefinitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ class CLuaFunctionDefinitions
438438
static int GetRuleValue ( lua_State* luaVM );
439439
static int SetRuleValue ( lua_State* luaVM );
440440
static int RemoveRuleValue ( lua_State* luaVM );
441+
static int GetPlayerAnnounceValue ( lua_State* luaVM );
442+
static int SetPlayerAnnounceValue ( lua_State* luaVM );
441443

442444
// Registry funcs
443445
static int ExecuteSQLCreateTable ( lua_State* luaVM );

MTA10_Server/mods/deathmatch/logic/lua/CLuaManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ void CLuaManager::LoadCFunctions ( void )
615615
CLuaCFunctions::AddFunction ( "getRuleValue", CLuaFunctionDefinitions::GetRuleValue );
616616
CLuaCFunctions::AddFunction ( "setRuleValue", CLuaFunctionDefinitions::SetRuleValue );
617617
CLuaCFunctions::AddFunction ( "removeRuleValue", CLuaFunctionDefinitions::RemoveRuleValue );
618+
CLuaCFunctions::AddFunction ( "getPlayerAnnounceValue", CLuaFunctionDefinitions::GetPlayerAnnounceValue );
619+
CLuaCFunctions::AddFunction ( "setPlayerAnnounceValue", CLuaFunctionDefinitions::SetPlayerAnnounceValue );
618620

619621
// Registry functions
620622
CLuaCFunctions::AddFunction ( "executeSQLCreateTable", CLuaFunctionDefinitions::ExecuteSQLCreateTable );

Shared/sdk/SharedUtil.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,28 +188,39 @@ namespace SharedUtil
188188
//
189189

190190
// Update or add a value for a key
191-
template < class T, class V >
192-
void MapSet ( std::map < T, V >& collection, const T& key, const V& value )
191+
template < class T, class V, class TR, class T2 >
192+
void MapSet ( std::map < T, V, TR >& collection, const T2& key, const V& value )
193193
{
194194
collection[ key ] = value;
195195
}
196196

197197
// Returns true if the item is in the collection
198-
template < class T, class V >
199-
bool MapContains ( const std::map < T, V >& collection, const T& key )
198+
template < class T, class V, class TR, class T2 >
199+
bool MapContains ( const std::map < T, V, TR >& collection, const T2& key )
200200
{
201201
return collection.find ( key ) != collection.end ();
202202
}
203203

204204
// Remove key from collection
205-
template < class T, class V >
206-
void MapRemove ( std::map < T, V >& collection, const T& key )
205+
template < class T, class V, class TR, class T2 >
206+
void MapRemove ( std::map < T, V, TR >& collection, const T2& key )
207207
{
208-
typename std::map < T, V > ::iterator it = collection.find ( key );
208+
typename std::map < T, V, TR > ::iterator it = collection.find ( key );
209209
if ( it != collection.end () )
210210
collection.erase ( it );
211211
}
212212

213+
// Find value in collection
214+
template < class T, class V, class TR, class T2 >
215+
V* MapFind ( std::map < T, V, TR >& collection, const T2& key )
216+
{
217+
typename std::map < T, V, TR > ::iterator it = collection.find ( key );
218+
if ( it != collection.end () )
219+
return NULL;
220+
return &it->second;
221+
}
222+
223+
213224

214225

215226
//

0 commit comments

Comments
 (0)