Skip to content

Commit 12a0c11

Browse files
emre1702ccw808
authored andcommitted
(#9562) Add new account functions (#164)
Added getAccountIP, getAccountsByIP, getAccountsByData & setAccountName.
1 parent b108390 commit 12a0c11

File tree

6 files changed

+219
-2
lines changed

6 files changed

+219
-2
lines changed

Server/mods/deathmatch/logic/CAccountManager.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,34 @@ void CAccountManager::GetAccountsBySerial ( const SString& strSerial, std::vecto
881881
}
882882
}
883883

884+
void CAccountManager::GetAccountsByIP( const SString& strIP, std::vector<CAccount*>& outAccounts )
885+
{
886+
CRegistryResult result;
887+
m_pDatabaseManager->QueryWithResultf( m_hDbConnection, &result, "SELECT name FROM accounts WHERE added_ip = ?", SQLITE_TEXT, strIP.c_str() );
888+
889+
for ( CRegistryResultIterator iter = result->begin(); iter != result->end(); ++iter )
890+
{
891+
const CRegistryResultRow& row = *iter;
892+
893+
CAccount* pAccount = Get( (const char*) row[0].pVal );
894+
outAccounts.push_back( pAccount );
895+
}
896+
}
897+
898+
void CAccountManager::GetAccountsByData ( const SString& dataName, const SString& value, std::vector<CAccount*>& outAccounts )
899+
{
900+
CRegistryResult result;
901+
m_pDatabaseManager->QueryWithResultf( m_hDbConnection, &result, "SELECT acc.name FROM accounts acc, userdata dat WHERE dat.key = ? AND dat.value = ? AND dat.userid = acc.id", SQLITE_TEXT, dataName.c_str(), SQLITE_TEXT, value.c_str() );
902+
903+
for ( CRegistryResultIterator iter = result->begin(); iter != result->end(); ++iter )
904+
{
905+
const CRegistryResultRow& row = *iter;
906+
907+
CAccount* pAccount = Get( (const char*) row[0].pVal );
908+
outAccounts.push_back( pAccount );
909+
}
910+
}
911+
884912
CAccount* CAccountManager::AddGuestAccount( const SString& strName )
885913
{
886914
CAccount* pAccount = new CAccount ( this, EAccountType::Guest, strName );

Server/mods/deathmatch/logic/CAccountManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class CAccountManager
147147
bool GetAllAccountData ( CAccount* pAccount, lua_State* pLua );
148148

149149
void GetAccountsBySerial ( const SString& strSerial, std::vector<CAccount*>& outAccounts );
150+
void GetAccountsByIP ( const SString& strIP, std::vector<CAccount*>& outAccounts );
151+
void GetAccountsByData ( const SString& dataName, const SString& value, std::vector<CAccount*>& outAccounts );
150152

151153
CAccount* AddGuestAccount ( const SString& strName );
152154
CAccount* AddConsoleAccount ( const SString& strName );

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11201,6 +11201,12 @@ bool CStaticFunctionDefinitions::GetAllAccountData ( lua_State* pLua, CAccount*
1120111201
return true;
1120211202
}
1120311203

11204+
bool CStaticFunctionDefinitions::GetAccountsByData ( const SString& dataName, const SString& value, std::vector<CAccount*>& outAccounts )
11205+
{
11206+
m_pAccountManager->GetAccountsByData ( dataName, value, outAccounts );
11207+
return true;
11208+
}
11209+
1120411210

1120511211
bool CStaticFunctionDefinitions::GetAccountSerial ( CAccount* pAccount, SString& strSerial )
1120611212
{
@@ -11219,6 +11225,23 @@ bool CStaticFunctionDefinitions::GetAccountsBySerial ( const SString& strSerial,
1121911225
}
1122011226

1122111227

11228+
bool CStaticFunctionDefinitions::GetAccountIP( CAccount* pAccount, SString& strIP )
11229+
{
11230+
bool bRegistered = pAccount->IsRegistered();
11231+
if ( bRegistered )
11232+
strIP = pAccount->GetIP();
11233+
11234+
return bRegistered;
11235+
}
11236+
11237+
11238+
bool CStaticFunctionDefinitions::GetAccountsByIP( const SString& strIP, std::vector<CAccount*>& outAccounts )
11239+
{
11240+
m_pAccountManager->GetAccountsByIP( strIP, outAccounts );
11241+
return true;
11242+
}
11243+
11244+
1122211245
CAccount* CStaticFunctionDefinitions::AddAccount ( const SString& strName, const SString& strPassword, bool bAllowCaseVariations, SString& strOutError )
1122311246
{
1122411247
// Check for case variations if not allowed
@@ -11302,6 +11325,42 @@ bool CStaticFunctionDefinitions::RemoveAccount ( CAccount* pAccount )
1130211325
}
1130311326

1130411327

11328+
bool CStaticFunctionDefinitions::SetAccountName ( CAccount* pAccount, SString strNewName, bool bAllowCaseVariations, SString& strOutError )
11329+
{
11330+
assert ( pAccount );
11331+
assert ( !strNewName.empty() );
11332+
11333+
if ( pAccount->IsRegistered () )
11334+
{
11335+
// Check for case variations if not allowed
11336+
if ( !bAllowCaseVariations )
11337+
{
11338+
SString strCaseVariation = m_pAccountManager->GetActiveCaseVariation( strNewName );
11339+
if ( !strCaseVariation.empty() )
11340+
{
11341+
strOutError = SString( "Already an account using a case variation of that name ('%s')", *strCaseVariation );
11342+
return false;
11343+
}
11344+
}
11345+
11346+
if ( m_pAccountManager->Get( strNewName ) != NULL )
11347+
{
11348+
strOutError = "Account already exists";
11349+
}
11350+
else if ( !CAccountManager::IsValidNewAccountName( strNewName ) )
11351+
{
11352+
strOutError = "Name invalid";
11353+
}
11354+
else
11355+
{
11356+
pAccount->SetName ( strNewName );
11357+
return true;
11358+
}
11359+
}
11360+
return false;
11361+
}
11362+
11363+
1130511364
bool CStaticFunctionDefinitions::SetAccountPassword ( CAccount* pAccount, SString strPassword, CAccountPassword::EAccountPasswordType ePasswordType )
1130611365
{
1130711366
assert ( pAccount );

Server/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,16 @@ class CStaticFunctionDefinitions
637637
static bool IsGuestAccount ( CAccount* pAccount, bool& bGuest );
638638
static std::shared_ptr<CLuaArgument> GetAccountData ( CAccount* pAccount, const char* szKey );
639639
static bool GetAllAccountData ( lua_State* pLua, CAccount* pAccount );
640+
static bool GetAccountsByData ( const SString& dataName, const SString& value, std::vector<CAccount*>& outAccounts );
640641
static bool GetAccountSerial ( CAccount* pAccount, SString& strSerial );
641642
static bool GetAccountsBySerial ( const SString& strSerial, std::vector<CAccount*>& outAccounts );
643+
static bool GetAccountIP ( CAccount* pAccount, SString& strIP );
644+
static bool GetAccountsByIP ( const SString& strIP, std::vector<CAccount*>& outAccounts );
642645

643646
// Account set funcs
644647
static CAccount* AddAccount ( const SString& strName, const SString& strPassword, bool bAllowCaseVariations, SString& strOutError );
645648
static bool RemoveAccount ( CAccount* pAccount );
649+
static bool SetAccountName ( CAccount* pAccount, SString strNewName, bool bAllowCaseVariations, SString& strOutError );
646650
static bool SetAccountPassword ( CAccount* pAccount, SString szPassword, CAccountPassword::EAccountPasswordType ePasswordType );
647651
static bool SetAccountData ( CAccount* pAccount, const char* szKey, CLuaArgument * pArgument );
648652
static bool CopyAccountData ( CAccount* pAccount, CAccount* pFromAccount );

Server/mods/deathmatch/logic/luadefs/CLuaAccountDefs.cpp

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ void CLuaAccountDefs::LoadFunctions ()
2525
CLuaCFunctions::AddFunction ( "getAllAccountData", GetAllAccountData );
2626
CLuaCFunctions::AddFunction ( "getAccount", GetAccount );
2727
CLuaCFunctions::AddFunction ( "getAccounts", GetAccounts );
28+
CLuaCFunctions::AddFunction ( "getAccountsByData", GetAccountsByData );
2829
CLuaCFunctions::AddFunction ( "getAccountSerial", GetAccountSerial );
2930
CLuaCFunctions::AddFunction ( "getAccountsBySerial", GetAccountsBySerial );
31+
CLuaCFunctions::AddFunction ( "getAccountIP", GetAccountIP );
32+
CLuaCFunctions::AddFunction ( "getAccountsByIP", GetAccountsByIP );
3033

3134
// Account set functions
3235
CLuaCFunctions::AddFunction ( "addAccount", AddAccount );
3336
CLuaCFunctions::AddFunction ( "removeAccount", RemoveAccount );
3437
CLuaCFunctions::AddFunction ( "setAccountPassword", SetAccountPassword );
3538
CLuaCFunctions::AddFunction ( "setAccountData", SetAccountData );
39+
CLuaCFunctions::AddFunction ( "setAccountName", SetAccountName );
3640
CLuaCFunctions::AddFunction ( "copyAccountData", CopyAccountData );
3741
}
3842

@@ -41,7 +45,9 @@ void CLuaAccountDefs::AddClass ( lua_State* luaVM )
4145
lua_newclass ( luaVM );
4246

4347
lua_classfunction ( luaVM, "getAll", "getAccounts" );
48+
lua_classfunction ( luaVM, "getAllByData", "getAccountsByData" );
4449
lua_classfunction ( luaVM, "getAllBySerial", "getAccountsBySerial" );
50+
lua_classfunction ( luaVM, "getAllByIP", "getAccountsByIP" );
4551
lua_classfunction ( luaVM, "getFromPlayer", "getPlayerAccount" );
4652
lua_classfunction ( luaVM, "logPlayerOut", "logOut" );
4753

@@ -52,16 +58,19 @@ void CLuaAccountDefs::AddClass ( lua_State* luaVM )
5258

5359
lua_classfunction ( luaVM, "setData", "setAccountData" );
5460
lua_classfunction ( luaVM, "setPassword", "setAccountPassword" );
61+
lua_classfunction ( luaVM, "setName", "setAccountName" );
5562

5663
lua_classfunction ( luaVM, "getSerial", "getAccountSerial" );
64+
lua_classfunction ( luaVM, "getIP", "getAccountIP" );
5765
lua_classfunction ( luaVM, "getData", "getAccountData" );
5866
lua_classfunction ( luaVM, "getAllData", "getAllAccountData" );
5967
lua_classfunction ( luaVM, "getName", "getAccountName" );
6068
lua_classfunction ( luaVM, "getPlayer", "getAccountPlayer" );
6169
lua_classfunction ( luaVM, "isGuest", "isGuestAccount" );
6270

6371
lua_classvariable ( luaVM, "serial", NULL, "getAccountSerial" );
64-
lua_classvariable ( luaVM, "name", NULL, "getAccountName" );
72+
lua_classvariable ( luaVM, "name", "setAccountName", "getAccountName" );
73+
lua_classvariable ( luaVM, "ip", NULL, "getAccountIP" );
6574
lua_classvariable ( luaVM, "player", NULL, "getAccountPlayer" );
6675
lua_classvariable ( luaVM, "guest", NULL, "isGuestAccount" );
6776
lua_classvariable ( luaVM, "password", "setAccountPassword", NULL );
@@ -229,6 +238,37 @@ int CLuaAccountDefs::GetAccounts ( lua_State* luaVM )
229238
return 1;
230239
}
231240

241+
int CLuaAccountDefs::GetAccountsByData( lua_State* luaVM )
242+
{
243+
// table GetAccountsByData ( string dataName, string value )
244+
SString dataName;
245+
SString value;
246+
247+
CScriptArgReader argStream( luaVM );
248+
argStream.ReadString( dataName );
249+
argStream.ReadString ( value );
250+
251+
if ( !argStream.HasErrors() )
252+
{
253+
lua_newtable( luaVM );
254+
std::vector<CAccount*> accounts;
255+
256+
CStaticFunctionDefinitions::GetAccountsByData( dataName, value, accounts );
257+
for ( unsigned int i = 0; i < accounts.size(); ++i )
258+
{
259+
lua_pushnumber( luaVM, i + 1 );
260+
lua_pushaccount( luaVM, accounts[i] );
261+
lua_settable( luaVM, -3 );
262+
}
263+
return 1;
264+
}
265+
else
266+
m_pScriptDebugging->LogCustom( luaVM, argStream.GetFullErrorMessage() );
267+
268+
lua_pushboolean( luaVM, false );
269+
return 1;
270+
}
271+
232272
int CLuaAccountDefs::GetAccountSerial ( lua_State* luaVM )
233273
{
234274
// string getAccountSerial ( account theAccount )
@@ -282,6 +322,58 @@ int CLuaAccountDefs::GetAccountsBySerial ( lua_State* luaVM )
282322
return 1;
283323
}
284324

325+
int CLuaAccountDefs::GetAccountIP( lua_State* luaVM ) {
326+
// string getAccountSerial ( account theAccount )
327+
CAccount* pAccount;
328+
329+
CScriptArgReader argStream( luaVM );
330+
argStream.ReadUserData( pAccount );
331+
332+
if ( !argStream.HasErrors() )
333+
{
334+
SString strIP;
335+
if ( CStaticFunctionDefinitions::GetAccountIP( pAccount, strIP ) )
336+
{
337+
lua_pushstring( luaVM, strIP );
338+
return 1;
339+
}
340+
}
341+
else
342+
m_pScriptDebugging->LogCustom( luaVM, argStream.GetFullErrorMessage() );
343+
344+
lua_pushboolean( luaVM, false );
345+
return 1;
346+
}
347+
348+
int CLuaAccountDefs::GetAccountsByIP( lua_State* luaVM )
349+
{
350+
// table getAccountsByIP ( string ip )
351+
SString strIP;
352+
353+
CScriptArgReader argStream( luaVM );
354+
argStream.ReadString( strIP );
355+
356+
if ( !argStream.HasErrors() )
357+
{
358+
lua_newtable( luaVM );
359+
std::vector<CAccount*> accounts;
360+
361+
CStaticFunctionDefinitions::GetAccountsByIP( strIP, accounts );
362+
for ( unsigned int i = 0; i < accounts.size(); ++i )
363+
{
364+
lua_pushnumber( luaVM, i + 1 );
365+
lua_pushaccount( luaVM, accounts[i] );
366+
lua_settable( luaVM, -3 );
367+
}
368+
return 1;
369+
}
370+
else
371+
m_pScriptDebugging->LogCustom( luaVM, argStream.GetFullErrorMessage() );
372+
373+
lua_pushboolean( luaVM, false );
374+
return 1;
375+
}
376+
285377
int CLuaAccountDefs::AddAccount ( lua_State* luaVM )
286378
{
287379
// account addAccount ( string name, string pass[, bool allowCaseVariations = false ] )
@@ -340,6 +432,34 @@ int CLuaAccountDefs::RemoveAccount ( lua_State* luaVM )
340432
}
341433

342434

435+
int CLuaAccountDefs::SetAccountName ( lua_State* luaVM )
436+
{
437+
// bool setAccountPassword ( account theAccount, string name[, bool allowCaseVariations = false ] )
438+
CAccount* pAccount;
439+
SString strNewName;
440+
bool bAllowCaseVariations;
441+
442+
CScriptArgReader argStream ( luaVM );
443+
argStream.ReadUserData ( pAccount );
444+
argStream.ReadString ( strNewName );
445+
argStream.ReadBool ( bAllowCaseVariations, false );
446+
447+
SString strError;
448+
if ( !argStream.HasErrors() )
449+
{
450+
if ( CStaticFunctionDefinitions::SetAccountName ( pAccount, strNewName, bAllowCaseVariations, strError ) )
451+
{
452+
lua_pushboolean ( luaVM, true );
453+
return 1;
454+
}
455+
}
456+
else
457+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
458+
lua_pushboolean ( luaVM, false );
459+
return 1;
460+
}
461+
462+
343463
int CLuaAccountDefs::SetAccountPassword ( lua_State* luaVM )
344464
{
345465
// bool setAccountPassword ( account theAccount, string password )

Server/mods/deathmatch/logic/luadefs/CLuaAccountDefs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ class CLuaAccountDefs : public CLuaDefs
3030
LUA_DECLARE ( IsGuestAccount );
3131
LUA_DECLARE ( GetAccountData );
3232
LUA_DECLARE ( GetAllAccountData );
33+
LUA_DECLARE ( GetAccountsByData );
3334
LUA_DECLARE ( GetAccountSerial );
3435
LUA_DECLARE ( GetAccountsBySerial );
36+
LUA_DECLARE ( GetAccountIP );
37+
LUA_DECLARE ( GetAccountsByIP );
3538

3639
// Account set funcs
3740
LUA_DECLARE ( AddAccount );
3841
LUA_DECLARE ( RemoveAccount );
42+
LUA_DECLARE ( SetAccountName );
3943
LUA_DECLARE ( SetAccountPassword );
4044
LUA_DECLARE ( SetAccountData );
4145
LUA_DECLARE ( CopyAccountData );
42-
};
46+
};

0 commit comments

Comments
 (0)