Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. Here's the Arena.php code: (The main file that controls the arena)
  2. [PHP]<?php
  3.  
  4. namespace Minifixio\onevsone;
  5.  
  6. use Minifixio\onevsone\model\Arena;
  7. use Minifixio\onevsone\utils\PluginUtils;
  8. use Minifixio\onevsone\model\SignRefreshTask;
  9.  
  10. use pocketmine\Server;
  11. use pocketmine\Player;
  12. use pocketmine\level\Location;
  13. use pocketmine\level\Position;
  14. use pocketmine\utils\Config;
  15. use pocketmine\tile\Sign;
  16. use pocketmine\utils\TextFormat;
  17.  
  18. /**
  19. * Manages PVP arenas
  20. */
  21. class ArenaManager{
  22.  
  23. /** @var Arena[] **/
  24. private $arenas = array();
  25.  
  26. /** @var Player[] **/
  27. private $queue = array();
  28.  
  29. /** @var Config $config **/
  30. private $config;
  31.  
  32. /** @var Sign[] **/
  33. private $signTiles = array();
  34.  
  35. const SIGN_REFRESH_DELAY = 5;
  36. private $signRefreshTaskHandler;
  37.  
  38. /**
  39. * Init the arenas
  40. * @param Config $config
  41. */
  42. public function init(Config $config){
  43. PluginUtils::logOnConsole(TextFormat::GREEN . "Init". TextFormat::RED . " ArenaManager");
  44. $this->config = $config;
  45.  
  46. if(!$this->config->get("arenas")){
  47. $this->config->set('arenas', []);
  48. $arenaPositions = [];
  49. }
  50. else{
  51. $arenaPositions = $this->config->get("arenas");
  52. }
  53.  
  54. if(!$this->config->get("signs")){
  55. $this->config->set('signs', []);
  56. $signPositions = [];
  57. }
  58. else{
  59. $signPositions = $this->config->get("signs");
  60. }
  61.  
  62. // Load arenas and signs
  63. $this->parseArenaPositions($arenaPositions);
  64. $this->parseSignPositions($signPositions);
  65.  
  66. // Launch sign refreshing task
  67. $task = new SignRefreshTask(OneVsOne::getInstance());
  68. $task->arenaManager = $this;
  69. $this->signRefreshTaskHandler = $this->getServer()->getScheduler()->scheduleRepeatingTask($task, self::SIGN_REFRESH_DELAY * 20); //Using a Static Function(Server::getInstance is bad practise! Fixed this.
  70. }
  71.  
  72. private function getServer() {
  73. return Server::getInstance();
  74. }
  75.  
  76. /**
  77. * Create arenas
  78. * @param array $arenaPositions
  79. */
  80. public function parseArenaPositions(array $arenaPositions) {
  81. foreach ($arenaPositions as $n => $arenaPosition) {
  82. Server::getInstance()->loadLevel($arenaPosition[3]);
  83. if(($level = $this->getServer()->getLevelByName($arenaPosition[3])) === null){ //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  84. $this->getServer()->getLogger()->error("[1vs1] - " . $arenaPosition[3] . " is not loaded. Arena " . $n . " is disabled."); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  85. }
  86. else{
  87. $newArenaPosition = new Position($arenaPosition[0], $arenaPosition[1], $arenaPosition[2], $level);
  88. $newArena = new Arena($newArenaPosition, $this);
  89. array_push($this->arenas, $newArena);
  90. $this->getServer()->getLogger()->debug("[1vs1] - Arena " . $n . " loaded at position " . $newArenaPosition->__toString()); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  91. }
  92. }
  93. }
  94.  
  95. /**
  96. * Load signs
  97. * @param array $signPositions
  98. */
  99. public function parseSignPositions(array $signPositions) {
  100. PluginUtils::logOnConsole(TextFormat::GREEN . "Load signs... " . TextFormat::RED . count($signPositions) . " signs");
  101. foreach ($signPositions as $n => $signPosition) {
  102. Server::getInstance()->loadLevel($signPosition[3]);
  103. if(($level = $this->getServer()->getLevelByName($signPosition[3])) !== null){ //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  104. $newSignPosition = new Position($signPosition[0], $signPosition[1], $signPosition[2], $level);
  105. $tile = $level->getTile($newSignPosition);
  106. if($tile instanceof Sign){
  107. $cleanTileTitle = TextFormat::clean($tile->getText()[0]);
  108. $cleanOnevsOneTitle = TextFormat::clean(OneVsOne::SIGN_TITLE);
  109.  
  110. // Load it only if it's a sign with OneVsOne title
  111. if($cleanTileTitle === $cleanOnevsOneTitle){
  112. array_push($this->signTiles, $tile);
  113. continue;
  114. }
  115. }
  116. }
  117. else{
  118. PluginUtils::logOnConsole(TextFormat::RED . "Level " . $signPosition[3] . " does not exists. Please check configuration." );
  119. }
  120. }
  121. }
  122.  
  123. /**
  124. * Add player into the queue
  125. * @param Player $newPlayer
  126. */
  127. public function addNewPlayerToQueue(Player $newPlayer){
  128.  
  129. // Check that player is not already in the queue
  130. if(in_array($newPlayer, $this->queue)){
  131. PluginUtils::sendDefaultMessage($newPlayer, OneVsOne::getMessage("queue_alreadyinqueue"));
  132. return;
  133. }
  134.  
  135. // Check that player is not currently in an arena
  136. $currentArena = $this->getPlayerArena($newPlayer);
  137. if($currentArena != null){
  138. PluginUtils::sendDefaultMessage($newPlayer, OneVsOne::getMessage("arena_alreadyinarena"));
  139. return;
  140. }
  141.  
  142. // add player to queue
  143. array_push($this->queue, $newPlayer);
  144.  
  145. // display some stats
  146. PluginUtils::logOnConsole("[1vs1] - There is actually " . count($this->queue) . " players in the queue");
  147. PluginUtils::sendDefaultMessage($newPlayer, OneVsOne::getMessage("queue_join"));
  148. PluginUtils::sendDefaultMessage($newPlayer, OneVsOne::getMessage("queue_playersinqueue"). count($this->queue));
  149. $newPlayer->sendTip(OneVsOne::getMessage("queue_popup"));
  150.  
  151. $this->launchNewRounds();
  152. $this->refreshSigns();
  153. }
  154.  
  155. /**
  156. * Launches new rounds if necessary
  157. */
  158. private function launchNewRounds(){
  159.  
  160. // Check that there is at least 2 players in the queue
  161. if(count($this->queue) < 2){
  162. $this->getServer()->getLogger()->debug("There is not enough players to start a duel : " . count($this->queue)); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  163. return;
  164. }
  165.  
  166. // Check if there is any arena free (not active)
  167. $this->getServer()->getLogger()->debug("Check ". count($this->arenas) . " arenas"); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  168.  
  169. $freeArena = NULL;
  170. foreach ($this->arenas as $arena){
  171. if(!$arena->active){
  172. $freeArena = $arena;
  173. break;
  174. }
  175. }
  176.  
  177. if($freeArena == NULL){
  178. $this->getServer()->getLogger()->debug("[1vs1] - No free arena found"); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  179. return;
  180. }
  181.  
  182. // Send the players into the arena (and remove them from queues)
  183. $roundPlayers = array();
  184. array_push($roundPlayers, array_shift($this->queue), array_shift($this->queue));
  185. $this->getServer()->getLogger()->debug("[1vs1] - Starting duel : " . $roundPlayers[0]->getName() . " vs " . $roundPlayers[1]->getName()); //Again, bad practise by using a static function(Server::getInstance), Fixed this.
  186. $freeArena->startRound($roundPlayers);
  187. }
  188.  
  189. /**
  190. * Allows to be notify when rounds ends
  191. * @param Arena $arena
  192. */
  193. public function notifyEndOfRound(Arena $arena){
  194. $this->launchNewRounds();
  195. }
  196.  
  197. /**
  198. * Get current arena for player
  199. * @param Player $player
  200. * @return Arena or NULL
  201. */
  202. public function getPlayerArena(Player $player){
  203. foreach ($this->arenas as $arena) {
  204. if($arena->isPlayerInArena($player)){
  205. return $arena;
  206. }
  207. }
  208. return NULL;
  209. }
  210.  
  211. /**
  212. * Reference a new arena at this location
  213. * @param Location $location for the new Arena
  214. */
  215. public function referenceNewArena(Location $location){
  216. // Create a new arena
  217. $newArena = new Arena($location, $this);
  218.  
  219. // Add it to the array
  220. array_push($this->arenas,$newArena);
  221.  
  222. // Save it to config
  223. $arenas = $this->config->get("arenas", []);
  224. array_push($arenas, [$newArena->position->getX(), $newArena->position->getY(), $newArena->position->getZ(), $newArena->position->getLevel()->getName()]);
  225. $this->config->set("arenas", $arenas);
  226. $this->config->save();
  227. }
  228.  
  229. /**
  230. * Remove a player from queue
  231. * @param Player $player
  232. */
  233. public function removePlayerFromQueueOrArena(Player $player){
  234. $currentArena = $this->getPlayerArena($player);
  235. if($currentArena != null){
  236. $currentArena->onPlayerDeath($player);
  237. return;
  238. }
  239.  
  240. $index = array_search($player, $this->queue);
  241. if($index != -1){
  242. unset($this->queue[$index]);
  243. }
  244. $this->refreshSigns();
  245. }
  246.  
  247. public function getNumberOfArenas(){
  248. return count($this->arenas);
  249. }
  250.  
  251. public function getNumberOfFreeArenas(){
  252. $numberOfFreeArenas = count($this->arenas);
  253. foreach ($this->arenas as $arena){
  254. if($arena->active){
  255. $numberOfFreeArenas--;
  256. }
  257. }
  258. return $numberOfFreeArenas;
  259. }
  260.  
  261. public function getNumberOfPlayersInQueue(){
  262. return count($this->queue);
  263. }
  264.  
  265. /**
  266. * Add a new 1vs1 sign
  267. * @param Sign $signTile
  268. */
  269. public function addSign(Sign $signTile){
  270. $signs = $this->config->get("signs", []);
  271. $signs[count($this->signTiles)] = [$signTile->getX(), $signTile->getY(), $signTile->getZ(), $signTile->getLevel()->getName()];
  272. $this->config->set("signs", $signs);
  273. $this->config->save();
  274. array_push($this->signTiles, $signTile);
  275. }
  276.  
  277. /**
  278. * Refresh all 1vs1 signs
  279. */
  280. public function refreshSigns(){
  281. foreach ($this->signTiles as $signTile){
  282. if($signTile->level != null){
  283.  
  284. $signTile->setText(OneVsOne::SIGN_TITLE, "-Waiting " . $this->getNumberOfPlayersInQueue(), "-Arenas: " . $this->getNumberOfFreeArenas(), "-+===+-");
  285. }
  286. }
  287. }
  288. }
  289. [/PHP]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement