Exemplo n.º 1
0
// Return true if data was changed
bool CAccount::SetData(const std::string& strKey, const std::string& strValue, int iType)
{
    if (strValue == "false" && iType == LUA_TBOOLEAN)
    {
        if (HasData(strKey))
        {
            RemoveData(strKey);
            return true;
        }
    }
    else
    {
        CAccountData* pData = GetDataPointer(strKey);

        if (pData)
        {
            if (pData->GetType() != iType || pData->GetStrValue() != strValue)
            {
                pData->SetStrValue(strValue);
                pData->SetType(iType);
                return true;
            }
        }
        else
        {
            MapSet(m_Data, strKey, CAccountData(strKey, strValue, iType));
            return true;
        }
    }
    return false;
}
Exemplo n.º 2
0
bool CAccountManager::CopyAccountData( CAccount* pFromAccount, CAccount* pToAccount )
{
    // list to store pFromAccount data to
    std::map < SString, CAccountData > copiedData;

    if ( !pFromAccount->IsRegistered () )   // is not registered account, retrieve data from memory
    {
        std::map < SString, CAccountData > ::iterator iter = pFromAccount->DataBegin ();
        for ( ; iter != pFromAccount->DataEnd (); iter++ )
        {
            MapSet( copiedData, iter->second.GetKey(), CAccountData( iter->second.GetKey(), iter->second.GetStrValue(), iter->second.GetType() ) );
        }
    }
    else    // is registered account, retrieve from database
    {
        SString strKey;
        SString strValue;

        //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;

        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 );

                MapSet( copiedData, strKey, CAccountData ( strKey, strValue, iType ) );
           }
        }
    }

    if (copiedData.size () > 0) // got anything to copy?
    {
        std::map < SString, CAccountData > ::iterator iter = copiedData.begin ();

        for (; iter != copiedData.end(); iter++)
        {
            if ( !pToAccount->IsRegistered () ) // store to memory
            {
                pToAccount->SetData ( iter->second.GetKey (), iter->second.GetStrValue (), iter->second.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->second.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->second.GetStrValue ().c_str(), SQLITE_INTEGER, iter->second.GetType (), SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, iter->second.GetKey ().c_str() );
                else
                    m_pDatabaseManager->Execf ( m_hDbConnection, "INSERT INTO userdata (userid, key, value, type) VALUES(?,?,?,?)", SQLITE_INTEGER, pToAccount->GetID (), SQLITE_TEXT, iter->second.GetKey ().c_str(), SQLITE_TEXT, iter->second.GetStrValue ().c_str(), SQLITE_INTEGER, iter->second.GetType () );
            }
        }
        return true;
    }
    else
        return false;
}