Skip to content

Commit

Permalink
添加三个自定义过滤器说明
Browse files Browse the repository at this point in the history
  • Loading branch information
iwxyi committed Jun 6, 2021
1 parent 8480071 commit c3ebed9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 19 deletions.
70 changes: 54 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,21 +772,24 @@ border-image: url(C:/Path/To/Image.png)

按指定格式,获取动态的数值,格式:`%>func(args)%`

| 函数 | 中文 | 描述 |
| -------------------------- | ------------ | -------------------------------- |
| time(format) | 格式化时间 | 当前时间转换为数值 |
| unameToUid(uname) | 查找用户名 | 由部分昵称倒找弹幕记录,获得UID |
| inputText(title, default) | 输入文本 | 输入文本,两个参数都可省略 |
| strlen(text) | 取文本长度 | 一串文字的长度 |
| trim(text) | 删首尾空 | 去掉字符串首尾的空格和制表符 |
| substr(text, left, length) | 取子串 | 获取文字的一部分 |
| simpleName(name) | 昵称简化 | |
| simpleNum(number) | 数值简化 | |
| inGameUsers(listId, uid) | 在游戏用户中 | listId可省略。程序重启数据会清空 |
| inGameNumbers(listId, num) | 在游戏数值中 | listId可省略,程序重启数据仍在 |
| inGameTexts(listId, text) | 在游戏文本中 | listId可省略,程序重启数据仍在 |
| getValue(key, def) | 取变量值 | 等同于`%{key}%`,默认值def可省略 |
| random(min, max) | 取随机数 | 包含两端数字 |
| 函数 | 中文 | 描述 |
| ------------------------------ | ------------ | --------------------------------------------------- |
| time(format) | 格式化时间 | 当前时间转换为数值 |
| unameToUid(uname) | 查找用户名 | 由部分昵称倒找弹幕记录,获得UID |
| inputText(title, default) | 输入文本 | 输入文本,两个参数都可省略 |
| strlen(text) | 取文本长度 | 一串文字的长度 |
| trim(text) | 删首尾空 | 去掉字符串首尾的空格和制表符 |
| substr(text, left, length) | 取子串 | 获取文字的一部分 |
| simpleName(name) | 昵称简化 | |
| simpleNum(number) | 数值简化 | |
| inGameUsers(listId, uid) | 在游戏用户中 | listId可省略。程序重启数据会清空 |
| inGameNumbers(listId, num) | 在游戏数值中 | listId可省略,程序重启数据仍在 |
| inGameTexts(listId, text) | 在游戏文本中 | listId可省略,程序重启数据仍在 |
| getValue(key, def) | 取变量值 | 等同于`%{key}%`,默认值def可省略 |
| random(min, max) | 取随机数 | 包含两端数字 |
| filterReject(filter) | 过滤器拒绝 | 被对应filter拒绝则返回1,否则返回0(参考过滤器示例) |
| inFilterList(filter, content) | 在过滤列表中 | 包含在空格分隔的词库中则返回1(参考过滤器示例) |
| inFilterMatch(filter, content) | 在过滤正则中 | 满足正则则返回1(参考过滤器示例) |

已获取时间为例:

Expand Down Expand Up @@ -1821,10 +1824,11 @@ tips:
| FILTER_DANMAKU_COME | 阻止进入消息显示在弹幕姬上 |
| FILTER_DANMAKU_GIFT | 阻止礼物/上船消息显示在弹幕姬上 |
| FILTER_DANMAKU_ATTENTION | 阻止关注消息显示在弹幕姬上 |
| | |

若有多个相同过滤器,只要任意其中一个包括 `>reject()` 命令,则本操作不允许。

注意:过滤器可能会在一定程度上造成卡顿,可在弹幕设置中关闭。
注意:默认的过滤器可能会在一定程度上造成卡顿,可在弹幕设置中关闭。



Expand Down Expand Up @@ -1866,6 +1870,40 @@ tips:



##### 示例:自定义过滤器

相关函数:`filterReject(filter)`

在事件中,事件为自定义的**过滤器名字**,即参数中的`filter`。相应动作带有`>reject()`则表示**拒绝**,返回1;若不拒绝,则**默认通过**,返回0。

除了 `>reject()`,过滤器中的其余操作都会如普通弹幕命令一样**正常执行**

![](README.assets/filterReject.png)



##### 示例:自定义词库过滤

相关函数:`inFilterList(filter, content)`

返回的弹幕改为词库,多个词语之间用空格或者换行分隔。

只要词库中其中一个词语在 `content` 中,则返回 1。否则返回 0。

![](pictures/inFilterList.png)



##### 示例:自定义正则过滤

相关函数:`inFilterMatch(filter, content)`

返回的弹幕改为正则表达式,多个表达式用多行表示。

只要 `content` 满足其中一个表达式,则返回 1。否则返回 0。

![](pictures/inFilterMatch.png)



#### 警告事件
Expand Down
33 changes: 31 additions & 2 deletions mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4697,7 +4697,7 @@ QString MainWindow::processDanmakuVariants(QString msg, const LiveDanmaku& danma
matchPos = 0;
while ((matchPos = msg.indexOf(re, matchPos, &match)) > -1)
{
QString rpls = replaceDynamicVariants(match.captured(1), match.captured(2));
QString rpls = replaceDynamicVariants(match.captured(1), match.captured(2), danmaku);
msg.replace(match.captured(0), rpls);
matchPos += rpls.length();
find = true;
Expand Down Expand Up @@ -5247,7 +5247,7 @@ QString MainWindow::replaceDanmakuExtras(const QJsonObject &json, const QString&
/**
* 函数替换
*/
QString MainWindow::replaceDynamicVariants(const QString &funcName, const QString &args)
QString MainWindow::replaceDynamicVariants(const QString &funcName, const QString &args, const LiveDanmaku& danmaku)
{
QRegularExpressionMatch match;
QStringList argList = args.split(QRegExp("\\s*,\\s*"));
Expand Down Expand Up @@ -5411,6 +5411,13 @@ QString MainWindow::replaceDynamicVariants(const QString &funcName, const QStrin
}
return snum(qrand() % (max-min+1) + min);
}
else if (funcName == "filterReject")
{
if (argList.size() < 1 || argList.first().trimmed().isEmpty())
return errorArg("最小值,最大值");
QString filterName = args;
return isFilterRejected(filterName, danmaku) ? "1" : "0";
}
else if (funcName == "inFilterList") // 匹配过滤器的内容,空白符分隔
{
if (argList.size() < 2)
Expand Down Expand Up @@ -5439,7 +5446,29 @@ QString MainWindow::replaceDynamicVariants(const QString &funcName, const QStrin
}
else if (funcName == "inFilterMatch") // 匹配过滤器的正则
{
if (argList.size() < 2)
return errorArg("过滤器名, 全部内容");
int index = args.indexOf(",");
if (index == -1) // 不应该没找到的
return "";
QString filterName = args.left(index);
QString content = args.right(args.length() - index - 1).trimmed();

for (int row = 0; row < ui->eventListWidget->count(); row++)
{
auto widget = ui->eventListWidget->itemWidget(ui->eventListWidget->item(row));
auto tw = static_cast<EventWidget*>(widget);
if (tw->eventEdit->text() != filterName || !tw->check->isChecked())
continue;

QStringList sl = tw->body().split("\n", QString::SkipEmptyParts);
foreach (QString s, sl)
{
if (content.indexOf(QRegularExpression(s)) > -1)
return "1";
}
}
return "0";
}
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ private slots:
QString processDanmakuVariants(QString msg, const LiveDanmaku &danmaku);
QString replaceDanmakuVariants(const LiveDanmaku &danmaku, const QString& key) const;
QString replaceDanmakuExtras(const QJsonObject& json, const QString &key_seq) const;
QString replaceDynamicVariants(const QString& funcName, const QString& args);
QString replaceDynamicVariants(const QString& funcName, const QString& args, const LiveDanmaku &danmaku);
QString processMsgHeaderConditions(QString msg) const;
bool processVariantConditions(QString exprs) const;
qint64 calcIntExpression(QString exp) const;
Expand Down
Binary file added pictures/filterReject.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/inFilterList.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/inFilterMatch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions resources/documents/translation_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@
昵称简化=simpleName
数值简化=simpleNum
取随机数=random
过滤器拒绝=filterReject
在过滤列表中=inFilterList
在过滤正则中=inFilterMatch

0 comments on commit c3ebed9

Please sign in to comment.