Hello,
I have a simple dice system script I am using in a chat program for a website and I was wondering if someone could simply tell me how to limit the number of dice being rolled. Here are the two chunks of script that make it work:
if($irc_cmd == '/d') {
if($irc_cmd == $txt) {
// this can only happen with a no dice - '/d' - a help request
// return an explanation of the correct format
$text = "dice command is /d [[n]D]s[Xm|+a|-d|/q]*";
$this->sendToUser(null, new Message($type, $this->userid, null, $text, $this->color));
return 'ok';
}
// create standard failure message
$invalid_msg = new Message($type, $this->userid, null, 'ERROR: invalid dice command - '.$txt.' - enter /d for help', $this->color);
// remove the original command from the dice string
$dicestring = str_replace($irc_cmd,'', $txt);
// use lowercase versions of D and X to make parsing simpler
$dicestring = strtolower($dicestring);
// remove any user entered spaces
$dicestring = str_replace(' ', '', $dicestring);
// note that all modifiers will follow the dice spec 'nDs'
// number of dice is optional e.g. 1d4 d4 and 4 all represent a single 4-sided die
// if the first token is blank, then the user intended for one die e.g. /d d20 means /d 1d20
// if the is no 'd' then the user intended one die e.g. /d 20 means /d 1d20
$parts = explode('d', $dicestring);
if(count($parts)>2) {
// only one 'd' is allowed
$this->sendToUser(null, $invalid_msg);
return 'ok';
} elseif(count($parts)==1) {
// if no 'D' assume '1' die
$number = 1;
} else {
$number = (int)$parts[0];
if ($parts[0] == "") { // if $number == 0
// if no number assume '1' die
$number=1;
} elseif ("$number" != "$parts[0]") {
// only integers allowed
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
$dicestring = $parts[1];
}
if($number < 1) {
// can't allow a negative number of dice
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
// check for sides and modifiers
// expand the string to put spaces around all the tokens we want
$dicestring = str_replace('+', ' + ', $dicestring);
$dicestring = str_replace('-', ' - ', $dicestring);
$dicestring = str_replace('x', ' x ', $dicestring);
$dicestring = str_replace('/', ' / ', $dicestring);
// explode the whole thing to create tokens
$parts = explode(' ', $dicestring);
// the only other tokens should be integers
// allowed formats from here are s[Xm][+a][-d][/q]
// we should allow any series of these in any order, applying them left to right
// the first part must be the sides
$sides = (int)$parts[0];
if ("$sides" != "$parts[0]") {
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
if($sides < 1) {
// can't allow a negative number of sides
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
// get the user's name
//$user = ChatServer::getUser($this->userid);
//$name= $user['login'];
// start writing the reply string
$text = '*rolls* '.$number.'d'.$sides.': ';
// seed the randomizer
srand(time());
// with number and sides, roll the dice, adding them up
$total = 0;
for($i = 0; $i < $number; $i++) {
$roll = (rand()%$sides)+1;
if($i != 0)
$text .= '+';
$text .= $roll;
$total += $roll;
}
// now apply all the modifiers to the roll, in the order the user specified them
for ($i = 1; $i < count($parts); $i+=2) {
// the value needs to be an integer
$value = (int)$parts[$i+1];
$v = $parts[$i+1];
if ("$value" != "$v") {
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
// the token needs to be one of the operators - otherwise abort
$token = $parts[$i];
switch ($token) {
case '+':
// add
$total += $value;
break;
case '-':
// subtract
$total -= $value;
// make minimum 1 - remove this like to allow 0 and lower
if ($total<1) $total=1;
break;
case 'x':
// multiply
$total *= $value;
break;
case '/':
// divide - round up so 1d6/3 will be the same as 1d2
$total = ceil($total/$value);
break;
default:
$this->sendToUser(null, $invalid_msg);
return 'ok';
}
// add the modifier to the display string
$text .= $token.$value;
}
// and display the final result
$text .= ': '.$total;
// gets sent to particular room, but with users name tacked on, so a user could spoof it
// at least 'msgu' looks different
$this->sendToRoom(null, new Message('rpg', $this->userid, $this->roomid, $text));
return 'ok';
}
and
function Message($command, $userid = null, $roomid = null, $txt = null, $color = null) {
$this->command = $command;
if ($command == 'rpg') $this->command = 'msgu';
$this->userid = $userid;
$this->roomid = $roomid;
$this->color = htmlColor($color);
if($command != 'rpg') {
$txt = str_replace('*rolls*', 'rolls', $txt);
}
if(isset($txt)) {
$this->txt = $this->parse($txt);
}
}
So again all I want it to do is limit the number of dice being rolled because right now someone can do a /d 1000000d100000 and completely crash the chat for everyone. I am thinking 100 on either variable would be plenty. Thanks for your help!