Changeset 234
- Timestamp:
- 04/14/08 02:47:33 (7 months ago)
- Files:
-
- trunk/Phergie/Plugin/Dice.php (modified) (4 diffs)
- trunk/Phergie/Plugin/Math.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Plugin/Dice.php
r218 r234 47 47 protected function processExpression($expr) 48 48 { 49 // Clean up the expression 50 $expr = str_replace('\\', '/', preg_replace('/\s/', '', $expr)); 51 49 52 // Parse equation 50 53 $out = ''; … … 69 72 } elseif ($substr === ')') { 70 73 $out .= $substr; 71 $next = substr($e xpr, 1, $ptr+1);74 $next = substr($equation, 1, 1); 72 75 if (!empty($next) && !in_array($next, array('+', '-', '/', '*', ')'))) { 73 76 $out .= '*'; … … 99 102 * @return void 100 103 */ 101 protected function processDice($message )104 protected function processDice($message, $recursive = false) 102 105 { 103 106 $message = trim($message); 104 107 if (!empty($message)) { 105 if (preg_match('{^(?:([0-9]+)[\#|:|\s])?(?:([0-9]+)[\s|d])?(?:[\s|d]?([0-9]+))(?:([+-])(.*))?$}ix', $message, $m)) { 106 if (isset($m[1]) && (!isset($m[2]) or empty($m[2]))) { 107 $m[2] = $m[1]; 108 $m[1] = null; 109 } 108 if (preg_match('{^(?:([0-9]+)[\#|:|\s])?(?:([0-9]+)[\s|d])?(?:[\s|d]?([0-9]+))(?:([+\*-])([^\s]*))?(.*)?$}ix', $message, $m)) { 110 109 $numDice = ($m[1] < 1 ? 1 : ($m[1] > $this->max['total'] ? $this->max['total'] : $m[1])); 111 110 $dice = ($m[2] < 1 ? 1 : ($m[2] > $this->max['dice'] ? $this->max['dice'] : $m[2])); 112 $sides = ($m[3] < 1 ? 1 : ($m[ 3] > $this->max['sides'] ? $this->max['sides'] : $m[3]));113 $operator = (isset($m[4]) && $m[4] == '+' ? true : false);111 $sides = ($m[3] < 1 ? 1 : ($m[4] > $this->max['sides'] ? $this->max['sides'] : $m[3])); 112 $operator = trim($m[4]); 114 113 $expression = ((isset($m[5]) && !empty($m[5])) ? trim($m[5]) : '0'); 114 $description = rtrim(trim($m[6]), '+-/*<>%&^|~'); 115 $diceMessage = ($numDice > 1 ? $numDice . '#' : '') . $dice . 'd' . $sides . (!empty($operator) && !empty($expression) ? $operator . $expression : ''); 115 116 116 117 $bonus = 0; 117 118 if (!empty($expression) && $this->allowExpressions) { 119 $expression = preg_replace('/((?:[0-9]+)?d[0-9]+(?:[+\*-][^\s]*)?)/e', '$this->processDice("\\1", true)', $expression); 118 120 $bonus = $this->processExpression($expression); 119 121 if (is_null($bonus)) { 120 return ' Error while processing the dice expression.';122 return ($recursive ? 0 : 'Error while processing the dice expression.'); 121 123 } 122 124 } … … 126 128 $total = 0; 127 129 for ($d = 0; $d < $dice; $d++) { 128 $total += intval(mt_rand(1, $sides) + ($operator ? $bonus : ($bonus * -1))); 130 $total += mt_rand(1, $sides); 131 switch ($operator) { 132 case '+': $total += $bonus; break; 133 case '-': $total -= $bonus; break; 134 case '*': $total *= $bonus; break; 135 } 129 136 } 130 137 $output[] = $total; 131 138 } 132 return 'Dice Results -> ' . implode(', ', $output);139 return ($recursive ? implode('+', $output) : 'Rolls a ' . $diceMessage . (!empty($description) ? ' ' . $description : '') . ' and gets ' . implode(', ', $output) . '.'); 133 140 } 134 141 } trunk/Phergie/Plugin/Math.php
r232 r234 32 32 'cos(', 'sin(', 'sqrt(', 'tan(', 'rad2deg(', 33 33 'acosh(', 'asin(', 'atan(', 'atanh(', 'cosh(', 34 'sinh(', 'tanh(', 'hexdec(', 'decoct(', 'dechex(', 35 'decbin(', 'bindec(', 'ord(', 'chr(', 34 'sinh(', 'tanh(', 36 35 'M_PI', 'INF', 'M_E', 'M_LOG2E', 'M_LOG10E', 37 36 'M_LN2', 'M_LN10', 'M_PI_2', 'M_PI_4', 'M_1_PI', … … 58 57 'round(', 'log(', 'pow(', 59 58 'max(', 'min(', 'rand(', 60 'atan2(', 'mt_rand(', 61 'base_convert(' 59 'atan2(', 'mt_rand(' 62 60 ); 63 61 /** … … 109 107 // Replace constants 110 108 $equation = str_ireplace( 111 array('pi', 'M_PI()', 'chucknorris', 'inf', ' e ' ),112 array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E ' ),109 array('pi', 'M_PI()', 'chucknorris', 'inf', ' e ', '\\'), 110 array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E ', '/'), 113 111 $expr 114 112 ); 115 113 $equationSrc = $equation; 114 $equation = preg_replace('/\s/', '', $equation); 116 115 $this->allowed = array_merge($this->allowed, $this->classFuncs); 117 116 … … 158 157 159 158 $out .= $substr; 160 $next = substr($equation, 1, $ptr+1);159 $next = substr($equation, 1, 1); 161 160 if (!empty($next) && !in_array($next, array('+', '-', '/', '*', ')'))) { 162 161 $out .= '*'; … … 183 182 if ($res === false) { 184 183 if (!$quietMode) { 185 $this->doNotice($user, 'Computation error, division by zero?');184 $this->doNotice($user, 'Computation error, nothing was returned, perhaps division by zero?'); 186 185 } 187 186 } else {