From 73ffc97cc90a2c0633e2b8a7125ca8611632d017 Mon Sep 17 00:00:00 2001 From: Aaron Brock Date: Fri, 3 Jul 2015 14:14:24 -0400 Subject: [PATCH] A more complete BenchSchedule. Signed-off-by: Aaron Brock --- .../src/mineplex/plugin/bench/game/Game.php | 2 +- .../countdown/GameStateCountdown.php | 11 +- .../gamestate/GameStateComponent.php | 2 +- .../gamestate/events/GameStateChangeEvent.php | 2 +- .../bench/game/games/pvp/PvpComponent.php | 2 +- .../plugin/bench/time/BenchSchedule.php | 123 +++++++++++++++--- .../mineplex/plugin/bench/time/BenchTask.php | 2 +- .../plugin/bench/time/BenchTaskData.php | 53 +++++++- 8 files changed, 160 insertions(+), 37 deletions(-) diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/Game.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/Game.php index e05da5701..afbb38aba 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/Game.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/Game.php @@ -1,7 +1,7 @@ scheduler = new BenchSchedule($arena->getPlugin()); - $this->arena = $arena; $this->gameStateComponent = $gameStateComponent; @@ -59,12 +53,11 @@ class GameStateCountdown implements Listener, BenchTask { if ($event->getToGameState() == $this->startGameState) { - - $this->scheduler->runTaskTimer($this, 1000, 1000); - + BenchSchedule::runTaskTimer($this, 1000, 1000); } else { + BenchSchedule::cancelTask($this); $this->count = $this->startCount; } } diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameStateComponent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameStateComponent.php index 029ac3b90..00f9c8036 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameStateComponent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/game/components/gamestate/GameStateComponent.php @@ -1,7 +1,7 @@ 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); @@ -37,28 +47,98 @@ class BenchSchedule extends PluginTask $task->getTaskData()->getTask()->run($task->getTaskData()); - if ($task->getTaskData()->getPeriod() == null or $task->getTaskData()->getPeriod() < 0) { - + if ($task->getTaskData()->getNextRun() !== null) + { + $task->setNextRun($task->getTaskData()->getNextRun()); + $task->getTaskData()->setNextRun(null); + } elseif ($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) + + /** + * @param BenchTask $taskToCancel + */ + public static function cancelTask(BenchTask $taskToCancel) { - $taskData = new BenchTaskData($task, $period); - $actualTask = new ActualBenchTask($taskData, round(microtime(true) * 1000) + $wait); - array_push($this->tasks, $actualTask); + foreach (self::getInstance()->tasks as $key => $task) + { + if ($task->getTaskData()->getTask() === $taskToCancel) + { + unset (self::getInstance()->tasks[$key]); + } + } } + + /** + * @param BenchTask $taskToCancel + * @param $id + */ + public static function cancelTaskWithId(BenchTask $taskToCancel, $id) + { + foreach (self::getInstance()->tasks as $key => $task) + { + if ($task->getTaskData()->getTask() === $taskToCancel && $task->getTaskData()->getId() === $id) + { + unset (self::getInstance()->tasks[$key]); + } + } + } + + /** + * @param BenchTask $task + * @param int $wait + * @param int $period + * @param $id + */ + public static function runTaskTimerWithId(BenchTask $task, $wait, $period, $id) + { + $taskData = new BenchTaskData($task, $period, $id); + $actualTask = new ActualBenchTask($taskData, round(microtime(true) * 1000) + $wait); + array_push(self::getInstance()->tasks, $actualTask); + } + + /** + * @param BenchTask $task + * @param int $wait + * @param int $period + */ + public static function runTaskTimer(BenchTask $task, $wait, $period) + { + self::runTaskTimerWithId($task, $wait, $period, null); + } + + /** + * @param BenchTask $task + * @param int $wait + * @param $id + */ + public static function runTaskLaterWithId(BenchTask $task, $wait, $id) + { + self::runTaskTimerWithId($task, $wait, null, $id); + } + + /** + * @param BenchTask $task + * @param int $wait + */ + public static function runTaskLater(BenchTask $task, $wait) + { + self::runTaskTimerWithId($task, $wait, null, null); + } + } class ActualBenchTask { + /** @var int */ private $nextRun; + /** @var BenchTaskData */ private $benchTaskData; public function __construct(BenchTaskData $benchTaskData, $nextRun) @@ -67,19 +147,28 @@ class ActualBenchTask $this->runNext = $nextRun; } + /** + * @return int + */ function getNextRun() { return $this->nextRun; } + /** + * @param $nextRun + */ function setNextRun($nextRun) { $this->nextRun = $nextRun; } + /** + * @return BenchTaskData + */ 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 index 518244060..6aaf8283b 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/bench/time/BenchTask.php @@ -1,7 +1,7 @@ task = $task; $this->period = $period; + $this->id = $id; } + /** + * @param int $period + */ + public function setPeriod($period) + { + $this->period = $period; + } + + /** + * @param int $nextRun + */ + public function setNextRun($nextRun) + { + $this->nextRun = $nextRun; + } + + /** + * @return int + */ + public function getNextRun() + { + return $this->nextRun; + } + + /** + * @return BenchTask + */ public function getTask() { return $this->task; } + /** + * @return int + */ public function getPeriod() { return $this->period; } - public function setPeriod($period) + /** + * @return mixed + */ + public function getId() { - $this->period = $period; + return $this->id; } + public function end() { $this->period = null;