Strolen's World · Legacy archive
ajax-chat updates (dice roller etc)
-
ajax-chat updates (dice roller etc)
#1AJAXChat.php
function insertParsedMessageRoll($textParts) { if(count($textParts) == 1) { // default is one d20: $number = 1; // need to set $parts to an empty array for the rest of this $dicestring = "20"; } else { // use lowercase, remove any user entered spaces and find a 'd' $diceParts = explode('d', strtolower($textParts).str_replace(' ', '')); if(count($diceParts) == 1) { // no 'd' was specified, so assume 1 die $number = 1; $dicestring = $diceParts; } else if(count($diceParts) == 2) { // note that the $number could still be blank/zero $number = (int)$diceParts; $dicestring = $diceParts; } else { // if dice syntax is invalid, roll one d20: // this culd be multiple 'd's but we can't deal with that $number = 1; $dicestring = "20"; } } // Dice number must be a positive integer, else roll only one: $number = ($number > 0) ? $number : 1; // still need to parse sides and the modifiers $dicestring = str_replace('+', ' + ', $dicestring); $dicestring = str_replace('-', ' - ', $dicestring); $dicestring = str_replace('x', ' x ', $dicestring); $dicestring = str_replace('*', ' x ', $dicestring); $dicestring = str_replace('/', ' / ', $dicestring); // explode the whole thing to create tokens $parts = explode(' ', $dicestring); // Sides must be a positive integer , else use 20: $sides = (int)$parts; $sides = ($sides > 0) ? $sides : 20; // now roll the dice, and total them up $text = '/roll '.$this->getUserName().' '.$number.'d'.$sides.' ('; $total = 0; for($i=0; $irollDice($sides); if($i != 0) $text .= ','; $text .= $roll; $total += $roll; } $text .= ')'; // add modifiers explicitly to $text // modify the value of $total // the rest of $parts should be modifier tokens - could check for the right number // 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") { // if the value being used is not an integer, use 1? $value = 1; } // the token needs to be one of the operators - otherwise skip $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