TrackManiaControl/application/plugins/ChatMessagePlugin.php

452 lines
13 KiB
PHP
Raw Normal View History

2013-12-30 13:10:26 +01:00
<?php
use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-01-20 20:51:03 +01:00
use ManiaControl\Plugins\Plugin;
use Maniaplanet\DedicatedServer\Xmlrpc\Exception;
2013-12-30 13:10:26 +01:00
/**
* ManiaControl Chat-Message Plugin
*
* @author kremsy
2014-04-13 12:00:08 +02:00
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-12-30 13:10:26 +01:00
*/
class ChatMessagePlugin implements CommandListener, Plugin {
/**
* Constants
*/
2014-02-25 18:53:43 +01:00
const PLUGIN_ID = 4;
2014-01-16 22:28:30 +01:00
const PLUGIN_VERSION = 0.1;
const PLUGIN_NAME = 'ChatMessagePlugin';
const PLUGIN_AUTHOR = 'kremsy';
2013-12-30 13:10:26 +01:00
const SETTING_AFK_FORCE_SPEC = 'AFK command forces spec';
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
/**
* Private properties
*/
2013-12-31 15:08:51 +01:00
/**
* @var maniaControl $maniaControl
*/
2013-12-30 13:10:26 +01:00
private $maniaControl = null;
2014-01-27 20:39:10 +01:00
/**
* Prepares the Plugin
*
* @param ManiaControl $maniaControl
* @return mixed
*/
public static function prepare(ManiaControl $maniaControl) {
2014-01-27 21:05:02 +01:00
//do nothing
2014-01-27 20:39:10 +01:00
}
2013-12-30 13:10:26 +01:00
/**
* Load the plugin
*
* @param \ManiaControl\ManiaControl $maniaControl
* @return bool
*/
2013-12-31 15:08:51 +01:00
public function load(ManiaControl $maniaControl) {
2013-12-30 13:10:26 +01:00
$this->maniaControl = $maniaControl;
2014-01-16 22:28:30 +01:00
2013-12-30 13:21:41 +01:00
$this->maniaControl->commandManager->registerCommandListener('me', $this, 'chat_me');
2013-12-30 13:10:26 +01:00
$this->maniaControl->commandManager->registerCommandListener('hi', $this, 'chat_hi');
$this->maniaControl->commandManager->registerCommandListener('bye', $this, 'chat_bye');
$this->maniaControl->commandManager->registerCommandListener('bb', $this, 'chat_bye');
$this->maniaControl->commandManager->registerCommandListener('thx', $this, 'chat_thx');
$this->maniaControl->commandManager->registerCommandListener('gg', $this, 'chat_gg');
$this->maniaControl->commandManager->registerCommandListener('gl', $this, 'chat_gl');
$this->maniaControl->commandManager->registerCommandListener('hf', $this, 'chat_hf');
$this->maniaControl->commandManager->registerCommandListener('glhf', $this, 'chat_glhf');
$this->maniaControl->commandManager->registerCommandListener('ns', $this, 'chat_ns');
$this->maniaControl->commandManager->registerCommandListener('n1', $this, 'chat_n1');
$this->maniaControl->commandManager->registerCommandListener('lol', $this, 'chat_lol');
$this->maniaControl->commandManager->registerCommandListener('lool', $this, 'chat_lool');
$this->maniaControl->commandManager->registerCommandListener('brb', $this, 'chat_brb');
$this->maniaControl->commandManager->registerCommandListener('bgm', $this, 'chat_bgm');
$this->maniaControl->commandManager->registerCommandListener('afk', $this, 'chat_afk');
$this->maniaControl->commandManager->registerCommandListener('bootme', $this, 'chat_bootme');
$this->maniaControl->commandManager->registerCommandListener('ragequit', $this, 'chat_ragequit');
$this->maniaControl->commandManager->registerCommandListener('rq', $this, 'chat_ragequit');
2014-04-13 12:00:08 +02:00
//TODO block commandlistener for muted people
2013-12-30 13:10:26 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AFK_FORCE_SPEC, true);
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
return true;
}
/**
* Unload the plugin and its resources
*/
2013-12-31 15:08:51 +01:00
public function unload() {
$this->maniaControl->commandManager->unregisterCommandListener($this);
2013-12-30 13:10:26 +01:00
unset($this->maniaControl);
}
2013-12-30 13:21:41 +01:00
/**
* Builds a chat message starting with the player's nickname, can used to express emotions
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:21:41 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_me(array $chat, Player $player) {
2014-01-09 21:49:23 +01:00
$message = substr($chat[1][2], 4);
2014-01-16 22:28:30 +01:00
2013-12-31 15:08:51 +01:00
$msg = '$<' . $player->nickname . '$>$s$i$fa0 ' . $message;
2013-12-30 13:21:41 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
2013-12-30 13:10:26 +01:00
/**
* Hello Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_hi(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iHello $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iHello All!';
2013-12-30 13:10:26 +01:00
}
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Bye Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_bye(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iBye $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iI have to go... Bye All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Thx Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_thx(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iThanks $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iThanks All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Good Game Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_gg(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Game $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Game All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Good Luck Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_gl(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Luck $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Luck All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Have Fun Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_hf(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iHave Fun $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iHave Fun All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Good Luck and Have Fun Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_glhf(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Luck and Have Fun $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iGood Luck and Have Fun All!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Nice Shot Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_ns(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iNice Shot $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iNice Shot!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Nice one Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_n1(array $chat, Player $player) {
2013-12-30 13:10:26 +01:00
$command = explode(" ", $chat[1][2]);
2014-01-16 22:28:30 +01:00
if (isset($command[1])) {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iNice One $z$<' . $this->getTarget($command[1]) . '$>$i!';
2014-01-16 22:28:30 +01:00
} else {
2013-12-31 15:08:51 +01:00
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iNice One!';
}
2014-01-16 22:28:30 +01:00
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Lol! Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_lol(array $chat, Player $player) {
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iLoL!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* LooOOooL! Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_lool(array $chat, Player $player) {
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iLooOOooL!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Be right back Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_brb(array $chat, Player $player) {
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iBe Right Back!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Bad game for me Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_bgm(array $chat, Player $player) {
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iBad Game for me :(';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
}
/**
* Leave the server with an Bootme Message
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_bootme(array $chat, Player $player) {
2014-01-09 22:01:00 +01:00
$msg = '$i$ff0 $<' . $player->nickname . '$>$s$39f chooses to boot back to the real world!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, true);
2014-01-16 22:28:30 +01:00
$message = '$39F Thanks for Playing, see you around!$z';
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->kick($player->login, $message);
} catch(Exception $e) {
2014-03-01 11:11:50 +01:00
$this->maniaControl->errorHandler->triggerDebugNotice("ChatMessagePlugin Debug Line 316: " . $e->getMessage());
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
2014-01-20 20:51:03 +01:00
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
2013-12-30 13:10:26 +01:00
return;
}
}
/**
* Leave the server with an Ragequit
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_ragequit(array $chat, Player $player) {
2014-01-09 22:01:00 +01:00
$msg = '$i$ff0 $<' . $player->nickname . '$>$s$f00 said: "@"#!" and ragequitted!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, true);
2014-01-16 22:28:30 +01:00
2014-02-08 00:00:00 +01:00
$message = '$39F Thanks for Playing, please come back soon!$z ';
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->kick($player->login, $message);
} catch(Exception $e) {
2014-03-01 11:11:50 +01:00
$this->maniaControl->errorHandler->triggerDebugNotice("ChatMessagePlugin Debug Line " . $e->getLine() . ": " . $e->getMessage());
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
2014-01-20 20:51:03 +01:00
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
2013-12-30 13:10:26 +01:00
return;
}
}
/**
* Afk Message and force player to spec
2013-12-31 15:08:51 +01:00
*
2014-01-16 22:28:30 +01:00
* @param array $chat
2013-12-30 13:10:26 +01:00
* @param Player $player
*/
2013-12-31 15:08:51 +01:00
public function chat_afk(array $chat, Player $player) {
$msg = '$g[$<' . $player->nickname . '$>$s] $ff0$iAway From Keyboard!';
2013-12-30 13:10:26 +01:00
$this->maniaControl->chat->sendChat($msg, null, false);
2014-01-16 22:28:30 +01:00
if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_AFK_FORCE_SPEC)) {
2014-03-13 18:47:40 +01:00
if ($player->isSpectator) {
2014-03-01 19:54:44 +01:00
return;
}
2013-12-31 15:08:51 +01:00
// force into spec
2014-01-20 20:51:03 +01:00
try {
$this->maniaControl->client->forceSpectator($player->login, 3);
} catch(Exception $e) {
2014-03-01 11:11:50 +01:00
$this->maniaControl->errorHandler->triggerDebugNotice("ChatMessagePlugin Debug Line " . $e->getLine() . ": " . $e->getMessage());
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
2014-01-20 20:51:03 +01:00
$this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login);
2013-12-30 13:10:26 +01:00
return;
}
2014-01-16 22:28:30 +01:00
2013-12-31 15:08:51 +01:00
// free player slot
2014-01-16 22:28:30 +01:00
try {
$this->maniaControl->client->spectatorReleasePlayerSlot($player->login);
} catch(Exception $e) {
2014-03-13 18:47:40 +01:00
if ($e->getMessage() != 'The player is not a spectator') {
$this->maniaControl->errorHandler->triggerDebugNotice("ChatMessagePlugin Debug Line " . $e->getLine() . ": " . $e->getMessage());
// TODO: only possible valid exception should be "wrong login" - throw others (like connection error)
//to nothing
}
2014-01-16 22:28:30 +01:00
}
2013-12-30 13:10:26 +01:00
}
}
/**
2013-12-31 15:08:51 +01:00
* 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)
*
2013-12-30 13:10:26 +01:00
* @param $login
* @return mixed
*/
2013-12-31 15:08:51 +01:00
private function getTarget($login) {
2014-04-27 15:21:57 +02:00
/** @var Player $player */
$player = null;
2014-01-16 22:28:30 +01:00
foreach($this->maniaControl->playerManager->getPlayers() as $player) {
2014-04-27 15:21:57 +02:00
if ($login == $player->login || $login == $player->pid || $login == $player->nickname) {
2013-12-30 13:10:26 +01:00
return $player->nickname;
}
}
2014-01-16 22:28:30 +01:00
2014-04-27 15:21:57 +02:00
if ($player && $login == 'lj') {
2014-01-16 22:28:30 +01:00
return $player->nickname;
}
2014-04-28 18:53:50 +02:00
//returns the text given if nothing matches
return $login;
2013-12-30 13:10:26 +01:00
}
/**
* Get plugin id
*
* @return int
*/
2013-12-31 15:08:51 +01:00
public static function getId() {
2013-12-30 13:10:26 +01:00
return self::PLUGIN_ID;
}
/**
* Get Plugin Name
*
* @return string
*/
2013-12-31 15:08:51 +01:00
public static function getName() {
2013-12-30 13:10:26 +01:00
return self::PLUGIN_NAME;
}
/**
* Get Plugin Version
*
* @return float,,
*/
2013-12-31 15:08:51 +01:00
public static function getVersion() {
2013-12-30 13:10:26 +01:00
return self::PLUGIN_VERSION;
}
/**
* Get Plugin Author
*
* @return string
*/
2013-12-31 15:08:51 +01:00
public static function getAuthor() {
2013-12-30 13:10:26 +01:00
return self::PLUGIN_AUTHOR;
}
/**
* Get Plugin Description
*
* @return string
*/
2013-12-31 15:08:51 +01:00
public static function getDescription() {
2014-03-01 11:11:50 +01:00
return "Plugin offers various Chat-Commands like /gg /hi /afk /rq...";
2013-12-30 13:10:26 +01:00
}
}