From e24890d292790b0a821fac115113762dfb134c69 Mon Sep 17 00:00:00 2001 From: Cheese Date: Sun, 5 Jul 2015 15:26:56 +1000 Subject: [PATCH 1/2] fixed some buggies --- .../game/components/world/WorldComponent.php | 62 +++++++------------ 1 file changed, 23 insertions(+), 39 deletions(-) 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 72aefb56a..22daaf0fa 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 @@ -26,19 +26,13 @@ class WorldComponent implements Listener private $world; private $worldName; - private $mapName; private $mapAuthor; private $mapTeams = array(); private $mapData = array(); - private $minX = -256; - private $maxX = 256; - private $minY = -256; - private $maxY = 256; - private $minZ = -256; - private $maxZ = 256; + private $mapSettings = array(); public function __construct(Arena $arena) { @@ -91,43 +85,28 @@ class WorldComponent implements Listener { $this->mapName = $tokens[1]; } - else if (strcmp($tokens[0], "MAP_AUTHOR") === 0) + elseif (strcmp($tokens[0], "MAP_AUTHOR") === 0) { $this->mapAuthor = $tokens[1]; } //Map Boundaries - else if (strcmp($tokens[0], "MIN_X") === 0) + elseif (strcmp($tokens[0], "MIN_X") === 0 || + strcmp($tokens[0], "MAX_X") === 0 || + strcmp($tokens[0], "MIN_Y") === 0 || + strcmp($tokens[0], "MAX_Y") === 0 || + strcmp($tokens[0], "MIN_Z") === 0 || + strcmp($tokens[0], "MAX_Z") === 0) { - $this->minX = $tokens[1]; - } - else if (strcmp($tokens[0], "MAX_X") === 0) - { - $this->maxX = $tokens[1]; - } - else if (strcmp($tokens[0], "MIN_Y") === 0) - { - $this->minY = $tokens[1]; - } - else if (strcmp($tokens[0], "MAX_Y") === 0) - { - $this->maxY = $tokens[1]; - } - else if (strcmp($tokens[0], "MIN_Z") === 0) - { - $this->minZ = $tokens[1]; - } - else if (strcmp($tokens[0], "MAX_Z") === 0) - { - $this->maxZ = $tokens[1]; + $this->mapSettings[$tokens[0]] = $tokens[1]; } //Team Spawns - else if (strcmp($tokens[0], "TEAM_NAME") === 0) + elseif (strcmp($tokens[0], "TEAM_NAME") === 0) { $currentTeamName = $tokens[1]; } - else if (strcmp($tokens[0], "TEAM_SPAWNS") === 0) + elseif (strcmp($tokens[0], "TEAM_SPAWNS") === 0) { $positions = array(); @@ -145,11 +124,11 @@ class WorldComponent implements Listener } //Data - else if (strcmp($tokens[0], "DATA_NAME") === 0) + elseif (strcmp($tokens[0], "DATA_NAME") === 0) { $currentDataName = $tokens[1]; } - else if (strcmp($tokens[0], "DATA_LOCS") === 0) + elseif (strcmp($tokens[0], "DATA_LOCS") === 0) { $positions = array(); @@ -180,9 +159,14 @@ class WorldComponent implements Listener return $this->mapTeams; } + public function getSetting($key) + { + return $this->mapSettings[$key]; + } + public function getData($key) { - return $this->worldData[$key]; + return $this->mapData[$key]; } protected function strToLoc($str) @@ -199,12 +183,12 @@ class WorldComponent implements Listener } return null; - } + } //This will return a UID for the game public function getNewGameId() -{ - return rand(0, 999999); //Make this acutally unique -} + { + return rand(0, 999999); //Make this acutally unique + } } From c7d758c8d8df020c608fa666ac3d7c7cb1723c28 Mon Sep 17 00:00:00 2001 From: Cheese Date: Sun, 5 Jul 2015 17:50:18 +1000 Subject: [PATCH 2/2] 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)); }