fixed UpdateEvent
added WorldComponent
This commit is contained in:
parent
e24890d292
commit
c7d758c8d8
@ -3,11 +3,12 @@
|
||||
namespace mineplex\plugin;
|
||||
|
||||
use mineplex\plugin\bench\arenas\MultiGameArena;
|
||||
use mineplex\plugin\bench\game\components\world\WorldComponent;
|
||||
use mineplex\plugin\bench\game\factory\TestGameFactory;
|
||||
use mineplex\plugin\core\updater\Updater;
|
||||
use pocketmine\event\Listener;
|
||||
use pocketmine\event\player\PlayerJoinEvent;
|
||||
use pocketmine\event\player\PlayerLoginEvent;
|
||||
use pocketmine\event\player\PlayerQuitEvent;
|
||||
use pocketmine\plugin\PluginBase;
|
||||
use mineplex\plugin\bench\arenas\Arena;
|
||||
use pocketmine\Server;
|
||||
@ -16,23 +17,24 @@ class Main extends PluginBase implements Listener
|
||||
{
|
||||
/** @var Arena */
|
||||
private $arena;
|
||||
private $test;
|
||||
|
||||
public function onEnable()
|
||||
{
|
||||
|
||||
$this->arena = new MultiGameArena($this, new TestGameFactory());
|
||||
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);
|
||||
//new Updater($this);
|
||||
|
||||
new Updater($this);
|
||||
}
|
||||
|
||||
public function onLogin(PlayerLoginEvent $event)
|
||||
{
|
||||
if ($this->arena->canJoin($event->getPlayer()))
|
||||
retun;
|
||||
return;
|
||||
|
||||
$event->setKickMessage("Unable to join game!");
|
||||
$event->setCancelled();
|
||||
|
@ -9,12 +9,20 @@
|
||||
namespace mineplex\plugin\bench\game\components\world;
|
||||
|
||||
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\level\Position;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\Server;
|
||||
use pocketmine\Player;
|
||||
|
||||
//require_once __DIR__ . '\GameState.php';
|
||||
use ZipArchive;
|
||||
use Exception;
|
||||
|
||||
class WorldComponent implements Listener
|
||||
{
|
||||
@ -24,44 +32,106 @@ class WorldComponent implements Listener
|
||||
private $gameFolder;
|
||||
|
||||
private $world;
|
||||
private $worldName;
|
||||
|
||||
private $mapName;
|
||||
private $mapAuthor;
|
||||
|
||||
private $mapTeams = array();
|
||||
private $mapData = array();
|
||||
|
||||
private $mapSettings = array();
|
||||
|
||||
private $posTest = null;
|
||||
private $player = null;
|
||||
|
||||
public function __construct(Arena $arena)
|
||||
{
|
||||
$this->arena = $arena;
|
||||
|
||||
$this->gameId = getNewGameId();
|
||||
$this->gameFolder = "Game" . $this->gameId . "_" . $this->worldName;
|
||||
$this->gameId = $this->getNewGameId();
|
||||
|
||||
Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin());
|
||||
|
||||
$this->loadWorld("Super Smash Mobs");
|
||||
}
|
||||
|
||||
private function loadWorld($worldName)
|
||||
{
|
||||
$this-$worldName = $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;
|
||||
// }
|
||||
// }
|
||||
|
||||
//Do this Async?
|
||||
if ($this->arena->getPlugin()->getServer()->loadLevel($worldName))
|
||||
private function loadWorld($gameName)
|
||||
{
|
||||
$this->world = $this->arena->getPlugin()->getServer()->getLevelByName($worldName);
|
||||
$files = scandir('../update/maps/' . $gameName . '/');
|
||||
|
||||
loadWorldData();
|
||||
$maps = array();
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (UtilString::endsWith($file, ".zip"))
|
||||
{
|
||||
array_push($maps, $file);
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
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");
|
||||
if ($handle)
|
||||
@ -110,13 +180,15 @@ class WorldComponent implements Listener
|
||||
{
|
||||
$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))
|
||||
continue;
|
||||
|
||||
$this->posTest = $position;
|
||||
|
||||
array_push($positions, $position);
|
||||
}
|
||||
|
||||
@ -132,9 +204,9 @@ class WorldComponent implements Listener
|
||||
{
|
||||
$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))
|
||||
continue;
|
||||
@ -150,7 +222,7 @@ class WorldComponent implements Listener
|
||||
}
|
||||
else
|
||||
{
|
||||
print("Error Opening File");
|
||||
print("Error Opening File.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -169,8 +241,17 @@ class WorldComponent implements Listener
|
||||
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);
|
||||
|
||||
try
|
||||
@ -179,7 +260,7 @@ class WorldComponent implements Listener
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
print("World Data Read Error: Invalid Position String [" . $str . "]");
|
||||
print("World Data Read Error: Invalid Position String [" . $str . "]\n");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -30,6 +30,14 @@ class UpdateEvent extends Event
|
||||
|
||||
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
|
||||
{
|
||||
const M64 = 3840000;
|
||||
conSt M32 = 1920000;
|
||||
conSt M16 = 960000;
|
||||
conSt M8 = 480000;
|
||||
conSt M4 = 240000;
|
||||
conSt M2 = 120000;
|
||||
conSt M1 = 60000;
|
||||
conSt S32 = 32000;
|
||||
conSt S16 = 16000;
|
||||
conSt S8 = 8000;
|
||||
conSt S4 = 4000;
|
||||
conSt S2 = 2000;
|
||||
conSt S1 = 1000;
|
||||
conSt MS500 = 500;
|
||||
conSt MS250 = 250;
|
||||
conSt MS125 = 125;
|
||||
const M32 = 1920000;
|
||||
const M16 = 960000;
|
||||
const M8 = 480000;
|
||||
const M4 = 240000;
|
||||
const M2 = 120000;
|
||||
const M1 = 60000;
|
||||
const S32 = 32000;
|
||||
const S16 = 16000;
|
||||
const S8 = 8000;
|
||||
const S4 = 4000;
|
||||
const S2 = 2000;
|
||||
const S1 = 1000;
|
||||
const MS500 = 500;
|
||||
const MS250 = 250;
|
||||
const MS125 = 125;
|
||||
|
||||
public $time;
|
||||
|
||||
private $lastTrigger = 0;
|
||||
|
||||
public function __construct($time=0)
|
||||
{
|
||||
$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;
|
||||
|
||||
use mineplex\plugin\core\updater\UpdateType;
|
||||
use pocketmine\plugin\PluginBase;
|
||||
use pocketmine\scheduler\PluginTask;
|
||||
|
||||
class Updater extends PluginTask
|
||||
{
|
||||
private $plugin;
|
||||
private $last;
|
||||
private $updateTypes;
|
||||
|
||||
public function __construct(PluginBase $host)
|
||||
@ -47,11 +47,10 @@ class Updater extends PluginTask
|
||||
{
|
||||
$updateTypes = array();
|
||||
|
||||
$timeSpent = round(microtime(true) * 1000) - $this->last;
|
||||
$this->last = round(microtime(true) * 1000);
|
||||
|
||||
foreach ($this->updateTypes as &$updateType) {
|
||||
if ($timeSpent >= $updateType->time) {
|
||||
foreach ($this->updateTypes as &$updateType)
|
||||
{
|
||||
if ($updateType->canTrigger())
|
||||
{
|
||||
array_push($updateTypes, $updateType);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user