djSon & changename command..
#1

I'm currently attempting to make a changename command, and i'm completely confused as to how to do this with djSon. Would anyone be able to provide me with maybe an example command of theirs, or explain to me the different functions needed?
Reply
#2

Well I've never personally used this library. Although I took a quick look at the release topic and an example that was provided in that topic, it would appear to be quite easy. Can you give me any information on how your script is setup? Where are your account files, how are they set up and so on....

It's impossible to provide help without such information, as there are so many dynamic possibilities to what you could've done.

Looking at the example, I can tell you that a probable function you need is this:

pawn Код:
djSet("accounts.json", str, value);
Have you tried anything yourself, if not, why? The best learning experience comes from trial and error.

There's a lovely Wiki for the library here.
Reply
#3

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Well I've never personally used this library. Although I took a quick look at the release topic and an example that was provided in that topic, it would appear to be quite easy. Can you give me any information on how your script is setup? Where are your account files, how are they set up and so on....

It's impossible to provide help without such information, as there are so many dynamic possibilities to what you could've done.

Looking at the example, I can tell you that a probable function you need is this:

pawn Код:
djSet("accounts.json", str, value);
Have you tried anything yourself, if not, why? The best learning experience comes from trial and error.

There's a lovely Wiki for the library here.
I've tried it myself on multiple occasions, and have always ended up in failure. I tend to get quite discouraged when this happens, although i'm exactly sure why, haha. Well, here's some information about my account system.

Files are saved under "PFiles/%s.json" - %s being their username.

I will definately take a look at that library you provided, however; Thank you very much.
Reply
#4

Quote:
Originally Posted by Skylar Paul
Посмотреть сообщение
I've tried it myself on multiple occasions, and have always ended up in failure. I tend to get quite discouraged when this happens, although i'm exactly sure why, haha. Well, here's some information about my account system.

Files are saved under "PFiles/%s.json" - %s being their username.

I will definately take a look at that library you provided, however; Thank you very much.
A simple note is that is not how djSon was intended to be used, it was actually intended to be used to have all of the players information stored in a single file.

Regardless, it's quite easy to change the name of that file then, all you need is to re-name the file.

Unfortunately there is no file re-naming function that comes with the SA-MP package, although there are plenty of PAWN implementations available around.

Read this great page I found for more information: http://dracoblue.net/dev/renaming-a-file-in-pawn/139/
Reply
#5

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
A simple note is that is not how djSon was intended to be used, it was actually intended to be used to have all of the players information stored in a single file.

Regardless, it's quite easy to change the name of that file then, all you need is to re-name the file.

Unfortunately there is no file re-naming function that comes with the SA-MP package, although there are plenty of PAWN implementations available around.

Read this great page I found for more information: http://dracoblue.net/dev/renaming-a-file-in-pawn/139/
That's an interesting page, thank you!

Well, here's my attempt at a changename command, and i'm going to go check it in-game right now. Wish me luck! :P

pawn Код:
CMD:rename(playerid, params[])
{
    if(PVar[playerid][pLevel] >= 4)
    {
        new Player, newname[MAX_PLAYER_NAME], string[128], File[50];
       
        if(!sscanf(params, "us[MAX_PLAYER_NAME]", Player, newname))
        {
       
            format(string, sizeof(string), "%s.json", newname);
            djCreateFile(string);
           
            format(File, sizeof(File), PFiles, newname);
           
            PVar[playerid][pKills]      = djInt     (File, "Kills");
            PVar[playerid][pDeaths]     = djInt     (File, "Deaths");
            PVar[playerid][pLevel]      = djInt     (File, "Level");
            PVar[playerid][pMuted]      = djInt     (File, "Muted");
            PVar[playerid][pCash]       = djInt     (File, "Cash");
            PVar[playerid][pSkin]       = djInt     (File, "Skin");
            PVar[playerid][pUseSkin]    = djInt     (File, "UseSkin");
            format(PVar[playerid][pMutedReason], 52, "%s", dj(File, "MutedReason"));
           
            format(string, sizeof(string), "%s.json", pName(playerid));
            djRemoveFile(string);
           
            SetPlayerName(playerid, newname);
       
        }
    }
    return 1;
}
EDIT: It worked other than one major problem.. The statistics did not transfer over (PVar[playerid][pKills] = djInt (File, "Kills"); <-- that part).. Any synopsis on what could be wrong?
Reply
#6

Well that's quite simply because you're not setting them in the new file at all, all you are doing is getting the information from the new file using djInt, which contains no information yet!

You should be using djSetInt to set the information in the new file!
Reply
#7

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Well that's quite simply because you're not setting them in the new file at all, all you are doing is getting the information from the new file using djInt, which contains no information yet!

You should be using djSetInt to set the information in the new file!
Right..

i'm still completely confused as to how this is not working - I've used djSetInt, and it's still not working at all...


pawn Код:
CMD:rename(playerid, params[])
{
    if(PVar[playerid][pLevel] >= 4)
    {
        new Player, newname[32], string[128], File[85];

        if(!sscanf(params, "us[32]", Player, newname))
        {

            format(File, sizeof(File), PFiles, newname);
            if(!fexist(File))
            {

                format(File, sizeof(File), PFiles, pName(playerid));

                PVar[playerid][pKills]      = djInt     (File, "Kills");
                PVar[playerid][pDeaths]     = djInt     (File, "Deaths");
                PVar[playerid][pLevel]      = djInt     (File, "Level");
                PVar[playerid][pMuted]      = djInt     (File, "Muted");
                PVar[playerid][pCash]       = djInt     (File, "Cash");
                PVar[playerid][pSkin]       = djInt     (File, "Skin");
                PVar[playerid][pUseSkin]    = djInt     (File, "UseSkin");
                format(PVar[playerid][pMutedReason], 52, "%s", dj(File, "MutedReason"));
               
                format(string, sizeof(string), "%s.json", newname);
                djCreateFile(string);
               
                format(File, sizeof(File), PFiles, newname);
               
                djSetInt        (File, "Kills",     PVar[playerid][pKills]);
                djSetInt        (File, "Deaths",    PVar[playerid][pDeaths]);
                djSetInt        (File, "Level",     PVar[playerid][pLevel]);
                djSetInt        (File, "Cash",      PVar[playerid][pCash]);
                djSetInt        (File, "Muted",     PVar[playerid][pMuted]);
                djSetInt        (File, "Skin",      PVar[playerid][pSkin]);
                djSetInt        (File, "UseSkin",   PVar[playerid][pUseSkin]);
                djSet           (File, "MutedReason", PVar[playerid][pMutedReason]);

                format(string, sizeof(string), "%s.json", pName(playerid));
                djRemoveFile(string);

                SetPlayerName(playerid, newname);
               
            }
            else return SendClientMessage(playerid, COLOR_RED, "File already exists!");

        }
        else return SendClientMessage(playerid, COLOR_LIGHTBLUE, "Usage: /rename < Player ID > < New Name >");
    }
    else return AdminCMD(playerid, 4);
    return 1;
}
Reply
#8

Try to do some simple debugging.

pawn Код:
CMD:rename(playerid, params[])
{
    if(PVar[playerid][pLevel] >= 4)
    {
        new Player, newname[32], string[128], File[85];

        if(!sscanf(params, "us[32]", Player, newname))
        {

            format(File, sizeof(File), PFiles, newname);
            if(!fexist(File))
            {

                format(File, sizeof(File), PFiles, pName(playerid));

                PVar[playerid][pKills]      = djInt     (File, "Kills");
                PVar[playerid][pDeaths]     = djInt     (File, "Deaths");
                PVar[playerid][pLevel]      = djInt     (File, "Level");
                PVar[playerid][pMuted]      = djInt     (File, "Muted");
                PVar[playerid][pCash]       = djInt     (File, "Cash");
                PVar[playerid][pSkin]       = djInt     (File, "Skin");
                PVar[playerid][pUseSkin]    = djInt     (File, "UseSkin");
                format(PVar[playerid][pMutedReason], 52, "%s", dj(File, "MutedReason"));
               
                format(string, sizeof(string), "%s.json", newname);
                djCreateFile(string);
               
                format(File, sizeof(File), PFiles, newname);

                printf("File: %s", File);
               
                djSetInt        (File, "Kills",     PVar[playerid][pKills]);
                djSetInt        (File, "Deaths",    PVar[playerid][pDeaths]);
                djSetInt        (File, "Level",     PVar[playerid][pLevel]);
                djSetInt        (File, "Cash",      PVar[playerid][pCash]);
                djSetInt        (File, "Muted",     PVar[playerid][pMuted]);
                djSetInt        (File, "Skin",      PVar[playerid][pSkin]);
                djSetInt        (File, "UseSkin",   PVar[playerid][pUseSkin]);
                djSet           (File, "MutedReason", PVar[playerid][pMutedReason]);

                format(string, sizeof(string), "%s.json", pName(playerid));
                djRemoveFile(string);

                SetPlayerName(playerid, newname);
               
            }
            else return SendClientMessage(playerid, COLOR_RED, "File already exists!");

        }
        else return SendClientMessage(playerid, COLOR_LIGHTBLUE, "Usage: /rename < Player ID > < New Name >");
    }
    else return AdminCMD(playerid, 4);
    return 1;
}
Also do you happen to be using dutils? There is a file re-name function in there that would make all of this a lot easier, it would all boil down to one function in fact.
Reply
#9

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Try to do some simple debugging.

pawn Код:
CMD:rename(playerid, params[])
{
    if(PVar[playerid][pLevel] >= 4)
    {
        new Player, newname[32], string[128], File[85];

        if(!sscanf(params, "us[32]", Player, newname))
        {

            format(File, sizeof(File), PFiles, newname);
            if(!fexist(File))
            {

                format(File, sizeof(File), PFiles, pName(playerid));

                PVar[playerid][pKills]      = djInt     (File, "Kills");
                PVar[playerid][pDeaths]     = djInt     (File, "Deaths");
                PVar[playerid][pLevel]      = djInt     (File, "Level");
                PVar[playerid][pMuted]      = djInt     (File, "Muted");
                PVar[playerid][pCash]       = djInt     (File, "Cash");
                PVar[playerid][pSkin]       = djInt     (File, "Skin");
                PVar[playerid][pUseSkin]    = djInt     (File, "UseSkin");
                format(PVar[playerid][pMutedReason], 52, "%s", dj(File, "MutedReason"));
               
                format(string, sizeof(string), "%s.json", newname);
                djCreateFile(string);
               
                format(File, sizeof(File), PFiles, newname);

                printf("File: %s", File);
               
                djSetInt        (File, "Kills",     PVar[playerid][pKills]);
                djSetInt        (File, "Deaths",    PVar[playerid][pDeaths]);
                djSetInt        (File, "Level",     PVar[playerid][pLevel]);
                djSetInt        (File, "Cash",      PVar[playerid][pCash]);
                djSetInt        (File, "Muted",     PVar[playerid][pMuted]);
                djSetInt        (File, "Skin",      PVar[playerid][pSkin]);
                djSetInt        (File, "UseSkin",   PVar[playerid][pUseSkin]);
                djSet           (File, "MutedReason", PVar[playerid][pMutedReason]);

                format(string, sizeof(string), "%s.json", pName(playerid));
                djRemoveFile(string);

                SetPlayerName(playerid, newname);
               
            }
            else return SendClientMessage(playerid, COLOR_RED, "File already exists!");

        }
        else return SendClientMessage(playerid, COLOR_LIGHTBLUE, "Usage: /rename < Player ID > < New Name >");
    }
    else return AdminCMD(playerid, 4);
    return 1;
}
Also do you happen to be using dutils? There is a file re-name function in there that would make all of this a lot easier, it would all boil down to one function in fact.
No, uhh.. Could you help me out as to explaining what that is?

Quote:

[19:35:05] K: 0, D: 0, Level: 5, Money: $0 [before]
[19:35:05] File: PFiles/Sky7.json
[19:35:05] K: 0, D: 0, Level: 5, Money: $0 [after]
[19:35:05] [nick] Sky6 nick changed to Sky7

Something's not right. :3
Reply
#10

djRemove() sometimes doesn't remove files, because it's based on Pawn's fremove() function which doesn't always remove files, every so often fremove() just won't work at all.

Honestly, you're just better off using a plugin for cmd/shell so you can use 'mv' to rename the file.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)