Jump to content

Recommended Posts

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!

elseif ($number > 100) {
	//Allow up to only 100 dice to be rolled.
	$this->sendToUser(null, $invalid_msg);
	return 'ok';
}

 

Try putting that on line 47, underneath

if($number < 1) {
        // can't allow a negative number of dice
        $this->sendToUser(null, $invalid_msg);
        return 'ok';
    }

 

and to limit the number of sides the dice have, try this:

 

elseif ($sides > 100) {
	//Allow up to only 100 side per dice
	$this->sendToUser(null, $invalid_msg);
	return 'ok';
}

 

That would go somewhere around line 74 after inserting the first snippet. Underneath

    if($sides < 1) {
        // can't allow a negative number of sides
        $this->sendToUser(null, $invalid_msg);
        return 'ok';
    }

elseif ($number > 100) {
//Allow up to only 100 dice to be rolled.
$this->sendToUser(null, $invalid_msg);
return 'ok';
}

 

This one would go in underneath the } on Line 46 in commands.php

 

elseif ($sides > 100) {
//Allow up to only 100 side per dice
$this->sendToUser(null, $invalid_msg);	
return 'ok';
}

 

After you have pasted the first one snippet in (because after you paste it the line numbers ill not be the same as the original file), you can put this snippet in underneath the } on line 68 on commands.php

I don't know if it will work, but it looks like it should.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.