1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00

Improve time intervals for commands

This commit is contained in:
Tim Düsterhus 2013-05-23 22:09:47 +02:00
parent 1cf7ad6f74
commit f570e9d1a6
8 changed files with 47 additions and 83 deletions

View File

@ -40,7 +40,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
parent::__construct($commandHandler); parent::__construct($commandHandler);
try { try {
list($color[1], $color[2]) = explode(' ', $commandHandler->getParameters()); list($color[1], $color[2]) = explode(' ', $commandHandler->getParameters(), 2);
} }
catch (\wcf\system\exception\SystemException $e) { catch (\wcf\system\exception\SystemException $e) {
$color[1] = $color[2] = $commandHandler->getParameters(); $color[1] = $color[2] = $commandHandler->getParameters();

View File

@ -24,12 +24,13 @@ class MuteCommand extends \chat\system\command\AbstractRestrictedCommand {
public function __construct(\chat\system\command\CommandHandler $commandHandler) { public function __construct(\chat\system\command\CommandHandler $commandHandler) {
parent::__construct($commandHandler); parent::__construct($commandHandler);
$parameters = $commandHandler->getParameters(); try {
if (($comma = strpos($parameters, ',')) !== false) { list($username, $modifier) = explode(',', $commandHandler->getParameters(), 2);
$username = substr($parameters, 0, $comma); $modifier = ChatUtil::timeModifier(\wcf\util\StringUtil::trim($modifier));
$this->time = ChatUtil::timeModifier(substr($parameters, $comma + 1)); $this->time = strtotime($modifier, TIME_NOW);
$this->time = min(max(-0x80000000, $this->time), 0x7FFFFFFF);
} }
else { catch (\wcf\system\exception\SystemException $e) {
throw new \chat\system\command\NotFoundException(); throw new \chat\system\command\NotFoundException();
} }
@ -48,7 +49,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
public function executeAction() { public function executeAction() {
if ($suspension = suspension\Suspension::getSuspensionByUserRoomAndType($this->user, $this->room, suspension\Suspension::TYPE_MUTE)) { if ($suspension = suspension\Suspension::getSuspensionByUserRoomAndType($this->user, $this->room, suspension\Suspension::TYPE_MUTE)) {
if ($suspension->time > TIME_NOW + $this->time) { if ($suspension->time > $this->time) {
throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.exists')); throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.exists'));
} }
@ -61,7 +62,7 @@ public function executeAction() {
'userID' => $this->user->userID, 'userID' => $this->user->userID,
'roomID' => ChatUtil::readUserData('roomID'), 'roomID' => ChatUtil::readUserData('roomID'),
'type' => suspension\Suspension::TYPE_MUTE, 'type' => suspension\Suspension::TYPE_MUTE,
'time' => TIME_NOW + $this->time 'time' => $this->time
) )
)); ));
$this->suspensionAction->executeAction(); $this->suspensionAction->executeAction();
@ -91,7 +92,7 @@ public function getType() {
public function getMessage() { public function getMessage() {
return serialize(array( return serialize(array(
'link' => $this->link, 'link' => $this->link,
'until' => TIME_NOW + $this->time, 'until' => $this->time,
'type' => str_replace(array('chat\system\command\commands\\', 'command'), '', strtolower(get_class($this))) 'type' => str_replace(array('chat\system\command\commands\\', 'command'), '', strtolower(get_class($this)))
)); ));
} }

View File

@ -43,7 +43,7 @@ public function executeAction() {
$action->executeAction(); $action->executeAction();
} }
else { else {
throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.notExists')); throw new \wcf\system\exception\UserInputException('text', WCF::getLanguage()->get('wcf.chat.suspension.notExists'));
} }
} }

View File

@ -24,7 +24,9 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler)
$username = substr($parameters, 0, $comma); $username = substr($parameters, 0, $comma);
$this->message = substr($parameters, $comma + 1); $this->message = substr($parameters, $comma + 1);
} }
else throw new \chat\system\command\NotFoundException(); else {
throw new \chat\system\command\NotFoundException();
}
$this->user = User::getUserByUsername($username); $this->user = User::getUserByUsername($username);
if (!$this->user->userID) throw new \chat\system\command\UserNotFoundException($username); if (!$this->user->userID) throw new \chat\system\command\UserNotFoundException($username);

View File

@ -1,41 +0,0 @@
<?php
namespace chat\system\option;
/**
* TimeIntervalOptionType is an implementation of IOptionType for time intervals.
*
* @author Tim Düsterhus
* @copyright 2010-2013 Tim Düsterhus
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
* @package be.bastelstu.chat
* @subpackage system.option
*/
class TimeIntervalOptionType extends \wcf\system\option\TextOptionType {
/**
* @see \wcf\system\option\IOptionType::getData()
*/
public function getData(\wcf\data\option\Option $option, $newValue) {
return \chat\util\ChatUtil::timeModifier($newValue);
}
/**
* @see \wcf\system\option\TextOptionType::getFormElement()
*/
public function getFormElement(\wcf\data\option\Option $option, $value) {
$tmp = '';
if ($value > 86400) {
$tmp = floor($value / 86400).'d';
$value -= floor($value / 86400) * 86400;
}
if ($value > 3600) {
$tmp .= floor($value / 3600).'h';
$value -= floor($value / 3600) * 3600;
}
$tmp .= floor($value / 60);
if ($value % 60 != 0) {
$tmp .= ','.($value % 60).'s';
}
return parent::getFormElement($option, $tmp);
}
}

View File

@ -127,6 +127,7 @@ public static function readUserData($field, \wcf\data\user\User $user = null) {
/** /**
* Writes user data * Writes user data
*
* @param array $data * @param array $data
* @param \wcf\data\user\User $user * @param \wcf\data\user\User $user
*/ */
@ -166,50 +167,45 @@ public static function str_split($string, $length = 1) {
* @return integer * @return integer
*/ */
public static function timeModifier($time) { public static function timeModifier($time) {
$regex = new \wcf\system\Regex('([0-9]+[s|h|d|w|m|y]?)', \wcf\system\Regex::CASE_INSENSITIVE); $regex = new \wcf\system\Regex('([0-9]+)([shdwmy]?)', \wcf\system\Regex::CASE_INSENSITIVE);
if (!$regex->match($time, true)) return 0; if (!$regex->match($time, true, \wcf\system\Regex::ORDER_MATCH_BY_SET)) return 0;
$matches = $regex->getMatches(); $matches = $regex->getMatches();
$result = 0; $result = '';
foreach ($matches[1] as $time) { foreach ($matches as $match) {
// 60 seconds a minute list(, $time, $modifier) = $match;
$multiplier = 60;
$modifier = substr($time, -1);
switch ($modifier) { switch ($modifier) {
case 'y': case 'y':
case 'Y': case 'Y':
// twelve months a year $result .= '+'.$time.'year';
$multiplier *= 12; break;
case 'm': case 'm':
case 'M': case 'M':
// about 4.3 weeks per month $result .= '+'.$time.'month';
$multiplier *= 4.34812141; break;
case 'w': case 'w':
case 'W': case 'W':
// seven days a weeks $result .= '+'.$time.'week';
$multiplier *= 7; break;
case 'd': case 'd':
case 'D': case 'D':
// 24 hours a day $result .= '+'.$time.'day';
$multiplier *= 24; break;
case 'h': case 'h':
case 'H': case 'H':
// 60 minutes an hour $result .= '+'.$time.'hour';
$multiplier *= 60;
$time = substr($time, 0, -1);
break; break;
case 's': case 's':
case 'S': case 'S':
// 60 seconds per minute $result .= '+'.$time.'second';
$multiplier /= 60; break;
$time = substr($time, 0, -1); default:
$result .= '+'.$time.'minute';
} }
$result += $time * $multiplier;
} }
return (int) round($result, 0); return $result;
} }
/** /**

View File

@ -37,18 +37,17 @@
<item name="wcf.acp.option.category.chat.general"><![CDATA[Allgemein]]></item> <item name="wcf.acp.option.category.chat.general"><![CDATA[Allgemein]]></item>
<item name="wcf.acp.option.chat_active"><![CDATA[Chat aktivieren]]></item> <item name="wcf.acp.option.chat_active"><![CDATA[Chat aktivieren]]></item>
<item name="wcf.acp.option.chat_reloadtime"><![CDATA[Nachladezeit]]></item> <item name="wcf.acp.option.chat_reloadtime"><![CDATA[Nachladezeit]]></item>
<item name="wcf.acp.option.chat_reloadtime.description"><![CDATA[Gibt die Zeit in Sekunden an, nach der neue Nachrichten geladen werden. (Standard: alle 5 Sekunden)]]></item> <item name="wcf.acp.option.chat_reloadtime.description"><![CDATA[Wartezeit zwischen zwei Anfragen zum Server. [Zeit in Sekunden]]]></item>
<item name="wcf.acp.option.chat_lastmessages"><![CDATA[Anzahl der letzten Nachrichten]]></item> <item name="wcf.acp.option.chat_lastmessages"><![CDATA[Anzahl der letzten Nachrichten]]></item>
<item name="wcf.acp.option.chat_lastmessages.description"><![CDATA[Gibt die Zahl der Nachrichten an, die beim Betreten des Chats angezeigt werden. (Standard: 5)]]></item> <item name="wcf.acp.option.chat_lastmessages.description"><![CDATA[Anzahl der Nachrichten, die beim Betreten des Chats angezeigt werden.]]></item>
<item name="wcf.acp.option.chat_display_join_leave"><![CDATA[Join- und Leave-Nachrichten anzeigen]]></item> <item name="wcf.acp.option.chat_display_join_leave"><![CDATA[Join- und Leave-Nachrichten anzeigen]]></item>
<item name="wcf.acp.option.chat_display_join_leave.description"><![CDATA[Gibt an, ob eine Nachricht angezeigt werden soll, wenn ein Nutzer den Chat betritt oder verlässt.]]></item> <item name="wcf.acp.option.chat_display_join_leave.description"><![CDATA[Soll eine Nachricht angezeigt werden, wenn ein Nutzer den Chat betritt oder verlässt?]]></item>
<item name="wcf.acp.option.chat_max_length"><![CDATA[Maximale Textlänge]]></item> <item name="wcf.acp.option.chat_max_length"><![CDATA[Maximale Textlänge]]></item>
<item name="wcf.acp.option.chat_max_length.description"><![CDATA[Gibt die maximale Länge einer Chatnachricht an.]]></item>
<item name="wcf.acp.option.category.chat.log"><![CDATA[Protokoll]]></item> <item name="wcf.acp.option.category.chat.log"><![CDATA[Protokoll]]></item>
<item name="wcf.acp.option.chat_log_active"><![CDATA[Protokoll aktivieren]]></item> <item name="wcf.acp.option.chat_log_active"><![CDATA[Protokoll aktivieren]]></item>
<item name="wcf.acp.option.chat_log_archivetime"><![CDATA[Speicherzeit]]></item> <item name="wcf.acp.option.chat_log_archivetime"><![CDATA[Speicherzeit]]></item>
<item name="wcf.acp.option.chat_log_archivetime.description"><![CDATA[Gibt die Zeit in Minuten an, die eine Nachricht im Protokoll gespeichert bleibt.]]></item> <item name="wcf.acp.option.chat_log_archivetime.description"><![CDATA[Zeit, die eine Nachricht im Protokoll abrufbar bleibt. [Zeit in Minuten, -1 für unendlich]]]></item>
</category> </category>
<category name="chat.error"> <category name="chat.error">

View File

@ -23,12 +23,16 @@
<categoryname>chat.general</categoryname> <categoryname>chat.general</categoryname>
<optiontype>integer</optiontype> <optiontype>integer</optiontype>
<defaultvalue>5</defaultvalue> <defaultvalue>5</defaultvalue>
<minvalue>1</minvalue>
<maxvalue>15</maxvalue>
<showorder>2</showorder> <showorder>2</showorder>
</option> </option>
<option name="chat_lastmessages"> <option name="chat_lastmessages">
<categoryname>chat.general</categoryname> <categoryname>chat.general</categoryname>
<optiontype>integer</optiontype> <optiontype>integer</optiontype>
<defaultvalue>5</defaultvalue> <defaultvalue>5</defaultvalue>
<minvalue>0</minvalue>
<maxvalue>50</maxvalue>
<showorder>4</showorder> <showorder>4</showorder>
</option> </option>
<option name="chat_display_join_leave"> <option name="chat_display_join_leave">
@ -41,6 +45,8 @@
<categoryname>chat.general</categoryname> <categoryname>chat.general</categoryname>
<optiontype>integer</optiontype> <optiontype>integer</optiontype>
<defaultvalue>500</defaultvalue> <defaultvalue>500</defaultvalue>
<minvalue>1</minvalue>
<maxvalue>5000</maxvalue>
<showorder>8</showorder> <showorder>8</showorder>
</option> </option>
<option name="chat_show_version"> <option name="chat_show_version">
@ -60,8 +66,9 @@
</option> </option>
<option name="chat_log_archivetime"> <option name="chat_log_archivetime">
<categoryname>chat.log</categoryname> <categoryname>chat.log</categoryname>
<optiontype>timeInterval</optiontype> <optiontype>integer</optiontype>
<defaultvalue>172800</defaultvalue> <defaultvalue>10080</defaultvalue>
<minvalue>-1</minvalue>
<showorder>2</showorder> <showorder>2</showorder>
</option> </option>
<!--log options end--> <!--log options end-->