• Forum
  • Doc
  • Screenshots
  • Download
  • Donate
  • Contributors
  • Contact
  • Follow @phpfreechat
  • DEMO
  • Board index ‹ Version 1.x branch ‹ Contributions (v1.x)
  • Change font size
  • FAQ
  • Register
  • Login

Random command

Post a bug fix, a new feature, a theme ...

Moderators: OldWolf, re*s.t.a.r.s.*2

Post a reply
3 posts • Page 1 of 1

Postby Hap » Tue Jun 06, 2006 11:17 pm

This will give a /random command (somewhat similar to the /roll command).

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.
Last edited by Hap on Wed Jun 07, 2006 11:10 pm, edited 1 time in total.
Hap
New member
 
Posts: 8
Joined: Tue May 30, 2006 12:09 am
Top

Postby phpfreechat » Wed Jun 07, 2006 9:03 am

Your class is not integrated in a phpfreechat command,
you should explain how to integrate to pfc your Random class or your code is useless here...
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby Hap » Wed Jun 07, 2006 11:04 pm

I knew I was missing something. I edited my first post.
Hap
New member
 
Posts: 8
Joined: Tue May 30, 2006 12:09 am
Top


Post a reply
3 posts • Page 1 of 1

Return to Contributions (v1.x)

Who is online

Users browsing this forum: No registered users and 19 guests

  • Board index
  • The team • Delete all board cookies • All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
Sign in
Wrong credentials
Sign up I forgot my password
.
jeu-gratuit.net | more partners
Fork me on GitHub