1
0
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:
Tim Düsterhus 2011-12-27 13:58:28 +01:00
parent d857575cf5
commit a2638046eb
9 changed files with 170 additions and 26 deletions

View File

@ -34,6 +34,7 @@ class ChatMessage extends \wcf\data\DatabaseObject {
const TYPE_CLEAR = 9; const TYPE_CLEAR = 9;
const TYPE_TEAM = 10; const TYPE_TEAM = 10;
const TYPE_GLOBALMESSAGE = 11; const TYPE_GLOBALMESSAGE = 11;
const TYPE_ERROR = 12;
/** /**
* @see \wcf\data\chat\message\ChatMessage::getFormattedMessage() * @see \wcf\data\chat\message\ChatMessage::getFormattedMessage()

View 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');
}
}

View File

@ -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');
}
}

View File

@ -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;
}
}

View 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);
}
}

View 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();
}

View 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();
}

View 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 { }

View 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';
}
}