<?PHP
/**
*
* $Id: css_rconpugbot.php 128 2006-12-28 08:07:29Z khaless $
* $Revision: 128 $
* $Author: khaless $
* $Date: 2006-12-28 19:07:29 +1100 (Thu, 28 Dec 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
*
*/
include('./rconBots/includes/common.inc.php');
include('./rconBots/includes/logParser.class.php');
include('./rconBots/includes/css_defines.inc.php');
include('./rconBots/includes/cssRconBot.class.php');
include('./rconBots/includes/logComms.class.php');
// our Rcon Class
require_once ('./rconBots/includes/sourceRcon.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;
// connect to rcon...
$this->server = new sourceRcon($this->dataArray['ip'], $this->dataArray['port'], $this->dataArray['rconPassword']);
$this->server->connect();
}
function sendToIRC($message)
{
fwrite(STDOUT,serialize(array('targets' => $this->dataArray['msgTargetChannels'], 'data' => $message))."\n");
}
function rconCommand($command)
{
return $this->server->rconCommand($command);
}
}
// instantiate out classes
$coreFuncs = new coreFuncWrapper($dataArray);
$cssRconBot = new cssRconBot($dataArray, $coreFuncs);
$logParser = new logParser;
// register our handlers...
// use ENTER_GAME not CONNECTION because it has STEAM ID
$logParser->registerHandler(CONECTION, $cssRconBot, 'connection1');
$logParser->registerHandler(ENTER_GAME, $cssRconBot, 'connection2');
$logParser->registerHandler(DISCONNECTION, $cssRconBot, 'disconnection');
$logParser->registerHandler(TEAM_SELECTION, $cssRconBot, 'teamSelection');
$logParser->registerHandler(KILLS, $cssRconBot, 'kill');
$logParser->registerHandler(PLAYER_ACTION, $cssRconBot, 'playerAction');
$logParser->registerHandler(SAY, $cssRconBot, 'say');
$logParser->registerHandler(TEAM_SAY, $cssRconBot, 'say');
$logParser->registerHandler(INJURING, $cssRconBot, 'damageCount');
$logParser->registerHandler(CHANGE_NAME, $cssRconBot, 'changeName');
// for the round endings.. WORLD will trigger a draw.. so we must include this...
$logParser->registerHandler(TEAM_WIN, $cssRconBot, 'roundEnd');
$logParser->registerHandler(WORLD_WIN, $cssRconBot, 'roundEnd');
// if we have kbot auth active..
$logParser->registerHandler(KBOT_AUTH_VALID_AUTH_CODE, $cssRconBot, 'kbotAuthValidCode');
$logParser->registerHandler(KBOT_AUTH_VALID_AUTH_STEAMID, $cssRconBot, 'kbotAuthValidSteamID');
if(stristr($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('map '.$dataArray['map']);
sleep(3);
$coreFuncs->rconCommand('sv_password '.$dataArray['sv_password']);
$coreFuncs->rconCommand('mp_logdetail 3');
$cssRconBot->init();
// ok, server is setup... lets enter our servicing loop.
while(1)
{
// maintinence routine
$cssRconBot->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()) {
$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(KBOT_WINDOWSOS && $incomingData == "SUGGEST\r\n")
{
fwrite(STDOUT,"BREAK\r\n");
}
elseif($incomingData == "KILLNOW\n")
{
$cssRconBot->cleanUp();
// delete this log address
$coreFuncs->rconCommand('logaddress_del '.$dataArray['botIP'].':'.$logCom->boundPort);
$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
$cssRconBot->matchAdminSteamID = NULL;
$cssRconBot->dataArray['adminPassword'] = substr($incomingData,9,7);
$coreFuncs->rconCommand('say "PUG has been overrided by an admin"');
}
elseif($incomingData == "RECHALLENGE\n")
{
$coreFuncs->rconCommand('log_addaddress "'.$dataArray['botIP'].':'.$logCom->boundPort.'"');
}
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));
$cssRconBot->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) {
$cssRconBot->addPendingAuthPlayer($player);
}
else {
$cssRconBot->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() - $cssRconBot->countTillEnd >= 10 && $cssRconBot->countTillEnd != 0 && !$cssRconBot->endingProcess)
{
$cssRconBot->cleanUp();
$cssRconBot->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);
}
}
?>