A more complete BenchSchedule.

Signed-off-by: Aaron Brock <TheMineBench@gmail.com>
This commit is contained in:
Aaron Brock 2015-07-03 14:14:24 -04:00
parent f2f038f1cc
commit 73ffc97cc9
8 changed files with 160 additions and 37 deletions

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 6/30/2015 * Date: 6/30/2015
* Time: 9:04 PM * Time: 9:04 PM
*/ */

View File

@ -28,15 +28,9 @@ class GameStateCountdown implements Listener, BenchTask {
private $startGameState; private $startGameState;
private $setGameState; private $setGameState;
private $scheduler;
public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState) public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState)
{ {
$this->scheduler = new BenchSchedule($arena->getPlugin());
$this->arena = $arena; $this->arena = $arena;
$this->gameStateComponent = $gameStateComponent; $this->gameStateComponent = $gameStateComponent;
@ -59,12 +53,11 @@ class GameStateCountdown implements Listener, BenchTask {
if ($event->getToGameState() == $this->startGameState) if ($event->getToGameState() == $this->startGameState)
{ {
BenchSchedule::runTaskTimer($this, 1000, 1000);
$this->scheduler->runTaskTimer($this, 1000, 1000);
} }
else else
{ {
BenchSchedule::cancelTask($this);
$this->count = $this->startCount; $this->count = $this->startCount;
} }
} }

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 7/1/2015 * Date: 7/1/2015
* Time: 10:26 AM * Time: 10:26 AM
*/ */

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 7/1/2015 * Date: 7/1/2015
* Time: 10:49 AM * Time: 10:49 AM
*/ */

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 6/30/2015 * Date: 6/30/2015
* Time: 10:15 PM * Time: 10:15 PM
*/ */

View File

@ -1,31 +1,41 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 7/2/2015 * Date: 7/2/2015
* Time: 1:53 PM * Time: 1:53 PM
*/ */
namespace mineplex\plugin\bench\time; namespace mineplex\plugin\bench\time;
use pocketmine\scheduler\PluginTask; use pocketmine\scheduler\Task;
use pocketmine\plugin\Plugin;
use pocketmine\Server; use pocketmine\Server;
class BenchSchedule extends PluginTask class BenchSchedule extends Task
{ {
/** @var BenchSchedule */
private static $instance = null;
/**
* @return BenchSchedule
*/
private static function getInstance()
{
if (self::$instance == null)
{
self::$instance = new BenchSchedule();
}
return self::$instance;
}
/** @var ActualBenchTask[] */ /** @var ActualBenchTask[] */
private $tasks = []; private $tasks = [];
public function __construct(Plugin $host) private function __construct()
{ {
parent::__construct($host);
Server::getInstance()->getScheduler()->scheduleRepeatingTask($this, 1); Server::getInstance()->getScheduler()->scheduleRepeatingTask($this, 1);
} }
//Fires off an event each tick, containing a list of all updateTypes
public function onRun($currentTick) public function onRun($currentTick)
{ {
$currentTime = round(microtime(true) * 1000); $currentTime = round(microtime(true) * 1000);
@ -37,28 +47,98 @@ class BenchSchedule extends PluginTask
$task->getTaskData()->getTask()->run($task->getTaskData()); $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]); unset($this->tasks[$key]);
} else { } else {
$task->setNextRun($currentTime + $task->getTaskData()->getPeriod()); $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); foreach (self::getInstance()->tasks as $key => $task)
$actualTask = new ActualBenchTask($taskData, round(microtime(true) * 1000) + $wait); {
array_push($this->tasks, $actualTask); 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 class ActualBenchTask
{ {
/** @var int */
private $nextRun; private $nextRun;
/** @var BenchTaskData */
private $benchTaskData; private $benchTaskData;
public function __construct(BenchTaskData $benchTaskData, $nextRun) public function __construct(BenchTaskData $benchTaskData, $nextRun)
@ -67,16 +147,25 @@ class ActualBenchTask
$this->runNext = $nextRun; $this->runNext = $nextRun;
} }
/**
* @return int
*/
function getNextRun() function getNextRun()
{ {
return $this->nextRun; return $this->nextRun;
} }
/**
* @param $nextRun
*/
function setNextRun($nextRun) function setNextRun($nextRun)
{ {
$this->nextRun = $nextRun; $this->nextRun = $nextRun;
} }
/**
* @return BenchTaskData
*/
function getTaskData() function getTaskData()
{ {
return $this->benchTaskData; return $this->benchTaskData;

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: TheMineBench
* Date: 7/2/2015 * Date: 7/2/2015
* Time: 2:01 PM * Time: 2:01 PM
*/ */

View File

@ -2,40 +2,81 @@
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: Exerosis * User: Exerosis
* Date: 7/2/2015 * Date: 7/3/2015
* Time: 2:58 PM * Time: 2:03 PM
*/ */
namespace mineplex\plugin\bench\time; namespace mineplex\plugin\bench\time;
class BenchTaskData { class BenchTaskData {
/** @var BenchTask */ /** @var BenchTask */
private $task; private $task;
/** @var Integer */
private $period; private $period;
/** @var Integer */
private $nextRun;
public function __construct(BenchTask $task, $period) private $id;
public function __construct(BenchTask $task, $period, $id)
{ {
$this->task = $task; $this->task = $task;
$this->period = $period; $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() public function getTask()
{ {
return $this->task; return $this->task;
} }
/**
* @return int
*/
public function getPeriod() public function getPeriod()
{ {
return $this->period; return $this->period;
} }
public function setPeriod($period) /**
* @return mixed
*/
public function getId()
{ {
$this->period = $period; return $this->id;
} }
public function end() public function end()
{ {
$this->period = null; $this->period = null;