2014-05-04 15:43:16 +02:00
|
|
|
<?php
|
2014-05-07 21:42:23 +02:00
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
namespace TheM;
|
|
|
|
|
|
|
|
use FML\Controls\Frame;
|
|
|
|
use FML\Controls\Labels\Label_Button;
|
|
|
|
use FML\Controls\Labels\Label_Text;
|
|
|
|
use FML\Controls\Quad;
|
|
|
|
use FML\Controls\Quads\Quad_Icons64x64_1;
|
|
|
|
use FML\ManiaLink;
|
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\Callbacks\Callbacks;
|
|
|
|
use ManiaControl\Callbacks\TimerListener;
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
|
|
|
use ManiaControl\Players\Player;
|
2014-07-19 23:40:16 +02:00
|
|
|
use ManiaControl\Players\PlayerActions;
|
2014-05-04 15:43:16 +02:00
|
|
|
use ManiaControl\Players\PlayerManager;
|
|
|
|
use ManiaControl\Plugins\Plugin;
|
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
|
2014-08-24 12:56:06 +02:00
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
|
|
|
|
use Maniaplanet\DedicatedServer\Xmlrpc\ServerOptionsException;
|
2014-05-04 15:43:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue plugin
|
|
|
|
*
|
|
|
|
* @author TheM
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TODO: worst kick function
|
|
|
|
// TODO: idlekick function (?)
|
|
|
|
|
2014-05-07 22:10:06 +02:00
|
|
|
class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, TimerListener, Plugin {
|
2014-08-02 22:31:46 +02:00
|
|
|
/*
|
2014-05-04 15:43:16 +02:00
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const ID = 22;
|
2014-05-08 19:40:49 +02:00
|
|
|
const VERSION = 0.12;
|
2014-05-13 16:40:05 +02:00
|
|
|
const AUTHOR = 'TheM';
|
|
|
|
const NAME = 'Queue Plugin';
|
2014-05-04 15:43:16 +02:00
|
|
|
const ML_ID = 'Queue.Widget';
|
|
|
|
const ML_ADDTOQUEUE = 'Queue.Add';
|
|
|
|
const ML_REMOVEFROMQUEUE = 'Queue.Remove';
|
|
|
|
|
|
|
|
const QUEUE_MAX = 'Maximum number in the queue';
|
|
|
|
const QUEUE_WIDGET_POS_X = 'X position of the widget';
|
|
|
|
const QUEUE_WIDGET_POS_Y = 'Y position of the widget';
|
|
|
|
const QUEUE_ACTIVE_ON_PASS = 'Activate queue when there is a play password';
|
2014-05-20 14:59:17 +02:00
|
|
|
const QUEUE_CHATMESSAGES = 'Activate chat messages on queue join/leave/move to play';
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
/*
|
2014-05-04 15:43:16 +02:00
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
/** @var ManiaControl $maniaControl */
|
|
|
|
private $maniaControl = null;
|
|
|
|
private $queue = array();
|
|
|
|
private $spectators = array();
|
|
|
|
private $showPlay = array();
|
|
|
|
private $maxPlayers = 0;
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::prepare()
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
|
|
|
public static function prepare(ManiaControl $maniaControl) {
|
|
|
|
// TODO: Implement prepare() method.
|
|
|
|
}
|
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::getId()
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
public static function getId() {
|
|
|
|
return self::ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::getName()
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
public static function getName() {
|
2014-05-13 16:40:05 +02:00
|
|
|
return self::NAME;
|
2014-05-13 16:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::getVersion()
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
public static function getVersion() {
|
|
|
|
return self::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::getAuthor()
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
public static function getAuthor() {
|
2014-05-13 16:40:05 +02:00
|
|
|
return self::AUTHOR;
|
2014-05-13 16:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::getDescription()
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
public static function getDescription() {
|
|
|
|
return 'Plugin offers the known AutoQueue/SpecJam options.';
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::load()
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
|
|
|
public function load(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
|
|
|
|
$this->maniaControl->timerManager->registerTimerListening($this, 'handleEverySecond', 1000);
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERDISCONNECT, $this, 'handlePlayerDisconnect');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERINFOCHANGED, $this, 'handlePlayerInfoChanged');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(Callbacks::BEGINMAP, $this, 'handleBeginMap');
|
|
|
|
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ML_ADDTOQUEUE, $this, 'handleManiaLinkAnswerAdd');
|
|
|
|
$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_WIDGET_POS_X, 0);
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::QUEUE_WIDGET_POS_Y, -46);
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::QUEUE_ACTIVE_ON_PASS, false);
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::QUEUE_CHATMESSAGES, true);
|
|
|
|
|
|
|
|
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
|
|
|
|
$this->maxPlayers = $maxPlayers['CurrentValue'];
|
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
if (!($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false)) {
|
|
|
|
foreach ($this->maniaControl->playerManager->getPlayers() as $player) {
|
2014-05-08 19:40:49 +02:00
|
|
|
if ($player->isSpectator) {
|
|
|
|
$this->spectators[$player->login] = $player->login;
|
|
|
|
$this->maniaControl->client->forceSpectator($player->login, 1);
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:03:26 +02:00
|
|
|
* Function shows the join queue widget to a player.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
2014-05-13 16:03:26 +02:00
|
|
|
private function showJoinQueueWidget(Player $player) {
|
|
|
|
$maniaLink = new ManiaLink(self::ML_ID);
|
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
$quadStyle = $this->maniaControl->manialinkManager->getStyleManager()->getDefaultMainWindowStyle();
|
|
|
|
$quadSubstyle = $this->maniaControl->manialinkManager->getStyleManager()->getDefaultMainWindowSubStyle();
|
2014-05-13 16:40:05 +02:00
|
|
|
$maxQueue = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_MAX);
|
2014-05-13 16:03:26 +02:00
|
|
|
|
|
|
|
// Main frame
|
|
|
|
$frame = new Frame();
|
|
|
|
$maniaLink->add($frame);
|
|
|
|
$frame->setSize(60, 6);
|
|
|
|
$xPos = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_WIDGET_POS_X);
|
|
|
|
$yPos = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_WIDGET_POS_Y);
|
|
|
|
$frame->setPosition($xPos, $yPos, 0);
|
|
|
|
|
|
|
|
// Background
|
|
|
|
$backgroundQuad = new Quad();
|
|
|
|
$frame->add($backgroundQuad);
|
|
|
|
$backgroundQuad->setPosition(0, 0, 0);
|
|
|
|
$backgroundQuad->setSize(70, 10);
|
|
|
|
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
|
|
|
|
|
|
|
$cameraQuad = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($cameraQuad);
|
|
|
|
$cameraQuad->setPosition(-29, 0.4, 2);
|
|
|
|
$cameraQuad->setSize(9, 9);
|
|
|
|
$cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera);
|
|
|
|
|
|
|
|
$statusLabel = new Label_Text();
|
|
|
|
$frame->add($statusLabel);
|
|
|
|
$statusLabel->setPosition(4.5, 2.8, 1);
|
|
|
|
$statusLabel->setSize(66, 4);
|
|
|
|
$statusLabel->setAlign('center', 'center');
|
|
|
|
$statusLabel->setScale(0.8);
|
|
|
|
$statusLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
|
|
|
|
|
|
|
|
$messageLabel = new Label_Button();
|
|
|
|
$frame->add($messageLabel);
|
|
|
|
$messageLabel->setPosition(4.5, -1.6, 1);
|
|
|
|
$messageLabel->setSize(56, 4);
|
|
|
|
$messageLabel->setAlign('center', 'center');
|
|
|
|
$messageLabel->setScale(1.0);
|
|
|
|
|
|
|
|
$inQueue = false;
|
|
|
|
foreach ($this->queue as $queuedPlayer) {
|
|
|
|
if ($queuedPlayer->login == $player->login) {
|
|
|
|
$inQueue = true;
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($inQueue) {
|
|
|
|
$message = '$fff$sYou\'re in the queue (click to unqueue).';
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$position = 0;
|
|
|
|
foreach (array_values($this->queue) as $i => $queuePlayer) {
|
|
|
|
if ($player->login == $queuePlayer->login) {
|
|
|
|
$position = ($i + 1);
|
|
|
|
}
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$statusLabel->setText('$aaaStatus: In queue (' . $position . '/' . count($this->queue) . ') Waiting: ' . count($this->queue) . '/' . $maxQueue . '');
|
|
|
|
$messageLabel->setAction(self::ML_REMOVEFROMQUEUE);
|
|
|
|
$backgroundQuad->setAction(self::ML_REMOVEFROMQUEUE);
|
|
|
|
$statusLabel->setAction(self::ML_REMOVEFROMQUEUE);
|
|
|
|
$cameraQuad->setAction(self::ML_REMOVEFROMQUEUE);
|
|
|
|
} else {
|
|
|
|
if (count($this->queue) < $maxQueue) {
|
|
|
|
$message = '$0ff$sClick to join spectator waiting list.';
|
|
|
|
$messageLabel->setAction(self::ML_ADDTOQUEUE);
|
|
|
|
$backgroundQuad->setAction(self::ML_ADDTOQUEUE);
|
|
|
|
$statusLabel->setAction(self::ML_ADDTOQUEUE);
|
|
|
|
$cameraQuad->setAction(self::ML_ADDTOQUEUE);
|
|
|
|
} else {
|
|
|
|
$message = '$f00The waiting list is full!';
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$statusLabel->setText('$aaaStatus: Not queued spectator Waiting: ' . count($this->queue) . '/' . $maxQueue . '');
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$messageLabel->setText($message);
|
|
|
|
$messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login);
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::unload()
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
2014-05-13 16:03:26 +02:00
|
|
|
public function unload() {
|
|
|
|
foreach ($this->spectators as $spectator) {
|
|
|
|
$this->maniaControl->client->forceSpectator($spectator, 3);
|
|
|
|
$this->maniaControl->client->forceSpectator($spectator, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->maniaControl->manialinkManager->hideManialink(self::ML_ID);
|
|
|
|
|
|
|
|
$this->queue = array();
|
|
|
|
$this->spectators = array();
|
|
|
|
$this->showPlay = array();
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function handling on the connection of a player.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function handlePlayerConnect(Player $player) {
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
2014-05-08 19:40:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
|
|
|
|
$this->maxPlayers = $maxPlayers['CurrentValue'];
|
|
|
|
|
|
|
|
if ($player->isSpectator) {
|
|
|
|
$this->spectators[$player->login] = $player->login;
|
|
|
|
$this->maniaControl->client->forceSpectator($player->login, 1);
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
} else {
|
|
|
|
if (count($this->queue) != 0) {
|
|
|
|
$this->maniaControl->client->forceSpectator($player->login, 1);
|
|
|
|
$this->spectators[$player->login] = $player->login;
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function handling on the disconnection of a player.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function handlePlayerDisconnect(Player $player) {
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
2014-05-08 19:40:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
|
|
|
|
$this->maxPlayers = $maxPlayers['CurrentValue'];
|
|
|
|
|
|
|
|
if (isset($this->spectators[$player->login])) {
|
|
|
|
unset($this->spectators[$player->login]);
|
|
|
|
}
|
|
|
|
$this->removePlayerFromQueue($player->login);
|
|
|
|
$this->moveFirstPlayerToPlay();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:40:05 +02:00
|
|
|
* Remove a Player from the Queue
|
2014-05-04 15:43:16 +02:00
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param string $login
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
2014-05-13 16:03:26 +02:00
|
|
|
private function removePlayerFromQueue($login) {
|
|
|
|
$count = 0;
|
|
|
|
$newQueue = array();
|
|
|
|
foreach ($this->queue as $queuePlayer) {
|
|
|
|
if ($queuePlayer->login != $login) {
|
|
|
|
$newQueue[$count] = $queuePlayer;
|
|
|
|
$count++;
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-13 16:03:26 +02:00
|
|
|
|
|
|
|
$this->queue = $newQueue;
|
|
|
|
$this->showQueueWidgetSpectators();
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 16:40:05 +02:00
|
|
|
/**
|
|
|
|
* Show the Queue Widgets to Spectators
|
|
|
|
*/
|
2014-05-04 15:43:16 +02:00
|
|
|
public function showQueueWidgetSpectators() {
|
2014-05-13 16:03:26 +02:00
|
|
|
foreach ($this->spectators as $login) {
|
2014-05-04 15:43:16 +02:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used to move the first queued player to the
|
|
|
|
*/
|
|
|
|
private function moveFirstPlayerToPlay() {
|
|
|
|
if (count($this->queue) > 0) {
|
|
|
|
$firstPlayer = $this->maniaControl->playerManager->getPlayer($this->queue[0]->login);
|
|
|
|
$this->forcePlayerToPlay($firstPlayer);
|
|
|
|
$this->showQueueWidgetSpectators();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to force a player to play status.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
private function forcePlayerToPlay(Player $player) {
|
2014-08-24 12:56:06 +02:00
|
|
|
if ($this->maniaControl->getClient()->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
2014-05-04 15:43:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-08 22:51:01 +02:00
|
|
|
if ($this->maxPlayers > $this->maniaControl->playerManager->getPlayerCount(true)) {
|
2014-05-04 15:43:16 +02:00
|
|
|
try {
|
2014-08-24 12:56:06 +02:00
|
|
|
$this->maniaControl->getClient()->forceSpectator($player->login, PlayerActions::SPECTATOR_PLAYER);
|
2014-05-13 16:03:26 +02:00
|
|
|
} catch (Exception $e) {
|
2014-05-04 15:43:16 +02:00
|
|
|
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
|
|
|
|
$this->maniaControl->chat->sendError("Error while leaving the Queue", $player->login);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-08-24 12:56:06 +02:00
|
|
|
$this->maniaControl->getClient()->forceSpectator($player->login, PlayerActions::SPECTATOR_USER_SELECTABLE);
|
2014-05-13 16:03:26 +02:00
|
|
|
} catch (Exception $e) {
|
2014-05-04 15:43:16 +02:00
|
|
|
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
|
|
|
|
}
|
|
|
|
|
|
|
|
$teams = array();
|
2014-07-08 22:11:44 +02:00
|
|
|
|
|
|
|
//Initialize at least two teams with 0 players
|
|
|
|
$teams[0] = 0;
|
|
|
|
$teams[1] = 0;
|
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
/** @var Player $playerObj */
|
2014-07-08 21:50:32 +02:00
|
|
|
foreach ($this->maniaControl->playerManager->getPlayers() as $playerObj) {
|
2014-08-13 11:14:29 +02:00
|
|
|
if ($playerObj->isSpectator) {
|
2014-07-08 21:50:32 +02:00
|
|
|
continue;
|
2014-08-13 11:14:29 +02:00
|
|
|
}
|
2014-07-08 21:50:32 +02:00
|
|
|
|
2014-05-04 15:43:16 +02:00
|
|
|
if (!isset($teams[$playerObj->teamId])) {
|
|
|
|
$teams[$playerObj->teamId] = 1;
|
|
|
|
} else {
|
|
|
|
$teams[$playerObj->teamId]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$smallestTeam = null;
|
|
|
|
$smallestSize = 999;
|
2014-05-13 16:03:26 +02:00
|
|
|
foreach ($teams as $team => $size) {
|
2014-05-04 15:43:16 +02:00
|
|
|
if ($size < $smallestSize) {
|
|
|
|
$smallestTeam = $team;
|
|
|
|
$smallestSize = $size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-24 12:56:06 +02:00
|
|
|
if ($smallestTeam !== null) {
|
|
|
|
try {
|
|
|
|
$this->maniaControl->getClient()->forcePlayerTeam($player->login, $smallestTeam);
|
|
|
|
} catch (GameModeException $e) {
|
|
|
|
try {
|
|
|
|
$this->maniaControl->getClient()->forceSpectator($player->login, PlayerActions::SPECTATOR_PLAYER);
|
|
|
|
$this->maniaControl->getClient()->forceSpectator($player->login, PlayerActions::SPECTATOR_USER_SELECTABLE);
|
|
|
|
} catch (ServerOptionsException $e) {
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->spectators[$player->login])) {
|
|
|
|
unset($this->spectators[$player->login]);
|
|
|
|
}
|
|
|
|
$this->removePlayerFromQueue($player->login);
|
|
|
|
$this->showPlayWidget($player);
|
|
|
|
$this->sendChatMessage('$<$fff' . $player->nickname . '$> has a free spot and is now playing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:03:26 +02:00
|
|
|
* Function shows the "You got a free spot, enjoy playing!" widget.
|
2014-05-04 15:43:16 +02:00
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
2014-05-13 16:03:26 +02:00
|
|
|
private function showPlayWidget(Player $player) {
|
2014-05-04 15:43:16 +02:00
|
|
|
$maniaLink = new ManiaLink(self::ML_ID);
|
|
|
|
|
2014-08-02 22:31:46 +02:00
|
|
|
$quadStyle = $this->maniaControl->manialinkManager->getStyleManager()->getDefaultMainWindowStyle();
|
|
|
|
$quadSubstyle = $this->maniaControl->manialinkManager->getStyleManager()->getDefaultMainWindowSubStyle();
|
2014-05-04 15:43:16 +02:00
|
|
|
|
|
|
|
// Main frame
|
|
|
|
$frame = new Frame();
|
|
|
|
$maniaLink->add($frame);
|
|
|
|
$frame->setSize(60, 6);
|
2014-05-13 16:54:32 +02:00
|
|
|
$xPos = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_WIDGET_POS_X);
|
|
|
|
$yPos = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_WIDGET_POS_Y);
|
|
|
|
$frame->setPosition($xPos, $yPos, 0);
|
2014-05-04 15:43:16 +02:00
|
|
|
|
|
|
|
// Background
|
|
|
|
$backgroundQuad = new Quad();
|
|
|
|
$frame->add($backgroundQuad);
|
|
|
|
$backgroundQuad->setPosition(0, 0, 0);
|
|
|
|
$backgroundQuad->setSize(70, 10);
|
|
|
|
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
|
|
|
|
|
|
|
$cameraQuad = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($cameraQuad);
|
|
|
|
$cameraQuad->setPosition(-29, 0.4, 2);
|
|
|
|
$cameraQuad->setSize(9, 9);
|
|
|
|
$cameraQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_Camera);
|
|
|
|
|
|
|
|
$messageLabel = new Label_Button();
|
|
|
|
$frame->add($messageLabel);
|
2014-05-13 16:03:26 +02:00
|
|
|
$messageLabel->setPosition(4.5, 0.6, 1);
|
2014-05-04 15:43:16 +02:00
|
|
|
$messageLabel->setSize(56, 4);
|
|
|
|
$messageLabel->setAlign('center', 'center');
|
|
|
|
$messageLabel->setScale(1.0);
|
2014-05-13 16:03:26 +02:00
|
|
|
$messageLabel->setText('$090You got a free spot, enjoy playing!');
|
|
|
|
$messageLabel->setStyle(Label_Text::STYLE_TextStaticSmall);
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login);
|
|
|
|
$this->showPlay[$player->login] = array('time' => time(), 'player' => $player);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-20 14:59:17 +02:00
|
|
|
* Function sends (or not depending on setting) chat messages for the queue.
|
2014-05-13 16:03:26 +02:00
|
|
|
*
|
2014-05-13 16:40:05 +02:00
|
|
|
* @param string $message
|
2014-05-13 16:03:26 +02:00
|
|
|
*/
|
|
|
|
private function sendChatMessage($message) {
|
|
|
|
if ($this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_CHATMESSAGES)) {
|
|
|
|
$this->maniaControl->chat->sendChat('$090[Queue] ' . $message);
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
2014-05-13 16:03:26 +02:00
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
/**
|
|
|
|
* Function handling the change of player information.
|
|
|
|
*
|
|
|
|
* @param array $callback
|
|
|
|
*/
|
|
|
|
public function handlePlayerInfoChanged(array $callback) {
|
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$login = $callback[1][0]['Login'];
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
if (!is_null($player)) {
|
|
|
|
if ($player->isSpectator) {
|
|
|
|
if (!isset($this->spectators[$player->login])) {
|
|
|
|
$this->maniaControl->client->forceSpectator($player->login, 1);
|
|
|
|
$this->spectators[$player->login] = $player->login;
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
$this->showQueueWidgetSpectators();
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
} else {
|
2014-05-13 16:03:26 +02:00
|
|
|
$this->removePlayerFromQueue($player->login);
|
|
|
|
if (isset($this->spectators[$player->login])) {
|
|
|
|
unset($this->spectators[$player->login]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($this->showPlay as $showPlay) {
|
|
|
|
if ($showPlay['player']->login == $player->login) {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$found) {
|
|
|
|
$this->hideQueueWidget($player);
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
2014-05-13 16:03:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function hides the queue widget from the player.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
private function hideQueueWidget(Player $player) {
|
|
|
|
$this->maniaControl->manialinkManager->hideManialink(self::ML_ID, $player);
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
/**
|
|
|
|
* Function called on every second.
|
|
|
|
*/
|
|
|
|
public function handleEverySecond() {
|
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
|
|
|
return;
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
2014-07-17 20:01:31 +02:00
|
|
|
if ($this->maxPlayers > ($this->maniaControl->playerManager->getPlayerCount() - count($this->spectators))) {
|
2014-05-13 16:03:26 +02:00
|
|
|
$this->moveFirstPlayerToPlay();
|
|
|
|
$this->showQueueWidgetSpectators();
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
foreach ($this->showPlay as $showPlay) {
|
|
|
|
if (($showPlay['time'] + 5) < time()) {
|
|
|
|
$this->hideQueueWidget($showPlay['player']);
|
|
|
|
unset($this->showPlay[$showPlay['player']->login]);
|
|
|
|
}
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:03:26 +02:00
|
|
|
* Checks for being of new map to retrieve maximum number of players.
|
2014-05-04 15:43:16 +02:00
|
|
|
*/
|
2014-05-13 16:40:05 +02:00
|
|
|
public function handleBeginMap() {
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$maxPlayers = $this->maniaControl->client->getMaxPlayers();
|
|
|
|
$this->maxPlayers = $maxPlayers['CurrentValue'];
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
/**
|
|
|
|
* Function handling the click of the widget to add them to the queue.
|
|
|
|
*
|
|
|
|
* @param array $chatCallback
|
|
|
|
* @param Player $player
|
|
|
|
*/
|
|
|
|
public function handleManiaLinkAnswerAdd(array $chatCallback, Player $player) {
|
|
|
|
$this->addPlayerToQueue($player);
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
/**
|
|
|
|
* Function adds a player to the queue.
|
|
|
|
*
|
|
|
|
* @param Player $player
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function addPlayerToQueue(Player $player) {
|
|
|
|
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
foreach ($this->queue as $queuedPlayer) {
|
|
|
|
if ($queuedPlayer->login == $player->login) {
|
|
|
|
$this->maniaControl->chat->sendError('You\'re already in the queue!', $player->login);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
if ($this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_MAX) > count($this->queue)) {
|
|
|
|
$this->queue[count($this->queue)] = $player;
|
|
|
|
$this->sendChatMessage('$<$fff' . $player->nickname . '$> just joined the queue!');
|
|
|
|
}
|
2014-05-04 15:43:16 +02:00
|
|
|
|
2014-05-13 16:03:26 +02:00
|
|
|
$this->showQueueWidgetSpectators();
|
|
|
|
|
|
|
|
return true;
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-13 16:03:26 +02:00
|
|
|
* Function handling the click of the widget to remove them from the queue.
|
2014-05-04 15:43:16 +02:00
|
|
|
*
|
2014-05-13 16:03:26 +02:00
|
|
|
* @param array $chatCallback
|
2014-05-04 15:43:16 +02:00
|
|
|
* @param Player $player
|
|
|
|
*/
|
2014-05-13 16:03:26 +02:00
|
|
|
public function handleManiaLinkAnswerRemove(array $chatCallback, Player $player) {
|
|
|
|
$this->removePlayerFromQueue($player->login);
|
|
|
|
$this->showJoinQueueWidget($player);
|
|
|
|
$this->sendChatMessage('$<$fff' . $player->nickname . '$> has left the queue!');
|
2014-05-04 15:43:16 +02:00
|
|
|
}
|
2014-05-07 21:42:23 +02:00
|
|
|
}
|