Update 3 files

- /minecraft.php
- /README.md
- /mineautoshutdown.sh
This commit is contained in:
Will Bradley 2023-01-27 01:21:18 +00:00
parent e0c61962ec
commit 6d22b4d999
3 changed files with 39 additions and 16 deletions

View File

@ -5,6 +5,10 @@ and automatically shut it down when not in use.
## Installation and Usage ## 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. 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 Place the remaining files on a server with CRON (could be the same server, just

View File

@ -1,17 +1,23 @@
#!/bin/bash
DO_STATUS_URL="http://example.com/minecraft.php" DO_STATUS_URL="http://example.com/minecraft.php"
PASSWORD="your_password_here" 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 if [[ $status == "active" ]]; then
players=$(python3 minestatus.py | jq ".players") players=$(python3 $(dirname $0)minestatus.py | jq ".players")
if [[ $players == 0 ]]; then if [[ $players == 0 ]]; then
if [ ! -f "/tmp/minecount" ]; then
touch /tmp/minecount
fi
count=$(cat /tmp/minecount) count=$(cat /tmp/minecount)
if [[ $count -gt 6 ]]; then if [[ $count -gt 6 ]]; then
echo "players $players count $count, killing" 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 echo "0" > /tmp/minecount
else else
echo $(($count+1)) > /tmp/minecount echo $(($count+1)) > /tmp/minecount
@ -22,5 +28,5 @@ if [[ $status == "active" ]]; then
echo "0" > /tmp/minecount echo "0" > /tmp/minecount
fi fi
else else
echo "not active" echo "not active: $status"
fi fi

View File

@ -9,7 +9,7 @@ if ($_GET['password'] !== $PASSWORD) {
exit("Invalid password."); exit("Invalid password.");
} }
if ($_GET['action'] == "on") { if ($_POST['action'] == "on") {
$out = `curl -X POST \ $out = `curl -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
@ -17,31 +17,44 @@ $out = `curl -X POST \
-d '{"type":"power_on"}' \ -d '{"type":"power_on"}' \
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`; "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
} elseif ($_GET['action'] == "off") { } elseif ($_POST['action'] == "off") {
$out = `curl -X POST \ $out = `curl -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
-d '{"type":"power_off"}' \ -d '{"type":"power_off"}' \
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`; "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
} elseif ($_GET['action'] == "status") { } elseif ($_POST['action'] == "status") {
$out = `curl -X GET \ $out = `curl -X GET \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \ -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
"https://api.digitalocean.com/v2/droplets/$DROPLET_ID"`; "https://api.digitalocean.com/v2/droplets/$DROPLET_ID"`;
} else {
echo "Invalid action.";
} }
if ($out) {
$json = json_decode($out, true); $json = json_decode($out, true);
if ($json['action']) { if ($json['action']) {
echo $json['action']['status']; $status = $json['action']['status'];
} elseif ($json['droplet']) { } elseif ($json['droplet']) {
echo $json['droplet']['status']; $status = $json['droplet']['status'];
} }
}
?><html>
<head><title>MineCollective Minecraft Server</title></head>
<body>
<p>
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.
</p>
<form action="#" method="POST">
<input type="hidden" name="action" value="on" />
<input type="submit" value="Turn On" />
</form>
</body>
</html>