Raised This Month: $51 Target: $400
 12% 

Reading/Writing String


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-15-2013 , 21:49   Reading/Writing String
Reply With Quote #1

I am trying to read a string by getting each character one by one. The saved data is binary, 1 and 0, representing true or false basically. The saving portion seems to work, but if someone has a better method than the one used, sure i'll take it.

Meanwhile, when I rejoin the game and successfully load, the Loading portion of the code below should activate, it does, but the method I am using to read the Binary String is not working...so I'm at an impass...

PHP Code:
/* Save To File */
ClientSaveToFileSave(targetid)
{
    if(
iSaveFeature[targetid] == 0)
        return;

    
decl Handle:DataValue;
    
decl String:cName[MAX_NAME_LENGTH];
    
DataValue CreateKeyValues("SurvivorUpgradesReloaded");
    
FileToKeyValues(DataValueSavePath);

    if(
IsFakeClient(targetid))
        
GetClientName(targetidcNamesizeof(cName));
    else
        
GetClientAuthString(targetidcNamesizeof(cName));

    
KvJumpToKey(DataValuecNametrue);
    
decl String:buffer[512], String:iBitVec[42], String:iDisabled[42];

    
Format(buffersizeof(buffer), "Save Feature");
    
KvSetNum(DataValuebufferiSaveFeature[targetid]);

    for(new 
0MAX_UPGRADESi++)
    {
        if(
== 0)
        {
            if(
iUpgrade[targetid][i] > 0)
                
Format(iBitVecsizeof(iBitVec), "1");
            else
                
Format(iBitVecsizeof(iBitVec), "0");

            if(
iUpgradeDisabled[targetid][i] > 0)
                
Format(iDisabledsizeof(iDisabled), "1");
            else
                
Format(iDisabledsizeof(iDisabled), "0");
        }
        else
        {
            if(
iUpgrade[targetid][i] > 0)
                
Format(iBitVecsizeof(iBitVec), "%s1"iBitVec);
            else
                
Format(iBitVecsizeof(iBitVec), "%s0"iBitVec);

            if(
iUpgradeDisabled[targetid][i] > 0)
                
Format(iDisabledsizeof(iDisabled), "%s1"iDisabled);
            else
                
Format(iDisabledsizeof(iDisabled), "%s0"iDisabled);
        }
        }

    
Format(buffersizeof(buffer), "Disabled");
    
KvSetString(DataValuebufferiDisabled);

    
Format(buffersizeof(buffer), "iBitVec");
    
KvSetString(DataValuebufferiBitVec);

    
KvRewind(DataValue);
    
KeyValuesToFile(DataValueSavePath);
    
CloseHandle(DataValue);
}

/* Load Save From File */
ClientSaveToFileLoad(targetid)
{
    
decl Handle:DataValue;
    
decl String:cName[MAX_NAME_LENGTH];
    
DataValue CreateKeyValues("SurvivorUpgradesReloaded");
    
FileToKeyValues(DataValueSavePath);

    if(
IsFakeClient(targetid))
        
GetClientName(targetidcNamesizeof(cName));
    else
        
GetClientAuthString(targetidcNamesizeof(cName));    

    
KvJumpToKey(DataValuecNametrue);

    
decl String:buffer[512], String:iBitVec[42], String:iDisabled[42];
    
Format(buffersizeof(buffer), "Save Feature");
    
iSaveFeature[targetid] = KvGetNum(DataValuebuffer0);

    
Format(buffersizeof(buffer), "iBitVec");
    
KvGetString(DataValuebufferiBitVecsizeof(iBitVec));

    new 
len strlen(iBitVec);
    for(new 
0leni++)
    {
        if(
StrEqual(iBitVec[i], "1"false))
            
iUpgrade[targetid][i-1] = UpgradeIndex[i-1];
            
//iUpgradeDisabled[targetid][n-1] = 1;
    
}

    
Format(buffersizeof(buffer), "Disabled");
    
KvGetString(DataValuebufferiDisabledsizeof(iDisabled));    

    
CloseHandle(DataValue);

__________________

Last edited by Marcus101RR; 09-15-2013 at 22:23.
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 09-15-2013 , 21:55   Re: Reading/Writing String
Reply With Quote #2

May i ask why you aren't using something like clientprefs?
__________________

Last edited by Impact123; 09-15-2013 at 21:56.
Impact123 is offline
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-15-2013 , 22:07   Re: Reading/Writing String
Reply With Quote #3

Quote:
Originally Posted by Impact123 View Post
May i ask why you aren't using something like clientprefs?
What is clientprefs?
__________________
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 09-15-2013 , 22:17   Re: Reading/Writing String
Reply With Quote #4

http://wiki.alliedmods.net/Client_Pr...PI_(SourceMod)
__________________
thetwistedpanda is offline
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-15-2013 , 22:23   Re: Reading/Writing String
Reply With Quote #5

Quote:
Originally Posted by thetwistedpanda View Post
Never used it, the Wiki is not very descriptive of how to use it with what I need. I'll try it out, where is the file stored? This only stored one value, I need about 40 of them...So I have to create 40 cookies...-_-
__________________

Last edited by Marcus101RR; 09-15-2013 at 22:35.
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
xf117
Senior Member
Join Date: Mar 2010
Location: Russia
Old 09-16-2013 , 06:14   Re: Reading/Writing String
Reply With Quote #6

Well, you can format a cookie to fit several values. I'm not sure about 40 tho.

Can you explain what are you doing here?
PHP Code:
KvGetString(DataValuebufferiBitVecsizeof(iBitVec));

new 
len strlen(iBitVec);
for(new 
0leni++)
{
    if(
StrEqual(iBitVec[i], "1"false))
        
iUpgrade[targetid][i-1] = UpgradeIndex[i-1];
        
//iUpgradeDisabled[targetid][n-1] = 1;

iBitVec is a string, but why are you using StrEqual to compare a char? And what if [i-1] will trigger with i == 0?

Last edited by xf117; 09-16-2013 at 06:16.
xf117 is offline
Send a message via ICQ to xf117
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-16-2013 , 16:22   Re: Reading/Writing String
Reply With Quote #7

Quote:
Originally Posted by xf117 View Post
Well, you can format a cookie to fit several values. I'm not sure about 40 tho.

Can you explain what are you doing here?
PHP Code:
KvGetString(DataValuebufferiBitVecsizeof(iBitVec));

new 
len strlen(iBitVec);
for(new 
0leni++)
{
    if(
StrEqual(iBitVec[i], "1"false))
        
iUpgrade[targetid][i-1] = UpgradeIndex[i-1];
        
//iUpgradeDisabled[targetid][n-1] = 1;

iBitVec is a string, but why are you using StrEqual to compare a char? And what if [i-1] will trigger with i == 0?
I am cycling through each character of the iBitVec string, since there are 40 upgrades, there are 40 characters either being 0 or 1, and so, i need to check each one and set their upgrades back.
__________________
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 09-16-2013 , 16:31   Re: Reading/Writing String
Reply With Quote #8

You could look at Skyy's rpg plugin, I think he does something similar and puts it into sql.
__________________
thetwistedpanda is offline
Marcus101RR
Veteran Member
Join Date: Aug 2009
Location: Tampa, FL
Old 09-16-2013 , 16:46   Re: Reading/Writing String
Reply With Quote #9

Quote:
Originally Posted by thetwistedpanda View Post
You could look at Skyy's rpg plugin, I think he does something similar and puts it into sql.
I rather use SQLite, and problem is, that would require me to learn SQLite Commands and recode it there too. I would do it however.

Everytime it saves, it saves as a number when it should be a string with 000100010010110000

PHP Code:
MySQL_Init()
{    
    
decl String:Error[192];

    
hDatabase SQLite_UseDatabase("SurvivorUpgradesReloaded"Errorsizeof(Error));
    
    if(
hDatabase == INVALID_HANDLE)
        
SetFailState("SQL error: %s"Error);

    
SQL_FastQuery(hDatabase"CREATE TABLE IF NOT EXISTS accounts (steamid TEXT PRIMARY KEY, savemode SMALLINT, notifications SMALLINT, upgrades_binary CHAR(40), disabled_binary CHAR(40));");
}

stock SaveData(client)
{
    
decl String:TQuery[3000], String:SteamID[64], String:UpgradeBinary[64], String:DisabledBinary[64];


    for(new 
0MAX_UPGRADESi++)
    {
        if(
== 0)
        {
            if(
iUpgrade[client][i] > 0)
                
Format(UpgradeBinarysizeof(UpgradeBinary), "1");
            else
                
Format(UpgradeBinarysizeof(UpgradeBinary), "0");

            if(
iUpgradeDisabled[client][i] > 0)
                
Format(DisabledBinarysizeof(DisabledBinary), "1");
            else
                
Format(DisabledBinarysizeof(DisabledBinary), "0");
        }
        else
        {
            if(
iUpgrade[client][i] > 0)
                
Format(UpgradeBinarysizeof(UpgradeBinary), "%s1"UpgradeBinary);
            else
                
Format(UpgradeBinarysizeof(UpgradeBinary), "%s0"UpgradeBinary);

            if(
iUpgradeDisabled[client][i] > 0)
                
Format(DisabledBinarysizeof(DisabledBinary), "%s1"DisabledBinary);
            else
                
Format(DisabledBinarysizeof(DisabledBinary), "%s0"DisabledBinary);
        }
    }

    
GetClientAuthString(clientSteamIDsizeof(SteamID));
    
Format(TQuerysizeof(TQuery), "INSERT OR REPLACE INTO accounts VALUES ('%s', %d, %d, %s, %s);"SteamIDiSaveFeature[client], iAnnounceText[client], UpgradeBinaryUpgradeBinary);
    
SQL_FastQuery(hDatabaseTQuery);
}

stock LoadData(client)
{
    
decl String:TQuery[192], String:SteamID[64];

    
GetClientAuthString(clientSteamIDsizeof(SteamID));
    
Format(TQuerysizeof(TQuery), "SELECT * FROM accounts WHERE steamId = '%s';"SteamID);
    
SQL_TQuery(hDatabaseLoadPlayerDataTQueryclient);
}

public 
LoadPlayerData(Handle:ownerHandle:hndl, const String:error[], any:client)
{
    if(
hndl == INVALID_HANDLE || SQL_GetRowCount(hndl) == 0)
    {
        return;
    }

    
decl String:UpgradeBinary[64], String:DisabledBinary[64];

    
iSaveFeature[client] = SQL_FetchInt(hndl1);
    
iAnnounceText[client] = SQL_FetchInt(hndl2);
    
SQL_FetchString(hndl3UpgradeBinarysizeof(UpgradeBinary));
    
SQL_FetchString(hndl4DisabledBinarysizeof(DisabledBinary));

    new 
len strlen(UpgradeBinary);
    for (new 
0<= leni++)
    {
        if(
UpgradeBinary[i] == 0)
        {
            
iUpgrade[client][i] = UpgradeIndex[i];
        }
    }

    
len strlen(DisabledBinary);
    for (new 
0<= leni++)
    {
        if(
DisabledBinary[i] == 1)
        {
            
iUpgradeDisabled[client][i] = 1;
        }
    }

__________________

Last edited by Marcus101RR; 09-16-2013 at 22:11.
Marcus101RR is offline
Send a message via AIM to Marcus101RR Send a message via Skype™ to Marcus101RR
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:32.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode