From d3bcf2e4366093049b5b372fbdbe14f902ae35be Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <cynerd@email.cz>
Date: Fri, 11 Dec 2015 18:51:23 +0100
Subject: Add automatic server shutdown

Wrapper can be set to stop server if no player is online for certain
time.
---
 README.md |  6 ++++++
 mcwrapper | 29 +++++++++++++++++++++++++++++
 todo.md   |  6 +++++-
 3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index a5041c1..adaa375 100644
--- a/README.md
+++ b/README.md
@@ -85,3 +85,9 @@ Defines command to start Minecraft server in `directory`.
 #### status
 Defines directory in which will be placed all status files generated by this
 wrapper.
+
+### timeout
+Numerical value, specifying time in minutes after which will be server
+automatically shutdown if no player is on server. If value is less or equal zero,
+automatic shutdown is disabled. If value is not specified in configuration file, 0
+is used.
diff --git a/mcwrapper b/mcwrapper
index b260d6f..f758707 100755
--- a/mcwrapper
+++ b/mcwrapper
@@ -9,6 +9,7 @@ import datetime
 import traceback
 import atexit
 from threading import Thread
+from threading import Timer
 import importlib.machinery as imp
 #################################################################################
 # Load configuration
@@ -50,6 +51,30 @@ if not 'verbose_level' in vars(conf):
     conf.verbose_level = 0
 if not 'server' in vars(conf):
     conf.server = dict()
+if not 'timeout' in vars(conf):
+    conf.timeout = 0
+
+#################################################################################
+
+def autoshutdown_enable():
+    global shutdownTimeout
+    if (conf.timeout > 0):
+        if (conf.verbose_level > 0):
+            print("Automatic shutdown after " + str(conf.timeout) +
+                " min.")
+        shutdownTimeout = Timer(conf.timeout * 60.0, __server_send_stop__)
+        shutdownTimeout.start();
+    pass
+
+def autoshutdown_disable():
+    global shutdownTimeout
+    try:
+        shutdownTimeout.cancel()
+        del shutdownTimeout
+        if (conf.verbose_level > 0):
+            print("Automatic shutdown disabled.")
+    except NameError:
+        pass
 
 #################################################################################
 
@@ -70,6 +95,7 @@ def __user_join__(username):
     with open(playersFile, 'a') as f:
         players.add(username)
         f.write(username + '\n')
+    autoshutdown_disable()
 
 def __user_leave__(username):
     global playerCount
@@ -83,6 +109,8 @@ def __user_leave__(username):
         f.writelines(players)
         if players:
             f.write('\n')
+    if (not players):
+        autoshutdown_enable()
 
 def __server_start__():
     if conf.verbose_level >= 0:
@@ -130,6 +158,7 @@ def __parse_line__(line):
         print("Server start.")
         with open(statusFile, 'w') as f:
             f.write(__STATUSSTRINGS__[2] + '\n')
+        autoshutdown_enable()
     elif ': Stopping the server' in line:
         print("Server stop.")
         with open(statusFile, 'w') as f:
diff --git a/todo.md b/todo.md
index 46905e0..ac9be6f 100644
--- a/todo.md
+++ b/todo.md
@@ -1,3 +1,7 @@
 mcwrapper
 =========
- * Add automatic server shutdown after elapsed time without any player
+ * Add option to specify configuration file instead of environment variable
+ * Add option to print used configuration (source of configuration and variables)
+ * Overwriting specific configuration options from command line
+ * Format prints same as Minecraft server (time stamp and source)
+ * Add options to print important paths for specific server status files
-- 
cgit v1.2.3