From 2b4ef28d9932ab6deaa6016e28d7b7a576aba93a Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Sun, 31 Jul 2016 10:27:54 +0300 Subject: [PATCH] Added signs and backups --- resources/config.yml | 20 ++++++- src/Ad5001/GameManager/Game.php | 6 +- src/Ad5001/GameManager/GameManager.php | 58 ++++++++++++++----- src/Ad5001/GameManager/Main.php | 9 +++ .../GameManager/tasks/SignReloadTask.php | 56 ++++++++++++++++++ 5 files changed, 131 insertions(+), 18 deletions(-) create mode 100644 src/Ad5001/GameManager/tasks/SignReloadTask.php diff --git a/resources/config.yml b/resources/config.yml index 22d7e6e..49145cb 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -1,3 +1,21 @@ --- -# This is the default config generated with ImagicalPlugCreator. (C) ImagicalPlugCreator - Ad5001 2016 +# Welcome to GameManager config. +# Here you can configure how does game signs looks. +# 1 is line 1, 2 is line 2, 3 is line 3 and 4 is line 4. +# Note that {Game} MUST be included in the first line and {level} in the second one (detection). + +Game1: "§l§o[§r§l{Game}§o]" +Game2: "-=<{level}>=-" + +# When the game need more players: +GameWait3: "{players}/{max}" +GameWait4: "> > > CLICK TO JOIN < < <" + +# When the game is already started: +InGame3: "Game already started" +InGame4: "> > > CLICK TO SPECTATE < < <" + +# Now you can choose all worlds game's +# Example: +#world: NameOfTheGame ... \ No newline at end of file diff --git a/src/Ad5001/GameManager/Game.php b/src/Ad5001/GameManager/Game.php index bb207a8..f378131 100644 --- a/src/Ad5001/GameManager/Game.php +++ b/src/Ad5001/GameManager/Game.php @@ -15,6 +15,7 @@ abstract class Game { protected $name; protected $level; + protected $server; public function __construct(string $name, Level $level) { @@ -22,6 +23,7 @@ abstract class Game { $this->level = $level; $this->name = $name; $this->main = $this->server->getPlugin("GameManager"); + $this->gm = $this->main->getGameManager(); $this->main->backup($level); } @@ -36,8 +38,8 @@ abstract class Game { } - public function getLevel() { - return $this->main; + public function isStarted() { + return isset($this->gm->getStartedGames()[$this->level->getName()]); } diff --git a/src/Ad5001/GameManager/GameManager.php b/src/Ad5001/GameManager/GameManager.php index 57a6faf..6944131 100644 --- a/src/Ad5001/GameManager/GameManager.php +++ b/src/Ad5001/GameManager/GameManager.php @@ -12,6 +12,13 @@ use Ad5001\GameManager\Main; class GameManager { + protected $main; + protected $server; + protected $games; + protected $levels; + protected $startedgames; + + public function __construct(Main $main) { $this->main = $main; $this->server = $main->getServer(); @@ -20,10 +27,10 @@ class GameManager { $this->levels = []; $this->startedgames = []; foreach ($files as $file) { - require($file); + require($this->getDataFolder() . "/games/" . $file); $classn = getClasses(file_get_contents($this->getDataFolder() . "/games/" . $file)); $this->games[explode(".php", $file)[0]] = $classn; - @mkdir($this->main->getDataFolder() . "games/$classn"); + @mkdir($this->main->getDataFolder() . "games/" . explode(".php", $file)[0]); } } @@ -51,14 +58,35 @@ class GameManager { } - - public function reloadLevel(Level $level) { - $this->backup($level); - rrmdir($this->main->getFilePath() . "worlds/{$level->getName()}"); - $this->restore($level); + public function getLevels() { + return $this->levels; } + public function getGames() { + return $this->games; + } + + + public function getStartedGames() { + return $this->startedgames; + } + + + + public function restoreBackup(Level $level) { + rrmdir($this->server->getFilePath() . "worlds/{$level->getName()}"); + copydir($this->server->getFilePath() . "worldsBackups/{$level->getName()}", $this->server->getFilePath() . "worlds/{$level->getName()}"); + } + + + + public function backup(Level $level) { + rrmdir($this->server->getFilePath() . "worldsBackups/{$level->getName()}"); + copydir($this->server->getFilePath() . "worlds/{$level->getName()}", $this->server->getFilePath() . "worldsBackup/{$level->getName()}"); + } + + private function rrmdir($dir) { // This is from PHP.NET if (is_dir($dir)) { $objects = scandir($dir); @@ -74,24 +102,24 @@ class GameManager { private function copydir( $source, $target ) { - if ( is_dir( $source ) ) { - @mkdir( $target ); - $d = dir( $source ); + if (is_dir( $source)) { + @mkdir($target); + $d = dir($source); while ( FALSE !== ( $entry = $d->read() ) ) { - if ( $entry == '.' || $entry == '..' ) { + if ($entry == '.' || $entry == '..') { continue; } $Entry = $source . '/' . $entry; - if ( is_dir( $Entry ) ) { - full_copy( $Entry, $target . '/' . $entry ); + if (is_dir($Entry)) { + copydir($Entry, $target . '/' . $entry); continue; } - copy( $Entry, $target . '/' . $entry ); + copy($Entry, $target . '/' . $entry); } $d->close(); } else { - copy( $source, $target ); + copy($source, $target); } } diff --git a/src/Ad5001/GameManager/Main.php b/src/Ad5001/GameManager/Main.php index 07ffdcf..b50c248 100644 --- a/src/Ad5001/GameManager/Main.php +++ b/src/Ad5001/GameManager/Main.php @@ -12,9 +12,13 @@ use Ad5001\GameManager\GameManager; class Main extends PluginBase{ + protected $manager; + + public function onEnable(){ $this->reloadConfig(); $this->getServer()->getPluginManager()->registerEvents($this, $this); + @mkdir($this->getServer()->getFilePath() . "worldsBackups/"); $this->manager = new GameManager($this); } @@ -25,6 +29,11 @@ class Main extends PluginBase{ } + public function getGameManager() { + return $this->manager; + } + + public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){ switch($cmd->getName()){ case "default": diff --git a/src/Ad5001/GameManager/tasks/SignReloadTask.php b/src/Ad5001/GameManager/tasks/SignReloadTask.php new file mode 100644 index 0000000..c642d51 --- /dev/null +++ b/src/Ad5001/GameManager/tasks/SignReloadTask.php @@ -0,0 +1,56 @@ +main = $main; + $this->server = $main->getServer(); + $this->cfg = $main->getConfig(); + $this->gameManager = $main->getGameManager(); + } + + + + public function onRun($tick) { + foreach($this->server->getLevels() as $level) { + foreach($level->getTiles() as $t) { + if($t instanceof \pocketmine\tile\Sign) { + foreach($this->gameManager->getLevels() as $name => $class) { + if(str_ireplace("{game}", $class->getName(), $this->cfg->get("Game1")) == $t->getText()[0]) { + $lvlex = explode("{level}", $this->cfg->get("Game2")); + $lvl = str_ireplace($lvlex[0], "", $t->getText()[1]); + $lvl = str_ireplace($lvlex[1], "", $lvl); + if($name == $lvl) { + if($this->gm->getLevels()[$lvl->getName()]->isStarted()) { + $l3 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("InGame3")); + $l3 = str_ireplace("{max}", $class->getMaxPlayers(), $l3); + $l4 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("InGame4")); + $l4 = str_ireplace("{max}", $class->getMaxPlayers(), $l4); + $t->setText($t->getText()[0], $t->getText()[1], $l3, $t4); + } else { + $l3 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("GameWait3")); + $l3 = str_ireplace("{max}", $class->getMaxPlayers(), $l3); + $l4 = str_ireplace("{players}", count($lvl->getPlayers()), $this->cfg->get("GameWait4")); + $l4 = str_ireplace("{max}", $class->getMaxPlayers(), $l4); + $t->setText($t->getText()[0], $t->getText()[1], $l3, $t4); + } + } + } + } + } + } + } + } +} \ No newline at end of file