Added signs and backups

This commit is contained in:
Ad5001 2016-07-31 10:27:54 +03:00
parent 3a2da7e8db
commit 2b4ef28d99
5 changed files with 131 additions and 18 deletions

View file

@ -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
... ...

View file

@ -15,6 +15,7 @@ abstract class Game {
protected $name; protected $name;
protected $level; protected $level;
protected $server;
public function __construct(string $name, Level $level) { public function __construct(string $name, Level $level) {
@ -22,6 +23,7 @@ abstract class Game {
$this->level = $level; $this->level = $level;
$this->name = $name; $this->name = $name;
$this->main = $this->server->getPlugin("GameManager"); $this->main = $this->server->getPlugin("GameManager");
$this->gm = $this->main->getGameManager();
$this->main->backup($level); $this->main->backup($level);
} }
@ -36,8 +38,8 @@ abstract class Game {
} }
public function getLevel() { public function isStarted() {
return $this->main; return isset($this->gm->getStartedGames()[$this->level->getName()]);
} }

View file

@ -12,6 +12,13 @@ use Ad5001\GameManager\Main;
class GameManager { class GameManager {
protected $main;
protected $server;
protected $games;
protected $levels;
protected $startedgames;
public function __construct(Main $main) { public function __construct(Main $main) {
$this->main = $main; $this->main = $main;
$this->server = $main->getServer(); $this->server = $main->getServer();
@ -20,10 +27,10 @@ class GameManager {
$this->levels = []; $this->levels = [];
$this->startedgames = []; $this->startedgames = [];
foreach ($files as $file) { foreach ($files as $file) {
require($file); require($this->getDataFolder() . "/games/" . $file);
$classn = getClasses(file_get_contents($this->getDataFolder() . "/games/" . $file)); $classn = getClasses(file_get_contents($this->getDataFolder() . "/games/" . $file));
$this->games[explode(".php", $file)[0]] = $classn; $this->games[explode(".php", $file)[0]] = $classn;
@mkdir($this->main->getDataFolder() . "games/$classn"); @mkdir($this->main->getDataFolder() . "games/" . explode(".php", $file)[0]);
} }
} }
@ -51,11 +58,32 @@ class GameManager {
} }
public function getLevels() {
return $this->levels;
}
public function reloadLevel(Level $level) {
$this->backup($level); public function getGames() {
rrmdir($this->main->getFilePath() . "worlds/{$level->getName()}"); return $this->games;
$this->restore($level); }
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()}");
} }
@ -74,24 +102,24 @@ class GameManager {
private function copydir( $source, $target ) { private function copydir( $source, $target ) {
if ( is_dir( $source ) ) { if (is_dir( $source)) {
@mkdir( $target ); @mkdir($target);
$d = dir( $source ); $d = dir($source);
while ( FALSE !== ( $entry = $d->read() ) ) { while ( FALSE !== ( $entry = $d->read() ) ) {
if ( $entry == '.' || $entry == '..' ) { if ($entry == '.' || $entry == '..') {
continue; continue;
} }
$Entry = $source . '/' . $entry; $Entry = $source . '/' . $entry;
if ( is_dir( $Entry ) ) { if (is_dir($Entry)) {
full_copy( $Entry, $target . '/' . $entry ); copydir($Entry, $target . '/' . $entry);
continue; continue;
} }
copy( $Entry, $target . '/' . $entry ); copy($Entry, $target . '/' . $entry);
} }
$d->close(); $d->close();
} else { } else {
copy( $source, $target ); copy($source, $target);
} }
} }

View file

@ -12,9 +12,13 @@ use Ad5001\GameManager\GameManager;
class Main extends PluginBase{ class Main extends PluginBase{
protected $manager;
public function onEnable(){ public function onEnable(){
$this->reloadConfig(); $this->reloadConfig();
$this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getPluginManager()->registerEvents($this, $this);
@mkdir($this->getServer()->getFilePath() . "worldsBackups/");
$this->manager = new GameManager($this); $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){ public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
switch($cmd->getName()){ switch($cmd->getName()){
case "default": case "default":

View file

@ -0,0 +1,56 @@
<?php
namespace Ad5001\GameManager;
use pocketmine\scheduler\PluginTask;
use pocketmine\Server;
use pocketmine\Player;
use Ad5001\GameManager\GameManager;
use Ad5001\GameManager\Main;
class SignReloadTask extends PluginTask {
protected $manager;
public function __construct(Main $main) {
parent::__construct($main);
$this->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);
}
}
}
}
}
}
}
}
}