From c7d758c8d8df020c608fa666ac3d7c7cb1723c28 Mon Sep 17 00:00:00 2001 From: Cheese Date: Sun, 5 Jul 2015 17:50:18 +1000 Subject: [PATCH] fixed UpdateEvent added WorldComponent --- .../Mineplex/src/mineplex/plugin/Main.php | 12 +- .../game/components/world/WorldComponent.php | 123 +++++++++++++++--- .../plugin/core/updater/UpdateEvent.php | 10 +- .../plugin/core/updater/UpdateType.php | 49 ++++--- .../mineplex/plugin/core/updater/Updater.php | 17 ++- 5 files changed, 160 insertions(+), 51 deletions(-) diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/Main.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/Main.php index fec53f2c5..b7992596c 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/Main.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/Main.php @@ -3,11 +3,12 @@ namespace mineplex\plugin; use mineplex\plugin\bench\arenas\MultiGameArena; +use mineplex\plugin\bench\game\components\world\WorldComponent; use mineplex\plugin\bench\game\factory\TestGameFactory; +use mineplex\plugin\core\updater\Updater; use pocketmine\event\Listener; use pocketmine\event\player\PlayerJoinEvent; use pocketmine\event\player\PlayerLoginEvent; -use pocketmine\event\player\PlayerQuitEvent; use pocketmine\plugin\PluginBase; use mineplex\plugin\bench\arenas\Arena; use pocketmine\Server; @@ -16,23 +17,24 @@ class Main extends PluginBase implements Listener { /** @var Arena */ private $arena; + private $test; public function onEnable() { - $this->arena = new MultiGameArena($this, new TestGameFactory()); Server::getInstance()->getPluginManager()->registerEvents($this, $this); - //$this->getServer()->getScheduler()->scheduleRepeatingTask(new TickTask($this), 1); + $this->test = new WorldComponent($this->arena); //$this->getServer()->getPluginManager()->registerEvents($this, $this); - //new Updater($this); + + new Updater($this); } public function onLogin(PlayerLoginEvent $event) { if ($this->arena->canJoin($event->getPlayer())) - retun; + return; $event->setKickMessage("Unable to join game!"); $event->setCancelled(); diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/world/WorldComponent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/world/WorldComponent.php index 22daaf0fa..0fbf814ee 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/world/WorldComponent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/world/WorldComponent.php @@ -9,12 +9,20 @@ namespace mineplex\plugin\bench\game\components\world; use mineplex\plugin\bench\arenas\Arena; +use mineplex\plugin\bench\game\components\world\event\WorldLoadFailEvent; +use mineplex\plugin\bench\game\components\world\event\WorldLoadSuccessEvent; +use mineplex\plugin\util\UtilString; +use mineplex\plugin\core\updater\UpdateEvent; +use mineplex\plugin\core\updater\UpdateType; +use pocketmine\event\level\LevelUnloadEvent; +use pocketmine\event\player\PlayerJoinEvent; use pocketmine\event\Listener; use pocketmine\level\Position; -use pocketmine\math\Vector3; use pocketmine\Server; +use pocketmine\Player; -//require_once __DIR__ . '\GameState.php'; +use ZipArchive; +use Exception; class WorldComponent implements Listener { @@ -24,44 +32,106 @@ class WorldComponent implements Listener private $gameFolder; private $world; - private $worldName; private $mapName; private $mapAuthor; private $mapTeams = array(); private $mapData = array(); - private $mapSettings = array(); + private $posTest = null; + private $player = null; + public function __construct(Arena $arena) { $this->arena = $arena; - $this->gameId = getNewGameId(); - $this->gameFolder = "Game" . $this->gameId . "_" . $this->worldName; + $this->gameId = $this->getNewGameId(); Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin()); + + $this->loadWorld("Super Smash Mobs"); } - private function loadWorld($worldName) + //This is just some wierd testiong. Ignore it :P +// public function onJoin(PlayerJoinEvent $event) +// { +// $this->player = $event->getPlayer(); +// } +// +// public function onEvent(UpdateEvent $event) +// { +// if (is_null($this->player)) +// return; +// +// if ($event->isTiming(UpdateType::S8)) +// { +// $this->player->teleport($this->posTest); +// +// print("Teleporting " . $this->player->getName() . " to...\n"); +// +// $this->player = null; +// } +// } + + private function loadWorld($gameName) { - $this-$worldName = $worldName; + $files = scandir('../update/maps/' . $gameName . '/'); - //Do this Async? - if ($this->arena->getPlugin()->getServer()->loadLevel($worldName)) + $maps = array(); + + foreach ($files as $file) { - $this->world = $this->arena->getPlugin()->getServer()->getLevelByName($worldName); + if (UtilString::endsWith($file, ".zip")) + { + array_push($maps, $file); + } + } - loadWorldData(); + $worldName = $maps[rand(0, count($maps) - 1)]; + + //Trim .zip + $worldName = substr($worldName, 0, strlen($worldName) - 4); + + print_r($worldName . "\n"); + + $this->gameFolder = "Game" . $this->gameId . "_" . $gameName . "_" . $worldName; + + //Unzip World + $zip = new ZipArchive; + $res = $zip->open('../update/maps/' . $gameName . '/' . $worldName . '.zip'); + if ($res === TRUE) + { + $zip->extractTo('worlds/' . $this->gameFolder . '/'); + $zip->close(); + print("Successfully Extracted: " . $this->gameFolder . "\n"); } else { - print("ERROR LOADING WORLD: " + $worldName); + print("Error Extracting: " . $this->gameFolder . "\n"); + } + + //Load World + if ($this->arena->getPlugin()->getServer()->loadLevel($this->gameFolder)) + { + $this->world = $this->arena->getPlugin()->getServer()->getLevelByName($this->gameFolder); + + $this->loadWorldData(); + + print("Successfully Loaded World: " . $this->gameFolder . "\n"); + + Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena)); + } + else + { + print("Error Loading World: " . $this->gameFolder . "\n"); + + Server::getInstance()->getPluginManager()->callEvent(new WorldLoadFailEvent($this->arena)); } } - public function loadWorldData($world) + public function loadWorldData() { $handle = fopen("WorldConfig.dat", "r"); if ($handle) @@ -110,13 +180,15 @@ class WorldComponent implements Listener { $positions = array(); - foreach ($tokens as $token) + for ($x=1 ; $xstrToPos($tokens[$x]); if (is_null($position)) continue; + $this->posTest = $position; + array_push($positions, $position); } @@ -132,9 +204,9 @@ class WorldComponent implements Listener { $positions = array(); - foreach ($tokens as $token) + for ($x=1 ; $xstrToPos($tokens[$x]); if (is_null($position)) continue; @@ -150,7 +222,7 @@ class WorldComponent implements Listener } else { - print("Error Opening File"); + print("Error Opening File."); } } @@ -169,8 +241,17 @@ class WorldComponent implements Listener return $this->mapData[$key]; } - protected function strToLoc($str) + public function getPosition() { + return $this->getTeams()[0][0]; + } + + + protected function strToPos($str) + { + if (strlen($str) < 5) + return null; + $tokens = explode(",", $str); try @@ -179,7 +260,7 @@ class WorldComponent implements Listener } catch (Exception $e) { - print("World Data Read Error: Invalid Position String [" . $str . "]"); + print("World Data Read Error: Invalid Position String [" . $str . "]\n"); } return null; diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateEvent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateEvent.php index d8d552dc4..f45e103d6 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateEvent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateEvent.php @@ -30,6 +30,14 @@ class UpdateEvent extends Event public function isTiming($type) { - return in_array(type, $this->updateTypes); + foreach ($this->updateTypes as $updateType) + { + if ($updateType->isTiming($type)) + { + return true; + } + } + + return false; } } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateType.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateType.php index ad92a0389..06c92c160 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateType.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/UpdateType.php @@ -12,26 +12,45 @@ namespace mineplex\plugin\core\updater; class UpdateType { const M64 = 3840000; - conSt M32 = 1920000; - conSt M16 = 960000; - conSt M8 = 480000; - conSt M4 = 240000; - conSt M2 = 120000; - conSt M1 = 60000; - conSt S32 = 32000; - conSt S16 = 16000; - conSt S8 = 8000; - conSt S4 = 4000; - conSt S2 = 2000; - conSt S1 = 1000; - conSt MS500 = 500; - conSt MS250 = 250; - conSt MS125 = 125; + const M32 = 1920000; + const M16 = 960000; + const M8 = 480000; + const M4 = 240000; + const M2 = 120000; + const M1 = 60000; + const S32 = 32000; + const S16 = 16000; + const S8 = 8000; + const S4 = 4000; + const S2 = 2000; + const S1 = 1000; + const MS500 = 500; + const MS250 = 250; + const MS125 = 125; public $time; + private $lastTrigger = 0; + public function __construct($time=0) { $this->time = $time; } + + public function canTrigger() + { + if (round(microtime(true) * 1000) - $this->lastTrigger > $this->time) + { + $this->lastTrigger = round(microtime(true) * 1000); + return true; + } + + return false; + } + + public function isTiming($timing) + { + return $this->time === $timing; + } + } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php index 57089225b..9fd2d4735 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php @@ -8,13 +8,13 @@ namespace mineplex\plugin\core\updater; +use mineplex\plugin\core\updater\UpdateType; use pocketmine\plugin\PluginBase; use pocketmine\scheduler\PluginTask; class Updater extends PluginTask { private $plugin; - private $last; private $updateTypes; public function __construct(PluginBase $host) @@ -47,14 +47,13 @@ class Updater extends PluginTask { $updateTypes = array(); - $timeSpent = round(microtime(true) * 1000) - $this->last; - $this->last = round(microtime(true) * 1000); - - foreach ($this->updateTypes as &$updateType) { - if ($timeSpent >= $updateType->time) { - array_push($updateTypes, $updateType); - } - } + foreach ($this->updateTypes as &$updateType) + { + if ($updateType->canTrigger()) + { + array_push($updateTypes, $updateType); + } + } //Call Event $this->plugin->getServer()->getPluginManager()->callEvent(new UpdateEvent($currentTick, $updateTypes)); }