<?php
require_once "fields.php";
/**
* set this true, is lowlvl output is wanted
*/
$headPrint = false;
$dataPrint = false;
class OgameControl extends Fields
{
/**
* The function takes a 'coords' for the offset, moves 'threshold'
* systems towards L/R of a planet
*
* @param coords - array[3] of coordinates in ogame
* @param threshold - nSystems to scan in galaxytool from base planet
*
* @returns a choord-array with a planet, suitable for fleeing to
*/
function findSuitable($coords, $threshold = 70)
{
mysql_connect("localhost","gtool_admin","*************") or die(mysql_error());
mysql_select_db("gtoolz") or die(mysql_error());
$lower = $coords[1] - $threshold;
if($lower < 1) $lower = 0;
$upper = $coords[1] + $threshold;
if($upper > 498) $upper = 499;
$avail_tgt = array(array(), array(), array());
$query="SELECT g.player_id,g.galaxy,g.system,g.planet,g.moon,".
"p.noob,p.banned,p.vacation,p.inactive ".
"FROM gtoolz g LEFT JOIN players p ON (g.player_id=p.id) ".
"WHERE g.galaxy='$coords[0]' ".
"AND (g.system BETWEEN '$lower' AND '$upper') ".
"AND (inactive='true') AND (noob='false') ".
"AND (vacation='false') ".
"AND (banned='false') AND (g.player_id > 0) ".
"ORDER BY galaxy,system,planet ASC LIMIT 0,200";
$res = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_object($res)) {
if ($line->noob == "true") continue;
if ($line->vacation == "true") continue;
if ($line->banned == "true") continue;
if ($line->inactive == "true")
{
array_push($avail_tgt[0], $line->galaxy);
array_push($avail_tgt[1], $line->system);
array_push($avail_tgt[2], $line->planet);
}
}
mysql_close();
$center = floor(count($avail_tgt[0])/2);
// TODO: validate further?
return (array($avail_tgt[0][$center], $avail_tgt[1][$center], $avail_tgt[2][$center]));
}
/**
*
* Sets up (in ctor chain or on demand) farms, collecting data from
* previously uploaded espionage reports from a galaxytool database
*
*/
function scanForFarms($coords, $threshold = 55, $metal = 10, $krystal = 10, $deut = 3)
{
while(array_pop($this->farmPlanets['COORDS'])) ;
while(array_pop($this->farmPlanets['SS'])) ;
while(array_pop($this->farmPlanets['SCANNED'])) ;
while(array_pop($this->farmPlanets['RETURNS'])) ;
$this->nFarms = 0;
mysql_connect("localhost","gtool_admin","gt_adminz&sv.") or die(mysql_error());
mysql_select_db("gtoolz") or die(mysql_error());
$galaxy = $coords[0];
//////////////////////////////////////////////
$syslower = ($coords[1] - $threshold);
$sysupper = ($coords[1] + $threshold);
//////////////////////////////////////////////
$query = "SELECT r.galaxy, r.system, r.planet, kt, gt, lj, sj, krz, ss, kolo, rec, spio, bomb, zerri, ds, skrz, sat, rak, ll, sl, ion, gauss, plasma, ksk, gsk, p.noob,p.banned,p.vacation,p.inactive,p.long_inactive, r.memi, r.krimi, r.moon, r.metal, r.crystal, g.player_id, r.scantime FROM reports r LEFT JOIN gtoolz g ON (r.galaxy = g.galaxy AND r.system = g.system AND r.planet = g.planet) LEFT JOIN players p ON (g.player_id = p.id) ";
// Reports within threshold range
$query .= "WHERE 1 AND r.galaxy='".$galaxy."' AND (r.system BETWEEN ".$syslower." AND ".$sysupper.") AND inactive='true' AND vacation='false' AND banned='false' ";
// No reports including fleet
$query .= "AND r.kt<'1' AND r.gt<'1' AND r.lj<'1' AND r.sj<'1' AND r.krz<'1' AND r.ss<'1' ".
"AND r.kolo<'1' AND r.rec<'1' AND r.spio<'1' AND r.bomb<'1' AND r.zerri<'1' ".
"AND r.ds<'1' AND r.skrz<'1' AND r.sat<'1' ";
// Specifically low defence reports, categorized below
$query .= "AND r.rak<'3' ".
"AND r.ll<'3' ".
"AND r.sl<'4' ".
"AND r.ion<'2' ".
"AND r.gauss<'3' ".
"AND r.plasma<'2' AND r.ksk<'1' AND r.gsk<'1' ";
// Secure enough resources, the totally lost 'good' mines are sorted out below
$query .= "AND r.memi>='".$metal."' AND r.krimi>='".$krystal."' ".
"AND r.deutsyn>='".$deut."' ORDER BY r.galaxy, r.system, r.planet";
echo $query."<br />";
$res = mysql_query($query) or die (mysql_error());
while($line = mysql_fetch_object($res))
{
if($line->memi > $metal || $line->krimi > $krystal)
{
if($line->metal > 25000 || $line->crystal > 10000) {
if($line->plasma < 1 && $line->gauss < 1 && $line->sl < 1 && $line->ion < 1 && $line->rk < 4) {
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet),0);
echo "Appended farm @ $galaxy:$line->system:$line->planet<br>";
}
/*
if($line->plasma > 0 && $line->gauss > 0)
{
echo "Category 10 -> ";
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet),10);
}
if($line->gauss > 0)
{
echo "Category 8 - >";
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet), 8);
}
else if($line->sl > 1 || $line->ion > 1 || $line->rk > 3)
{
echo "Category 2 -> ";
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet), 2);
} else if($line->ll > 1 && $line->rk > 1)
{
echo "Category 2 -> ";
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet), 2);
} else if($line->plasma < 1 && $line->gauss < 1 && $line->sl < 1)
{
echo "Category 0 -> ";
$this->printDefence($line);
$this->setFarm(array( $galaxy, $line->system, $line->planet));
}
*/
}
}
} // WHILE result
mysql_close();
echo "Farms now available : ".$this->nFarms."<br>";
$this->farmTrigger = 0;
}
/**
* prints out an mysql-object, item 0 through 24
*/
function printDefence($line)
{
$i = 0;
foreach($line as $key=>$value)
{
if($i > 16 && $i<(16+8))
print("Ident: $key > $value |");
$i++;
}
}
/**
* Utilizes the form from fields.php, setting the login-name an -password
* When logged in, cookies are parsed from the response and session ids set
*/
function login()
{
/* Send the POST for login
* null as begin, record from responseheader - end is line with sessionid
*/
$this->setHeader('login');
// socket output:
if($headPrint)
$this->outputHeader($this->header);
/* Send login request */
$this->request(null, "lgn", true);
/* Parse the received data for sessionid */
foreach($this->content as $line) {
if (preg_match("/session=/i", $line))
$this->setSession($line);
}
if($dataPrint)
$this.outputContents($this->content);
}
/**
* Sets headerfields, targeting the overview ingame page
* (front-page with overview)
* attackStar is poopped and reset, identifying attacking fleettimes(star)
*/
function overview()
{
for($i = 0; $i < count($this->attackStar); $i++)
{
array_pop($this->attackStar['CP']);
array_pop($this->attackStar['STAR']);
}
$this->setHeader('overview');
$this->request("HEADER", "END CONTENT", true);
if($headPrint)
$this->outputHeader();
if($dataPrint)
$this->outputContents($this->content);
$buff = array();
$i = 0;
/* CP from haha_this */
$search0 = "messageziel";
// Synchronization issues
$search1 = "Servertid";
// Tokens to look for @ attack
$search2 = "En fjendtlig";
$pattern1 = "nærmer sig Planeten";
$pattern2 = "[0-9]*:[0-9]*:[0-9]*";
$head_parse = false;
$cp_event_delimiter = "Begivenheder";
foreach($this->content as $line)
{
switch($head_parse) {
case false:
/* Syncronize with servertime */
if( preg_match("/$search1/i", $line) ) {
$dato = date_parse($this->content[$i+1]. " " .date("Y"));
$this->servertime = mktime ($dato['hour'], $dato['minute'], $dato['second'], $dato['month'], $dato['day']);
$head_parse=true; // OPTIMIZE
}
if($this->planetsloaded)
continue; // OPTIMIZE
/* Extract planetnames and coordinates */
if( preg_match("/$search0/i", $line) )
{
$begin = strpos($line, "cp=")+strlen("cp=");
array_push($this->myplanets['CP'], substr($line, $begin, 8));
array_push($this->myplanets['COORD'], substr($line, (strpos($line, "[") + 1), (strpos($line, "]") - strpos($line, "[") - 1) ));
$off = strpos($line, ">", $begin);
$sub = substr($line, ($off + 1), (strpos($line, "<", $off) - $off - 1));
if(preg_match("/selected/i", $line))
array_push($this->myplanets['MAINP'], true);
else
array_push($this->myplanets['MAINP'], false);
array_push($this->myplanets['NAME'], preg_replace("/[\t\ ]*/", "" , $sub));
}
break;
case true:
/* Get the attacking fleets and ETA's */
if( strstr($line, $search2) != false )
{
$sub = substr($line, strpos($line, $pattern1), 255);
ereg($pattern2, $sub, $coord);
array_push($this->targetPlanet,$coord[0]);
/* grab a few lines, for intermediate parsing */
for($x = 0; $x < 6; $x++)
$buff[$x] = $this->content[($i-4+$x)];
$this->setTimer($buff);
}
}
$i++;
} // FOREACH
$this->planetsloaded = true;
}
function order_return($cp)
{
$this->cp = $cp;
$this->contentBuffer = "";
$this->setHeader('fleet1');
// $this->outputHeader($this->header);
$this->sock = null;
$this->request("CONTENT AREA", "END CONTENT", true);
$search = ">Transportere<\/a>";
$pattern1 = "\ Krystal.*";
$pattern2 = "[^0-9\.]";
$index = 0;
foreach($this->content as $line)
{
if( preg_match("/$search/", $line) )
{
$metal = preg_replace("/\ Krystal.*/", "", $line);
$metal = preg_replace("/[^0-9\.]/", "", $metal);
echo "Metal: $metal vs ".$this->shipment[0]."<br>";
if(preg_replace("/\./", "", $metal) == $this->shipment[0])
{
//['metal_string'])
for($i = $index; $i < ($index + 20); $i++)
{
if( preg_match("/order/", $this->content[$i]) )
{
$off = strpos($this->content[$i], "value");
$sub = substr($this->content[$i], $off, 30);
$this->contentBuffer[0] = "order_return=".
preg_replace("/[^0-9]*/", "", $sub);
$this->setHeader("return");
// $this->outputHeader($this->header);
$this->request("CONTENT AREA", "END CONTENT", true);
return;
}
}
}
}
$index++;
}
if($headPrint)
$this->outputHeader();
if($dataPrint)
$this->outputContents($this->content);
}
function fleet1($cp)
{
$this->cp = $cp;
$this->setHeader('fleet1');
// $this->outputHeader($this->header);
$this->request("CONTENT AREA", "END CONTENT", true);
if($headPrint)
$this->outputHeader();
if($dataPrint)
$this->outputContents($this->content);
/* A current line indicator */
$index = 0;
/* The combined POST string in an array */
$query = array();
foreach($this->content as $line)
{
if(strstr($line, "Hastighed"))
{
/* Collect current fleettype data */
foreach($this->fleets as $array)
{
$regx = "/".$array['REALNAME']."/i";
if(preg_match($regx, $line)) {
$curtype = $array;
break;
}
}
$fleettype = $curtype['FORMNAME'];
for($i = $index; $i < ($index+10); $i++)
{
/* maxshipXX field */
$field = "maxship".$fleettype;
if(preg_match("/$field/", $this->content[$i]))
{
$off = strpos($this->content[$i], "value");
$sub = substr($this->content[$i], $off, 12);
ereg('([0-9]{1,4})', $sub, $ret);
array_push($query, ($field."=".$ret[0]));
array_push($query, ("ship".$fleettype."=".$ret[0]));
$this->fleets[$fleettype-202]['MAXVALUE'] = $ret[0];
}
/* consumtionXX field */
$field = "consumption".$fleettype;
if(preg_match("/$field/", $this->content[$i]))
{
$off = strpos($this->content[$i], "value");
$sub = substr($this->content[$i], $off, 12);
ereg('([0-9]{1,4})', $sub, $ret);
array_push($query, ($field."=".$ret[0]));
$this->fleets[$fleettype-202]['CONSUMPT'] = $ret[0];
}
/* speedXX field */
$field = "speed".$fleettype;
if(preg_match("/$field/", $this->content[$i]))
{
$off = strpos($this->content[$i], "value");
$sub = substr($this->content[$i], $off, 12);
ereg('([0-9]{1,7})', $sub, $ret);
array_push($query, ($field."=".$ret[0]));
$this->fleets[$fleettype-202]['SPEED'] = $ret[0];
}
}
/* capacityXX field */
$field = "capacity".$fleettype;
array_push($query, ($field."=".$curtype['CAPACITY']));
}
$index++;
}
$this->contentBuffer = $query;
}
function fleet2($order = 'save')
{
$this->setHeader('fleet2');
error_reporting(E_ALL); // Enables Notices!
$this->request("CONTENT AREA", "END CONTENT", true);
if($headPrint)
$this->outputHeader();
if($dataPrint)
$this->outputContents($this->content);
$query = array();
foreach($this->content as $curline)
{
/* Pick up ressources fields, current resource must
* match closely to reality
*/
if(preg_match("/thisresource1/", $curline))
{
$off = strpos($curline, "value");
$sub = substr($curline, $off, 20); // value=" + 8 cifre
ereg('([0-9]{1,8})', $sub, $ret);
array_push($query, ("thisresource1=".$ret[0]));
$this->resource['metal'] = $ret[0];
}
if(preg_match("/thisresource2/", $curline))
{
$off = strpos($curline, "value");
$sub = substr($curline, $off, 20); // value=" + 8 cifre
ereg('([0-9]{1,8})', $sub, $ret);
array_push($query, ("thisresource2=".$ret[0]));
$this->resource['krystal'] = $ret[0];
}
if(preg_match("/thisresource3/", $curline))
{
$off = strpos($curline, "value");
$sub = substr($curline, $off, 20); // value=" + 8 cifre
ereg('([0-9]{1,8})', $sub, $ret);
array_push($query, ("thisresource3=".$ret[0]));
$this->resource['deut'] = $ret[0];
}
}
if($order == 'farm') {
$source = split(":", $this->getMain());
} else {
$source = split(":", $this->getCoordsByCP($this->cp));
}
/* From this coordinate */
array_push($query, ("thisgalaxy=".$source[0]));
array_push($query, ("thissystem=".$source[1]));
array_push($query, ("thisplanet=".$source[2]));
array_push($query, "thisplanettype=1");
array_push($query, "planettype=1");
array_push($query, "speedfactor=1");
/* Push the fleet into query */
$ships_sent = 0;
foreach($this->fleets as $ship)
{
$fleettype = $ship['FORMNAME'];
if($fleettype == "212") continue;
if($ship['MAXVALUE'] != "")
{
$ships_sent++;
array_push($query, "consumption".$fleettype."=".$ship['CONSUMPT']);
array_push($query, "capacity".$fleettype."=".$ship['CAPACITY']);
if($order == 'save') { // DONT FARM, LIFT ALL!
array_push($query, "ship".$fleettype."=".$ship['MAXVALUE']);
}
array_push($query, "speed".$fleettype."=".$ship['SPEED']);
}
}
if($order == 'farm')
{
$destination = $this->setFarmTarget();
if($destination == "") $ships_sent = 0;
}
else
{
$destination = $this->findSuitable($source);
if($destination == null) {
$destination = $this->findSuitable($source, 100);
}
}
array_push($query, ("galaxy=".$destination[0]));
array_push($query, ("system=".$destination[1]));
array_push($query, ("planet=".$destination[2]));
if($order == 'farm')
{
$cat = $this->getFarmDefenceCategory();
if($cat == 0)
array_push($query, "ship202=18"); // DO FARM
else {
array_push($query, "ship203=6");
array_push($query, "ship207=".$cat);
}
}
/* Easen on the wearing of speedo's */
if($order == 'farm') {
array_push($query, "speed=10"); // TO FARM OR NOT TO
} else if($order == 'save') {
array_push($query, "speed=1");
}
$this->contentBuffer = $query;
if( $ships_sent == 0 ) {
return false;
} else return true;
}
/**
* Completes the fleetform, sets speed and ressources
* Returns true for sucessfull farms/fleetsaves and false,
* if no fleetslots are available
*
* @param string $order
* @return bool
*/
function fleet3($order = 'save')
{
$this->setHeader('fleet3');
if($headPrint)
$this->outputHeader($this->header);
$this->talkToTheHand();
/* Append known values */
$query = $this->contentBuffer;
/* set Order */
if($order == 'save') { // TRANSPORT
array_push($this->contentBuffer, "order=3");
/* Lift of resources */
$i = 1;
$this->checkCapacity();
for($i = 1; $i < 4; $i++) {
$eq = $this->shipment[($i-1)];
array_push($this->contentBuffer, "resource".$i."=".$eq);
}
$_SESSION['metal_string'] = $this->shipment[0];
} else { // ATTACK
array_push($this->contentBuffer, "order=1");
}
/* doForm Flottenversand */
$this->setHeader('fleet4');
if($headPrint)
$this->outputHeader($this->header);
if($order == 'farm') {
$this->request("CONTENT", "END CONTENT", true);
if($dataPrint)
$this->outputContents($this->content);
// Loop 'versand-status' for success / failure and set return time
foreach($this->content as $line)
{
if( preg_match( "/kunne ikke sendes/i", $line) ) {
$this->readDebug($line);
return false;
} elseif( preg_match( "/<th>Tilbage/i", $line) )
{
$line = preg_replace("/.*><th>/", "", $line);
$line = preg_replace("/<\/th>/", "", $line);
$epoch = strtotime($line);
$this->setFarmReturns($epoch);
}
}
} else { // FleetSave
print "Fleetsaving";
$this->talkToTheHand();
}
return true;
}
function checkCapacity()
{
// KB FLEETSABE
$this->shipment[0] = 520000;
$this->shipment[1] = 0;
$this->shipment[2] = 0;
return;
// KB
$used = array ( 0, 0, 0); $rest = array( 0, 0, 0);
$total = 0; $shippedtotal = 0; $divider = 0; $storage = 0;
/* Maximum capacity */
foreach($this->fleets as $ship) {
if($ship['MAXVALUE'] != "") {
$storage += $ship['CAPACITY'] * $ship['MAXVALUE'];
}
}
/* Total resource-count */
$i = 0;
foreach($this->resource as $res) {
$total += $res;
$theres[$i] = $res;
$i++;
}
$fillin = floor(($storage )/ 3);
/* Loop while everything is filled in storage */
while( ($shippedtotal != $total) )
{
/* Loop each res until max is reached */
for($i = 0; $i < 3; $i++)
{
// echo $i;
// echo "this: ".$theres[$i];
// echo "that: ".$this->shipment[$i];
if($theres[$i] == $this->shipment[$i])
continue;
if(($theres[$i] - $used[$i]) < $fillin)
{
$rest[$i] = $fillin - ($theres[$i] - $used[$i]);
$used[$i] += ($fillin - $rest[$i]);
// echo "used : $used[$i] and rest: $rest[$i]<br>";
} else {
$rest[$i] = 0;
$used[$i] += $fillin;
// echo "used : $used[$i] and rest: $rest[$i]<br>";
}
//echo "rest $i $rest[$i] ";
$this->shipment[$i] += ($fillin - $rest[$i]);
// var_dump($this->shipment);
if($this->shipment[$i] <= $theres[$i]) $divider++;
}
/* Evaluate total against storage capacity */
$shippedtotal = 0;
foreach($this->shipment as $res) $shippedtotal += $res;
if($divider == 0) break;
$fillin = floor(($storage - $shippedtotal) / $divider);
$divider = 0;
/* Storage capacity level breached!! Break */
if($fillin == 0 || $storage == $shippedtotal) break;
}
}
} // CLASS
?>