PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/libraries/ManiaLivePlugins/Standard/TeamSpeak/Connection.php

http://manialive.googlecode.com/
PHP | 246 lines | 200 code | 24 blank | 22 comment | 20 complexity | c9b3ae648d1457ad81faf254b32ebfe0 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * TeamSpeak Plugin - Connect to a TeamSpeak 3 server
  4. * Original work by refreshfr
  5. *
  6. * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
  7. * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
  8. * @version $Revision: 317 $:
  9. * @author $Author: martin.gwendal $:
  10. * @date $Date: 2012-01-03 14:21:24 +0100 (Tue, 03 Jan 2012) $:
  11. */
  12. namespace ManiaLivePlugins\Standard\TeamSpeak;
  13. use ManiaLive\Cache\Cache;
  14. use ManiaLive\Event\Dispatcher;
  15. use ManiaLive\Application\Listener as AppListener;
  16. use ManiaLive\Application\Event as AppEvent;
  17. use ManiaLive\Features\Tick\Listener as TickListener;
  18. use ManiaLive\Features\Tick\Event as TickEvent;
  19. use ManiaLive\Threading\ThreadHandler;
  20. use ManiaLivePlugins\Standard\TeamSpeak\Structures\Client;
  21. use ManiaLivePlugins\Standard\TeamSpeak\Structures\Channel;
  22. use ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3;
  23. /**
  24. * Description of Connection
  25. */
  26. class Connection extends \ManiaLib\Utils\Singleton implements AppListener, TickListener
  27. {
  28. static public $languages = array
  29. (
  30. 'ar' => 'Arabic',
  31. 'cz' => '?eština',
  32. 'da' => 'Dansk',
  33. 'de' => 'Deutsch',
  34. 'el' => '????????',
  35. 'en' => 'English',
  36. 'es' => 'Espańol',
  37. 'et' => 'Eesti',
  38. 'fi' => 'Suomi',
  39. 'fr' => 'Français',
  40. 'hu' => 'Magyar',
  41. 'it' => 'Italiano',
  42. 'jp' => '???',
  43. 'kr' => '???',
  44. 'lv' => 'Latviešu',
  45. 'nb' => 'Norsk',
  46. 'nl' => 'Nederlands',
  47. 'pl' => 'Polski',
  48. 'pt' => 'Portuguęs',
  49. 'ro' => 'Român?',
  50. 'ru' => '???????',
  51. 'sk' => 'Sloven?ina',
  52. 'sv' => 'Svenska',
  53. 'tr' => 'Türkçe',
  54. 'zh' => '??'
  55. );
  56. private $server;
  57. private $processHandler;
  58. private $processId;
  59. private $tick = 0;
  60. function open()
  61. {
  62. $config = Config::getInstance();
  63. try
  64. {
  65. $this->server = TeamSpeak3::factory('serverquery://'.$config->queryLogin.':'.$config->queryPassword.'@'.$config->ipAddress.':'.$config->queryPort.'/?server_port='.$config->voicePort.'&blocking=0#no_query_clients');
  66. }
  67. catch(\Exception $e)
  68. {
  69. $this->server = null;
  70. return;
  71. }
  72. // Threading is really useful for this plugin !!!
  73. $this->processHandler = ThreadHandler::getInstance();
  74. $this->processId = $this->processHandler->launchThread();
  75. // Enable events (from ML and from TS)
  76. Dispatcher::register(AppEvent::getClass(), $this, AppEvent::ON_PRE_LOOP);
  77. Dispatcher::register(TickEvent::getClass(), $this);
  78. $this->server->notifyRegister('channel');
  79. // Populate
  80. foreach($this->server->channelList() as $channel)
  81. Channel::CreateFromTeamSpeak($channel->getInfo());
  82. foreach($this->server->clientList() as $client)
  83. Client::CreateFromTeamSpeak($client->getInfo());
  84. // Handle default channels
  85. if($config->useDedicatedChannel)
  86. {
  87. if( !($defaultChannel = Channel::GetDefault()) )
  88. $this->processHandler->addTask(
  89. $this->processId,
  90. new Tasks\ChannelCreate($config, $config->dedicatedChannelName)
  91. );
  92. else if($config->useLangChannels)
  93. $this->createLangChannels($defaultChannel);
  94. }
  95. }
  96. function isConnected()
  97. {
  98. return $this->server != null;
  99. }
  100. function createLangChannels($parentChannel)
  101. {
  102. $wantedChannels = array_values(self::$languages);
  103. $existingChannels = array_map(function ($subChannel) { return $subChannel->name; }, $parentChannel->subChannels);
  104. foreach(array_diff($wantedChannels, $existingChannels) as $channelName)
  105. $this->processHandler->addTask(
  106. $this->processId,
  107. new Tasks\ChannelCreate(Config::getInstance(), $channelName, $parentChannel->channelId)
  108. );
  109. }
  110. function movePlayer($login, $channelId)
  111. {
  112. if( ($client = Client::GetByLogin($login)) )
  113. $this->processHandler->addTask(
  114. $this->processId,
  115. new Tasks\ClientMove(Config::getInstance(), $client->clientId, $channelId)
  116. );
  117. }
  118. function toggleGlobalComment($login, $enable)
  119. {
  120. $config = Config::getInstance();
  121. $defaultId = Channel::GetDefault() ? Channel::GetDefault()->channelId : -1;
  122. foreach(Channel::GetAll() as $channel)
  123. if($channel->hasToBeListed)
  124. $this->processHandler->addTask(
  125. $this->processId,
  126. new Tasks\ChannelToggleComment(Config::getInstance(), $channel->channelId, $enable)
  127. );
  128. }
  129. function toggleChannelComment($login, $channelId, $enable)
  130. {
  131. if( ($channel = Channel::Get($channelId)) )
  132. $this->processHandler->addTask(
  133. $this->processId,
  134. new Tasks\ChannelToggleComment(Config::getInstance(), $channelId, $enable)
  135. );
  136. }
  137. function toggleClientComment($login, $clientId, $enable)
  138. {
  139. if( ($client = Client::Get($clientId)) )
  140. {
  141. $client->isCommentator = $enable;
  142. $client->notifyObservers();
  143. if( ($channel = Channel::Get($client->channelId)) && $channel->commentatorEnabled)
  144. $this->processHandler->addTask(
  145. $this->processId,
  146. new Tasks\ClientToggleComment(Config::getInstance(), $clientId, $enable)
  147. );
  148. }
  149. }
  150. function close($isDisconnected=false)
  151. {
  152. if($this->server)
  153. {
  154. if(!$isDisconnected)
  155. $this->server->notifyUnregister();
  156. $this->server->clientListReset();
  157. $this->server->channelListReset();
  158. $this->server = null;
  159. }
  160. if($this->processHandler && $this->processId)
  161. {
  162. $this->processHandler->killThread($this->processId);
  163. $this->processId = null;
  164. }
  165. Dispatcher::unregister(AppEvent::getClass(), $this);
  166. Dispatcher::unregister(TickEvent::getClass(), $this);
  167. // TODO something to avoid memory leaks when unloading the plugin
  168. // but the TS framework is f*cked up (plenty of circular references
  169. // which can't be unset from outside...)
  170. }
  171. // Events
  172. function onClientEdited($command)
  173. {
  174. print_r($command->result);
  175. list($clientId, $isCommentator) = $command->result;
  176. if( ($client = Client::Get($clientId)) )
  177. {
  178. $client->isCommentator = (bool) $isCommentator;
  179. $client->notifyObservers();
  180. }
  181. }
  182. function onTick()
  183. {
  184. if(++$this->tick % 5 == 0)
  185. {
  186. try
  187. {
  188. foreach(Client::GetAll() as $client)
  189. $client->update($this->server->request('clientinfo clid='.$client->clientId)->toList());
  190. if($this->tick % 60 == 0)
  191. {
  192. $this->server->request('whoami');
  193. $this->processHandler->addTask(
  194. $this->processId,
  195. new Tasks\ConnectionKeepAlive(Config::getInstance())
  196. );
  197. }
  198. }
  199. catch(\Exception $e)
  200. {
  201. $this->close(true);
  202. }
  203. }
  204. }
  205. function onPreLoop()
  206. {
  207. // Receiving TeamSpeak events
  208. try
  209. {
  210. $this->server->getAdapter()->readNotifications();
  211. }
  212. catch(\Exception $e)
  213. {
  214. $this->close(true);
  215. }
  216. }
  217. function onInit() {}
  218. function onRun() {}
  219. function onPostLoop() {}
  220. function onTerminate() {}
  221. }
  222. ?>