Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function insertParsedMessageRoll($textParts) {
if(count($textParts) == 1) {
// default is one d6:
$text = '/roll '.$this->getUserName().' 1d6 '.$this->rollDice(6);
} else {
$diceParts = explode('d', $textParts[1]);
if(count($diceParts) == 2) {
//Number of times to roll
$number = $diceParts[0];
//Polarity of modifier to use (negative or positive)
$polarity = '+';
if(strstr($diceParts[1], '-'))
$polarity = '-';
//Split using the correct modifier; if one does not exist, you'll get only the number of sides on the die.
$sidesParts = explode($polarity, $diceParts[1]);
$sides = $sidesParts[0];
// Dice number must be an integer between 1 and 100, else roll only one:
$number = ($number > 0 && $number <= 100) ? $number : 1;
// Add the modifier to the sum if it exists, otherwise set the sum to 0:
$sum = 0;
if(count($sidesParts) == 2)
$sum = $polarity == '+' ? $sidesParts[1] : -1*$sidesParts[1];
// Sides must be an integer between 1 and 100, else take 6:
$sides = ($sides > 0 && $sides <= 100) ? $sides : 6;
$text = '/roll '.$this->getUserName().' '.$textParts[1].' ';
for($i=0; $i<$number; $i++) {
if($i != 0)
$text .= ',';
$roll = $this->rollDice($sides);
$text .= $roll;
$sum = $sum + $roll;
$roll = 0;
}
$text .= '('.$sum.') ';
} else {
// if dice syntax is invalid, roll one d6:
$text = '/roll '.$this->getUserName().' 1d6 '.$this->rollDice(6);
}
}
$this->insertChatBotMessage(
$this->getChannel(),
$text
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.