Moving forward !

This commit is contained in:
Ad5001 2016-07-31 22:46:09 +03:00
parent fb22b22bd3
commit 665426e7cf
5 changed files with 72 additions and 23 deletions

View file

@ -5,6 +5,26 @@ use pocketmine\Player;
class Example extends Game {
public function onGameStart() {
$this->getLogger()->info("Game");
$this->getLogger()->info("Game started");
}
public function onGameStop() {
$this->getLogger()->info("Game stoped");
}
public function getName() : string {
return "Example";
}
public function getMinPlayers() : int {
return 1;
}
public function getMaxPlayers() : int {
return 5;
}
}

View file

@ -24,7 +24,7 @@ abstract class Game {
$this->name = $name;
$this->main = $this->server->getPlugin("GameManager");
$this->gm = $this->main->getGameManager();
$this->main->backup($level);
$this->gm->backup($level);
}
@ -43,22 +43,33 @@ abstract class Game {
}
public function onGameStart();
abstract public function onGameStart();
public function onGameStop();
abstract public function onGameStop();
public function stopGame() {
$this->main->getGameManager()->reloadLevel($this->level);
public function stop() {
$this->gm->stopGame($this->level);
return true;
}
public function onJoin(Player $player) {}
public function onJoin(Player $player) {
if($this->getLevel()->getPlayers() >= $this->getMinPlayers() and !$this->isStarted()) {
$this->gm->startGame($this->getLevel());
}
if($this->getLevel()->getPlayers() <= $this->getMaxPlayers()) {
$player->teleport($this->getServer()->getDefaultLevel()->getDefaultSpawn());
}
}
public function onQuit(Player $player) {}
public function onQuit(Player $player) {
if($this->getLevel()->getPlayers() <= $this->getMinPlayers()) {
$this->gm->stopGame($this->getLevel());
}
}
public function onInteract(\pocketmine\event\player\PlayerInteract $event) {}
@ -79,21 +90,19 @@ abstract class Game {
public function saveDefaultConfig() {
@mkdir($this->main->getDataFolder() . "games/" . $this->name);
file_put_contents($this->main->getDataFolder() . "games/$this->name", "");
}
public function getName() : string;
abstract public function getName() : string;
public function getMinPlayers() : int;
abstract public function getMinPlayers() : int;
public function getMaxPlayers() : int;
public function useEvent(\pocketmine\event\Event $event) : bool;
abstract public function getMaxPlayers() : int;
public function getDataFolder() {

View file

@ -27,10 +27,12 @@ class GameManager {
$this->levels = [];
$this->startedgames = [];
foreach ($files as $file) {
require($this->getDataFolder() . "/games/" . $file);
$classn = getClasses(file_get_contents($this->getDataFolder() . "/games/" . $file));
$this->games[explode(".php", $file)[0]] = $classn;
@mkdir($this->main->getDataFolder() . "games/" . explode(".php", $file)[0]);
if(!is_dir($this->main->getDataFolder() . "/games/" . $file)) {
require($this->main->getDataFolder() . "/games/" . $file);
$classn = $this->main->getClasses(file_get_contents($this->main->getDataFolder() . "/games/" . $file));
$this->games[explode(".php", $file)[0]] = $classn;
@mkdir($this->main->getDataFolder() . "games/" . explode(".php", $file)[0]);
}
}
}
@ -47,6 +49,17 @@ class GameManager {
public function stopGame(Level $level) {
if(isset($this->startedgames[$level->getName()])) {
unset($this->startedgames[$level->getName()]);
$this->levels[$level->getName()]->onGameStop();
return true;
}
return false;
}
public function registerLevel(Level $level, string $game) {
if(!array_key_exists($level->getName(), $this->levels)) {
if(isset($this->games[$game])) {

View file

@ -7,8 +7,10 @@ use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
use pocketmine\level\Level;
use pocketmine\block\Block;
use pocketmine\Player;
use Ad5001\GameManager\GameManager;
use Ad5001\GameManager\tasks\SignReloadTask;
class Main extends PluginBase implements Listener {
@ -23,6 +25,7 @@ class Main extends PluginBase implements Listener {
@mkdir($this->getServer()->getFilePath() . "worldsBackups/");
@mkdir($this->getDataFolder() . "games");
$this->manager = new GameManager($this);
$this->getServer()->getScheduler()->scheduleRepeatingTask(new SignReloadTask($this), 5);
foreach(array_diff_key($this->getConfig()->getAll(), ["Game1" => "", "Game2" => "", "InGame3" => "", "InGame4" => "", "GameWait3" => "", "GameWait4" => ""]) as $worldname => $gamename) {
if($this->getServer()->getLevelByName($worldname) instanceof Level) {
$this->manager->registerLevel($this->getServer()->getLevelByName($worldname), $gamename);
@ -32,8 +35,10 @@ class Main extends PluginBase implements Listener {
public function onInteract(PlayerInteractEvent $event) {
if($event->getBlock() instanceof \pocketmine\block\SignPost and $event->getBlock() instanceof \pocketmine\block\WallSign) {
$t = $event->getBlock()->getLevel()->getTile($block);
// echo "Interacted";
if($event->getBlock()->getId() == Block::SIGN_POST and $event->getBlock()->getId() == Block::WALL_SIGN) {
$t = $event->getBlock()->getLevel()->getTile($event->getBlock());
echo "Sign.";
if(str_ireplace("{game}", $class->getName(), $this->getConfig()->get("Game1")) == $t->getText()[0]) {
$lvlex = explode("{level}", $this->getConfig()->get("Game2"));
$lvl = str_ireplace($lvlex[0], "", $t->getText()[1]);
@ -72,7 +77,7 @@ class Main extends PluginBase implements Listener {
public function getClasses(string $file) {
$tokens = token_get_all($php_file);
$tokens = token_get_all($file);
$class_token = false;
foreach ($tokens as $token) {
if (is_array($token)) {

View file

@ -1,5 +1,5 @@
<?php
namespace Ad5001\GameManager;
namespace Ad5001\GameManager\tasks;
use pocketmine\scheduler\PluginTask;
use pocketmine\Server;
use pocketmine\Player;
@ -24,14 +24,16 @@ class SignReloadTask extends PluginTask {
public function onRun($tick) {
// echo "Running...";
foreach($this->server->getLevels() as $level) {
foreach($level->getTiles() as $t) {
if($t instanceof \pocketmine\tile\Sign) {
foreach($this->gameManager->getLevels() as $name => $class) {
echo $class->getLevel()->getName();
if($t->getText()[0] == "[GAME]" and $class->getLevel()->getName() == $t->getText()[1]) {
$texts = $t->getText();
foreach($texts as $key => $text) {
$texts[$key] = str_ireplace("{players}", count($class->getLevel()->getPlayers()), str_ireplace("{max}", $class->getMaxPlayers(), str_ireplace("{game}", $class->getName(), str_ireplace("{level}", $class->getLevel()->getName(), ))))
$texts[$key] = str_ireplace("{players}", count($class->getLevel()->getPlayers()), str_ireplace("{max}", $class->getMaxPlayers(), str_ireplace("{game}", $class->getName(), str_ireplace("{level}", $class->getLevel()->getName(), $text))));
}
$t->setText($texts[0], $texts[1], $texts[2], $texts[3]);
}