Roll20: Dice expression checker
/* | |
Copyright (c) 2013 J. Ryan Littlefield | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
on("chat:message", function(msg) { | |
log(msg); | |
if (msg.type == 'rollresult' || msg.type == 'gmrollresult') { | |
var parts = msg.origRoll.split('|'); | |
if (parts.length < 3) { | |
return; | |
} | |
var oper = parts[1]; | |
var expression = parts[2]; | |
log(expression); | |
var characters = findObjs({ | |
_type: "character", | |
name: msg.who | |
}); | |
var character = characters.shift(); | |
if (character) { | |
var attributes = findObjs({ | |
_type: "attribute", | |
_characterid: character.get('_id') | |
}); | |
for (var i = 0; i < attributes.length; i++) { | |
var current_attribute = attributes[i]; | |
log ('@'+current_attribute.get('name') + '=' + current_attribute.get('current')); | |
expression = expression.replace('@'+current_attribute.get('name'), current_attribute.get('current')); | |
} | |
} | |
log(expression); | |
var content = JSON.parse(msg.content); | |
log(content); | |
var result = null; | |
var difference = 0; | |
var new_expression = 'result = (' + content.total + ' ' + oper + ' ' + expression + ');'; | |
log(new_expression); | |
try { | |
eval(new_expression); | |
} catch (e) { | |
log(e); | |
} | |
try { | |
difference = Math.abs(content.total - eval(expression)); | |
} catch (e) { | |
log(e); | |
} | |
switch (msg.type) { | |
case 'gmrollresult': | |
var whisper_origin = true; | |
try { | |
var who = msg.who.split(' '); | |
who = who[0]; | |
} catch (e) { | |
whisper_origin = false; | |
sendChat('RollResult', '/w gm Failure to find player by name "'+msg.who+'". Try making them remove spaces from their name.'); | |
} | |
if (msg.who.indexOf('(GM)') !== -1) { | |
whisper_origin = false; | |
} | |
if (result) { | |
sendChat('RollResult', '/w gm Success by '+difference+''); | |
if (whisper_origin) { | |
sendChat('RollResult', '/w ' + who + ' Success by '+difference+''); | |
} | |
} else { | |
sendChat('RollResult', '/w gm Failure by '+difference+''); | |
if (whisper_origin) { | |
sendChat('RollResult', '/w ' + who + ' Failure by '+difference+''); | |
} | |
} | |
break; | |
default: | |
if (result) { | |
sendChat('RollResult', 'Success by '+difference+''); | |
} else { | |
sendChat('RollResult', 'Failure by '+difference+''); | |
} | |
break; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment