Javascript - Roll20 API Script

Javascript - Roll20 API Script

本文关键字:Script API Roll20 Javascript      更新时间:2023-09-26

我在roll20上玩一个游戏,我正在写一个向"角色"添加宏的脚本,我已经制作了一个将添加所有技能的脚本,但我不知道如何编写逻辑,让它检查角色表上是否存在属性,然后编写宏。

所以这是我的代码,一些建议会很棒!我对这一切都一无所知,我正在努力学习语言,但我可以举一个例子来合作!

        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 + "+@{" + 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 + "+@{" + 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 + "+@{" + 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 + "+@{" + bigmike.saves[index].name + "} ]] save VS " + bigmike.saves[index].name + ".";
                createObj("ability", {
                    name: bigmike.saves[index].name,
                    description: "",   
                    action: macro,   
                    characterid: character.id
                });
            };
        }

要搜索不知道_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脚本时,您还可以访问Undercore.js,这可能非常有用。

为了将来参考,Roll20.net留言板上有一个论坛,专门用于请求有关Roll20 API脚本的帮助(并展示已完成的脚本)。那里的用户将能够比在SO上更快地帮助您。虽然SO通常非常快,但Roll20 API仅在Roll20上使用,因此这是一个非常本地化的问题。