cant find the bug json question

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
wolph42
Deity
Posts: 9790
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

cant find the bug json question

Post by wolph42 »

this has been bugging me for quite a while and I can't find where it goes wrong:

Code: Select all

[hasGroup = 1]
[Skills = "{'Acrobatics':{'bonus':10,'char':'Ag'},'Speak Language':{'chosenGroups':{'High Gothic':{'char':'Int','bonus':10},'Hive Dialect':{'char':'Int','bonus':20}}}}"]
[newSkill = "Speak Language"]
[chooseGroup = "High Gothic"]

[mySkillDetail = json.get(Skills, newSkill)]
[if(hasGroup): chosenGroups = json.get(mySkillDetail, "chosenGroups"); chosenGroups = "{}"]
[pause("chosenGroups")]
<!--output as expected-->

<!--if theres only one group left or if no groups at all, delete the skill. Else delete only the choosen group-->
[if(json.length(chosenGroups) <= 1):chosenGroups="{}"; chosenGroups = json.remove(chosenGroups, chooseGroup)]
[pause("chosenGroups")]
<!--output as expected, High Gothic has been removed-->

[mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups ]
[pause("mySkillDetail", "chosenGroups")]
<!--here it goes wrong, myskilldetail isnt updated-->

[Skills = json.set(Skills, newSkill, mySkillDetail]
[pause("Skills")]
[if(json.length(chosenGroups) < 1):Skills = json.remove(Skills, newSkill)]


the 'pause' is a debug function that shows the output in between, if you want to test this, you can remove them.

EDIT:
ok its weirder than that... if I remove the pauses from the above function it works:

Code: Select all

[hasGroup = 1]
[Skills = "{'Acrobatics':{'bonus':10,'char':'Ag'},'Speak Language':{'chosenGroups':{'High Gothic':{'char':'Int','bonus':10},'Hive Dialect':{'char':'Int','bonus':20}}}}"]
[newSkill = "Speak Language"]
[chooseGroup = "High Gothic"]
        [mySkillDetail = json.get(Skills, newSkill)]
        [if(hasGroup): chosenGroups = json.get(mySkillDetail, "chosenGroups"); chosenGroups = "{}"]
        [if(json.length(chosenGroups) <= 1):chosenGroups="{}"; chosenGroups = json.remove(chosenGroups, chooseGroup)]
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups) ]
<br><br>
        [r:Skills = json.set(Skills, newSkill, mySkillDetail)]


however this is a simplified version of the routine that really counts (everything has been copy pasted) and in the big routine it doesn't work...
(look for the <!--000000000000)

Code: Select all

[h:selectedSkills = macro.args]

[h:SkillLibrary = getLibProperty("Skills", "Lib:Compendium")]
[h:hasGroup = 0]

[h:switchToken(json.get(selectedSkills, "token"))]

<!--Remove the "token" entry from the arguments so it doesn't throw an error-->
[h:selectedSkills = json.remove(selectedSkills, "token")]

<!--Loop through each selected item in the arguments-->
[h,foreach(newSkill, selectedSkills,""),CODE:
{
    [SkillDetail = json.get(SkillLibrary, newSkill)]
    [bonus = json.get(selectedSkills, newSkill)]
    [skillChar = json.get(SkillDetail, "char")]
    <!--If the Skills is a Group Skill, present an input to select the group-->
    [if(json.get(SkillDetail, "groups")!="-"), CODE:
    {
        [hasGroup = 1]
        [status=input("chooseGroup|"+json.get(SkillDetail,"groups")+"|Select Group for "+newSkill+"|LIST|SELECT=0 VALUE=STRING")]
        [abort(status)]

        <!--create a json object for the new group-->
        <!--in case of Trade, split the char and group up like Agri (S) -->
        [if(skillChar  == "Varies"): groupChar = getGroup(strfind(chooseGroup,  "(?<=\\()[A-Za-z]+"), 1,0); groupChar = skillChar]
        [if(skillChar  == "Varies"): chooseGroup = getGroup(strfind(chooseGroup, "[A-Za-z]+(?=\\s\\()"), 1,0)]
        [groupDetails = "{}"]
        [groupDetails = json.set(groupDetails, "char", groupChar)]
        [groupDetails = json.set(groupDetails, "bonus", bonus)]
    };{
        [chooseGroup = "{}"]
        [groupDetails = "-"]
    }]

    <!--delete skill and/or group if 'not' has been chosen-->
    [if(bonus == "-"), CODE:{
        [mySkillDetail = json.get(Skills, newSkill)]
        <!--again limited by the amount of nested codes...-->
        [if(hasGroup): chosenGroups = json.get(mySkillDetail, "chosenGroups"); chosenGroups = "{}"]
        <!--if theres only one group left or if no groups at all, delete the skill. Else delete only the choosen group-->
        [if(json.length(chosenGroups) <= 1):chosenGroups="{}"; chosenGroups = json.remove(chosenGroups, chooseGroup)]
<!--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO-->
<!--this is the spot where it goes wrong, high gothic has been removed from chosenGroups, however mySkillDetail isnt updated in the following step-->
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups ]
<!--OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO-->
        <!--cant nest anymore if statements so its a bit sloppy...-->
        [Skills = json.set(Skills, newSkill, mySkillDetail]
        [if(json.length(chosenGroups) < 1):Skills = json.remove(Skills, newSkill)]
    };{}]

    <!--add new skill or group to list-->
    [if(bonus != "-"), CODE:{
        <!--check if you have the skill and if so retrieve the details-->
        [if(json.contains(Skills, newSkill)): mySkillDetail = json.get(Skills, newSkill); mySkillDetail = "{}"]
        <!--check if the skills has groups-->

        <!--if the skill is already chosen AND it has groups, retrieve them-->
        [if(json.length(mySkillDetail) != 0 && hasGroup): chosenGroups = json.get(mySkillDetail, "chosenGroups"); chosenGroups = "{}"]
        [chosenGroups = json.set(chosenGroups, chooseGroup, groupDetails)]

        <!--Update the SkillDetail JSON with the new list of groups-->
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups)]
        [if(!hasGroup):mySkillDetail = json.set(mySkillDetail, "bonus", bonus)]
        [if(!hasGroup):mySkillDetail = json.set(mySkillDetail, "char", skillChar)]
       
        <!--and finally add the skill to the list-->
        [Skills = json.set(Skills, newSkill, mySkillDetail)]
  };{}]
}]

<br>
[r:mySkillDetail] <!--high gothic is still there-->
Skills have been updated.
<!--h:refreshSheet()-->

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: cant find the bug json question

Post by CoveredInFish »

Code: Select all

<!--this is the spot where it goes wrong, high gothic has been removed from chosenGroups, however mySkillDetail isnt updated in the following step-->
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups ]

Here is a missing closing bracket.

User avatar
wolph42
Deity
Posts: 9790
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: cant find the bug json question

Post by wolph42 »

CoveredInFish wrote:

Code: Select all

<!--this is the spot where it goes wrong, high gothic has been removed from chosenGroups, however mySkillDetail isnt updated in the following step-->
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups ]

Here is a missing closing bracket.



argh... *faint sound in the distance of head banging into wall*

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: cant find the bug json question

Post by biodude »

CoveredInFish wrote:

Code: Select all

<!--this is the spot where it goes wrong, high gothic has been removed from chosenGroups, however mySkillDetail isnt updated in the following step-->
        [mySkillDetail = json.set(mySkillDetail, "chosenGroups", chosenGroups ]

Here is a missing closing bracket.


Aha! I'm not the only one who calls them "brackets" :P
Yes, this is a common error. God bless any text editor that can check pairing for you :D I use TextWrangler, wherein you can double-click on any bracket/parentheses/brace and it will automatically select everything up to the matching closing character. It makes it much easier to spot when things are mismatched.

P.S. Same problem in the next line:

Code: Select all

        <!--cant nest anymore if statements so its a bit sloppy...-->
        [Skills = json.set(Skills, newSkill, mySkillDetail]
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

User avatar
aliasmask
Deity
Posts: 8609
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: cant find the bug json question

Post by aliasmask »

biodude wrote:Aha! I'm not the only one who calls them "brackets"

Only proves that you're both odd... or British... same diff I guess :)

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: cant find the bug json question

Post by CoveredInFish »

As i'm not a native speaker I might be not the one to team up with to defend the bracket=parentheses team. But now I remember the definitions rumble posted and that i wanted to remember them :oops: since it is surely useful to distinguish them (and "eckige/geschweifte/runde klammer"GER wont help :wink: )

I think the english I learned at school was more british than american.


User avatar
aliasmask
Deity
Posts: 8609
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: cant find the bug json question

Post by aliasmask »

I find the wiki definition of "haakjes" interesting.

"The left half of a capital 0"

User avatar
wolph42
Deity
Posts: 9790
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: cant find the bug json question

Post by wolph42 »

aliasmask wrote:I find the wiki definition of "haakjes" interesting.

"The left half of a capital 0"


lol, I think the unintentional use of reversed smilies is also wort a smile 8) :
# an open parenthesis looks like the left half of the capital O: (
a closing parenthesis looks like the right half of the capital O:)

Post Reply

Return to “Macros”