From 060a928dd5992b629bf4155eaa4986c0d76b25df Mon Sep 17 00:00:00 2001 From: Aaron Brock Date: Fri, 3 Jul 2015 01:02:16 -0400 Subject: [PATCH] Added bench time stuff. Signed-off-by: Aaron Brock --- Pocket/plugins/Mineplex/src/Test.php | 17 ++++ .../countdown/GameStateCountdown.php | 26 +++--- .../bench/game/components/feature/Feature.php | 20 +++++ .../feature/GameStateFeatureManager.php | 65 ++++++++++++++ .../feature/features/NoBlockBreak.php | 51 +++++++++++ .../game/components/gamestate/GameState.php | 10 +-- .../bench/game/components/gamestate/Test.php | 15 ---- .../plugin/bench/game/games/pvp/Pvp.php | 4 + .../plugin/bench/time/BenchSchedule.php | 85 +++++++++++++++++++ .../mineplex/plugin/bench/time/BenchTask.php | 17 ++++ .../plugin/bench/time/BenchTaskData.php | 43 ++++++++++ .../mineplex/plugin/core/updater/Updater.php | 11 +-- 12 files changed, 327 insertions(+), 37 deletions(-) create mode 100644 Pocket/plugins/Mineplex/src/Test.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/Feature.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/GameStateFeatureManager.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/features/NoBlockBreak.php delete mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/Test.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchSchedule.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php create mode 100644 Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTaskData.php diff --git a/Pocket/plugins/Mineplex/src/Test.php b/Pocket/plugins/Mineplex/src/Test.php new file mode 100644 index 000000000..1a14d662a --- /dev/null +++ b/Pocket/plugins/Mineplex/src/Test.php @@ -0,0 +1,17 @@ + $moo) +{ + if ($moo == '2') + unset ($array[$key]); +} + +print_r($array); \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/countdown/GameStateCountdown.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/countdown/GameStateCountdown.php index 472c362e9..55708de72 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/countdown/GameStateCountdown.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/countdown/GameStateCountdown.php @@ -8,30 +8,35 @@ namespace mineplex\plugin\bench\game\components\countdown; - use mineplex\plugin\bench\arenas\Arena; use mineplex\plugin\bench\arenas\events\ArenaEndEvent; use mineplex\plugin\bench\game\components\gamestate\GameStateComponent; +use mineplex\plugin\bench\time\BenchSchedule; +use mineplex\plugin\bench\time\BenchTask; +use mineplex\plugin\bench\time\BenchTaskData; use pocketmine\event\HandlerList; use pocketmine\event\Listener; -use pocketmine\scheduler\PluginTask; use pocketmine\Server; use mineplex\plugin\bench\game\components\gamestate\events\GameStateChangeEvent; -class GameStateCountdown extends PluginTask implements Listener { +class GameStateCountdown implements Listener, BenchTask { private $startCount; private $count; private $gameStateComponent; private $arena; - private $task; - private $startGameState; private $setGameState; + private $scheduler; + + + public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState) { - parent::__construct($arena->getPlugin()); + + $this->scheduler = new BenchSchedule($arena->getPlugin()); + $this->arena = $arena; $this->gameStateComponent = $gameStateComponent; @@ -55,14 +60,12 @@ class GameStateCountdown extends PluginTask implements Listener { if ($event->getToGameState() == $this->startGameState) { - $this->task = Server::getInstance()->getScheduler()->scheduleRepeatingTask($this, 20); + $this->scheduler->runTaskTimer($this, 1000, 1000); } else { $this->count = $this->startCount; - $this->task->cancel(); - } } @@ -74,7 +77,7 @@ class GameStateCountdown extends PluginTask implements Listener { HandlerList::unregisterAll($this); } - public function onRun($currentTick) + public function run(BenchTaskData $data) { print "Count: $this->count"."\n"; @@ -85,9 +88,11 @@ class GameStateCountdown extends PluginTask implements Listener { if ($this->count <= 0) { $this->gameStateComponent->setGameState($this->setGameState); + $data->end(); } } + public function popup() { foreach ($this->arena->getPlayers() as $player) @@ -95,4 +100,5 @@ class GameStateCountdown extends PluginTask implements Listener { $player->sendPopup("Countdown: $this->count"); } } + } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/Feature.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/Feature.php new file mode 100644 index 000000000..026352b4b --- /dev/null +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/Feature.php @@ -0,0 +1,20 @@ +arena = $arena; + + unset ($features[GameState::RESTARTING]); + + $this->features = $features; + + Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin()); + } + + public function onGameStateChange(GameStateChangeEvent $event) + { + if ($event->getArena() !== $this->arena) + return; + + $lastFeatures = $this->features[$event->getFromGameState()]; + $theseFeatures = $this->features[$event->getToGameState()]; + + $toEnable = array_diff($theseFeatures, $lastFeatures); + $toDisable = array_diff($lastFeatures, $theseFeatures); + + foreach ($toEnable as $feature) { + //$feature-> + } + + } + + public function onEnd(ArenaEndEvent $event) + { + if ($event->getArena() !== $this->arena) + return; + HandlerList::unregisterAll($this); + } +} \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/features/NoBlockBreak.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/features/NoBlockBreak.php new file mode 100644 index 000000000..e69533750 --- /dev/null +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/feature/features/NoBlockBreak.php @@ -0,0 +1,51 @@ +arena = $arena; + } + + public function onBlockBreak(BlockBreakEvent $event) + { + if (!in_array($event->getPlayer(), $this->arena->getPlayers())) + return; + $event->setCancelled(true); + } + + public function enable() + { + $this->enabled = true; + Server::getInstance()->getPluginManager()->registerEvents($this, $this->arena->getPlugin()); + } + + public function disable() + { + $this->enabled = false; + HandlerList::unregisterAll($this); + } + public function isEnabled() + { + return $this->enabled; + } +} \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameState.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameState.php index d72fbcbc8..46413d66d 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameState.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameState.php @@ -12,10 +12,10 @@ namespace mineplex\plugin\bench\game\components\gamestate; //Yes yes I know this is a horrible way of doing things, I'm just trying to get done fast. class GameState { - const LOBBY = 1; - const PRE_GAME = 2; - const GAME = 3; - const POST_GAME = 4; - const RESTARTING = 5; + const LOBBY = 0; + const PRE_GAME = 1; + const GAME = 2; + const POST_GAME = 3; + const RESTARTING = 4; } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/Test.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/Test.php deleted file mode 100644 index eb777d4a3..000000000 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/Test.php +++ /dev/null @@ -1,15 +0,0 @@ -getGameState(); \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/games/pvp/Pvp.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/games/pvp/Pvp.php index 9312d955e..4b1d25002 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/games/pvp/Pvp.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/games/pvp/Pvp.php @@ -8,6 +8,7 @@ namespace mineplex\plugin\bench\game\games\pvp; use mineplex\plugin\bench\game\components\countdown\GameStateCountdown; +use mineplex\plugin\bench\game\components\feature\features\NoBlockBreak; use mineplex\plugin\bench\game\components\gamestate\GameState; use mineplex\plugin\bench\game\components\gamestate\GameStateComponent; use pocketmine\event\Listener; @@ -20,6 +21,9 @@ class Pvp implements Game, Listener { { $gameStateComponent = new GameStateComponent($arena); + array( + ); + new GameStateCountdown($arena, $gameStateComponent, 20, GameState::LOBBY, GameState::PRE_GAME); //new GameStateCountdown($arena, $gameStateComponent, 10, GameState::PRE_GAME, GameState::GAME); diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchSchedule.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchSchedule.php new file mode 100644 index 000000000..98645c167 --- /dev/null +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchSchedule.php @@ -0,0 +1,85 @@ +getScheduler()->scheduleRepeatingTask($this, 1); + } + + //Fires off an event each tick, containing a list of all updateTypes + + public function onRun($currentTick) + { + $currentTime = round(microtime(true) * 1000); + + foreach ($this->tasks as $key => $task) { + + if (!($currentTime >= $task->getNextRun())) + continue; + + $task->getTaskData()->getTask()->run($task->getTaskData()); + + if ($task->getTaskData()->getPeriod() == null or $task->getTaskData()->getPeriod() < 0) { + + unset($this->tasks[$key]); + + } else { + $task->setNextRun($currentTime + $task->getTaskData()->getPeriod()); + } + } + } + + public function runTaskTimer(BenchTask $task, $wait, $period) + { + $taskData = new BenchTaskData($task, $period); + $actualTask = new ActualBenchTask($taskData, round(microtime(true) * 1000) + $wait); + array_push($this->tasks, $actualTask); + } +} + +class ActualBenchTask +{ + private $nextRun; + + private $benchTaskData; + + public function __construct(BenchTaskData $benchTaskData, $nextRun) + { + $this->benchTaskData = $benchTaskData; + $this->runNext = $nextRun; + } + + function getNextRun() + { + return $this->nextRun; + } + + function setNextRun($nextRun) + { + $this->nextRun = $nextRun; + } + + function getTaskData() + { + return $this->benchTaskData; + } + +} \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php new file mode 100644 index 000000000..518244060 --- /dev/null +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php @@ -0,0 +1,17 @@ +task = $task; + $this->period = $period; + } + + public function getTask() + { + return $this->task; + } + + public function getPeriod() + { + return $this->period; + } + + public function setPeriod($period) + { + $this->period = $period; + } + + public function end() + { + $this->period = null; + } +} \ 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 e219e179c..57089225b 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/core/updater/Updater.php @@ -19,8 +19,8 @@ class Updater extends PluginTask public function __construct(PluginBase $host) { + parent::__construct($host); $this->plugin = $host; - $this->owner = $host; $this->plugin->getServer()->getScheduler()->scheduleRepeatingTask($this, 1); @@ -50,14 +50,11 @@ class Updater extends PluginTask $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 ($timeSpent >= $updateType->time) { + array_push($updateTypes, $updateType); } } - //Call Event $this->plugin->getServer()->getPluginManager()->callEvent(new UpdateEvent($currentTick, $updateTypes)); }