Difference between revisions of "Mod:Cookbook"
From Roll20 Wiki
(→getSenderForName) |
(→Utility Functions) |
||
Line 91: | Line 91: | ||
} | } | ||
});</pre> | });</pre> | ||
+ | |||
+ | === levenshteinDistance === | ||
+ | '''Source:''' [http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript en.wikibooks.org] | ||
+ | |||
+ | [[wikipedia:Levenshtein distance|''Levenshtein Distance'']] is a metric for measuring the difference between strings. The return value for this function will be the number of substitutions, insertions, and deletions required to transform string ''a'' into string ''b''. | ||
+ | <pre data-language="javascript">function levenshteinDistance(a, b) { | ||
+ | var i, j, | ||
+ | matrix = []; | ||
+ | |||
+ | if (a.length === 0) { | ||
+ | return b.length; | ||
+ | } | ||
+ | if (b.length === 0) { | ||
+ | return a.length; | ||
+ | } | ||
+ | |||
+ | // Increment along the first column of each row | ||
+ | for (i = 0; i <= b.length; i++) { | ||
+ | matrix[i] = [i]; | ||
+ | } | ||
+ | |||
+ | // Increment each column in the first row | ||
+ | for (j = 0; j <= a.length; j++) { | ||
+ | matrix[0][j] = j; | ||
+ | } | ||
+ | |||
+ | // Fill in the rest of the matrix | ||
+ | for (i = 1; i <= b.length; i++) { | ||
+ | for (j = 1; j <= a.length; j++) { | ||
+ | if (b.charAt(i - 1) === a.charAt(j - 1)) { | ||
+ | matrix[i][j] = matrix[i - 1][j - 1]; | ||
+ | } else { | ||
+ | matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // Substitution | ||
+ | Math.min(matrix[i][j - 1] + 1, // Insertion | ||
+ | matrix[i - 1][j] + 1)); // Deletion | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return matrix[b.length][a.length]; | ||
+ | }</pre> | ||
[[Category:API|Cookbook]] | [[Category:API|Cookbook]] |
Revision as of 23:03, 29 December 2014
The following are not full scripts. They are meant to be stitched together along with business logic to assist in the creation of full scripts, not create scripts on their own.
Contents |
Revealing Module Pattern
The Module Pattern emulates the concept of classes from other languages by encapsulating private and public members within an object. The Revealing Module Pattern improves upon the Module Pattern by making the syntax more consistent.
var myRevealingModule = myRevealingModule || (function() { var privateVar = 'This variable is private', publicVar = 'This variable is public'; function privateFunction() { log(privateVar); } function publicSet(text) { privateVar = text; } function publicGet() { privateFunction(); } return { setFunc: publicSet, myVar: publicVar, getFunc: publicGet }; })(); log(myRevealingModule.getFunc()); // "This variable is private" myRevealingModule.setFunc('But I can change its value'); log(myRevealingModule.getFunc()); // "But I can change its value" log(myRevealingModule.myVar); // "This variable is public" myRevealingModule.myVar = 'So I can change it all I want'; log(myRevealingModule.myVar); // "So I can change it all I want"
Memoization
Memoization is an optimization technique which stores the result for a given input, allowing the same output to be produced without computing it twice. This is especially useful in expensive computations. Of course, if it is rare that your function will receive the same input, memoization will be of limited utility while the storage requirements for it continue to grow.
var factorialCache = {}; function factorial(n) { var x; n = parseInt(n || 0); if (n < 0) { throw 'Factorials of negative numbers are not well-defined'; } if (n === 0) { return 1; } else if (factorialCache[n]) { return factorialCache[n]; } x = factorial(n - 1) * n; factorialCache[n] = x; return x; }
In a Roll20 API script, the cached values could potentially be stored in state
, which will persist between game sessions. If you have a large number of potential inputs, however, be aware that Roll20 may throttle your use of state
.
Utility Functions
Utility functions complete common tasks that you may want to use throughout many scripts. If you place a function at the outermost scope of a script tab, that function should be available to all of your scripts, reducing your overhead. Below is a selection of such functions.
getSenderForName
Dependencies: None
Given a string name, this function will return a string appropriate for the first parameter of sendChat. If there is a character that shares a name with a player, the player will be used. You may also pass an options
object, which is structured identically to the options
parameter of findObjs.
function getSenderForName(name, options) { var character = findObjs({ type: 'character', name: name }, options)[0], player = findObjs({ type: 'player', displayname: name.lastIndexOf(' (GM)') === name.length - 5 ? name.substring(0, name.length - 5) : name }, options)[0]; if (player) { return 'player|' + player.id; } if (character) { return 'character|' + character.id; } return name; } // example use on('chat:message', function(msg) { if (msg.content === '!nsp') { sendChat(getPlayerCharacterFrom(msg.who), 'None shall pass!'); } });
levenshteinDistance
Source: en.wikibooks.org
Levenshtein Distance is a metric for measuring the difference between strings. The return value for this function will be the number of substitutions, insertions, and deletions required to transform string a into string b.
function levenshteinDistance(a, b) { var i, j, matrix = []; if (a.length === 0) { return b.length; } if (b.length === 0) { return a.length; } // Increment along the first column of each row for (i = 0; i <= b.length; i++) { matrix[i] = [i]; } // Increment each column in the first row for (j = 0; j <= a.length; j++) { matrix[0][j] = j; } // Fill in the rest of the matrix for (i = 1; i <= b.length; i++) { for (j = 1; j <= a.length; j++) { if (b.charAt(i - 1) === a.charAt(j - 1)) { matrix[i][j] = matrix[i - 1][j - 1]; } else { matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // Substitution Math.min(matrix[i][j - 1] + 1, // Insertion matrix[i - 1][j] + 1)); // Deletion } } } return matrix[b.length][a.length]; }