3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00

Switch the file format back to UNIX

This commit is contained in:
Luke Slater 2012-11-25 15:55:35 +00:00
parent 7faa5c1207
commit 047c2e45e5

View File

@ -1,105 +1,105 @@
var parseDiceSpec = function (specString) { var parseDiceSpec = function (specString) {
var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)(|[+-][0-9]+)$/i, 4); var rawSpec = specString.valMatch(/^([0-9]*)d(%|[0-9]*)(|[+-][0-9]+)$/i, 4);
if (rawSpec !== false) { if (rawSpec !== false) {
if (rawSpec[2] === "%") { if (rawSpec[2] === "%") {
rawSpec[2] = 100; rawSpec[2] = 100;
} }
return { return {
"count": parseInt(rawSpec[1] || 1), "count": parseInt(rawSpec[1] || 1),
"sides": parseInt(rawSpec[2] || 6), "sides": parseInt(rawSpec[2] || 6),
"modifier": parseInt(rawSpec[3] || 0) "modifier": parseInt(rawSpec[3] || 0)
}; };
} else { } else {
return false; return false;
} }
}; };
var normalizeDiceSpec = function (specString) { var normalizeDiceSpec = function (specString) {
var diceSpec = parseDiceSpec(specString); var diceSpec = parseDiceSpec(specString);
if (diceSpec["sides"] > 10000) { if (diceSpec["sides"] > 10000) {
return false; return false;
} }
if (diceSpec["count"] > 1000) { if (diceSpec["count"] > 1000) {
return false; return false;
} }
if (diceSpec["count"] > 1) { if (diceSpec["count"] > 1) {
var count = diceSpec["count"]; var count = diceSpec["count"];
} else { } else {
var count = ""; var count = "";
} }
if (diceSpec["sides"] === 100) { if (diceSpec["sides"] === 100) {
var sides = "%"; var sides = "%";
} else { } else {
var sides = diceSpec["sides"]; var sides = diceSpec["sides"];
} }
if (diceSpec["modifier"] > 0) { if (diceSpec["modifier"] > 0) {
var modifier = "+" + diceSpec["modifier"]; var modifier = "+" + diceSpec["modifier"];
} else if (diceSpec["modifier"] < 0) { } else if (diceSpec["modifier"] < 0) {
var modifier = diceSpec["modifier"]; var modifier = diceSpec["modifier"];
} else { } else {
var modifier = ""; var modifier = "";
} }
return (count + "d" + sides + modifier); return (count + "d" + sides + modifier);
}; };
var dice = function(dbot) { var dice = function(dbot) {
var commands = { var commands = {
'~roll': function (event) { '~roll': function (event) {
var rolls = []; var rolls = [];
if (event.params.length === 1) { if (event.params.length === 1) {
event.params.push("d6"); event.params.push("d6");
} }
for (var i = 1; i < event.params.length; i++) { for (var i = 1; i < event.params.length; i++) {
var diceSpec = parseDiceSpec(event.params[i]); var diceSpec = parseDiceSpec(event.params[i]);
if (diceSpec === false) { if (diceSpec === false) {
rolls.push([event.params[i], false]); rolls.push([event.params[i], false]);
} else { } else {
rolls.push([normalizeDiceSpec(event.params[i]), [], diceSpec["modifier"]]); rolls.push([normalizeDiceSpec(event.params[i]), [], diceSpec["modifier"]]);
for (var j = 0; j < diceSpec["count"] ; j++) { for (var j = 0; j < diceSpec["count"] ; j++) {
rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"])); rolls[rolls.length-1][1].push(Math.ceil(Math.random() * diceSpec["sides"]));
} }
} }
} }
for (var i = 0; i < rolls.length; i++) { for (var i = 0; i < rolls.length; i++) {
if (rolls[i][1] === false) { if (rolls[i][1] === false) {
event.reply(rolls[i][0] + ": invalid dice spec"); event.reply(rolls[i][0] + ": invalid dice spec");
} else { } else {
if (rolls[i][1].length > 1) { if (rolls[i][1].length > 1) {
var total = " (total " + rolls[i][1].sum(); var total = " (total " + rolls[i][1].sum();
if (rolls[i][2] != 0) { if (rolls[i][2] != 0) {
if (rolls[i][2] > 0) { if (rolls[i][2] > 0) {
total += " + "; total += " + ";
} else { } else {
total += " - "; total += " - ";
} }
total += Math.abs(rolls[i][2]) + " -> " + (rolls[i][1].sum() + rolls[i][2]); total += Math.abs(rolls[i][2]) + " -> " + (rolls[i][1].sum() + rolls[i][2]);
} }
total += ")" total += ")"
} else { } else {
var total = ""; var total = "";
} }
event.reply(rolls[i][0] + ": " + rolls[i][1].join(" ") + total); event.reply(rolls[i][0] + ": " + rolls[i][1].join(" ") + total);
} }
} }
} }
}; };
return { return {
'name': 'dice', 'name': 'dice',
'commands': commands, 'commands': commands,
'ignorable': true 'ignorable': true
}; };
} }
exports.fetch = function(dbot) { exports.fetch = function(dbot) {
return dice(dbot); return dice(dbot);
}; };