Merge branch 'master' of ssh://184.154.0.242:7999/min/mineplex

This commit is contained in:
Aaron Brock 2015-07-05 03:52:45 -04:00
commit 085f9f16ca
5 changed files with 182 additions and 89 deletions

View File

@ -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,17 +17,18 @@ 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)

View File

@ -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,50 +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 $minX = -256; private $posTest = null;
private $maxX = 256; private $player = null;
private $minY = -256;
private $maxY = 256;
private $minZ = -256;
private $maxZ = 256;
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-$worldName = $worldName; // {
// $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? private function loadWorld($gameName)
if ($this->arena->getPlugin()->getServer()->loadLevel($worldName))
{ {
$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 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)
@ -97,29 +161,14 @@ class WorldComponent implements Listener
} }
//Map Boundaries //Map Boundaries
else if (strcmp($tokens[0], "MIN_X") === 0) elseif (strcmp($tokens[0], "MIN_X") === 0 ||
strcmp($tokens[0], "MAX_X") === 0 ||
strcmp($tokens[0], "MIN_Y") === 0 ||
strcmp($tokens[0], "MAX_Y") === 0 ||
strcmp($tokens[0], "MIN_Z") === 0 ||
strcmp($tokens[0], "MAX_Z") === 0)
{ {
$this->minX = $tokens[1]; $this->mapSettings[$tokens[0]] = $tokens[1];
}
else if (strcmp($tokens[0], "MAX_X") === 0)
{
$this->maxX = $tokens[1];
}
else if (strcmp($tokens[0], "MIN_Y") === 0)
{
$this->minY = $tokens[1];
}
else if (strcmp($tokens[0], "MAX_Y") === 0)
{
$this->maxY = $tokens[1];
}
else if (strcmp($tokens[0], "MIN_Z") === 0)
{
$this->minZ = $tokens[1];
}
else if (strcmp($tokens[0], "MAX_Z") === 0)
{
$this->maxZ = $tokens[1];
} }
//Team Spawns //Team Spawns
@ -131,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);
} }
@ -153,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;
@ -171,7 +222,7 @@ class WorldComponent implements Listener
} }
else else
{ {
print("Error Opening File"); print("Error Opening File.");
} }
} }
@ -180,13 +231,27 @@ class WorldComponent implements Listener
return $this->mapTeams; return $this->mapTeams;
} }
public function getData($key) public function getSetting($key)
{ {
return $this->worldData[$key]; return $this->mapSettings[$key];
} }
protected function strToLoc($str) public function getData($key)
{ {
return $this->mapData[$key];
}
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
@ -195,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;

View File

@ -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;
} }
} }

View File

@ -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;
}
} }

View File

@ -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,11 +47,10 @@ 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);
} }
} }