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)]
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)]
(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()-->