Skip to content

Instantly share code, notes, and snippets.

@jhead
Last active December 28, 2015 20:38
Show Gist options
  • Save jhead/7558501 to your computer and use it in GitHub Desktop.
Save jhead/7558501 to your computer and use it in GitHub Desktop.
<?php
require_once("_api/init.php");
global $MCApi, $DB;
$result = $MCApi->listServers();
if ($result['success'] != 1) die('Unable to fetch server list.');
$servers = $result['data']['Servers'];
foreach ($servers as $id => $serverName) {
if (strpos($serverName, "InstantMC - ") !== 0) {
echo "Skipping non-IMC server " . $id . ".\n";
continue;
}
if (strlen($serverName) != 24) continue;
$result = $MCApi->getServerStatus($id);
if ($result['success'] != 1) continue;
$serverStatus = !($result['data']['status'] == "offline");
$query = $DB->query("SELECT * FROM servers WHERE `serverid`=" . $id . " LIMIT 1");
if (!$query) continue;
$result = $DB->fetch($query);
if (!$result) {
echo "Found unknown IMC server " . $id . "; delete? (y/n) ";
$prompt = strtolower(fgetc(STDIN));
if ($prompt == "y") {
fullDelete($id);
} else {
echo "Skipping server " . $id . ".\n";
}
continue;
}
$result = $result[0];
$hash = $result->hash;
if ($result) {
$expiresOn = strtotime($result->expireson);
$expired = $expiresOn < time();
$expiredAgo = (time() - $expiresOn) / 3600;
echo "Found server " . $id . ($expired ? " expired " . $expiredAgo . " hrs ago " : "") . " [" . $result->expireson . "]" . "\n";
if ($expired === true) {
echo "Stopping server " . $id . "... ";
$result = $MCApi->stopServer($id);
echo ($result['success'] ? "Success" : "Failed") . ". ";
if ($result['success']) {
fullDelete($id);
} else {
echo "Unable to stop server; re-run script.\n";
}
}
}
}
function fullDelete($id) {
global $MCApi;
echo "Waiting...";
$waitCount = 1;
do {
$result = $MCApi->getServerStatus($id);
if ($result && $result['success']) {
$result = strtolower($result['data']['status']);
if ($result != "online") {
echo " Deleting... ";
$result = $MCApi->deleteServer($id, true);
echo ($result['success'] ? "Success" : "Failed") . ".\n";
return;
}
}
echo ".";
$waitCount++;
sleep(1);
} while ($waitCount < 5);
echo "Took too long to stop server; re-run script.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment