Location: PHPKode > projects > Kbot > kbot_1.3/rconBots/wsw_rconpugbot.php
<?PHP
/**
 *
 * $Id: wsw_rconpugbot.php 117 2006-09-30 07:37:15Z khaless $
 * $Revision: 117 $
 * $Author: khaless $
 * $Date: 2006-09-30 17:37:15 +1000 (Sat, 30 Sep 2006) $
 * 
 * Copyright (c) 2005 Mathew Rodley <hide@address.com>
 * 
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

define('KBOT_DEBUG', 1);

include('./rconBots/includes/common.inc.php');
include('./rconBots/includes/logParser.class.php');
include('./rconBots/includes/wsw_defines.inc.php');
include('./rconBots/includes/wswRconBot.class.php');
include('./rconBots/includes/logComms.class.php');

// our Rcon Class
require_once ('./rconBots/includes/q3rcon.class.php');

// send back to IRC.
class coreFuncWrapper
{
	function coreFuncWrapper ($dataArray)
	{
		$this->dataArray = &$dataArray;

		// connect to rcon...
		$this->server = new q3query($this->dataArray['ip'], (int)$this->dataArray['port']);
		$this->server->set_rconpassword($this->dataArray['rconPassword']);
	}

	function sendToIRC($message)
	{
		fwrite(STDOUT,serialize(array('targets' => $this->dataArray['msgTargetChannels'], 'data' => $message))."\n");
	}

	function rconCommand($command)
	{
		$this->server->rcon($command);
		return $this->server->get_response();
	}
}

// instantiate out classes
$coreFuncs = new coreFuncWrapper($dataArray);
$wswRconBot = new wswRconBot($dataArray, $coreFuncs);
$logParser = new logParser;

// register our handlers...
// use ENTER_GAME not CONNECTION because it has STEAM ID
//$logParser->registerHandler(CONECTION, $wswRconBot, 'connection1');
$logParser->registerHandler(CONNECTION, $wswRconBot, 'connection');
$logParser->registerHandler(ANY, $wswRconBot, 'any');

if($coreFuncs->rconCommand('echo isConnected') == "print\nisConnected \n")
{
	// ok we want to start our 'log server' here, we will open it on 1 of 10 ports... 40000 to 40010
	$logCom = new logComms;
	$logCom->setSpecificPort(40000);
	$logCom->incomingPortComparison(true);
	if(!$logCom->bind($dataArray['ip'], $dataArray['port'])) {
		$coreFuncs->sendToIRC('Error: Could not bind to socket.');
		$coreFuncs->sendToIRC('ERR:2');
	}

	// wsw does not have a logaddress command through UDP, so for now, setup the server
	// to continually stream logs with nc so, server should be
	// nc -u <address> <port>

	// ok, were in... lets setup the server
	$coreFuncs->rconCommand('exec cfgs/pug_tdm.cfg');
	$coreFuncs->rconCommand('map "'.$dataArray['map'].'"');
	sleep(3);
	$coreFuncs->rconCommand('password '.$dataArray['sv_password']);

	$wswRconBot->init();

	// ok, server is setup... lets enter our servicing loop.
	while(1)
	{
		// maintinence routine
		$wswRconBot->maintinence();

		// data from the server log stream... one line at a time :o
		// pass this directly into the Handler Handler  :)
		if(KBOT_WINDOWSOS) {
			// if we have windows.. poll through ALL data in buffer befire checking the stdin.
			while($data = $logCom->read()) {
				// I dont think this will be valid for win32
				$logParser->useHandler($data);
			}
		}
		else {
			// wsw log script sends in chunks, so we need to analyse each line
			$bucket = $logCom->read();
			if($bucket) {
				$chunks = explode("\n", $bucket);

				foreach($chunks as $chunk) {
					if($chunk != '') {
						$logParser->useHandler($chunk);
					}
				}
			}
		}
		// data from the STDIN from the parent.. (this could be messages like kill or timeis runnign out, or some random
		// thing ....
		$incomingData = fgets(STDIN, 1024);
		// do what ever with incomingData...
		// this will be FROM the IRC bot. :)

		if(KBOT_WINDOWSOS && $incomingData == "SUGGEST\r\n")
		{
			fwrite(STDOUT,"BREAK\r\n");
		}
		elseif($incomingData == "KILLNOW\n")
		{
			$wswRconBot->cleanUp();
			// delete this log address
			$coreFuncs->rconCommand('map "'.$dataArray['map'].'"');
			sleep(3);
			// clean up
			$logCom->close();
			die();
			//

		}
		elseif(substr($incomingData,0,8) == "NEWADMIN")
		{
			// ok put in place the new admin shit :o
			$wswRconBot->matchAdminSteamID = NULL;
			$wswRconBot->dataArray['adminPassword'] = substr($incomingData,9,7);
			$coreFuncs->rconCommand('say "PUG has been overrided by an admin"');

		}

		/*elseif($incomingData == "RECHALLENGE\n")
		{

		}*/

		elseif(substr($incomingData,0,4) == "CHAT")
		{
			$coreFuncs->rconCommand('say "Message from IRC:"');
			$coreFuncs->rconCommand('say '.substr($incomingData,5));
		}
		/*elseif(substr($incomingData, 0, 12) == "RENAMEPLAYER") {
		$array = unserialize(substr($incomingData, 13));
		$wswRconBot->authChangeNick($array['to'], $array['from']);
		}
		elseif(substr($incomingData, 0, 9) == "NEWPLAYER") {
		$array = unserialize(substr($incomingData, 10));
		$player = $array;
		unset($player[0]);
		if($array[0] == 1) {
		$wswRconBot->addPendingAuthPlayer($player);
		}
		else {
		$wswRconBot->addAuthedPlayer($player);
		}
		}*/

		// tell the bot to start the end pug process, which in return will tell this process to end the pug :P
		if(time() - $wswRconBot->countTillEnd >= 10 && $wswRconBot->countTillEnd != 0 && !$wswRconBot->endingProcess)
		{
			$wswRconBot->cleanUp();
			$wswRconBot->endingProcess = TRUE;
			$coreFuncs->rconCommand('say "PUG Closing Down..."');
			$coreFuncs->sendToIRC('CMD:ENDPUG');
		}

		// lay it to bed for a bit :)
		usleep(2500);
	}
}
else {
	// handle an error here, this means we could not connect to the server.. :(
	$coreFuncs->sendToIRC('ERR:1');
	fwrite(STDOUT,"BREAK\r\n");
	while(1) {
		$incomingData = fgets(STDIN, 1024);
		if($incomingData == "KILLNOW\n") die();
		sleep(1);
	}
}
?>
Return current item: Kbot