Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2015 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f2a07562a9fda6a733e3 to your computer and use it in GitHub Desktop.
Save anonymous/f2a07562a9fda6a733e3 to your computer and use it in GitHub Desktop.
Index: MTA10_Server/mods/deathmatch/logic/CAccount.cpp
===================================================================
--- MTA10_Server/mods/deathmatch/logic/CAccount.cpp (revision 7069)
+++ MTA10_Server/mods/deathmatch/logic/CAccount.cpp (working copy)
@@ -123,3 +123,77 @@
m_iUserID = iUserID;
}
}
+
+CAccountData* CAccount::GetDataPointer ( const std::string& strKey )
+{
+ list < CAccountData > ::iterator iter = m_Data.begin ();
+ for ( ; iter != m_Data.end (); iter++ )
+ {
+ if ( iter->GetKey () == strKey )
+ return (CAccountData *) &(*iter);
+ }
+ return NULL;
+}
+
+CLuaArgument* CAccount::GetData ( const std::string& strKey )
+{
+ CAccountData* pData = GetDataPointer ( strKey );
+ CLuaArgument* pResult = new CLuaArgument ();
+
+ if ( pData )
+ {
+ if ( pData->GetType () == LUA_TBOOLEAN )
+ {
+ pResult->ReadBool ( pData->GetStrValue () == "true" );
+ }
+ else
+ if ( pData->GetType () == LUA_TNUMBER )
+ {
+ pResult->ReadNumber ( strtod ( pData->GetStrValue ().c_str(), NULL ) );
+ }
+ else
+ {
+ pResult->ReadString ( pData->GetStrValue () );
+ }
+ }
+ else
+ {
+ pResult->ReadBool ( false );
+ }
+ return pResult;
+}
+
+void CAccount::SetData ( const std::string& strKey, const std::string& strValue, int iType )
+{
+ if ( strValue == "false" && iType == LUA_TBOOLEAN )
+ {
+ RemoveData ( strKey );
+ }
+ else
+ {
+ CAccountData* pData = GetDataPointer ( strKey );
+
+ if ( pData )
+ {
+ pData->SetStrValue ( strValue );
+ pData->SetType ( iType );
+ }
+ else
+ {
+ m_Data.push_back ( CAccountData ( strKey, strValue, iType ) );
+ }
+ }
+}
+
+void CAccount::RemoveData ( const std::string& strKey )
+{
+ list < CAccountData > ::iterator iter = m_Data.begin();
+ for (; iter != m_Data.end(); iter++)
+ {
+ if (iter->GetKey() == strKey)
+ {
+ m_Data.erase(iter);
+ break;
+ }
+ }
+}
\ No newline at end of file
Index: MTA10_Server/mods/deathmatch/logic/CAccount.h
===================================================================
--- MTA10_Server/mods/deathmatch/logic/CAccount.h (revision 7069)
+++ MTA10_Server/mods/deathmatch/logic/CAccount.h (working copy)
@@ -68,7 +68,15 @@
inline bool HasChanged ( void ) { return m_bChanged; }
uint GetScriptID ( void ) const { return m_uiScriptID; }
+ CLuaArgument* GetData ( const std::string& strKey );
+ void SetData ( const std::string& strKey, const::std::string& strValue, int iType );
+ void RemoveData ( const std::string& strKey );
+ std::list < CAccountData > ::iterator DataBegin ( void ) { return m_Data.begin (); }
+ std::list < CAccountData > ::iterator DataEnd ( void ) { return m_Data.end (); }
+
protected:
+ CAccountData* GetDataPointer ( const std::string& strKey );
+
CAccountManager* m_pManager;
bool m_bRegistered;
@@ -84,6 +92,29 @@
class CClient* m_pClient;
uint m_uiScriptID;
+
+ std::list < CAccountData > m_Data;
};
+class CAccountData
+{
+public:
+ inline CAccountData ( const std::string strKey, const std::string strValue, int iType )
+ {
+ m_strKey = strKey;
+ m_strValue = strValue;
+ m_iType = iType;
+ }
+ inline const std::string GetKey ( void ) { return m_strKey; }
+ inline const std::string GetStrValue ( void ) { return m_strValue; }
+ inline int GetType ( void ) { return m_iType; }
+ inline void SetStrValue ( const std::string& strValue ) { m_strValue = strValue; }
+ inline void SetType ( int iType ) { m_iType = iType; }
+
+private:
+ std::string m_strKey;
+ std::string m_strValue;
+ int m_iType;
+};
+
#endif
Index: MTA10_Server/mods/deathmatch/logic/CAccountManager.cpp
===================================================================
--- MTA10_Server/mods/deathmatch/logic/CAccountManager.cpp (revision 7069)
+++ MTA10_Server/mods/deathmatch/logic/CAccountManager.cpp (working copy)
@@ -900,6 +900,11 @@
CLuaArgument* CAccountManager::GetAccountData( CAccount* pAccount, const char* szKey )
{
+ if ( !pAccount->IsRegistered () )
+ {
+ return pAccount->GetData ( szKey );
+ }
+
//Get the user ID
int iUserID = pAccount->GetID();
//create a new registry result for the query return
@@ -943,6 +948,12 @@
if ( iType != LUA_TSTRING && iType != LUA_TNUMBER && iType != LUA_TBOOLEAN && iType != LUA_TNIL )
return false;
+ if ( !pAccount->IsRegistered () )
+ {
+ pAccount->SetData ( szKey, strValue, iType );
+ return true;
+ }
+
//Get the user ID
int iUserID = pAccount->GetID();
SString strKey = szKey;
@@ -961,50 +972,110 @@
bool CAccountManager::CopyAccountData( CAccount* pFromAccount, CAccount* pToAccount )
{
- //Get the user ID of the from account
- int iUserID = pFromAccount->GetID();
- //create a new registry result for the from account query return value
- CRegistryResult result;
- //create a new registry result for the to account query return value
- //initialize key and value strings
- SString strKey;
- SString strValue;
+ // list to store pFromAccount data to
+ std::list < CAccountData > copiedData;
- //Select the key and value from the database where the user is our from account
- m_pDatabaseManager->QueryWithResultf ( m_hDbConnection, &result, "SELECT key,value,type from userdata where userid=? LIMIT 1", SQLITE_INTEGER, iUserID );
+ if ( !pFromAccount->IsRegistered () ) // is not registered account, retrieve data from memory
+ {
+ std::list < CAccountData > ::iterator iter = pFromAccount->DataBegin ();
+ for ( ; iter != pFromAccount->DataEnd (); iter++ )
+ {
+ copiedData.push_back(CAccountData(iter->GetKey(), iter->GetStrValue(), iter->GetType()));
+ }
+ }
+ else // is registered account, retrieve from database
+ {
+ SString strKey;
+ SString strValue;
- //Do we have any results?
- if ( result->nRows > 0 ) {
- //Loop through until i is the same as the number of rows
- for ( CRegistryResultIterator iter = result->begin() ; iter != result->end() ; ++iter )
- {
- const CRegistryResultRow& row = *iter;
- //Get our key
- strKey = (const char *)row[0].pVal;
- //Get our value
- strValue = (const char *)row[1].pVal;
- int iType = static_cast < int > ( row[2].nVal );
- //Select the id and userid where the user is the to account and the key is strKey
- CRegistryResult subResult;
- m_pDatabaseManager->QueryWithResultf ( m_hDbConnection, &subResult, "SELECT id,userid from userdata where userid=? and key=? LIMIT 1", SQLITE_INTEGER, iUserID, SQLITE_TEXT, strKey.c_str () );
- //If there is a key with this value update it otherwise insert it and store the return value in bRetVal
- if ( subResult->nRows > 0 )
- m_pDatabaseManager->Execf ( m_hDbConnection, "UPDATE userdata SET value=?, type=? WHERE userid=? AND key=?", SQLITE_TEXT, strValue.c_str (), SQLITE_INTEGER, iType, SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, strKey.c_str () );
- else
- m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO userdata (userid, key, value, type) VALUES(?,?,?,?)", SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, strKey.c_str (), SQLITE_TEXT, strValue.c_str (), SQLITE_INTEGER, iType );
+ //Get the user ID of the from account
+ int iUserID = pFromAccount->GetID ();
+ //create a new registry result for the from account query return value
+ CRegistryResult result;
- }
- }
- else
- //We had no results so return false (Nothing has changed)
- return false;
-
- return true;
+ m_pDatabaseManager->QueryWithResultf ( m_hDbConnection, &result, "SELECT key,value,type from userdata where userid=?", SQLITE_INTEGER, iUserID );
+
+ //Do we have any results?
+ if ( result->nRows > 0 )
+ {
+ for ( CRegistryResultIterator iter = result->begin() ; iter != result->end() ; ++iter )
+ {
+ const CRegistryResultRow& row = *iter;
+ //Get our key
+ strKey = (const char *)row[0].pVal;
+ //Get our value
+ strValue = (const char *)row[1].pVal;
+ int iType = static_cast < int > ( row[2].nVal );
+
+ copiedData.push_back ( CAccountData ( strKey, strValue, iType ) );
+ }
+ }
+ }
+
+ if (copiedData.size () > 0) // got anything to copy?
+ {
+ std::list < CAccountData > ::iterator iter = copiedData.begin ();
+
+ for (; iter != copiedData.end(); iter++)
+ {
+ if ( !pToAccount->IsRegistered () ) // store to memory
+ {
+ pToAccount->SetData ( iter->GetKey (), iter->GetStrValue (), iter->GetType () );
+ }
+ else // store to database
+ {
+ CRegistryResult subResult;
+
+ m_pDatabaseManager->QueryWithResultf ( m_hDbConnection, &subResult, "SELECT id,userid from userdata where userid=? and key=? LIMIT 1", SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, iter->GetKey ().c_str() );
+ //If there is a key with this value update it otherwise insert it and store the return value in bRetVal
+ if ( subResult->nRows > 0 )
+ m_pDatabaseManager->Execf ( m_hDbConnection, "UPDATE userdata SET value=?, type=? WHERE userid=? AND key=?", SQLITE_TEXT, iter->GetStrValue ().c_str(), SQLITE_INTEGER, iter->GetType (), SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, iter->GetKey ().c_str() );
+ else
+ m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO userdata (userid, key, value, type) VALUES(?,?,?,?)", SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, iter->GetKey ().c_str(), SQLITE_TEXT, iter->GetStrValue ().c_str(), SQLITE_INTEGER, iter->GetType () );
+ }
+ }
+ return true;
+ }
+ else
+ return false;
}
bool CAccountManager::GetAllAccountData( CAccount* pAccount, lua_State* pLua )
{
+ if ( !pAccount->IsRegistered () )
+ {
+ std::list < CAccountData > ::iterator iter = pAccount->DataBegin ();
+ for ( ; iter != pAccount->DataEnd (); iter++ )
+ {
+ if ( iter->GetType() == LUA_TNIL )
+ {
+ lua_pushstring ( pLua, iter->GetKey ().c_str() );
+ lua_pushnil ( pLua );
+ lua_settable ( pLua, -3 );
+ }
+ if ( iter->GetType() == LUA_TBOOLEAN )
+ {
+ lua_pushstring ( pLua, iter->GetKey ().c_str() );
+ lua_pushboolean ( pLua, iter->GetStrValue () == "true" ? true : false );
+ lua_settable ( pLua, -3 );
+ }
+ if ( iter->GetType() == LUA_TNUMBER )
+ {
+ lua_pushstring ( pLua, iter->GetKey ().c_str() );
+ lua_pushnumber ( pLua, strtod ( iter->GetStrValue ().c_str(), NULL ) );
+ lua_settable ( pLua, -3 );
+ }
+ else
+ {
+ lua_pushstring ( pLua, iter->GetKey ().c_str() );
+ lua_pushstring ( pLua, iter->GetStrValue ().c_str() );
+ lua_settable ( pLua, -3 );
+ }
+ }
+ return true;
+ }
+
//Get the user ID
int iUserID = pAccount->GetID();
//create a new registry result for the query return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment