From 7b17819ca44c475c4c712baf29cb4eed44c4c5ad Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Tue, 11 Dec 2012 20:24:32 +0100 Subject: [PATCH] Automatic updates! --- classes/API/ConsoleAPI.php | 3 +++ classes/API/ServerAPI.php | 23 +++++++++++++++++++++++ common/default.properties | 1 + 3 files changed, 27 insertions(+) diff --git a/classes/API/ConsoleAPI.php b/classes/API/ConsoleAPI.php index dd727c16d..b6e96e3ba 100644 --- a/classes/API/ConsoleAPI.php +++ b/classes/API/ConsoleAPI.php @@ -48,6 +48,9 @@ class ConsoleAPI{ $cmd = strtolower(array_shift($params)); console("[INFO] Issued server command: /$cmd ".implode(" ", $params)); switch($cmd){ + case "update-done": + $this->server->api->setProperty("last-update", time()); + break; case "stop": console("[INFO] Stopping server..."); $this->server->close(); diff --git a/classes/API/ServerAPI.php b/classes/API/ServerAPI.php index 999209fb8..fbc0daf55 100644 --- a/classes/API/ServerAPI.php +++ b/classes/API/ServerAPI.php @@ -57,6 +57,22 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run define("DEBUG", $this->config["debug"]); $this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), $this->getProperty("seed"), $this->getProperty("protocol"), $this->getProperty("port"), $this->getProperty("server-id")); $this->server->api = $this; + if($this->getProperty("last-update") === false or ($this->getProperty("last-update") + 3600) < time()){ + console("[INFO] Checking for new version..."); + $info = json_decode(Utils::curl_get("https://api.github.com/repos/shoghicp/Pocket-Minecraft-PHP"), true); + $last = date_parse($info["updated_at"]); + $last = mktime($last["hour"], $last["minute"], $last["second"], $last["month"], $last["day"], $last["year"]); + if($last >= $this->getProperty("last-update") and $this->getProperty("last-update") !== false){ + console("[NOTICE] Pocket-PHP-Minecraft has been updated at ".date("Y-m-d H:i:s", $last)); + console("[NOTICE] If you want to update, download the latest version at https://github.com/shoghicp/Pocket-Minecraft-PHP/archive/master.zip"); + console("[NOTICE] This message will dissapear when you issue the command \"/update-done\""); + sleep(5); + }else{ + $last = time(); + $this->setProperty("last-update", $last); + console("[INFO] Last check at ".date("Y-m-d H:i:s", $last)); + } + } if(file_exists(FILE_PATH."data/maps/level.dat")){ console("[NOTICE] Detected unimported map data. Importing..."); $this->importMap(FILE_PATH."data/maps/", true); @@ -193,6 +209,13 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $n = strtolower(array_shift($d)); $v = implode("=", $d); switch($n){ + case "last-update": + if(trim($v) == "false"){ + $v = time(); + }else{ + $v = (int) $v; + } + break; case "protocol": if(trim($v) == "CURRENT"){ $v = CURRENT_PROTOCOL; diff --git a/common/default.properties b/common/default.properties index 569c6b4b3..053e57975 100644 --- a/common/default.properties +++ b/common/default.properties @@ -4,6 +4,7 @@ description= This is a Work in Progress custom server. motd=Welcome to PHP Server port=19132 protocol=CURRENT +last-update=false white-list=false debug=2 max-players=20