Skip to content

Instantly share code, notes, and snippets.

@bmonkman
Created March 19, 2014 20:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmonkman/9651069 to your computer and use it in GitHub Desktop.
Save bmonkman/9651069 to your computer and use it in GitHub Desktop.
<?php
define(COOKIE_FILE, "/var/tmp/sn-run");
define(GAME_NUMBER, "5566012476882944");
define(MY_ID, 5); // Player id
touch(COOKIE_FILE);
$allies = array(1,10); // Optional ally ids - disable notifications for these players if it gets too annoying.
$from = $to = "spacenorad@yourdomain.com"; // Notification address
$username = "YourUsername";
$password = "YourPassword";
// Get data, log in if necessary
$data = getData();
if (!$data)
{
doLogin($username, $password);
$data = getData();
}
$msg = '';
$alert = false;
foreach ($data['report']['fleets'] as $fleet)
{
$playerId = $fleet['puid'];
$dest = $fleet['o'][0][1];
if ($playerId != MY_ID && !in_array($playerId, $allies) && !empty($dest))
{
$destName = @$data['report']['stars'][$dest]['n'];
$destOwner = @$data['report']['stars'][$dest]['puid'];
$destOwnerName = @$data['report']['players'][$destOwner]['alias'];
$currentSystem = @$fleet['ouid'];
$currentSystemName = @$data['report']['stars'][$currentSystem]['n'];
$fleetName = $fleet['n'];
$fleetShips = $fleet['st'];
$playerName = $data['report']['players'][$playerId]['alias'];
// Write and check cache file
// Cache based on fleet name and destination. If a fleet/dest pair already exists, don't bother notifying
// Clear out cache files if the fleet/dest pair hasn't been seen for more than 10 minutes (i.e. the fleet landed)
$cacheFile = "/var/tmp/NORAD-{$fleetName}-{$destName}";
if (file_exists($cacheFile))
{
touch($cacheFile);
continue; // Bail out
}
touch($cacheFile);
$msg .= "{$playerName} - {$fleetShips} ships ({$fleetName}) spotted heading for {$destName} ({$destOwnerName})!\n";
if ($destOwner == $me)
{
$alert = true;
$msg .= "SPACE NORAD SAYS: WAKE THE F UP!\n";
}
}
}
$subject = $alert ? 'Fleet Movement - INCOMING ATTACK' : 'Fleet Movement';
if (!empty($msg))
mail($to, $subject, $msg, "From: {$from}");
// Remove old cache files
if ($dh = opendir('/var/tmp/'))
{
while (($file = readdir($dh)) !== false)
{
if (strstr($file, 'NORAD') !== false)
{
$diff = time() - filemtime("/var/tmp/{$file}");
if ($diff > 10*60)
unlink("/var/tmp/{$file}");
}
}
closedir($dh);
}
function getData()
{
$opts = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "type=order&order=full_universe_report&version=7&game_number=".GAME_NUMBER,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => COOKIE_FILE,
CURLOPT_COOKIEJAR => COOKIE_FILE,
);
$ch = curl_init('http://triton.ironhelmet.com/grequest/order');
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$err = curl_errno($ch);
$msg = curl_error($ch);
curl_close($ch);
if ($err != 0)
{
echo "ERROR: \n";
var_dump($err);
var_dump($msg);
exit;
}
}
function doLogin($username, $password)
{
$opts = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "type=login&alias={$username}&password={$password}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => COOKIE_FILE,
CURLOPT_COOKIEJAR => COOKIE_FILE,
);
$ch = curl_init('http://triton.ironhelmet.com/arequest/login');
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$err = curl_errno($ch);
$msg = curl_error($ch);
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment