Improved PHPDoc, Type Hints & Parameter Names

This commit is contained in:
Steffen Schröder
2014-05-13 16:40:05 +02:00
parent 3e69e03292
commit 2a705e05d9
27 changed files with 434 additions and 447 deletions

View File

@ -142,14 +142,14 @@ class ChatMessagePlugin implements CommandListener, Plugin {
* Checks if a Player is in the PlayerList and returns the nickname if he is, can be called per login, pid or nickname or lj for
* (last joined)
*
* @param $login
* @param mixed $login
* @return mixed
*/
private function getTarget($login) {
/** @var Player $player */
$player = null;
foreach ($this->maniaControl->playerManager->getPlayers() as $player) {
if ($login == $player->login || $login == $player->pid || $login == $player->nickname) {
if ($login == $player && $login == $player->login || $login == $player->pid || $login == $player->nickname) {
return $player->nickname;
}
}

View File

@ -486,8 +486,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
/**
* Handle Standard Votes
*
* @param $voteName
* @param $voteResult
* @param string $voteName
* @param float $voteResult
*/
public function handleVoteFinished($voteName, $voteResult) {
if ($voteResult >= $this->currentVote->neededRatio) {
@ -563,9 +563,9 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
}
/**
* Undefines a Vote
* Undefine a Vote
*
* @param $voteIndex
* @param int $voteIndex
*/
public function undefineVote($voteIndex) {
unset($this->voteCommands[$voteIndex]);
@ -600,11 +600,9 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
}
/**
* Handle ManiaControl 1 Second callback
*
* @param $time
* Handle ManiaControl 1 Second Callback
*/
public function handle1Second($time) {
public function handle1Second() {
if (!isset($this->currentVote)) {
return;
}
@ -774,10 +772,10 @@ class VoteCommand {
/**
* Construct a new Vote Command
*
* @param $index
* @param $name
* @param $idBased
* @param $neededRatio
* @param int $index
* @param string $name
* @param bool $idBased
* @param float $neededRatio
*/
public function __construct($index, $name, $idBased, $neededRatio) {
$this->index = $index;

View File

@ -3,6 +3,7 @@
namespace MCTeam\Dedimania;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use Maniaplanet\DedicatedServer\Structures\Version;
/**
@ -52,6 +53,11 @@ class DedimaniaData {
$this->serverBuild = $serverVersion->build;
}
/**
* Build the Data Array
*
* @return array
*/
public function toArray() {
$array = array();
foreach (get_object_vars($this) as $key => $value) {
@ -63,6 +69,11 @@ class DedimaniaData {
return $array;
}
/**
* Get the Number of Records
*
* @return int
*/
public function getRecordCount() {
return count($this->records);
}
@ -70,10 +81,11 @@ class DedimaniaData {
/**
* Get Max Rank for a certain Player
*
* @param $login
* @param mixed $login
* @return int
*/
public function getPlayerMaxRank($login) {
$login = Player::parseLogin($login);
$maxRank = $this->serverMaxRank;
foreach ($this->players as $player) {
/** @var DedimaniaPlayer $player */

View File

@ -14,12 +14,12 @@ use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Utils\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Utils\Formatter;
/**
* ManiaControl Dedimania Plugin
@ -226,11 +226,11 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
/**
* Handle xml rpc fault
*
* @param $fault
* @param $method
* @param array $fault
* @param string $method
*/
private function handleXmlRpcFault($fault, $method) {
trigger_error('XmlRpc Fault on ' . $method . ': ' . $fault['faultString'] . ' (' . $fault['faultCode'] . ')');
private function handleXmlRpcFault(array $fault, $method) {
trigger_error("XmlRpc Fault on '{$method}': '{$fault['faultString']} ({$fault['faultCode']})!");
}
/**
@ -505,11 +505,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Check if the session is alive every minute
*
* @param null $callback
* Handle 1 Minute Callback
*/
public function handleEveryMinute($callback = null) {
public function handleEveryMinute() {
if (!$this->init) {
return;
}
@ -633,21 +631,17 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Handle Begin Map
*
* @param $callback
* Handle Begin Map Callback
*/
public function handleBeginMap($callback) {
public function handleBeginMap() {
unset($this->dedimaniaData->records);
$this->fetchDedimaniaRecords(true);
}
/**
* Handle EndMap callback
*
* @param $callback
* Handle EndMap Callback
*/
public function handleMapEnd($callback) {
public function handleMapEnd() {
if (!$this->dedimaniaData || !$this->dedimaniaData->records) {
return;
}
@ -714,11 +708,9 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Update the Playerlist every 3 Minutes
*
* @param $callback
* Update the PlayerList every 3 Minutes
*/
public function updatePlayerList($callback) {
public function updatePlayerList() {
$serverInfo = $this->getServerInfo();
$playerList = $this->getPlayerList();
$votesInfo = $this->getVotesInfo();
@ -769,11 +761,11 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Handle PlayerCheckpoint callback
* Handle PlayerCheckpoint Callback
*
* @param $callback
* @param array $callback
*/
public function handlePlayerCheckpoint($callback) {
public function handlePlayerCheckpoint(array $callback) {
$data = $callback[1];
$login = $data[1];
$time = $data[2];
@ -786,11 +778,11 @@ class DedimaniaPlugin implements CallbackListener, CommandListener, TimerListene
}
/**
* Player finished callback
* Handle Player Finished Callback
*
* @param $callback
* @param array $callback
*/
public function handlePlayerFinished($callback) {
public function handlePlayerFinished(array $callback) {
//var_dump($callback);
$data = $callback[1];
if ($data[0] <= 0 || $data[2] <= 0) {

View File

@ -11,7 +11,6 @@ use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Utils\ColorUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
@ -20,6 +19,7 @@ use ManiaControl\Plugins\Plugin;
use ManiaControl\Plugins\PluginMenu;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\ColorUtil;
/**
* ManiaControl Karma Plugin
@ -280,7 +280,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/**
* Activates the MX-Karma Session
*
* @param $mxKarmaCode
* @param string $mxKarmaCode
*/
private function activateSession($mxKarmaCode) {
$hash = $this->buildActivationHash($this->mxKarma['session']->sessionSeed, $mxKarmaCode);
@ -323,8 +323,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
/**
* Builds a sha512 activation Hash for the MX-Karma
*
* @param $sessionSeed
* @param $mxKey
* @param string $sessionSeed
* @param string $mxKey
* @return string
*/
private function buildActivationHash($sessionSeed, $mxKey) {
@ -756,11 +756,9 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
}
/**
* Handle ManiaControl 1 Second callback
*
* @param $time
* Handle ManiaControl 1 Second Callback
*/
public function handle1Second($time) {
public function handle1Second() {
if (!$this->updateManialink) {
return;
}

View File

@ -15,7 +15,6 @@ use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Utils\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Maps\Map;
@ -24,6 +23,7 @@ use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use ManiaControl\Utils\Formatter;
/**
* ManiaControl Local Records Plugin
@ -182,11 +182,9 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
}
/**
* Handle 1Second callback
*
* @param $time
* Handle 1 Second Callback
*/
public function handle1Second($time) {
public function handle1Second() {
if (!$this->updateManialink) {
return;
}
@ -352,12 +350,13 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/**
* Handle PlayerCheckpoint callback
*
* @param $callback
* @param array $callback
*/
public function handlePlayerCheckpoint($callback) {
public function handlePlayerCheckpoint(array $callback) {
$data = $callback[1];
$login = $data[1];
$time = $data[2];
// TODO: lap
// $lap = $data[3];
$cpIndex = $data[4];
if (!isset($this->checkpoints[$login]) || $cpIndex <= 0) {

View File

@ -34,6 +34,8 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
*/
const ID = 22;
const VERSION = 0.12;
const AUTHOR = 'TheM';
const NAME = 'Queue Plugin';
const ML_ID = 'Queue.Widget';
const ML_ADDTOQUEUE = 'Queue.Add';
const ML_REMOVEFROMQUEUE = 'Queue.Remove';
@ -55,65 +57,49 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
private $maxPlayers = 0;
/**
* Prepares the Plugin
*
* @param ManiaControl $maniaControl
* @return mixed
* @see \ManiaControl\Plugins\Plugin::prepare()
*/
public static function prepare(ManiaControl $maniaControl) {
// TODO: Implement prepare() method.
}
/**
* Get plugin id
*
* @return int
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
return self::ID;
}
/**
* Get Plugin Name
*
* @return string
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
return 'Queue Plugin';
return self::NAME;
}
/**
* Get Plugin Version
*
* @return float
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
return self::VERSION;
}
/**
* Get Plugin Author
*
* @return string
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
return 'TheM';
return self::AUTHOR;
}
/**
* Get Plugin Description
*
* @return string
* @see \ManiaControl\Plugins\Plugin::getDescription()
*/
public static function getDescription() {
return 'Plugin offers the known AutoQueue/SpecJam options.';
}
/**
* Load the plugin
*
* @param ManiaControl $maniaControl
* @return bool
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
@ -156,7 +142,7 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultMainWindowSubStyle();
$maxQueue = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_MAX);
$maxQueue = $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_MAX);
// Main frame
$frame = new Frame();
@ -237,7 +223,7 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
}
/**
* Unload the plugin and its resources
* @see \ManiaControl\Plugins\Plugin::unload()
*/
public function unload() {
foreach ($this->spectators as $spectator) {
@ -299,9 +285,9 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
}
/**
* Function removes a player from the queue.
* Remove a Player from the Queue
*
* @param $login
* @param string $login
*/
private function removePlayerFromQueue($login) {
$count = 0;
@ -317,6 +303,9 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
$this->showQueueWidgetSpectators();
}
/**
* Show the Queue Widgets to Spectators
*/
public function showQueueWidgetSpectators() {
foreach ($this->spectators as $login) {
$player = $this->maniaControl->playerManager->getPlayer($login);
@ -445,7 +434,7 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
/**
* Function sends (or not depending on setting) chatmessages for the queue.
*
* @param $message
* @param string $message
*/
private function sendChatMessage($message) {
if ($this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_CHATMESSAGES)) {
@ -526,10 +515,8 @@ class QueuePlugin implements CallbackListener, ManialinkPageAnswerListener, Time
/**
* Checks for being of new map to retrieve maximum number of players.
*
* @param $currentMap
*/
public function handleBeginMap($currentMap) {
public function handleBeginMap() {
if ($this->maniaControl->client->getServerPassword() != false && $this->maniaControl->settingManager->getSettingValue($this, self::QUEUE_ACTIVE_ON_PASS) == false) {
return;
}

View File

@ -11,12 +11,12 @@ use FML\ManiaLink;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener;
use ManiaControl\Utils\Formatter;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use ManiaControl\Plugins\Plugin;
use ManiaControl\Utils\Formatter;
/**
* TeamSpeak Info plugin
@ -70,66 +70,6 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
$maniaControl->settingManager->initSetting(get_class(), self::TEAMSPEAK_QUERYPASS, '');
}
/**
* Load the plugin
*
* @param \ManiaControl\ManiaControl $maniaControl
* @return bool
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->checkConfig();
$this->refreshTime = time();
$this->maniaControl->manialinkManager->iconManager->addIcon(self::TS_ICON);
$this->maniaControl->manialinkManager->iconManager->addIcon(self::TS_ICON_MOVER);
$this->maniaControl->timerManager->registerTimerListening($this, 'ts3_queryServer', 1000);
$this->addToMenu();
}
/**
* Function used to check certain configuration options to check if they can be used.
*
* @throws \Exception
*/
private function checkConfig() {
if ($this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SERVERHOST) == 'ts3.somehoster.com') {
$error = 'Missing the required serverhost, please set it up before enabling the TeamSpeak plugin!';
throw new \Exception($error);
}
$this->ts3_queryServer(); // Get latest information from the TeamSpeak server
if (!isset($this->serverData['channels']) || count($this->serverData['channels']) == 0) {
$error = 'Could not make proper connections with the server!';
throw new \Exception($error);
}
}
/**
* Function used insert the icon into the menu.
*/
private function addToMenu() {
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_TSVIEWER, $this, 'command_tsViewer');
$itemQuad = new Quad();
$itemQuad->setImage($this->maniaControl->manialinkManager->iconManager->getIcon(self::TS_ICON));
$itemQuad->setImageFocus($this->maniaControl->manialinkManager->iconManager->getIcon(self::TS_ICON_MOVER));
$itemQuad->setAction(self::ACTION_OPEN_TSVIEWER);
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 1, 'Open TeamSpeak Viewer');
}
/**
* Unload the plugin and its resources
*/
public function unload() {
$this->serverData = array();
$this->maniaControl->actionsMenu->removeMenuItem(1, true);
}
/**
* Get plugin id
*
@ -175,6 +115,215 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
return 'Plugin offers a connection with a TeamSpeak server (via widgets).';
}
/**
* Load the plugin
*
* @param \ManiaControl\ManiaControl $maniaControl
* @return bool
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->checkConfig();
$this->refreshTime = time();
$this->maniaControl->manialinkManager->iconManager->addIcon(self::TS_ICON);
$this->maniaControl->manialinkManager->iconManager->addIcon(self::TS_ICON_MOVER);
$this->maniaControl->timerManager->registerTimerListening($this, 'ts3_queryServer', 1000);
$this->addToMenu();
}
/**
* Function used to check certain configuration options to check if they can be used.
*
* @throws \Exception
*/
private function checkConfig() {
if ($this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SERVERHOST) == 'ts3.somehoster.com') {
$error = 'Missing the required serverhost, please set it up before enabling the TeamSpeak plugin!';
throw new \Exception($error);
}
$this->ts3_queryServer(); // Get latest information from the TeamSpeak server
if (!isset($this->serverData['channels']) || count($this->serverData['channels']) == 0) {
$error = 'Could not make proper connections with the server!';
throw new \Exception($error);
}
}
/**
* TeamSpeak related functions
* The functions are based upon tsstatus.php from http://tsstatus.sebastien.me/
* and were optimized by SilentStorm.
* Functions originally from the TeamSpeakInfo plugin made by undef.de for XAseco(2) and MPAseco.
*/
public function ts3_queryServer() {
if (time() >= $this->refreshTime) {
$this->refreshTime = (time() + $this->refreshInterval);
$queryHost = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYHOST);
$host = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SERVERHOST);
$host = ($queryHost != '') ? $queryHost : $host;
$queryPort = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYPORT);
$socket = fsockopen(@$host, $queryPort, $errno, $errstr, 2);
if ($socket) {
socket_set_timeout($socket, 2);
$is_ts3 = trim(fgets($socket)) == 'TS3';
if (!$is_ts3) {
trigger_error('[TeamSpeakPlugin] Server at "' . $host . '" is not a Teamspeak3-Server or you have setup a bad query-port!', E_USER_WARNING);
}
$queryuser = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYUSER);
$querypass = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYPASS);
if (($queryuser != '') && !is_numeric($queryuser) && $queryuser != false && ($querypass != '') && !is_numeric($querypass) && $querypass != false) {
$ret = $this->ts3_sendCommand($socket, 'login client_login_name=' . $this->ts3_escape($queryuser) . ' client_login_password=' . $this->ts3_escape($querypass));
if (stripos($ret, "error id=0") === false) {
trigger_error("[TeamSpeakPlugin] Failed to authenticate with TS3 Server! Make sure you put the correct username & password in teamspeak.xml", E_USER_WARNING);
return;
}
}
$response = '';
$response .= $this->ts3_sendCommand($socket, 'use sid=' . $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SID));
$this->ts3_sendCommand($socket, 'clientupdate client_nickname=' . $this->ts3_escape('ManiaControl Viewer'));
$response .= $this->ts3_sendCommand($socket, 'serverinfo');
$response .= $this->ts3_sendCommand($socket, 'channellist -topic -flags -voice -limits');
$response .= $this->ts3_sendCommand($socket, 'clientlist -uid -away -voice -groups');
fputs($socket, "quit\n");
fclose($socket);
$lines = explode("error id=0 msg=ok\n\r", $response);
if (count($lines) == 5) {
$serverdata = $this->ts3_parseLine($lines[1]);
$this->serverData['server'] = $serverdata[0];
$this->serverData['channels'] = $this->ts3_parseLine($lines[2]);
$users = $this->ts3_parseLine($lines[3]);
$this->serverData['users'] = array(); // reset userslist
foreach ($users as $user) {
if ($user['client_nickname'] != 'ManiaControl Viewer') {
$this->serverData['users'][] = $user;
}
}
// Subtract reserved slots
$this->serverData['server']['virtualserver_maxclients'] -= $this->serverData['server']['virtualserver_reserved_slots'];
// Make ping value int
$this->serverData['server']['virtualserver_total_ping'] = intval($this->serverData['server']['virtualserver_total_ping']);
// Format the Date of server startup
$this->serverData['server']['virtualserver_uptime'] = date('Y-m-d H:i:s', (time() - $this->serverData['server']['virtualserver_uptime']));
// Always subtract all Query Clients
$this->serverData['server']['virtualserver_clientsonline'] -= $this->serverData['server']['virtualserver_queryclientsonline'];
}
} else {
trigger_error("[TeamSpeakPlugin] Failed to connect with TS3 server; socket error: " . $errstr . " [" . $errno . "]", E_USER_WARNING);
}
}
}
/**
* TS Function to send a command to the TeamSpeak server.
*
* @param resource $socket
* @param string $cmd
* @return string
*/
private function ts3_sendCommand($socket, $cmd) {
fputs($socket, "$cmd\n");
$response = '';
/*while(strpos($response, 'error id=') === false) {
$response .= fread($socket, 8096);
}*/
/*while (!feof($socket)) {
$response .= fread($socket, 8192);
}*/
$info = array('timed_out' => false);
while (!feof($socket) && !$info['timed_out'] && strpos($response, 'error id=') === false) {
$response .= fread($socket, 1024);
$info = stream_get_meta_data($socket);
}
return $response;
}
/**
* TS Function used to escape characters in channelnames.
*
* @param string $str
* @return mixed
*/
private function ts3_escape($str) {
return str_replace(array(chr(92), chr(47), chr(32), chr(124), chr(7), chr(8), chr(12), chr(10), chr(3), chr(9), chr(11)), array('\\\\', "\/", "\s", "\p", "\a", "\b", "\f", "\n", "\r", "\t", "\v"), $str);
}
/**
* TS Function used to parse lines in the serverresponse.
*
* @param string $rawLine
* @return array
*/
private function ts3_parseLine($rawLine) {
$datas = array();
$rawItems = explode('|', $rawLine);
foreach ($rawItems as &$rawItem) {
$rawDatas = explode(' ', $rawItem);
$tempDatas = array();
foreach ($rawDatas as &$rawData) {
$ar = explode("=", $rawData, 2);
$tempDatas[$ar[0]] = isset($ar[1]) ? $this->ts3_unescape($ar[1]) : '';
}
$datas[] = $tempDatas;
}
unset($rawItem, $rawData);
return $datas;
}
/**
* TS Function used to unescape characters in channelnames.
*
* @param string $str
* @return mixed
*/
private function ts3_unescape($str) {
return str_replace(array('\\\\', "\/", "\s", "\p", "\a", "\b", "\f", "\n", "\r", "\t", "\v"), array(chr(92), chr(47), chr(32), chr(124), chr(7), chr(8), chr(12), chr(10), chr(3), chr(9), chr(11)), $str);
}
/**
* Function used insert the icon into the menu.
*/
private function addToMenu() {
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_TSVIEWER, $this, 'command_tsViewer');
$itemQuad = new Quad();
$itemQuad->setImage($this->maniaControl->manialinkManager->iconManager->getIcon(self::TS_ICON));
$itemQuad->setImageFocus($this->maniaControl->manialinkManager->iconManager->getIcon(self::TS_ICON_MOVER));
$itemQuad->setAction(self::ACTION_OPEN_TSVIEWER);
$this->maniaControl->actionsMenu->addMenuItem($itemQuad, true, 1, 'Open TeamSpeak Viewer');
}
/**
* Unload the plugin and its resources
*/
public function unload() {
$this->serverData = array();
$this->maniaControl->actionsMenu->removeMenuItem(1, true);
}
/**
* Function handling the pressing of the icon.
*
@ -188,7 +337,7 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
/**
* Function showing the TeamSpeak widget to the player.
*
* @param $player
* @param mixed $player
*/
private function showWidget($player) {
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
@ -256,7 +405,7 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
$channels->setHAlign('left');
$channels->setTextSize(1);
$nochannels = 0;
foreach($this->serverData['channels'] as $channel) {
foreach ($this->serverData['channels'] as $channel) {
if ($channel['channel_maxclients'] == 0 || strpos($channel['channel_name'], 'spacer') > 0) {
continue;
}
@ -288,51 +437,51 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
$userid = 0;
$i = 0;
$startx = -69.5;
foreach($this->serverData['channels'] as $channel) {
foreach ($this->serverData['channels'] as $channel) {
if ($channel['channel_maxclients'] == 0 || strpos($channel['channel_name'], 'spacer') > 0) {
continue;
}
$channelLabel = new Label_Text();
$frame->add($channelLabel);
$y = 17.5 + ($i * 2.5);
$channelLabel->setY($height / 2 - $y);
$channelLabel->setY($height / 2 - $y);
$x = $startx;
if ($channel['pid'] != 0) {
$x = $startx + 5;
}
$channelLabel->setX($x);
$channelLabel->setStyle($channelLabel::STYLE_TextCardMedium);
$channelLabel->setHAlign('left');
$channelLabel->setTextSize(1);
$channelLabel->setScale(0.9);
$channelLabel->setX($x);
$channelLabel->setStyle($channelLabel::STYLE_TextCardMedium);
$channelLabel->setHAlign('left');
$channelLabel->setTextSize(1);
$channelLabel->setScale(0.9);
if ($channel['channel_flag_default'] == 1) {
$channel['total_clients'] = ($channel['total_clients'] - 1);
} // remove query client
$channelLabel->setText('$o' . $channel['channel_name'] . '$o (' . $channel['total_clients'] . ')');
$channelLabel->setTextColor('fff');
$channelLabel->setText('$o' . $channel['channel_name'] . '$o (' . $channel['total_clients'] . ')');
$channelLabel->setTextColor('fff');
$channels[$i] = $channelLabel;
$channels[$i] = $channelLabel;
$i++;
foreach($this->serverData['users'] as $user) {
foreach ($this->serverData['users'] as $user) {
if ($user['cid'] == $channel['cid']) {
$userLabel = new Label_Text();
$userLabel = new Label_Text();
$frame->add($userLabel);
$y = 17.5 + ($i * 2.5);
$userLabel->setY($height / 2 - $y);
$userLabel->setY($height / 2 - $y);
if ($channel['pid'] != 0) {
$x = $startx + 7;
} else {
$x = $startx + 2;
}
$userLabel->setX($x);
$userLabel->setStyle($userLabel::STYLE_TextCardMedium);
$userLabel->setHAlign('left');
$userLabel->setTextSize(1);
$userLabel->setScale(0.9);
$userLabel->setText($user['client_nickname']);
$userLabel->setTextColor('fff');
$users[$userid] = $userLabel;
$userLabel->setX($x);
$userLabel->setStyle($userLabel::STYLE_TextCardMedium);
$userLabel->setHAlign('left');
$userLabel->setTextSize(1);
$userLabel->setScale(0.9);
$userLabel->setText($user['client_nickname']);
$userLabel->setTextColor('fff');
$users[$userid] = $userLabel;
$userid++;
$i++;
@ -359,156 +508,4 @@ class TeamSpeakPlugin implements CallbackListener, CommandListener, ManialinkPag
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'TSViewer');
}
/**
* TeamSpeak related functions
* The functions are based upon tsstatus.php from http://tsstatus.sebastien.me/
* and were optimized by SilentStorm.
* Functions originally from the TeamSpeakInfo plugin made by undef.de for XAseco(2) and MPAseco.
*/
public function ts3_queryServer() {
if (time() >= $this->refreshTime) {
$this->refreshTime = (time() + $this->refreshInterval);
$queryHost = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYHOST);
$host = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SERVERHOST);
$host = ($queryHost != '') ? $queryHost : $host;
$queryPort = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYPORT);
$socket = fsockopen(@$host, $queryPort, $errno, $errstr, 2);
if ($socket) {
socket_set_timeout($socket, 2);
$is_ts3 = trim(fgets($socket)) == 'TS3';
if (!$is_ts3) {
trigger_error('[TeamSpeakPlugin] Server at "' . $host . '" is not a Teamspeak3-Server or you have setup a bad query-port!', E_USER_WARNING);
}
$queryuser = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYUSER);
$querypass = $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_QUERYPASS);
if (($queryuser != '') && !is_numeric($queryuser) && $queryuser != false && ($querypass != '') && !is_numeric($querypass) && $querypass != false) {
$ret = $this->ts3_sendCommand($socket, 'login client_login_name=' . $this->ts3_escape($queryuser) . ' client_login_password=' . $this->ts3_escape($querypass));
if (stripos($ret, "error id=0") === false) {
trigger_error("[TeamSpeakPlugin] Failed to authenticate with TS3 Server! Make sure you put the correct username & password in teamspeak.xml", E_USER_WARNING);
return;
}
}
$response = '';
$response .= $this->ts3_sendCommand($socket, 'use sid=' . $this->maniaControl->settingManager->getSettingValue($this, self::TEAMSPEAK_SID));
$this->ts3_sendCommand($socket, 'clientupdate client_nickname=' . $this->ts3_escape('ManiaControl Viewer'));
$response .= $this->ts3_sendCommand($socket, 'serverinfo');
$response .= $this->ts3_sendCommand($socket, 'channellist -topic -flags -voice -limits');
$response .= $this->ts3_sendCommand($socket, 'clientlist -uid -away -voice -groups');
fputs($socket, "quit\n");
fclose($socket);
$lines = explode("error id=0 msg=ok\n\r", $response);
if (count($lines) == 5) {
$serverdata = $this->ts3_parseLine($lines[1]);
$this->serverData['server'] = $serverdata[0];
$this->serverData['channels'] = $this->ts3_parseLine($lines[2]);
$users = $this->ts3_parseLine($lines[3]);
$this->serverData['users'] = array(); // reset userslist
foreach($users as $user) {
if ($user['client_nickname'] != 'ManiaControl Viewer') {
$this->serverData['users'][] = $user;
}
}
// Subtract reserved slots
$this->serverData['server']['virtualserver_maxclients'] -= $this->serverData['server']['virtualserver_reserved_slots'];
// Make ping value int
$this->serverData['server']['virtualserver_total_ping'] = intval($this->serverData['server']['virtualserver_total_ping']);
// Format the Date of server startup
$this->serverData['server']['virtualserver_uptime'] = date('Y-m-d H:i:s', (time() - $this->serverData['server']['virtualserver_uptime']));
// Always subtract all Query Clients
$this->serverData['server']['virtualserver_clientsonline'] -= $this->serverData['server']['virtualserver_queryclientsonline'];
}
} else {
trigger_error("[TeamSpeakPlugin] Failed to connect with TS3 server; socket error: " . $errstr . " [" . $errno . "]", E_USER_WARNING);
}
}
}
/**
* TS Function to send a command to the TeamSpeak server.
*
* @param $socket
* @param $cmd
* @return string
*/
private function ts3_sendCommand($socket, $cmd) {
fputs($socket, "$cmd\n");
$response = '';
/*while(strpos($response, 'error id=') === false) {
$response .= fread($socket, 8096);
}*/
/*while (!feof($socket)) {
$response .= fread($socket, 8192);
}*/
$info = array('timed_out' => false);
while(!feof($socket) && !$info['timed_out'] && strpos($response, 'error id=') === false) {
$response .= fread($socket, 1024);
$info = stream_get_meta_data($socket);
}
return $response;
}
/**
* TS Function used to parse lines in the serverresponse.
*
* @param $rawLine
* @return array
*/
private function ts3_parseLine($rawLine) {
$datas = array();
$rawItems = explode('|', $rawLine);
foreach($rawItems as &$rawItem) {
$rawDatas = explode(' ', $rawItem);
$tempDatas = array();
foreach($rawDatas as &$rawData) {
$ar = explode("=", $rawData, 2);
$tempDatas[$ar[0]] = isset($ar[1]) ? $this->ts3_unescape($ar[1]) : '';
}
$datas[] = $tempDatas;
}
unset($rawItem, $rawData);
return $datas;
}
/**
* TS Function used to escape characters in channelnames.
*
* @param $str
* @return mixed
*/
private function ts3_escape($str) {
return str_replace(array(chr(92), chr(47), chr(32), chr(124), chr(7), chr(8), chr(12), chr(10), chr(3), chr(9), chr(11)), array('\\\\', "\/", "\s", "\p", "\a", "\b", "\f", "\n", "\r", "\t", "\v"), $str);
}
/**
* TS Function used to unescape characters in channelnames.
*
* @param $str
* @return mixed
*/
private function ts3_unescape($str) {
return str_replace(array('\\\\', "\/", "\s", "\p", "\a", "\b", "\f", "\n", "\r", "\t", "\v"), array(chr(92), chr(47), chr(32), chr(124), chr(7), chr(8), chr(12), chr(10), chr(3), chr(9), chr(11)), $str);
}
}
}