Added bench time stuff.

Signed-off-by: Aaron Brock <TheMineBench@gmail.com>
This commit is contained in:
Aaron Brock 2015-07-03 01:02:16 -04:00
parent 61d2247cee
commit 060a928dd5
12 changed files with 327 additions and 37 deletions

View File

@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/1/2015
* Time: 1:25 PM
*/
$array = array('1','2','3','4','5');
foreach ($array as $key => $moo)
{
if ($moo == '2')
unset ($array[$key]);
}
print_r($array);

View File

@ -8,30 +8,35 @@
namespace mineplex\plugin\bench\game\components\countdown; namespace mineplex\plugin\bench\game\components\countdown;
use mineplex\plugin\bench\arenas\Arena; use mineplex\plugin\bench\arenas\Arena;
use mineplex\plugin\bench\arenas\events\ArenaEndEvent; use mineplex\plugin\bench\arenas\events\ArenaEndEvent;
use mineplex\plugin\bench\game\components\gamestate\GameStateComponent; 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\HandlerList;
use pocketmine\event\Listener; use pocketmine\event\Listener;
use pocketmine\scheduler\PluginTask;
use pocketmine\Server; use pocketmine\Server;
use mineplex\plugin\bench\game\components\gamestate\events\GameStateChangeEvent; use mineplex\plugin\bench\game\components\gamestate\events\GameStateChangeEvent;
class GameStateCountdown extends PluginTask implements Listener { class GameStateCountdown implements Listener, BenchTask {
private $startCount; private $startCount;
private $count; private $count;
private $gameStateComponent; private $gameStateComponent;
private $arena; private $arena;
private $task;
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)
{ {
parent::__construct($arena->getPlugin());
$this->scheduler = new BenchSchedule($arena->getPlugin());
$this->arena = $arena; $this->arena = $arena;
$this->gameStateComponent = $gameStateComponent; $this->gameStateComponent = $gameStateComponent;
@ -55,14 +60,12 @@ class GameStateCountdown extends PluginTask implements Listener {
if ($event->getToGameState() == $this->startGameState) if ($event->getToGameState() == $this->startGameState)
{ {
$this->task = Server::getInstance()->getScheduler()->scheduleRepeatingTask($this, 20); $this->scheduler->runTaskTimer($this, 1000, 1000);
} }
else else
{ {
$this->count = $this->startCount; $this->count = $this->startCount;
$this->task->cancel();
} }
} }
@ -74,7 +77,7 @@ class GameStateCountdown extends PluginTask implements Listener {
HandlerList::unregisterAll($this); HandlerList::unregisterAll($this);
} }
public function onRun($currentTick) public function run(BenchTaskData $data)
{ {
print "Count: $this->count"."\n"; print "Count: $this->count"."\n";
@ -85,9 +88,11 @@ class GameStateCountdown extends PluginTask implements Listener {
if ($this->count <= 0) if ($this->count <= 0)
{ {
$this->gameStateComponent->setGameState($this->setGameState); $this->gameStateComponent->setGameState($this->setGameState);
$data->end();
} }
} }
public function popup() public function popup()
{ {
foreach ($this->arena->getPlayers() as $player) foreach ($this->arena->getPlayers() as $player)
@ -95,4 +100,5 @@ class GameStateCountdown extends PluginTask implements Listener {
$player->sendPopup("Countdown: $this->count"); $player->sendPopup("Countdown: $this->count");
} }
} }
} }

View File

@ -0,0 +1,20 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/1/2015
* Time: 10:18 PM
*/
namespace mineplex\plugin\bench\game\components\feature;
interface Feature {
public function enable();
public function disable();
/**
* @return boolean
*/
public function isEnabled();
}

View File

@ -0,0 +1,65 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/2/2015
* Time: 12:44 AM
*/
namespace mineplex\plugin\bench\game\components\feature;
use mineplex\plugin\bench\arenas\Arena;
use mineplex\plugin\bench\arenas\events\ArenaEndEvent;
use mineplex\plugin\bench\game\components\gamestate\events\GameStateChangeEvent;
use mineplex\plugin\bench\game\components\gamestate\GameState;
use pocketmine\event\HandlerList;
use pocketmine\event\Listener;
use pocketmine\Server;
class GameStateFeatureManager implements Listener {
private $arena;
/** @var feature[][] */
private $features;
/**
* @param Arena $arena
* @param feature[][] $features
*/
public function __construct(Arena $arena, array $features)
{
$this->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);
}
}

View File

@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/1/2015
* Time: 10:20 PM
*/
namespace mineplex\plugin\bench\game\components\feature\features;
use mineplex\plugin\bench\arenas\Arena;
use mineplex\plugin\bench\game\components\feature\Feature;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\HandlerList;
use pocketmine\event\Listener;
use pocketmine\Server;
class NoBlockBreak implements Feature, Listener {
private $arena;
private $enabled = false;
public function __construct(Arena $arena)
{
$this->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;
}
}

View File

@ -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. //Yes yes I know this is a horrible way of doing things, I'm just trying to get done fast.
class GameState { class GameState {
const LOBBY = 1; const LOBBY = 0;
const PRE_GAME = 2; const PRE_GAME = 1;
const GAME = 3; const GAME = 2;
const POST_GAME = 4; const POST_GAME = 3;
const RESTARTING = 5; const RESTARTING = 4;
} }

View File

@ -1,15 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/1/2015
* Time: 1:25 PM
*/
namespace mineplex\plugin\bench\game\components\gamestate;
print 'hi\n';
$gameStateComponent = new GameStateComponent();
echo 'State: '.$gameStateComponent->getGameState();

View File

@ -8,6 +8,7 @@
namespace mineplex\plugin\bench\game\games\pvp; namespace mineplex\plugin\bench\game\games\pvp;
use mineplex\plugin\bench\game\components\countdown\GameStateCountdown; 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\GameState;
use mineplex\plugin\bench\game\components\gamestate\GameStateComponent; use mineplex\plugin\bench\game\components\gamestate\GameStateComponent;
use pocketmine\event\Listener; use pocketmine\event\Listener;
@ -20,6 +21,9 @@ class Pvp implements Game, Listener {
{ {
$gameStateComponent = new GameStateComponent($arena); $gameStateComponent = new GameStateComponent($arena);
array(
);
new GameStateCountdown($arena, $gameStateComponent, 20, GameState::LOBBY, GameState::PRE_GAME); new GameStateCountdown($arena, $gameStateComponent, 20, GameState::LOBBY, GameState::PRE_GAME);
//new GameStateCountdown($arena, $gameStateComponent, 10, GameState::PRE_GAME, GameState::GAME); //new GameStateCountdown($arena, $gameStateComponent, 10, GameState::PRE_GAME, GameState::GAME);

View File

@ -0,0 +1,85 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/2/2015
* Time: 1:53 PM
*/
namespace mineplex\plugin\bench\time;
use pocketmine\scheduler\PluginTask;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
class BenchSchedule extends PluginTask
{
/** @var ActualBenchTask[] */
private $tasks = [];
public function __construct(Plugin $host)
{
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);
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;
}
}

View File

@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/2/2015
* Time: 2:01 PM
*/
namespace mineplex\plugin\bench\time;
use SplObjectStorage;
interface BenchTask {
public function run(BenchTaskData $data);
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* Date: 7/2/2015
* Time: 2:58 PM
*/
namespace mineplex\plugin\bench\time;
class BenchTaskData {
/** @var BenchTask */
private $task;
private $period;
public function __construct(BenchTask $task, $period)
{
$this->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;
}
}

View File

@ -19,8 +19,8 @@ class Updater extends PluginTask
public function __construct(PluginBase $host) public function __construct(PluginBase $host)
{ {
parent::__construct($host);
$this->plugin = $host; $this->plugin = $host;
$this->owner = $host;
$this->plugin->getServer()->getScheduler()->scheduleRepeatingTask($this, 1); $this->plugin->getServer()->getScheduler()->scheduleRepeatingTask($this, 1);
@ -50,14 +50,11 @@ class Updater extends PluginTask
$timeSpent = round(microtime(true) * 1000) - $this->last; $timeSpent = round(microtime(true) * 1000) - $this->last;
$this->last = round(microtime(true) * 1000); $this->last = round(microtime(true) * 1000);
foreach ($this->updateTypes as &$updateType) foreach ($this->updateTypes as &$updateType) {
{ if ($timeSpent >= $updateType->time) {
if ($timeSpent >= $updateType->time) array_push($updateTypes, $updateType);
{
array_push($updateTypes, $updateType);
} }
} }
//Call Event //Call Event
$this->plugin->getServer()->getPluginManager()->callEvent(new UpdateEvent($currentTick, $updateTypes)); $this->plugin->getServer()->getPluginManager()->callEvent(new UpdateEvent($currentTick, $updateTypes));
} }