2013-11-23 13 views
0

我在roll20上玩遊戲,我正在寫一個腳本給宏添加一個「Character」,我已經創建了一個腳本來添加所有的技巧,但是我不知道如何編寫邏輯讓它檢查字符表中是否存在屬性,然後寫入宏。Javascript - Roll20 API腳本

所以這裏是我的代碼,一些建議會很棒!我是所有這些的小菜鳥,我正在努力學習這門語言,但我可以用一個例子來處理!

 var bigmike = bigmike || {}; 

     bigmike.dicetype = "1d20"; 

     bigmike.charClasses = [ 
       'Monster' 
     ]; 

     bigmike.attribute = []; 
      bigmike.attribute["AC"] = { name: "AC", current: "", max:"" }; 
      bigmike.attribute["Init"] = { name: "Init", current: "", max:"" }; 
      bigmike.attribute["fort"] = { name: "Fort", current: "", max:"" }; 
      bigmike.attribute["ref"] = { name: "Ref", current: "", max:"" }; 
      bigmike.attribute["will"] = { name: "Will", current: "", max:"" }; 
      bigmike.attribute["str"] = { name: "Str", current: "", max:"" };  
      bigmike.attribute["dex"] = { name: "Dex", current: "", max:"" }; 
      bigmike.attribute["con"] = { name: "Con", current: "", max:"" }; 
      bigmike.attribute["int"] = { name: "Int", current: "", max:"" }; 
      bigmike.attribute["wis"] = { name: "Wis", current: "", max:"" }; 
      bigmike.attribute["cha"] = { name: "Cha", current: "", max:"" }; 
      bigmike.attribute["Size"] = { name: "Size", current: "", max:"" }; 
      bigmike.attribute["CR"] = { name: "CR", current: "", max:"" }; 
      bigmike.attribute["HP"] = { name: "HP", current: "", max:"" }; 
      bigmike.attribute["BaseAtk"] = { name: "BaseAtk", current: "", max:"" }; 
      bigmike.attribute["CMB"] = { name: "CMB", current: "", max:"" }; 
      bigmike.attribute["CMD"] = { name: "CMD", current: "", max:"" }; 


     bigmike.stats = [ 
      { name: bigmike.attribute["str"].name }, 
      { name: bigmike.attribute["dex"].name }, 
      { name: bigmike.attribute["con"].name }, 
      { name: bigmike.attribute["int"].name }, 
      { name: bigmike.attribute["wis"].name }, 
      { name: bigmike.attribute["cha"].name } 
     ]; 

     bigmike.saves = [ 
      { name: bigmike.attribute["fort"].name }, 
      { name: bigmike.attribute["ref"].name }, 
      { name: bigmike.attribute["will"].name } 
     ]; 

     /** Skills 
     Skills are represented as an object, which consists of one value, since Monsters have static skill settings. 
     */ 
     bigmike.skill = [ 
      { name: "Acrobatics", start: 0 }, 
      { name: "Appraise", start: 0 }, 
      { name: "Bluff", start: 0 }, 
      { name: "Climb", start: 0 }, 
      { name: "Diplomacy", start: 0 }, 
      { name: "Disable-Device", start: 0 }, 
      { name: "Disguise", start: 0 }, 
      { name: "Escape-Artist", start: 0 }, 
      { name: "Fly", start: 0 }, 
      { name: "Handle-Animal", start: 0 }, 
      { name: "Heal", start: 0 }, 
      { name: "Intimidate", start: 0 }, 
      { name: "Linguistics", start: 0 }, 
      { name: "Perception", start: 0 }, 
      { name: "Perform", start: 0 }, 
      { name: "Ride", start: 0 }, 
      { name: "Sense-Motive", start: 0 }, 
      { name: "Sleight-of-Hand", start: 0 }, 
      { name: "Spellcraft", start: 0 }, 
      { name: "Stealth", start: 0 }, 
      { name: "Survival", start: 0 }, 
      { name: "Swim", start: 0 }, 
      { name: "Use-Magic-Device", start: 0 } 
     ]; 

     bigmike.knowledge = [ 
      { name: "Knowledge-Arcana", start: 0 }, 
      { name: "Knowledge-Dungeoneering", start: 0 }, 
      { name: "Knowledge-Engineering", start: 0 }, 
      { name: "Knowledge-Geography", start: 0 }, 
      { name: "Knowledge-History", start: 0 }, 
      { name: "Knowledge-Local", start: 0 }, 
      { name: "Knowledge-Nature", start: 0 }, 
      { name: "Knowledge-Nobility", start: 0 }, 
      { name: "Knowledge-Planes", start: 0 }, 
      { name: "Knowledge-Religion", start: 0 } 
     ]; 

     bigmike.craft = [ 
      { name: "Craft-Any", start: 0 }, 
      { name: "Craft-Alchemy", start: 0 }, 
      { name: "Craft-Armorer", start: 0 }, 
      { name: "Craft-Basketweaving", start: 0 }, 
      { name: "Craft-Bookbinding", start: 0 }, 
      { name: "Craft-Bowyer", start: 0 }, 
      { name: "Craft-Blacksmith", start: 0 }, 
      { name: "Craft-Brewing", start: 0 }, 
      { name: "Craft-Calligraphy", start: 0 }, 
      { name: "Craft-Carpentry", start: 0 }, 
      { name: "Craft-Cobbling", start: 0 }, 
      { name: "Craft-Cooking", start: 0 }, 
      { name: "Craft-Crystal-Carving", start: 0 }, 
      { name: "Craft-Gemcutting", start: 0 }, 
      { name: "Craft-Glassblowing", start: 0 }, 
      { name: "Craft-Goldsmith", start: 0 }, 
      { name: "Craft-Leatherworking", start: 0 }, 
      { name: "Craft-Locksmithing", start: 0 }, 
      { name: "Craft-Jewelry", start: 0 }, 
      { name: "Craft-Painting", start: 0 }, 
      { name: "Craft-Pottery", start: 0 }, 
      { name: "Craft-Sculpting", start: 0 }, 
      { name: "Craft-Stonemasonry", start: 0 }, 
      { name: "Craft-Tailoring", start: 0 }, 
      { name: "Craft-Traps", start: 0 }, 
      { name: "Craft-Weaponsmith", start: 0 }, 
      { name: "Craft-Weaving", start: 0 } 
     ]; 

     bigmike.parseCommand = function parseCommand(msg) { 
      if (msg.type == "api" && msg.who.indexOf("(GM)") == -1) { 
       sendChat("System", "Only the GM is allowed to switch classes."); 
       return; 
      } 
      if(msg.type == "api" && msg.content.indexOf("!skill ") !== -1) { 
       var cmdParts = msg.content.replace("!skill ", "").split(" "); 
       var charClass; 

       _.each(cmdParts, function(part) { 
        var indexOfClass = _.indexOf(bigmike.charClasses, part); 
        if (indexOfClass > -1) { 
         charClass = bigmike.charClasses[indexOfClass]; 
         cmdParts = _.without(cmdParts, part); 
        } 
       }); 

       if (typeof charClass === undefined) { 
        sendChat('System', 'Usage: !skill charName charClass'); 
        sendChat('System', 'Value for charClass: ' + bigmike.charClasses); 
        return; 
       } 

       var result = {}; 
       result.charName = cmdParts.join(" "); 
       log("Charname: " + result.charName); 
       result.charType = charClass; 
       log("CharType: " + result.charType); 
       return result; 
      } 
     } 

     on("ready", function() { 
      sendChat("system", "/w GM API is ready and running"); 
      on("add:character", function(character) { 
     //  insertAttributes(character); 
     //  insertStatRolls(character); 
       insertSaves(character); 
     //  insertAttacks(character); 
       insertSkills(character); 
       insertKnowledges(character); 
       insertCrafts(character); 
      }); 
     }); 

     on("chat:message", function(msg) { 
       var chatCmd = bigmike.parseCommand(msg); 
       if (typeof chatCmd === 'undefined') { 
        return; 
       } 

       sendChat('System', 'Setting class ' + chatCmd.charType + ' for ' + chatCmd.charName); 

       var characters = findObjs({ 
        _type: "character", 
        archived: false, 
        name: chatCmd.charName 
       }, {caseInsensitive: true}); 
       _.each(characters, function(character) { 
        log("Name: " + character.get("name")); 
        log("Bio: " + character.get("bio")); 
        log("GM-Notes: " + character.get("gmnotes")); 
        log("Archived: " + character.get("archived")); 
        log("In player journals: " + character.get("inplayerjournals")); 
        log("Controlled By: " + character.get("controlledby")); 
        insertSaves(character, chatCmd.charType); 
        insertSkills(character, chatCmd.charType); 
        insertKnowledges(character, chatCmd.charType); 
        insertCrafts(character, chatCmd.charType); 
       }); 
      }); 

     /** Functions 
      list of functions: (planned or in place) 
       insertAttribute 
       insertAbilities 
       insertSkills 
       rollStats 
     */ 

     function insertAttributes(character) { 
      for (var index in bigmike.attribute) { 
       createObj("attribute", { 
        name: bigmike.attribute[index].name, 
        current: bigmike.attribute[index].start, 
        max: bigmike.attribute[index].max, 
        characterid: character.id 
       }); 
      }; 
      for(var index in bigmike.skill) { 
       createObj("attribute", { 
        name: bigmike.skill[index].name, 
        current: bigmike.skill[index].start, 
        max: "", 
        characterid: character.id, 
       }); 
      }; 
      for(var index = 0; index < bigmike.knowledge.length; index++) { 
       createObj(".attribute", { 
        name: bigmike.knowledge[index].name, 
        current: bigmike.knowledge[index].start, 
        max: "", 
        characterid: character.id, 
       }); 
      }; 
     }; 

     function insertAttacks(character) { 
      for (var index = 0; index < bigmike.attack.length; index++) { 
       createObj("ability", { 
        name: bigmike.attack[index].name, 
        description: "", 
        action: bigmike.attack[index].macro, 
        characterid: character.id, 
       }); 
      }; 
     } 

     function insertSkills(character) { 
      for (var index = 0; index < bigmike.skill.length; index++) { 
       var macro = "/emas @{selected|token_name} makes a check vs " + bigmike.skill[index].name + " ([[" + bigmike.dicetype + " + @{" + bigmike.skill[index].name + "} ]])"; 
       createObj("ability", { 
        name: bigmike.skill[index].name, 
        description: "", 
        action: macro, 
        characterid: character.id 
       }); 
      }; 
     } 

     function insertKnowledges(character) { 
      for (var index = 0; index < bigmike.knowledge.length; index++) { 
       var macro = "/emas @{selected|token_name} makes a check vs " + bigmike.knowledge[index].name + ": ([[" + bigmike.dicetype + "[email protected]{" + bigmike.knowledge[index].name + "} ]])"; 
       createObj("ability", { 
        name: bigmike.knowledge[index].name, 
        description: "", 
        action: macro, 
        characterid: character.id 
       }); 
      }; 
     } 

     function insertCrafts(character) { 
      for (var index = 0; index < bigmike.craft.length; index++) { 
       var macro = "/emas @{selected|token_name} makes a check vs " + bigmike.craft[index].name + ": ([[" + bigmike.dicetype + "[email protected]{" + bigmike.craft[index].name + "} ]])"; 
       createObj("ability", { 
        name: bigmike.craft[index].name, 
        description: "", 
        action: macro, 
        characterid: character.id 
       }); 
      }; 
     } 

     function insertStatRolls(character) { 
      for (var index = 0; index < bigmike.stats.length; index++) { 
       var macro = "/emas @{selected|token_name} rolls a [[" + bigmike.dicetype + "[email protected]{" + bigmike.stats[index].name + "} ]] for his " + bigmike.stats[index].name + " check."; 
       createObj("ability", { 
        name: bigmike.stats[index].name, 
        description: "", 
        action: macro, 
        characterid: character.id 
       }); 
      }; 
     } 

     function insertSaves(character) { 
      for (var index = 0; index < bigmike.saves.length; index++) { 
       var macro = "/emas @{selected|token_name} rolls a [[" + bigmike.dicetype + "[email protected]{" + bigmike.saves[index].name + "} ]] save VS " + bigmike.saves[index].name + "."; 
       createObj("ability", { 
        name: bigmike.saves[index].name, 
        description: "", 
        action: macro, 
        characterid: character.id 
       }); 
      }; 
     } 
+0

這個腳本提供了兩個函數,第一個是創建一個新字符的所有屬性和宏,第二個函數是通過調用它並通過聊天系統修改一個現有的對象。 https://wiki.roll20.net/API:Introduction –

+0

來吧夥計們,這是小孩子給你玩的! –

+0

我還不完全熟悉Roll20的API,但是不能只檢查它是否爲空? 'bigmike.attribute ['AC']!== undefined;' – Heilemann

回答

1

搜索現有的屬性(S)你不知道的_id,使用findObjs

function insertAttributes(character) { 
    var characterAttrs = findObjs({ 
     _type: 'attribute', 
     //name: 'MyAttrName', 
     _characterid: character.id 
    }); 
    // `characterAttrs` is now an array of all attributes owned by `character` 
    // If you uncomment the name line and `character` has one or more attributes 
    // named "MyAttrName", `characterAttrs` will be an array of all of the 
    // attributes named "MyAttrName" owned by `character` 

    ... 
} 

更多信息可以在Roll20 Wiki找到。

FWIW,您的代碼中還有許多額外的分號(例如在for循環塊的末尾)。在您的知識屬性循環中,您還嘗試創建.attribute類型的對象,而不是attribute。編寫Roll20腳本時,您還可以訪問Underscore.js,這可能非常有用。

爲了將來的參考,Roll20.net留言板有一個論壇特別是向Roll20 API腳本尋求幫助(並展示完成的腳本)。那裏的用戶將能夠比這裏更快地爲您提供幫助。雖然SO通常非常快,但Roll20 API僅在Roll20上使用,所以這是一個非常本地化的問題。