It will take the following commands:
/random
Will generate a number between 1 and 100.
/random 250
Will generate a number between 1 and 250.
/random 100 250
Will generate a number between 100 and 250.
Create a file called random.class.php and save the following in your chat/src/ directory. Copy the following into the file.
- Code: Select all
class Random {
var $command;
function check($text){
$this->errors = array();
$this->command= '';
$pieces = explode(' ',$text);
if(count($pieces)==1 && $pieces[0]!='') {
$this->command['min'] = 1;
$this->command['max'] = (int) $pieces[0];
}
else if(count($pieces)==2) {
$this->command['min'] = (int) $pieces[0];
$this->command['max'] = (int) $pieces[1];
}
else {
$this->command['min'] = 1;
$this->command['max'] = 100;
}
if(!is_numeric($this->command['min']) {
$this->command['min'] = 1;
}
if(!is_numeric($this->command['max']) {
$this->command['max'] = 100;
}
srand((double)microtime()*1000000);
return true;
}
function roll(){
$result = '<span style="background: #000000; color: gold;">Random ('.$this->command['min'].'-'.$this->command['max'].') » ' ;
$num = rand($this->command['min'], $this->command['max']);
return $result . '<strong>' . $num . '</strong></span>';
}
function error_get(){
if(!count($this->errors)){
return '';
} else {
return join("<br />n", $this->errors);
}
}
}
On the page that calls the chat, before $chat->printChat(); copy:
- Code: Select all
class pfcCommand_random extends pfcCommand
{
function run(&$xml_reponse, $clientid, $msg)
{
$c =& $this->c;
$nick = $c->nick;
$container =& $c->getContainerInstance();
$text = trim($msg);
// Call parse roll
require_once 'chat/src/random.class.php'; // EDIT THIS LINE TO SUIT YOUR SETUP
$random = new Random();
if (!$random->check($text))
{
$result = $random->error_get();
$cmd =& pfcCommand::Factory("error", $c);
$cmd->run($xml_reponse, $clientid, "Cmd_random failed: " . $result);
}
else
{
$result = $random->roll();
$container->writeMsg($nick, $result);
}
if ($c->debug) pxlog("Cmd_random[".$c->sessionid."]: msg=".$result, "chat", $c->getId());
}
}
Be sure to edit:
require_once 'chat/src/random.class.php';
to match the path to class file.