queue fixes

This commit is contained in:
kremsy 2014-01-24 21:40:12 +01:00 committed by Steffen Schröder
parent d9ba67947a
commit 1d8c9e60e3

View File

@ -3,9 +3,9 @@ use FML\Controls\Frame;
use FML\Controls\Labels\Label_Button; use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text; use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad; use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Icons64x64_1; use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink; use FML\ManiaLink;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
@ -25,348 +25,353 @@ use ManiaControl\Plugins\Plugin;
// TODO: idlekick function (?) // TODO: idlekick function (?)
class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAnswerListener, Plugin { class QueuePlugin implements CallbackListener, CommandListener, ManialinkPageAnswerListener, Plugin {
/** /**
* Constants * Constants
*/ */
const ID = 12; const ID = 12;
const VERSION = 0.1; const VERSION = 0.1;
const ML_ID = 'Queue.Widget'; const ML_ID = 'Queue.Widget';
const ML_ADDTOQUEUE = 'Queue.Add'; const ML_ADDTOQUEUE = 'Queue.Add';
const ML_REMOVEFROMQUEUE = 'Queue.Remove'; const ML_REMOVEFROMQUEUE = 'Queue.Remove';
const QUEUE_MAX = 'Maximum number in the queue'; const QUEUE_MAX = 'Maximum number in the queue';
/** /**
* Private properties * Private properties
*/ */
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl = null; private $maniaControl = null;
private $queue = array(); private $queue = array();
private $spectators = array(); private $spectators = array();
private $showPlay = array(); private $showPlay = array();
/** /**
* Load the plugin * Load the plugin
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
* @return bool * @return bool
*/ */
public function load(ManiaControl $maniaControl) { public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'handlePlayerDisconnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECTED, $this, 'handlePlayerDisconnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'handlePlayerInfoChanged'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'handlePlayerInfoChanged');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handleEverySecond'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handleEverySecond');
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ML_ADDTOQUEUE, $this, 'handleManiaLinkAnswerAdd'); $this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ML_ADDTOQUEUE, $this, 'handleManiaLinkAnswerAdd');
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ML_REMOVEFROMQUEUE, $this, 'handleManiaLinkAnswerRemove'); $this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ML_REMOVEFROMQUEUE, $this, 'handleManiaLinkAnswerRemove');
$this->maniaControl->settingManager->initSetting($this, self::QUEUE_MAX, 8); $this->maniaControl->settingManager->initSetting($this, self::QUEUE_MAX, 8);
foreach($this->maniaControl->playerManager->getPlayers() as $player) { foreach($this->maniaControl->playerManager->getPlayers() as $player) {
if($player->isSpectator) { if($player->isSpectator) {
$this->spectators[$player->login] = $player->login; $this->spectators[$player->login] = $player->login;
$this->maniaControl->client->forceSpectator($player->login, 1); $this->maniaControl->client->forceSpectator($player->login, 1);
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
} }
} }
} }
/** /**
* Unload the plugin and its resources * Unload the plugin and its resources
*/ */
public function unload() { public function unload() {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($this); $this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($this);
$this->maniaControl->callbackManager->unregisterCallbackListener($this); $this->maniaControl->callbackManager->unregisterCallbackListener($this);
foreach($this->spectators as $spectator) { foreach($this->spectators as $spectator) {
$this->maniaControl->client->forceSpectator($spectator, 3); $this->maniaControl->client->forceSpectator($spectator, 3);
$this->maniaControl->client->forceSpectator($spectator, 0); $this->maniaControl->client->forceSpectator($spectator, 0);
} }
foreach($this->maniaControl->playerManager->getPlayers() as $player) { foreach($this->maniaControl->playerManager->getPlayers() as $player) {
$this->hideQueueWidget($player); $this->hideQueueWidget($player);
} }
$this->queue = array(); $this->queue = array();
$this->spectators = array(); $this->spectators = array();
$this->showPlay = array(); $this->showPlay = array();
$this->maniaControl = null; $this->maniaControl = null;
} }
/** /**
* Get plugin id * Get plugin id
* *
* @return int * @return int
*/ */
public static function getId() { public static function getId() {
return self::ID; return self::ID;
} }
/** /**
* Get Plugin Name * Get Plugin Name
* *
* @return string * @return string
*/ */
public static function getName() { public static function getName() {
return 'Queue Plugin'; return 'Queue Plugin';
} }
/** /**
* Get Plugin Version * Get Plugin Version
* *
* @return float * @return float
*/ */
public static function getVersion() { public static function getVersion() {
return self::VERSION; return self::VERSION;
} }
/** /**
* Get Plugin Author * Get Plugin Author
* *
* @return string * @return string
*/ */
public static function getAuthor() { public static function getAuthor() {
return 'TheM'; return 'TheM';
} }
/** /**
* Get Plugin Description * Get Plugin Description
* *
* @return string * @return string
*/ */
public static function getDescription() { public static function getDescription() {
return 'Plugin offers the known AutoQueue/SpecJam options.'; return 'Plugin offers the known AutoQueue/SpecJam options.';
} }
public function handlePlayerConnect(array $callback) { public function handlePlayerConnect(array $callback) {
$login = $callback[1]->login; $login = $callback[1]->login;
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if($player->isSpectator) { if($player->isSpectator) {
$this->spectators[$player->login] = $player->login; $this->spectators[$player->login] = $player->login;
$this->maniaControl->client->forceSpectator($player->login, 1); $this->maniaControl->client->forceSpectator($player->login, 1);
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
} else { } else {
if(count($this->queue) != 0) { if(count($this->queue) != 0) {
$this->maniaControl->client->forceSpectator($player->login, 1); $this->maniaControl->client->forceSpectator($player->login, 1);
$this->spectators[$player->login] = $player->login; $this->spectators[$player->login] = $player->login;
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
} }
} }
} }
public function handlePlayerDisconnect(array $callback) { public function handlePlayerDisconnect(array $callback) {
$login = $callback[1]->login; /** @var Player $player */
if(isset($this->spectators[$login])) unset($this->spectators[$login]); $player = $callback[1];
$this->removePlayerFromQueue($login); if(isset($this->spectators[$player->login])) {
$this->moveFirstPlayerToPlay(); unset($this->spectators[$player->login]);
} }
$this->removePlayerFromQueue($player->login);
$this->moveFirstPlayerToPlay();
}
public function handlePlayerInfoChanged(array $callback) { public function handlePlayerInfoChanged(array $callback) {
$login = $callback[1][0]['Login']; $login = $callback[1][0]['Login'];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if(!is_null($player)) { if(!is_null($player)) {
if($player->isSpectator) { if($player->isSpectator) {
if(!isset($this->spectators[$player->login])) { if(!isset($this->spectators[$player->login])) {
$this->maniaControl->client->forceSpectator($player->login, 1); $this->maniaControl->client->forceSpectator($player->login, 1);
$this->spectators[$player->login] = $player->login; $this->spectators[$player->login] = $player->login;
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
} }
} }
} }
} }
public function handleEverySecond() { public function handleEverySecond() {
if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) { if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) {
$this->moveFirstPlayerToPlay(); $this->moveFirstPlayerToPlay();
} }
foreach($this->spectators as $login) { foreach($this->spectators as $login) {
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
} }
foreach($this->showPlay as $showPlay) { foreach($this->showPlay as $showPlay) {
if(($showPlay['time']+5) < time()) { if(($showPlay['time'] + 5) < time()) {
$this->hideQueueWidget($showPlay['player']); $this->hideQueueWidget($showPlay['player']);
unset($this->showPlay[$showPlay['player']->login]); unset($this->showPlay[$showPlay['player']->login]);
} }
} }
} }
public function handleManiaLinkAnswerAdd(array $chatCallback, Player $player) { public function handleManiaLinkAnswerAdd(array $chatCallback, Player $player) {
$this->addPlayerToQueue($player); $this->addPlayerToQueue($player);
} }
public function handleManiaLinkAnswerRemove(array $chatCallback, Player $player) { public function handleManiaLinkAnswerRemove(array $chatCallback, Player $player) {
$this->removePlayerFromQueue($player); $this->removePlayerFromQueue($player->login);
$this->showJoinQueueWidget($player); $this->showJoinQueueWidget($player);
$this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff'.$player->nickname.'$> has left the queue!'); $this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff' . $player->nickname . '$> has left the queue!');
} }
private function moveFirstPlayerToPlay() { private function moveFirstPlayerToPlay() {
if(count($this->queue) > 0) { if(count($this->queue) > 0) {
$firstPlayer = $this->maniaControl->playerManager->getPlayer($this->queue[0]->login); $firstPlayer = $this->maniaControl->playerManager->getPlayer($this->queue[0]->login);
$this->forcePlayerToPlay($firstPlayer); $this->forcePlayerToPlay($firstPlayer);
} }
} }
private function forcePlayerToPlay(Player $player) { private function forcePlayerToPlay(Player $player) {
if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) { if($this->maniaControl->client->getMaxPlayers()['CurrentValue'] > count($this->maniaControl->playerManager->players)) {
$this->maniaControl->client->forceSpectator($player->login, 2); $this->maniaControl->client->forceSpectator($player->login, 2);
$this->maniaControl->client->forceSpectator($player->login, 0); $this->maniaControl->client->forceSpectator($player->login, 0);
if(isset($this->spectators[$player->login])) unset($this->spectators[$player->login]); if(isset($this->spectators[$player->login])) {
$this->removePlayerFromQueue($player->login); unset($this->spectators[$player->login]);
$this->showPlayWidget($player); }
$this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff'.$player->nickname.'$> has a free spot and is now playing!'); $this->removePlayerFromQueue($player->login);
} $this->showPlayWidget($player);
} $this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff' . $player->nickname . '$> has a free spot and is now playing!');
}
}
private function addPlayerToQueue(Player $player) { private function addPlayerToQueue(Player $player) {
if($this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX) > count($this->queue)) { if($this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX) > count($this->queue)) {
$this->queue[count($this->queue)] = $player; $this->queue[count($this->queue)] = $player;
$this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff'.$player->nickname.'$> just joined the queue!'); $this->maniaControl->chat->sendChat('$z$s$090[Queue] $<$fff' . $player->nickname . '$> just joined the queue!');
} }
} }
private function removePlayerFromQueue($login) { private function removePlayerFromQueue($login) {
$count = 0; $count = 0;
$newQueue = array(); $newQueue = array();
foreach($this->queue as $queuePlayer) { foreach($this->queue as $queuePlayer) {
if($queuePlayer->login != $login) { if($queuePlayer->login != $login) {
$newQueue[$count] = $queuePlayer; $newQueue[$count] = $queuePlayer;
$count++; $count++;
} }
} }
$this->queue = $newQueue; $this->queue = $newQueue;
} }
private function showJoinQueueWidget(Player $player) { private function showJoinQueueWidget(Player $player) {
$maniaLink = new ManiaLink(self::ML_ID); $maniaLink = new ManiaLink(self::ML_ID);
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle(); $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle(); $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
$max_queue = $this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX); $max_queue = $this->maniaControl->settingManager->getSetting($this, self::QUEUE_MAX);
// Main frame // Main frame
$frame = new Frame(); $frame = new Frame();
$maniaLink->add($frame); $maniaLink->add($frame);
$frame->setSize(60, 6); $frame->setSize(60, 6);
$frame->setPosition(0, 67, 0); $frame->setPosition(0, 67, 0);
// Background // Background
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
$frame->add($backgroundQuad); $frame->add($backgroundQuad);
$backgroundQuad->setPosition(0, 0, 0); $backgroundQuad->setPosition(0, 0, 0);
$backgroundQuad->setSize(70, 10); $backgroundQuad->setSize(70, 10);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle); $backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$cameraQuad = new Quad_Icons64x64_1(); $cameraQuad = new Quad_Icons64x64_1();
$frame->add($cameraQuad); $frame->add($cameraQuad);
$cameraQuad->setPosition(-29, 0.4, 2); $cameraQuad->setPosition(-29, 0.4, 2);
$cameraQuad->setSize(9, 9); $cameraQuad->setSize(9, 9);
$cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera); $cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera);
$statusLabel = new Label_Text(); $statusLabel = new Label_Text();
$frame->add($statusLabel); $frame->add($statusLabel);
$statusLabel->setPosition(4.5, 2.8, 1); $statusLabel->setPosition(4.5, 2.8, 1);
$statusLabel->setSize(66, 4); $statusLabel->setSize(66, 4);
$statusLabel->setAlign('center', 'center'); $statusLabel->setAlign('center', 'center');
$statusLabel->setScale(0.8); $statusLabel->setScale(0.8);
$statusLabel->setStyle(Label_Text::STYLE_TextStaticSmall); $statusLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
$messageLabel = new Label_Button(); $messageLabel = new Label_Button();
$frame->add($messageLabel); $frame->add($messageLabel);
$messageLabel->setPosition(4.5, -1.6, 1); $messageLabel->setPosition(4.5, -1.6, 1);
$messageLabel->setSize(56, 4); $messageLabel->setSize(56, 4);
$messageLabel->setAlign('center', 'center'); $messageLabel->setAlign('center', 'center');
$messageLabel->setScale(1.0); $messageLabel->setScale(1.0);
$inQueue = false; $inQueue = false;
foreach($this->queue as $queuedPlayer) { foreach($this->queue as $queuedPlayer) {
if($queuedPlayer->login == $player->login) { if($queuedPlayer->login == $player->login) {
$inQueue = true; $inQueue = true;
} }
} }
if($inQueue) { if($inQueue) {
$message = '$fff$sYou\'re in the queue (click to unqueue).'; $message = '$fff$sYou\'re in the queue (click to unqueue).';
$position = 0; $position = 0;
foreach (array_values($this->queue) as $i => $queuePlayer) { foreach(array_values($this->queue) as $i => $queuePlayer) {
if($player->login == $queuePlayer->login) { if($player->login == $queuePlayer->login) {
$position = ($i+1); $position = ($i + 1);
} }
} }
$statusLabel->setText('$aaaStatus: In queue, ('.$position.'/'.count($this->queue).') Waiting: '.count($this->queue).'/'.$max_queue.''); $statusLabel->setText('$aaaStatus: In queue, (' . $position . '/' . count($this->queue) . ') Waiting: ' . count($this->queue) . '/' . $max_queue . '');
$messageLabel->setAction(self::ML_REMOVEFROMQUEUE); $messageLabel->setAction(self::ML_REMOVEFROMQUEUE);
$backgroundQuad->setAction(self::ML_REMOVEFROMQUEUE); $backgroundQuad->setAction(self::ML_REMOVEFROMQUEUE);
$statusLabel->setAction(self::ML_REMOVEFROMQUEUE); $statusLabel->setAction(self::ML_REMOVEFROMQUEUE);
$cameraQuad->setAction(self::ML_REMOVEFROMQUEUE); $cameraQuad->setAction(self::ML_REMOVEFROMQUEUE);
} else { } else {
if(count($this->queue) < $max_queue) { if(count($this->queue) < $max_queue) {
$message = '$0ff$sClick to join spectator waiting list.'; $message = '$0ff$sClick to join spectator waiting list.';
$messageLabel->setAction(self::ML_ADDTOQUEUE); $messageLabel->setAction(self::ML_ADDTOQUEUE);
$backgroundQuad->setAction(self::ML_ADDTOQUEUE); $backgroundQuad->setAction(self::ML_ADDTOQUEUE);
$statusLabel->setAction(self::ML_ADDTOQUEUE); $statusLabel->setAction(self::ML_ADDTOQUEUE);
$cameraQuad->setAction(self::ML_ADDTOQUEUE); $cameraQuad->setAction(self::ML_ADDTOQUEUE);
} else { } else {
$message = '$f00The waiting list is full!'; $message = '$f00The waiting list is full!';
} }
$statusLabel->setText('$aaaStatus: Not queued spectator Waiting: '.count($this->queue).'/'.$max_queue.''); $statusLabel->setText('$aaaStatus: Not queued spectator Waiting: ' . count($this->queue) . '/' . $max_queue . '');
} }
$messageLabel->setText($message); $messageLabel->setText($message);
$messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall); $messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue'); $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue');
} }
private function showPlayWidget(Player $player) { private function showPlayWidget(Player $player) {
$maniaLink = new ManiaLink(self::ML_ID); $maniaLink = new ManiaLink(self::ML_ID);
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle(); $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle(); $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
// Main frame // Main frame
$frame = new Frame(); $frame = new Frame();
$maniaLink->add($frame); $maniaLink->add($frame);
$frame->setSize(60, 6); $frame->setSize(60, 6);
$frame->setPosition(0, 67, 0); $frame->setPosition(0, 67, 0);
// Background // Background
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
$frame->add($backgroundQuad); $frame->add($backgroundQuad);
$backgroundQuad->setPosition(0, 0, 0); $backgroundQuad->setPosition(0, 0, 0);
$backgroundQuad->setSize(70, 10); $backgroundQuad->setSize(70, 10);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle); $backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$cameraQuad = new Quad_Icons64x64_1(); $cameraQuad = new Quad_Icons64x64_1();
$frame->add($cameraQuad); $frame->add($cameraQuad);
$cameraQuad->setPosition(-29, 0.4, 2); $cameraQuad->setPosition(-29, 0.4, 2);
$cameraQuad->setSize(9, 9); $cameraQuad->setSize(9, 9);
$cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera); $cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera);
$messageLabel = new Label_Button(); $messageLabel = new Label_Button();
$frame->add($messageLabel); $frame->add($messageLabel);
$messageLabel->setPosition(4.5, 0.6, 1); $messageLabel->setPosition(4.5, 0.6, 1);
$messageLabel->setSize(56, 4); $messageLabel->setSize(56, 4);
$messageLabel->setAlign('center', 'center'); $messageLabel->setAlign('center', 'center');
$messageLabel->setScale(1.0); $messageLabel->setScale(1.0);
$messageLabel->setText('$090You got a free spot, enjoy playing!'); $messageLabel->setText('$090You got a free spot, enjoy playing!');
$messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall); $messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue'); $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue');
$this->showPlay[$player->login] = array('time' => time(), 'player' => $player); $this->showPlay[$player->login] = array('time' => time(), 'player' => $player);
} }
private function hideQueueWidget(Player $player) { private function hideQueueWidget(Player $player) {
$maniaLink = new ManiaLink(self::ML_ID); $maniaLink = new ManiaLink(self::ML_ID);
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue'); $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'Queue');
} }
} }