From 3f202d39aec5bdb5e6a3f2fc3c05dd750a1629d7 Mon Sep 17 00:00:00 2001 From: Aaron Brock Date: Sun, 5 Jul 2015 22:45:14 -0400 Subject: [PATCH] LobbyCountdown stuff Signed-off-by: Aaron Brock --- .../components/countdown/LobbyCountdown.php | 119 +++++++++++++++--- .../gamestate/GameStateComponent.php | 6 +- .../game/components/world/WorldComponent.php | 2 +- .../world/event/WorldLoadSuccessEvent.php | 12 +- .../game/games/sg/SurvivalGames.php | 6 +- .../plugin/gameengine/time/BenchSchedule.php | 41 ++++++ 6 files changed, 165 insertions(+), 21 deletions(-) diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/countdown/LobbyCountdown.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/countdown/LobbyCountdown.php index 2c61747cd..bac78d405 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/countdown/LobbyCountdown.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/countdown/LobbyCountdown.php @@ -10,12 +10,17 @@ namespace mineplex\plugin\gameengine\game\components\countdown; use mineplex\plugin\gameengine\arenas\Arena; use mineplex\plugin\gameengine\arenas\events\ArenaEndEvent; +use mineplex\plugin\gameengine\arenas\events\ArenaJoinEvent; +use mineplex\plugin\gameengine\arenas\events\ArenaQuitEvent; use mineplex\plugin\gameengine\game\components\gamestate\GameStateComponent; +use mineplex\plugin\gameengine\game\components\world\event\WorldLoadSuccessEvent; +use mineplex\plugin\gameengine\game\components\world\WorldComponent; use mineplex\plugin\gameengine\time\BenchSchedule; use mineplex\plugin\gameengine\time\BenchTask; use mineplex\plugin\gameengine\time\BenchTaskData; use pocketmine\event\HandlerList; use pocketmine\event\Listener; +use pocketmine\Player; use pocketmine\Server; use mineplex\plugin\gameengine\game\components\gamestate\events\GameStateChangeEvent; @@ -24,14 +29,26 @@ class LobbyCountdown implements Listener, BenchTask { private $startCount; private $count; private $gameStateComponent; + private $worldComponent; private $arena; private $startGameState; private $setGameState; + private $minPlayers; - public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState) + private $message; + + const WAITING_FOR_PLAYERS = "Waiting for players!"; + + const POPUP_ID = "popup"; + const COUNTDOWN_ID = "count"; + + + public function __construct(Arena $arena, GameStateComponent $gameStateComponent, WorldComponent $worldComponent, $startGameState, $setGameState, $count, $minPlayers = 1) { $this->arena = $arena; $this->gameStateComponent = $gameStateComponent; + $this->worldComponent = $worldComponent; + $this->minPlayers = $minPlayers; $this->startCount = $count; $this->count = $count; @@ -39,18 +56,60 @@ class LobbyCountdown implements Listener, BenchTask { $this->startGameState = $startGameState; $this->setGameState = $setGameState; + $this->message = self::WAITING_FOR_PLAYERS; + Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin()); } - - function checkCountdown() + /** + * @param bool $addOne + */ + function checkCountdown($addOne = false) { + $playerCount = (count($this->arena->getPlayers()) + $addOne); + if ($this->gameStateComponent->getGameState() == $this->startGameState && $this->worldComponent->isWorldReady() && $playerCount >= $this->minPlayers) + { + if (!BenchSchedule::isRunningWithId($this, self::COUNTDOWN_ID)) + { + $this->count = $this->startCount; + $this->setCount(); + BenchSchedule::runTaskTimerWithId($this, 1000, 1000, self::COUNTDOWN_ID); + } + } + else + { + $this->setWaiting($playerCount); + BenchSchedule::cancelTaskWithId($this, self::COUNTDOWN_ID); + } } + public function onJoin(ArenaJoinEvent $event) + { + if ($event->getArena() !== $this->arena) + return; + $this->checkCountdown(true); + $this->popup($event->getPlayer()); + } + + public function onQuit(ArenaQuitEvent $event) + { + if ($event->getArena() !== $this->arena) + return; + $this->checkCountdown(); + } + + public function onWorldCreation(WorldLoadSuccessEvent $event) + { + if ($event->getArena() !== $this->arena) + return; + + $this->checkCountdown(); + } + public function onGameStateChange(GameStateChangeEvent $event) { if ($event->getArena() !== $this->arena) @@ -58,11 +117,15 @@ class LobbyCountdown implements Listener, BenchTask { if ($event->getToGameState() == $this->startGameState) { - BenchSchedule::runTaskTimer($this, 1000, 1000); + $this->checkCountdown(); + BenchSchedule::runTaskTimerWithId($this, 500, 500, self::POPUP_ID); + } - else + elseif ($event->getFromGameState() == $this->startGameState) { + //Cancels both tasks BenchSchedule::cancelTask($this); + $this->count = $this->startCount; } } @@ -77,27 +140,51 @@ class LobbyCountdown implements Listener, BenchTask { public function run(BenchTaskData $data) { - print "Count: $this->count"."\n"; - - if ($this->count <= 0) + if ($data->getId() == self::POPUP_ID) { - $this->gameStateComponent->setGameState($this->setGameState); - $data->end(); + $this->popupAll(); } + else //if ($data->getId() == self::COUNTDOWN_ID) + { + if ($this->count <= 0) + { + $this->gameStateComponent->setGameState($this->setGameState); + $data->end(); + return; + } - $this->popup(); - - $this->count--; - + $this->message = "§9Game starting in:§c $this->count"; + $this->count--; + } } - public function popup() + private function setWaiting($playerCount = null) + { + if ($playerCount == null) + $playerCount = count($this->arena->getPlayers()); + $this->message = "§2Waiting for players! §a($playerCount/$this->minPlayers)"; + $this->popupAll(); + } + + private function setCount($count = null) + { + if ($count == null) + $count = $this->count; + $this->message = "§9Game starting in:§c $count"; + $this->popupAll(); + } + + private function popupAll() { foreach ($this->arena->getPlayers() as $player) { - $player->sendPopup("Countdown: $this->count"); + $this->popup($player); } } + private function popup(Player $player) + { + $player->sendPopup($this->message); + } } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/gamestate/GameStateComponent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/gamestate/GameStateComponent.php index 016942ea2..ecaad7131 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/gamestate/GameStateComponent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/gamestate/GameStateComponent.php @@ -75,12 +75,14 @@ class GameStateComponent implements Listener { private function localSetState($gameState) { + $oldGameState = $this->getGameState(); - $event = new GameStateChangeEvent($this->arena, $this->gameState, $gameState); + $this->gameState = $gameState; + + $event = new GameStateChangeEvent($this->arena, $oldGameState, $gameState); //Not sure if I should call the event before of after... Server::getInstance()->getPluginManager()->callEvent($event); - $this->gameState = $gameState; } public function getGameState() diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/WorldComponent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/WorldComponent.php index f8f313bf7..e1d69501b 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/WorldComponent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/WorldComponent.php @@ -124,7 +124,7 @@ class WorldComponent implements Listener print("Successfully Loaded World: " . $this->gameFolder . "\n"); - Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena)); + Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena, $this->world)); } else { diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/event/WorldLoadSuccessEvent.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/event/WorldLoadSuccessEvent.php index 9e15445e2..d668bcdd8 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/event/WorldLoadSuccessEvent.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/components/world/event/WorldLoadSuccessEvent.php @@ -5,13 +5,23 @@ namespace mineplex\plugin\gameengine\game\components\world\event; use mineplex\plugin\gameengine\arenas\ArenaEvent; use mineplex\plugin\gameengine\arenas\Arena; +use pocketmine\level\Level; class WorldLoadSuccessEvent extends ArenaEvent { public static $handlerList = null; - public function __construct(Arena $arena) + private $level; + + public function __construct(Arena $arena, Level $level) { parent::__construct($arena); + $this->level = $level; } + + function getLevel() + { + return $this->level; + } + } \ No newline at end of file diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/games/sg/SurvivalGames.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/games/sg/SurvivalGames.php index 2636056f9..fdf04488e 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/games/sg/SurvivalGames.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/game/games/sg/SurvivalGames.php @@ -9,11 +9,13 @@ namespace mineplex\plugin\gameengine\game\games\sg; use mineplex\plugin\gameengine\game\components\countdown\GameStateCountdown; +use mineplex\plugin\gameengine\game\components\countdown\LobbyCountdown; use mineplex\plugin\gameengine\game\components\feature\features\NoBlockBreak; use mineplex\plugin\gameengine\game\components\feature\GameStateFeatureManager; use mineplex\plugin\gameengine\game\components\gamestate\GameState; use mineplex\plugin\gameengine\game\components\gamestate\GameStateComponent; use mineplex\plugin\gameengine\game\components\lobby\LobbyComponent; +use mineplex\plugin\gameengine\game\components\world\WorldComponent; use mineplex\plugin\gameengine\game\Game; use mineplex\plugin\gameengine\arenas\Arena; use mineplex\plugin\gameengine\game\components\feature\Feature; @@ -40,7 +42,9 @@ class SurvivalGames implements Game { //new GameStateFeatureManager($arena, $features); - new GameStateCountdown($arena, $gameStateComponent, 20, GameState::LOBBY, GameState::PRE_GAME); + $worldComponent = new WorldComponent($arena); + + new LobbyCountdown($arena, $gameStateComponent, $worldComponent, GameState::LOBBY, GameState::PRE_GAME, 50); new GameStateCountdown($arena, $gameStateComponent, 10, GameState::PRE_GAME, GameState::GAME); diff --git a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/time/BenchSchedule.php b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/time/BenchSchedule.php index a6ed2a870..16aaa19e7 100644 --- a/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/time/BenchSchedule.php +++ b/Pocket/plugins/Mineplex/src/mineplex/plugin/gameengine/time/BenchSchedule.php @@ -62,31 +62,72 @@ class BenchSchedule extends Task /** * @param BenchTask $taskToCancel + * @return bool */ public static function cancelTask(BenchTask $taskToCancel) { + $deleted = false; foreach (self::getInstance()->tasks as $key => $task) { if ($task->getTaskData()->getTask() === $taskToCancel) { unset (self::getInstance()->tasks[$key]); + $deleted = true; } } + return $deleted; } /** * @param BenchTask $taskToCancel * @param $id + * @return bool */ public static function cancelTaskWithId(BenchTask $taskToCancel, $id) { + $deleted = false; foreach (self::getInstance()->tasks as $key => $task) { if ($task->getTaskData()->getTask() === $taskToCancel && $task->getTaskData()->getId() === $id) { unset (self::getInstance()->tasks[$key]); + $deleted = true; } } + return $deleted; + } + + /** + * @param BenchTask $taskToCancel + * @return bool + */ + public static function isRunning(BenchTask $taskToCancel) + { + foreach (self::getInstance()->tasks as $key => $task) + { + if ($task->getTaskData()->getTask() === $taskToCancel) + { + return true; + } + } + return false; + } + + /** + * @param BenchTask $taskToCancel + * @param $id + * @return bool + */ + public static function isRunningWithId(BenchTask $taskToCancel, $id) + { + foreach (self::getInstance()->tasks as $key => $task) + { + if ($task->getTaskData()->getTask() === $taskToCancel && $task->getTaskData()->getId() === $id) + { + return true; + } + } + return false; } /**