fixed UpdateEvent
added WorldComponent
This commit is contained in:
parent
e24890d292
commit
c7d758c8d8
@ -3,11 +3,12 @@
|
|||||||
namespace mineplex\plugin;
|
namespace mineplex\plugin;
|
||||||
|
|
||||||
use mineplex\plugin\bench\arenas\MultiGameArena;
|
use mineplex\plugin\bench\arenas\MultiGameArena;
|
||||||
|
use mineplex\plugin\bench\game\components\world\WorldComponent;
|
||||||
use mineplex\plugin\bench\game\factory\TestGameFactory;
|
use mineplex\plugin\bench\game\factory\TestGameFactory;
|
||||||
|
use mineplex\plugin\core\updater\Updater;
|
||||||
use pocketmine\event\Listener;
|
use pocketmine\event\Listener;
|
||||||
use pocketmine\event\player\PlayerJoinEvent;
|
use pocketmine\event\player\PlayerJoinEvent;
|
||||||
use pocketmine\event\player\PlayerLoginEvent;
|
use pocketmine\event\player\PlayerLoginEvent;
|
||||||
use pocketmine\event\player\PlayerQuitEvent;
|
|
||||||
use pocketmine\plugin\PluginBase;
|
use pocketmine\plugin\PluginBase;
|
||||||
use mineplex\plugin\bench\arenas\Arena;
|
use mineplex\plugin\bench\arenas\Arena;
|
||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
@ -16,23 +17,24 @@ class Main extends PluginBase implements Listener
|
|||||||
{
|
{
|
||||||
/** @var Arena */
|
/** @var Arena */
|
||||||
private $arena;
|
private $arena;
|
||||||
|
private $test;
|
||||||
|
|
||||||
public function onEnable()
|
public function onEnable()
|
||||||
{
|
{
|
||||||
|
|
||||||
$this->arena = new MultiGameArena($this, new TestGameFactory());
|
$this->arena = new MultiGameArena($this, new TestGameFactory());
|
||||||
Server::getInstance()->getPluginManager()->registerEvents($this, $this);
|
Server::getInstance()->getPluginManager()->registerEvents($this, $this);
|
||||||
|
|
||||||
//$this->getServer()->getScheduler()->scheduleRepeatingTask(new TickTask($this), 1);
|
$this->test = new WorldComponent($this->arena);
|
||||||
|
|
||||||
//$this->getServer()->getPluginManager()->registerEvents($this, $this);
|
//$this->getServer()->getPluginManager()->registerEvents($this, $this);
|
||||||
//new Updater($this);
|
|
||||||
|
new Updater($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onLogin(PlayerLoginEvent $event)
|
public function onLogin(PlayerLoginEvent $event)
|
||||||
{
|
{
|
||||||
if ($this->arena->canJoin($event->getPlayer()))
|
if ($this->arena->canJoin($event->getPlayer()))
|
||||||
retun;
|
return;
|
||||||
|
|
||||||
$event->setKickMessage("Unable to join game!");
|
$event->setKickMessage("Unable to join game!");
|
||||||
$event->setCancelled();
|
$event->setCancelled();
|
||||||
|
@ -9,12 +9,20 @@
|
|||||||
namespace mineplex\plugin\bench\game\components\world;
|
namespace mineplex\plugin\bench\game\components\world;
|
||||||
|
|
||||||
use mineplex\plugin\bench\arenas\Arena;
|
use mineplex\plugin\bench\arenas\Arena;
|
||||||
|
use mineplex\plugin\bench\game\components\world\event\WorldLoadFailEvent;
|
||||||
|
use mineplex\plugin\bench\game\components\world\event\WorldLoadSuccessEvent;
|
||||||
|
use mineplex\plugin\util\UtilString;
|
||||||
|
use mineplex\plugin\core\updater\UpdateEvent;
|
||||||
|
use mineplex\plugin\core\updater\UpdateType;
|
||||||
|
use pocketmine\event\level\LevelUnloadEvent;
|
||||||
|
use pocketmine\event\player\PlayerJoinEvent;
|
||||||
use pocketmine\event\Listener;
|
use pocketmine\event\Listener;
|
||||||
use pocketmine\level\Position;
|
use pocketmine\level\Position;
|
||||||
use pocketmine\math\Vector3;
|
|
||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
|
use pocketmine\Player;
|
||||||
|
|
||||||
//require_once __DIR__ . '\GameState.php';
|
use ZipArchive;
|
||||||
|
use Exception;
|
||||||
|
|
||||||
class WorldComponent implements Listener
|
class WorldComponent implements Listener
|
||||||
{
|
{
|
||||||
@ -24,44 +32,106 @@ class WorldComponent implements Listener
|
|||||||
private $gameFolder;
|
private $gameFolder;
|
||||||
|
|
||||||
private $world;
|
private $world;
|
||||||
private $worldName;
|
|
||||||
|
|
||||||
private $mapName;
|
private $mapName;
|
||||||
private $mapAuthor;
|
private $mapAuthor;
|
||||||
|
|
||||||
private $mapTeams = array();
|
private $mapTeams = array();
|
||||||
private $mapData = array();
|
private $mapData = array();
|
||||||
|
|
||||||
private $mapSettings = array();
|
private $mapSettings = array();
|
||||||
|
|
||||||
|
private $posTest = null;
|
||||||
|
private $player = null;
|
||||||
|
|
||||||
public function __construct(Arena $arena)
|
public function __construct(Arena $arena)
|
||||||
{
|
{
|
||||||
$this->arena = $arena;
|
$this->arena = $arena;
|
||||||
|
|
||||||
$this->gameId = getNewGameId();
|
$this->gameId = $this->getNewGameId();
|
||||||
$this->gameFolder = "Game" . $this->gameId . "_" . $this->worldName;
|
|
||||||
|
|
||||||
Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin());
|
Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin());
|
||||||
|
|
||||||
|
$this->loadWorld("Super Smash Mobs");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function loadWorld($worldName)
|
//This is just some wierd testiong. Ignore it :P
|
||||||
|
// public function onJoin(PlayerJoinEvent $event)
|
||||||
|
// {
|
||||||
|
// $this->player = $event->getPlayer();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public function onEvent(UpdateEvent $event)
|
||||||
|
// {
|
||||||
|
// if (is_null($this->player))
|
||||||
|
// return;
|
||||||
|
//
|
||||||
|
// if ($event->isTiming(UpdateType::S8))
|
||||||
|
// {
|
||||||
|
// $this->player->teleport($this->posTest);
|
||||||
|
//
|
||||||
|
// print("Teleporting " . $this->player->getName() . " to...\n");
|
||||||
|
//
|
||||||
|
// $this->player = null;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
private function loadWorld($gameName)
|
||||||
{
|
{
|
||||||
$this-$worldName = $worldName;
|
$files = scandir('../update/maps/' . $gameName . '/');
|
||||||
|
|
||||||
//Do this Async?
|
$maps = array();
|
||||||
if ($this->arena->getPlugin()->getServer()->loadLevel($worldName))
|
|
||||||
|
foreach ($files as $file)
|
||||||
{
|
{
|
||||||
$this->world = $this->arena->getPlugin()->getServer()->getLevelByName($worldName);
|
if (UtilString::endsWith($file, ".zip"))
|
||||||
|
{
|
||||||
|
array_push($maps, $file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loadWorldData();
|
$worldName = $maps[rand(0, count($maps) - 1)];
|
||||||
|
|
||||||
|
//Trim .zip
|
||||||
|
$worldName = substr($worldName, 0, strlen($worldName) - 4);
|
||||||
|
|
||||||
|
print_r($worldName . "\n");
|
||||||
|
|
||||||
|
$this->gameFolder = "Game" . $this->gameId . "_" . $gameName . "_" . $worldName;
|
||||||
|
|
||||||
|
//Unzip World
|
||||||
|
$zip = new ZipArchive;
|
||||||
|
$res = $zip->open('../update/maps/' . $gameName . '/' . $worldName . '.zip');
|
||||||
|
if ($res === TRUE)
|
||||||
|
{
|
||||||
|
$zip->extractTo('worlds/' . $this->gameFolder . '/');
|
||||||
|
$zip->close();
|
||||||
|
print("Successfully Extracted: " . $this->gameFolder . "\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
print("ERROR LOADING WORLD: " + $worldName);
|
print("Error Extracting: " . $this->gameFolder . "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Load World
|
||||||
|
if ($this->arena->getPlugin()->getServer()->loadLevel($this->gameFolder))
|
||||||
|
{
|
||||||
|
$this->world = $this->arena->getPlugin()->getServer()->getLevelByName($this->gameFolder);
|
||||||
|
|
||||||
|
$this->loadWorldData();
|
||||||
|
|
||||||
|
print("Successfully Loaded World: " . $this->gameFolder . "\n");
|
||||||
|
|
||||||
|
Server::getInstance()->getPluginManager()->callEvent(new WorldLoadSuccessEvent($this->arena));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print("Error Loading World: " . $this->gameFolder . "\n");
|
||||||
|
|
||||||
|
Server::getInstance()->getPluginManager()->callEvent(new WorldLoadFailEvent($this->arena));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadWorldData($world)
|
public function loadWorldData()
|
||||||
{
|
{
|
||||||
$handle = fopen("WorldConfig.dat", "r");
|
$handle = fopen("WorldConfig.dat", "r");
|
||||||
if ($handle)
|
if ($handle)
|
||||||
@ -110,13 +180,15 @@ class WorldComponent implements Listener
|
|||||||
{
|
{
|
||||||
$positions = array();
|
$positions = array();
|
||||||
|
|
||||||
foreach ($tokens as $token)
|
for ($x=1 ; $x<count($tokens) ; $x++)
|
||||||
{
|
{
|
||||||
$position = strToPos($token);
|
$position = $this->strToPos($tokens[$x]);
|
||||||
|
|
||||||
if (is_null($position))
|
if (is_null($position))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
$this->posTest = $position;
|
||||||
|
|
||||||
array_push($positions, $position);
|
array_push($positions, $position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,9 +204,9 @@ class WorldComponent implements Listener
|
|||||||
{
|
{
|
||||||
$positions = array();
|
$positions = array();
|
||||||
|
|
||||||
foreach ($tokens as $token)
|
for ($x=1 ; $x<count($tokens) ; $x++)
|
||||||
{
|
{
|
||||||
$position = strToPos($token);
|
$position = $this->strToPos($tokens[$x]);
|
||||||
|
|
||||||
if (is_null($position))
|
if (is_null($position))
|
||||||
continue;
|
continue;
|
||||||
@ -150,7 +222,7 @@ class WorldComponent implements Listener
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
print("Error Opening File");
|
print("Error Opening File.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,8 +241,17 @@ class WorldComponent implements Listener
|
|||||||
return $this->mapData[$key];
|
return $this->mapData[$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function strToLoc($str)
|
public function getPosition()
|
||||||
{
|
{
|
||||||
|
return $this->getTeams()[0][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function strToPos($str)
|
||||||
|
{
|
||||||
|
if (strlen($str) < 5)
|
||||||
|
return null;
|
||||||
|
|
||||||
$tokens = explode(",", $str);
|
$tokens = explode(",", $str);
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -179,7 +260,7 @@ class WorldComponent implements Listener
|
|||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (Exception $e)
|
||||||
{
|
{
|
||||||
print("World Data Read Error: Invalid Position String [" . $str . "]");
|
print("World Data Read Error: Invalid Position String [" . $str . "]\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -30,6 +30,14 @@ class UpdateEvent extends Event
|
|||||||
|
|
||||||
public function isTiming($type)
|
public function isTiming($type)
|
||||||
{
|
{
|
||||||
return in_array(type, $this->updateTypes);
|
foreach ($this->updateTypes as $updateType)
|
||||||
|
{
|
||||||
|
if ($updateType->isTiming($type))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,26 +12,45 @@ namespace mineplex\plugin\core\updater;
|
|||||||
class UpdateType
|
class UpdateType
|
||||||
{
|
{
|
||||||
const M64 = 3840000;
|
const M64 = 3840000;
|
||||||
conSt M32 = 1920000;
|
const M32 = 1920000;
|
||||||
conSt M16 = 960000;
|
const M16 = 960000;
|
||||||
conSt M8 = 480000;
|
const M8 = 480000;
|
||||||
conSt M4 = 240000;
|
const M4 = 240000;
|
||||||
conSt M2 = 120000;
|
const M2 = 120000;
|
||||||
conSt M1 = 60000;
|
const M1 = 60000;
|
||||||
conSt S32 = 32000;
|
const S32 = 32000;
|
||||||
conSt S16 = 16000;
|
const S16 = 16000;
|
||||||
conSt S8 = 8000;
|
const S8 = 8000;
|
||||||
conSt S4 = 4000;
|
const S4 = 4000;
|
||||||
conSt S2 = 2000;
|
const S2 = 2000;
|
||||||
conSt S1 = 1000;
|
const S1 = 1000;
|
||||||
conSt MS500 = 500;
|
const MS500 = 500;
|
||||||
conSt MS250 = 250;
|
const MS250 = 250;
|
||||||
conSt MS125 = 125;
|
const MS125 = 125;
|
||||||
|
|
||||||
public $time;
|
public $time;
|
||||||
|
|
||||||
|
private $lastTrigger = 0;
|
||||||
|
|
||||||
public function __construct($time=0)
|
public function __construct($time=0)
|
||||||
{
|
{
|
||||||
$this->time = $time;
|
$this->time = $time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canTrigger()
|
||||||
|
{
|
||||||
|
if (round(microtime(true) * 1000) - $this->lastTrigger > $this->time)
|
||||||
|
{
|
||||||
|
$this->lastTrigger = round(microtime(true) * 1000);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isTiming($timing)
|
||||||
|
{
|
||||||
|
return $this->time === $timing;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
namespace mineplex\plugin\core\updater;
|
namespace mineplex\plugin\core\updater;
|
||||||
|
|
||||||
|
use mineplex\plugin\core\updater\UpdateType;
|
||||||
use pocketmine\plugin\PluginBase;
|
use pocketmine\plugin\PluginBase;
|
||||||
use pocketmine\scheduler\PluginTask;
|
use pocketmine\scheduler\PluginTask;
|
||||||
|
|
||||||
class Updater extends PluginTask
|
class Updater extends PluginTask
|
||||||
{
|
{
|
||||||
private $plugin;
|
private $plugin;
|
||||||
private $last;
|
|
||||||
private $updateTypes;
|
private $updateTypes;
|
||||||
|
|
||||||
public function __construct(PluginBase $host)
|
public function __construct(PluginBase $host)
|
||||||
@ -47,14 +47,13 @@ class Updater extends PluginTask
|
|||||||
{
|
{
|
||||||
$updateTypes = array();
|
$updateTypes = array();
|
||||||
|
|
||||||
$timeSpent = round(microtime(true) * 1000) - $this->last;
|
foreach ($this->updateTypes as &$updateType)
|
||||||
$this->last = round(microtime(true) * 1000);
|
{
|
||||||
|
if ($updateType->canTrigger())
|
||||||
foreach ($this->updateTypes as &$updateType) {
|
{
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user