A more complete BenchSchedule.
Signed-off-by: Aaron Brock <TheMineBench@gmail.com>
This commit is contained in:
parent
f2f038f1cc
commit
73ffc97cc9
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 6/30/2015
|
||||
* Time: 9:04 PM
|
||||
*/
|
||||
|
@ -28,15 +28,9 @@ class GameStateCountdown implements Listener, BenchTask {
|
||||
private $startGameState;
|
||||
private $setGameState;
|
||||
|
||||
private $scheduler;
|
||||
|
||||
|
||||
|
||||
public function __construct(Arena $arena, GameStateComponent $gameStateComponent, $count, $startGameState, $setGameState)
|
||||
{
|
||||
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 7/1/2015
|
||||
* Time: 10:26 AM
|
||||
*/
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 7/1/2015
|
||||
* Time: 10:49 AM
|
||||
*/
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 6/30/2015
|
||||
* Time: 10:15 PM
|
||||
*/
|
||||
|
@ -1,31 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 7/2/2015
|
||||
* Time: 1:53 PM
|
||||
*/
|
||||
|
||||
namespace mineplex\plugin\bench\time;
|
||||
|
||||
use pocketmine\scheduler\PluginTask;
|
||||
use pocketmine\plugin\Plugin;
|
||||
use pocketmine\scheduler\Task;
|
||||
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[] */
|
||||
private $tasks = [];
|
||||
|
||||
public function __construct(Plugin $host)
|
||||
private function __construct()
|
||||
{
|
||||
parent::__construct($host);
|
||||
|
||||
Server::getInstance()->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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* User: TheMineBench
|
||||
* Date: 7/2/2015
|
||||
* Time: 2:01 PM
|
||||
*/
|
||||
|
@ -2,40 +2,81 @@
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Exerosis
|
||||
* Date: 7/2/2015
|
||||
* Time: 2:58 PM
|
||||
* Date: 7/3/2015
|
||||
* Time: 2:03 PM
|
||||
*/
|
||||
|
||||
namespace mineplex\plugin\bench\time;
|
||||
|
||||
|
||||
|
||||
class BenchTaskData {
|
||||
|
||||
/** @var BenchTask */
|
||||
/** @var BenchTask */
|
||||
private $task;
|
||||
/** @var Integer */
|
||||
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->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;
|
||||
|
Loading…
Reference in New Issue
Block a user