King of the Hill (IRC game)

By Gforce20 on May 28, 2009

This script uses the PHP IRC framework found here: http://www.hawkee.com/snippet/5658/

This is a simple PHP script that will connect to an IRC server and channel, and will allow users to "roll" a die that determines what action will be taken. The actions include kicks, temporary bans, and receiving +v, +h, or +o. Kicks, bans, and +o are less likely to be rolled than the other choices.

There are some variables you will need to change. Most of them are at the bottom of the script; also change the words "yourchannelhere" and "yourpasswordhere" to the appropriate values (use Ctrl+F in notepad or any text editor). The channel name ("yourchannelhere") does not require a hash ("#") before the name; it's automatically added.

If you run this script, it would be wise to register your bot's nickname with NickServ and either register a separate channel for it to use, or use a channel that you founded. It would also be wise to use "/msg ChanServ SOP #yourchannelhere ADD botnicknamehere" so that your bot will not be kicked by players or have difficulties changing user modes.

If you find any bugs or needs for improvement, send me a comment. Enjoy! :)

<?php
class Client {
  var $server;
  function server($dstaddr=NULL) {
   if (!$dstaddr) {
     return $this->server;
   }
   $this->server = $dstaddr;
  }
  var $port;
  function port($dstport=NULL) {
   if (!$dstport) {
     return $this->port;
   }
   $this->port = $dstport;
  }
  var $nick;
  function nick($alias=NULL) {
   if (!$alias) {
     return $this->nick;
   }
   $this->nick = $alias;
  }
  var $ident;
  function ident($user=NULL) {
   if (!$user) {
     return $this->ident;
   }
   $this->ident = $user;
  }
  var $gecos;
  function gecos($name=NULL) {
   if (!$name) {
     return $this->gecos;
   }
   $this->gecos = $name;
  }
  var $fd;
  var $BUFFER_SIZE = 512;
  function run() {
   $canonical = gethostbyname($this->server());
   if ($canonical == $this->server()) {
     return;
   }
   $this->fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   if (!$this->fd) {
     return;
   }
   $err = socket_connect($this->fd, $this->server(), $this->port());
   if (!$err) {
     return;
   }
   $this->channel = "yourchannelhere";
   socket_write($this->fd, "NICK " . $this->nick() . "\r\n");
   socket_write($this->fd, "USER " . $this->ident() . " \"\" \"\" :" . $this->gecos() . "\r\n");
   socket_write($this->fd, "PRIVMSG NickServ :IDENTIFY yourpasswordhere\r\n");
   socket_write($this->fd, "JOIN #" . $this->channel . "\r\n");
   while (($in = socket_read($this->fd, $this->BUFFER_SIZE))) {
     if (preg_match("/^PING (:[^ ]+)$/i", $in, $regex)) {
       socket_write($this->fd, "PONG " . $regex[0] . "\r\n");
     }
     elseif (stripos($in, 'PRIVMSG') && stripos($in, "#". $this->channel)) {
      $userMsg = substr($in, stripos($in, ':', stripos($in, '#')) + 1);
      if (substr($userMsg, 0, 1) == "!") {
        $userName = substr($in, 1, stripos($in, "!") - 1);
        $userCmd = trim(substr($userMsg, 1, (stripos($userMsg, ' ') ? stripos($userMsg, ' ') : strlen($userMsg))));
        $userArgs = explode(' ', trim(substr($userMsg, (stripos($userMsg, ' ') ? stripos($userMsg, ' ') : strlen($userMsg)))));

        if ($userCmd == 'roll') {
          $rand = rand(1,6);
          if ($rand == 1) {
            if (rand(1,2) == 1) {
              socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 1 (Kickban for 5 seconds).\r\n");
              socket_write($this->fd, "MODE #" . $this->channel . " +b " . $userName . "!*@*\r\n");
              socket_write($this->fd, "KICK #" . $this->channel . " " . $userName . "\r\n");
              sleep(5);
              socket_write($this->fd, "MODE #" . $this->channel . " -b " . $userName . "!*@*\r\n");
            }
            else {
              $rand = rand(2,6);
            }
          }
          if ($rand == 2) {
            if (rand(1,2) == 1) {
              socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 2 (Kick).\r\n");
              socket_write($this->fd, "KICK #" . $this->channel . " " . $userName . "\r\n");
            }
            else {
              $rand = rand(3,6);
            }
          }
          if ($rand == 6) {
            if (rand(1,2) == 1) {
              socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 6 (Op).\r\n");
              socket_write($this->fd, "MODE #" . $this->channel . " +o " . $userName . "\r\n");
              socket_write($this->fd, "MODE #" . $this->channel . " -v " . $userName . "\r\n");
              socket_write($this->fd, "MODE #" . $this->channel . " -h " . $userName . "\r\n");
            }
            else {
              $rand = rand(3,5);
            }
          }
          if ($rand == 3) {
            socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 3 (Lose all special modes).\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -v " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -h " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -o " . $userName . "\r\n");
          }
          if ($rand == 4) {
            socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 4 (Voice).\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " +v " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -h " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -o " . $userName . "\r\n");
          }
          if ($rand == 5) {
            socket_write($this->fd, "PRIVMSG #" . $this->channel . " :" . $userName . ": you rolled a 5 (Half-op).\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " +h " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -v " . $userName . "\r\n");
            socket_write($this->fd, "MODE #" . $this->channel . " -o " . $userName . "\r\n");
          }
        }

      }
     }
     elseif (stripos($in, 'MODE #' . $this->channel . ' +b')) {
       socket_write($this->fd, str_replace(" +b ", " -b ", substr(trim($in, "\r\n"), stripos($in, "MODE"))) . "\r\n");
     }
   }
  }
}

$client = new Client();
$client->server("your.server.here");
$client->port(6667);
$client->nick("KingBot");
$client->ident("kingbot");
$client->gecos("kingbot");
$client->run();
?>

Comments

Sign in to comment.
Jonesy44   -  May 31, 2009

Nice one, i'll check this out in a bit ;D

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.