mirror of
https://github.com/wbbaddons/Tims-Chat.git
synced 2025-01-09 00:20:08 +00:00
Adding Basic command system.
To be improved!
This commit is contained in:
parent
d857575cf5
commit
a2638046eb
@ -34,6 +34,7 @@ class ChatMessage extends \wcf\data\DatabaseObject {
|
||||
const TYPE_CLEAR = 9;
|
||||
const TYPE_TEAM = 10;
|
||||
const TYPE_GLOBALMESSAGE = 11;
|
||||
const TYPE_ERROR = 12;
|
||||
|
||||
/**
|
||||
* @see \wcf\data\chat\message\ChatMessage::getFormattedMessage()
|
||||
|
22
file/lib/system/chat/commands/AbstractCommand.class.php
Normal file
22
file/lib/system/chat/commands/AbstractCommand.class.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
use \wcf\system\event\EventHandler;
|
||||
|
||||
/**
|
||||
* Default implementation for commands.
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
abstract class AbstractCommand implements ICommand {
|
||||
public $commandHandler = null;
|
||||
|
||||
public function __construct(CommandHandler $commandHandler) {
|
||||
EventHandler::getInstance()->fireAction($this, 'shouldInit');
|
||||
$this->commandHandler = $commandHandler;
|
||||
EventHandler::getInstance()->fireAction($this, 'didInit');
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
use \wcf\system\event\EventHandler;
|
||||
|
||||
/**
|
||||
* Default implementation for restricted commands
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
abstract class AbstractRestrictedCommand extends AbstractCommand implements IRestrictedCommand {
|
||||
public function __construct(CommandHandler $commandHandler) {
|
||||
parent::__construct($commandHandler);
|
||||
|
||||
$this->checkPermission();
|
||||
}
|
||||
|
||||
public function checkPermission() {
|
||||
EventHandler::getInstance()->fireAction($this, 'checkPermission');
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
use \wcf\util\StringUtil;
|
||||
|
||||
/**
|
||||
* Inserts a message.
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
class ChatCommandHandler {
|
||||
const COMMAND_CHAR = '/';
|
||||
|
||||
/**
|
||||
* Checks whether the given text is a command.
|
||||
*
|
||||
* @param string $text
|
||||
* @return boolean
|
||||
*/
|
||||
public function isCommand($text) {
|
||||
return StringUtil::substring($text, 0, StringUtil::length(static::COMMAND_CHAR)) == static::COMMAND_CHAR;
|
||||
}
|
||||
}
|
52
file/lib/system/chat/commands/CommandHandler.class.php
Normal file
52
file/lib/system/chat/commands/CommandHandler.class.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
use \wcf\util\StringUtil;
|
||||
|
||||
/**
|
||||
* Handles commands
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
class CommandHandler {
|
||||
const COMMAND_CHAR = '/';
|
||||
public $text = '';
|
||||
|
||||
/**
|
||||
* Initialises the CommandHandler
|
||||
*
|
||||
* @param string $text
|
||||
*/
|
||||
public function __construct($text) {
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given text is a command.
|
||||
*/
|
||||
public function isCommand($text = null) {
|
||||
if ($text === null) $text = $this->text;
|
||||
return StringUtil::substring($text, 0, StringUtil::length(static::COMMAND_CHAR)) == static::COMMAND_CHAR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the command.
|
||||
*/
|
||||
public function loadCommand() {
|
||||
$parts = explode(' ', StringUtil::substring($this->text, StringUtil::length(static::COMMAND_CHAR)), 2);
|
||||
|
||||
if ($this->isCommand($parts[0])) {
|
||||
return new \wcf\system\chat\commands\PlainCommand();
|
||||
}
|
||||
|
||||
$class = '\wcf\system\chat\commands\commands\\'.ucfirst($parts[0]);
|
||||
if (!class_exists($class)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new $class($this);
|
||||
}
|
||||
}
|
20
file/lib/system/chat/commands/ICommand.class.php
Normal file
20
file/lib/system/chat/commands/ICommand.class.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
|
||||
/**
|
||||
* Interface for chat-commands.
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
interface ICommand {
|
||||
const SMILEY_OFF = 0;
|
||||
const SMILEY_ON = 1;
|
||||
const SMILEY_USER = 2;
|
||||
|
||||
public function getType();
|
||||
public function getMessage();
|
||||
}
|
15
file/lib/system/chat/commands/IRestrictedCommand.class.php
Normal file
15
file/lib/system/chat/commands/IRestrictedCommand.class.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
|
||||
/**
|
||||
* Interface for Restricted commands.
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
interface IRestrictedCommand {
|
||||
protected function checkPermission();
|
||||
}
|
13
file/lib/system/chat/commands/NotFoundException.class.php
Normal file
13
file/lib/system/chat/commands/NotFoundException.class.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands;
|
||||
|
||||
/**
|
||||
* Thrown when a command is not found.
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands
|
||||
*/
|
||||
class NotFoundException extends \Exception { }
|
23
file/lib/system/chat/commands/commands/Free.class.php
Normal file
23
file/lib/system/chat/commands/commands/Free.class.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace wcf\system\chat\commands\commands;
|
||||
|
||||
/**
|
||||
* Free-Command
|
||||
*
|
||||
* @author Tim Düsterhus
|
||||
* @copyright 2010-2011 Tim Düsterhus
|
||||
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
||||
* @package timwolla.wcf.chat
|
||||
* @subpackage system.chat.commands.commands
|
||||
*/
|
||||
class Free extends \wcf\system\chat\commands\AbstractCommand {
|
||||
const ENABLE_SMILIES = \wcf\system\chat\commands\ICommand::SMILEY_USER;
|
||||
|
||||
public function getType() {
|
||||
return \wcf\data\chat\message\ChatMessage::TYPE_ME;
|
||||
}
|
||||
|
||||
public function getMessage() {
|
||||
return 'freed the fish';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user