diff --git a/README.md b/README.md index 08254c1..23a851e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ and automatically shut it down when not in use. ## Installation and Usage +These packages require PHP, Python3, the PIP module mcstatus, and a webserver. + +`python3 -m pip install mcstatus` + Install the .php file on a webserver and configure the variables as desired. Place the remaining files on a server with CRON (could be the same server, just diff --git a/mineautoshutdown.sh b/mineautoshutdown.sh index 4a6d70f..71292ea 100755 --- a/mineautoshutdown.sh +++ b/mineautoshutdown.sh @@ -1,17 +1,23 @@ +#!/bin/bash + DO_STATUS_URL="http://example.com/minecraft.php" PASSWORD="your_password_here" -status=$(curl -s "$DO_STATUS_URL?action=status&password=$PASSWORD") +status=$(curl -s -X POST -F "action=status" "$DO_STATUS_URL?password=$PASSWORD") if [[ $status == "active" ]]; then - players=$(python3 minestatus.py | jq ".players") + players=$(python3 $(dirname $0)minestatus.py | jq ".players") if [[ $players == 0 ]]; then + if [ ! -f "/tmp/minecount" ]; then + touch /tmp/minecount + fi + count=$(cat /tmp/minecount) if [[ $count -gt 6 ]]; then echo "players $players count $count, killing" - curl -s "$DO_STATUS_URL?action=off&password=$PASSWORD" + curl -s -X POST -F "action=off" "$DO_STATUS_URL?password=$PASSWORD" echo "0" > /tmp/minecount else echo $(($count+1)) > /tmp/minecount @@ -22,5 +28,5 @@ if [[ $status == "active" ]]; then echo "0" > /tmp/minecount fi else - echo "not active" + echo "not active: $status" fi diff --git a/minecraft.php b/minecraft.php index 936502d..f6b13c3 100644 --- a/minecraft.php +++ b/minecraft.php @@ -9,7 +9,7 @@ if ($_GET['password'] !== $PASSWORD) { exit("Invalid password."); } -if ($_GET['action'] == "on") { +if ($_POST['action'] == "on") { $out = `curl -X POST \ -H "Content-Type: application/json" \ @@ -17,31 +17,44 @@ $out = `curl -X POST \ -d '{"type":"power_on"}' \ "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`; -} elseif ($_GET['action'] == "off") { +} elseif ($_POST['action'] == "off") { $out = `curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ + -d '{"type":"power_off"}' \ "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`; -} elseif ($_GET['action'] == "status") { +} elseif ($_POST['action'] == "status") { $out = `curl -X GET \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ "https://api.digitalocean.com/v2/droplets/$DROPLET_ID"`; -} else { - -echo "Invalid action."; - } -$json = json_decode($out, true); +if ($out) { + $json = json_decode($out, true); -if ($json['action']) { - echo $json['action']['status']; -} elseif ($json['droplet']) { - echo $json['droplet']['status']; + if ($json['action']) { + $status = $json['action']['status']; + } elseif ($json['droplet']) { + $status = $json['droplet']['status']; + } } + +?> +MineCollective Minecraft Server + +

+ Press this button to turn on the server. It'll take up to 5 minutes to boot, and turn off + after about 30 minutes of inactivity. +

+
+ + +
+ +