Assembla home | Assembla project page
 

Changeset 234

Show
Ignore:
Timestamp:
04/14/08 02:47:33 (7 months ago)
Author:
Slynderdale
Message:

Fixed a couple bugs in Math and Dice as well as tweaking the dice reponse and add recursive checking of the dice expressions.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Dice.php

    r218 r234  
    4747    protected function processExpression($expr) 
    4848    { 
     49        // Clean up the expression 
     50        $expr = str_replace('\\', '/', preg_replace('/\s/', '', $expr)); 
     51 
    4952        // Parse equation 
    5053        $out = ''; 
     
    6972            } elseif ($substr === ')') { 
    7073                $out .= $substr; 
    71                 $next = substr($expr, 1, $ptr+1); 
     74                $next = substr($equation, 1, 1); 
    7275                if (!empty($next) && !in_array($next, array('+', '-', '/', '*', ')'))) { 
    7376                    $out .= '*'; 
     
    99102     * @return void 
    100103     */ 
    101     protected function processDice($message
     104    protected function processDice($message, $recursive = false
    102105    { 
    103106        $message = trim($message); 
    104107        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)) { 
    110109                $numDice = ($m[1] < 1 ? 1 : ($m[1] > $this->max['total'] ? $this->max['total'] : $m[1])); 
    111110                $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]); 
    114113                $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 : ''); 
    115116 
    116117                $bonus = 0; 
    117118                if (!empty($expression) && $this->allowExpressions) { 
     119                    $expression = preg_replace('/((?:[0-9]+)?d[0-9]+(?:[+\*-][^\s]*)?)/e', '$this->processDice("\\1", true)', $expression); 
    118120                    $bonus = $this->processExpression($expression); 
    119121                    if (is_null($bonus)) { 
    120                         return ' Error while processing the dice expression.'
     122                        return ($recursive ? 0 : 'Error while processing the dice expression.')
    121123                    } 
    122124                } 
     
    126128                    $total = 0; 
    127129                    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                        } 
    129136                    } 
    130137                    $output[] = $total; 
    131138                } 
    132                 return 'Dice Results -> ' . implode(', ', $output); 
     139                return ($recursive ? implode('+', $output) : 'Rolls a ' . $diceMessage . (!empty($description) ? ' ' . $description : '') . ' and gets ' . implode(', ', $output) . '.'); 
    133140            } 
    134141        } 
  • trunk/Phergie/Plugin/Math.php

    r232 r234  
    3232        'cos(', 'sin(', 'sqrt(', 'tan(', 'rad2deg(', 
    3333        'acosh(', 'asin(', 'atan(', 'atanh(', 'cosh(', 
    34         'sinh(', 'tanh(', 'hexdec(', 'decoct(', 'dechex(', 
    35         'decbin(', 'bindec(', 'ord(', 'chr(', 
     34        'sinh(', 'tanh(', 
    3635        'M_PI', 'INF', 'M_E', 'M_LOG2E', 'M_LOG10E', 
    3736        'M_LN2', 'M_LN10', 'M_PI_2', 'M_PI_4', 'M_1_PI', 
     
    5857        'round(', 'log(', 'pow(', 
    5958        'max(', 'min(', 'rand(', 
    60         'atan2(', 'mt_rand(', 
    61         'base_convert(' 
     59        'atan2(', 'mt_rand(' 
    6260    ); 
    6361    /** 
     
    109107        // Replace constants 
    110108        $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 ', '/'), 
    113111            $expr 
    114112        ); 
    115113        $equationSrc = $equation; 
     114        $equation = preg_replace('/\s/', '', $equation); 
    116115        $this->allowed = array_merge($this->allowed, $this->classFuncs); 
    117116 
     
    158157 
    159158                $out .= $substr; 
    160                 $next = substr($equation, 1, $ptr+1); 
     159                $next = substr($equation, 1, 1); 
    161160                if (!empty($next) && !in_array($next, array('+', '-', '/', '*', ')'))) { 
    162161                    $out .= '*'; 
     
    183182        if ($res === false) { 
    184183            if (!$quietMode) { 
    185                 $this->doNotice($user, 'Computation error, division by zero?'); 
     184                $this->doNotice($user, 'Computation error, nothing was returned, perhaps division by zero?'); 
    186185            } 
    187186        } else {