Location: PHPKode > projects > Kbot > kbot_1.3/rconBots/ns_rconpugbot.php
<?PHP
/**
 *
 * $Id: ns_rconpugbot.php 74 2005-12-31 03:38:16Z khaless $
 * $Revision: 74 $
 * $Author: khaless $
 * $Date: 2005-12-31 14:38:16 +1100 (Sat, 31 Dec 2005) $
 * 
 * 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
 *
 */
include('./rconBots/includes/common.inc.php'); 
include('./rconBots/includes/logParser.class.php');
include('./rconBots/includes/defines.inc.php');
include('./rconBots/includes/nsRconBot.class.php');
include('./rconBots/includes/logComms.class.php');
// our Rcon Class
require_once "./includes/rcon_hl_net.class.php";

// wrapping this into a single function just to make sure we dont loose rcon
// sure we could do it as a single open connection but, eh this works
// and is more reliable... more overhead tho :(
// but eh, loosing nit has happened...

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

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

	function rconCommand($command)
	{
		$server = new rcon();
		$server->Connect($this->dataArray['ip'], $this->dataArray['port'], $this->dataArray['rconPassword']);
		$return = $server->rconCommand($command);
		$server->Disconnect();
		unset($server);
		return $return;
	}
}


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

// register our handlers...
// use ENTER_GAME not CONNECTION because it has STEAM ID
$logParser->registerHandler(CONNECTION, $nsRconBot, 'connection1');
$logParser->registerHandler(ENTER_GAME, $nsRconBot, 'connection2');
$logParser->registerHandler(DISCONNECTION, $nsRconBot, 'disconnection');
$logParser->registerHandler(ROLE_SELECTION, $nsRconBot, 'roleSelection');
$logParser->registerHandler(KILLS, $nsRconBot, 'kill');
$logParser->registerHandler(INJURING, $nsRconBot, 'damageCount');
$logParser->registerHandler(CHANGE_NAME, $nsRconBot, 'changeName');
//$logParser->registerHandler(PLAYER_ACTION, &$nsRconBot, 'playerAction');
$logParser->registerHandler(SAY, $nsRconBot, 'say');
$logParser->registerHandler(TEAM_SAY, $nsRconBot, 'say');

// for end round...
$logParser->registerHandler(NS_ROUND_END, $nsRconBot, 'roundEnd');

// if you want to use the $testData...
//foreach($testData as $line)
//{
//	$logParser->useHandler($line);
//}

// next is out quick kicky trick to actually find out if we have rcon, we ask the serve to send
// 'isConnected' back to us, and then we test for it :o
if($coreFuncs->rconCommand('echo isConnected') == 'isConnected')
{
	// 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->bind($dataArray['ip'], $dataArray['port']);
	$coreFuncs->rconCommand('logaddress_add '.$dataArray['botIP'].' '.$logCom->boundPort);

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

	// ok setup needed vars
	$coreFuncs->rconCommand('mp_tournamentmode 1');
	$coreFuncs->rconCommand('mp_friendlyfire 1');

	sleep(2);

	// ok setup needed vars
	$coreFuncs->rconCommand('mp_tournamentmode 1');
	$coreFuncs->rconCommand('mp_friendlyfire 1');

	// ok, server is setup... lets enter our servicing loop.
	while(1)
	{
		// data from the server log stream... one line at a time :o
		// pass this directly into the Handler Handler  :)
		if($os == 'WIN') {
			// if we have windows.. poll through ALL data in buffer befire checking the stdin.
			while($data = $logCom->read()) {
				$logParser->useHandler($data);
			}
		}
		else {
			$logParser->useHandler($logCom->read());
		}

		// 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($os == 'WIN' && $incomingData == "SUGGEST\r\n")
		{
			fwrite(STDOUT,"BREAK\r\n");
		}
		elseif($incomingData == "KILLNOW\n")
		{
			// delete this log address
			$coreFuncs->rconCommand('logaddress_del '.$dataArray['botIP'].' '.$logCom->boundPort);
			$coreFuncs->rconCommand("map ".$dataArray['map']);
			sleep(3);
			$coreFuncs->rconCommand("sv_password \"\"");
			// clean up
			$logCom->close();
			die();
			//
		}
		elseif(substr($incomingData,0,8) == "NEWADMIN")
		{
			// ok put in place the new admin shit :o
			$nsRconBot->matchAdminSteamID = NULL;
			$nsRconBot->dataArray['adminPassword'] = substr($incomingData,9,7);
			$coreFuncs->rconCommand('say PUG has been overrided by an admin');
		}
		elseif($incomingData == "RECHALLENGE\n")
		{
			$coreFuncs->rconCommand('logaddress_add '.$dataArray['botIP'].' '.$logCom->boundPort);
			//
		}
		elseif(substr($incomingData,0,4) == "CHAT")
		{
			$coreFuncs->rconCommand('say Message from IRC OP:');
			$coreFuncs->rconCommand('say '.substr($incomingData,5));
		}

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

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