From a7faac54258c93ccf457243306fe6dde2a76a7ff Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 30 Nov 2019 23:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9C=89=E6=97=B6=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=B8=8D=E8=83=BD=E6=AD=A3=E7=A1=AE=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/ChatRenderer/index.vue | 83 ++++++++++++------- frontend/src/views/Room.vue | 6 +- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/frontend/src/components/ChatRenderer/index.vue b/frontend/src/components/ChatRenderer/index.vue index 71fe5a2..b31544d 100644 --- a/frontend/src/components/ChatRenderer/index.vue +++ b/frontend/src/components/ChatRenderer/index.vue @@ -118,44 +118,63 @@ export default { addMessages(messages) { this.enqueueMessages(messages) }, - mergeSimilar(content) { - let remainNum = 5 - for (let arr of [this.messagesBuffer, this.messages]) { - for (let i = arr.length - 1; i >= 0 && --remainNum > 0; i--) { - let message = arr[i] - let longer, shorter - if (message.content.length > content.length) { - longer = message.content - shorter = content - } else { - longer = content - shorter = message.content - } - if (longer.indexOf(shorter) !== -1 // 长的包含短的 - && longer.length - shorter.length < shorter.length // 长度差较小 - ) { - message.repeated++ - return true - } + mergeSimilarText(content) { + let res = false + this.forEachRecentMessage(5, message => { + if (message.type !== constants.MESSAGE_TYPE_TEXT) { + return true } - } - return false + let longer, shorter + if (message.content.length > content.length) { + longer = message.content + shorter = content + } else { + longer = content + shorter = message.content + } + if (longer.indexOf(shorter) !== -1 // 长的包含短的 + && longer.length - shorter.length < shorter.length // 长度差较小 + ) { + message.repeated++ + res = true + return false + } + return true + }) + return res }, mergeSimilarGift(authorName, price) { - let remainNum = 5 - for (let arr of [this.messagesBuffer, this.messages]) { - for (let i = arr.length - 1; i >= 0 && --remainNum > 0; i--) { - let message = arr[i] - if (message.type === constants.MESSAGE_TYPE_SUPER_CHAT - && message.content === '' - && message.authorName === authorName - ) { - message.price += price - return true + let res = false + this.forEachRecentMessage(5, message => { + if (message.type === constants.MESSAGE_TYPE_SUPER_CHAT + && message.content === '' + && message.authorName === authorName + ) { + message.price += price + res = true + return false + } + return true + }) + return res + }, + forEachRecentMessage(num, callback) { + // 从新到老遍历num条消息 + for (let i = this.smoothedMessageQueue.length - 1; i >= 0 && num > 0; i--) { + let messageGroup = this.smoothedMessageQueue[i] + for (let j = messageGroup.length - 1; j >= 0 && num-- > 0; j--) { + if (!callback(messageGroup[j])) { + return + } + } + } + for (let arr of [this.messagesBuffer, this.messages]) { + for (let i = arr.length - 1; i >= 0 && num-- > 0; i--) { + if (!callback(arr[i])) { + return } } } - return false }, delMessage(id) { this.delMessages([id]) diff --git a/frontend/src/views/Room.vue b/frontend/src/views/Room.vue index 9743c4e..e881a83 100644 --- a/frontend/src/views/Room.vue +++ b/frontend/src/views/Room.vue @@ -100,7 +100,7 @@ export default { let time = data.timestamp ? new Date(data.timestamp * 1000) : new Date() switch (cmd) { case COMMAND_ADD_TEXT: - if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilar(data.content)) { + if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilarText(data.content)) { break } message = { @@ -206,11 +206,11 @@ export default { } return true }, - mergeSimilar(content) { + mergeSimilarText(content) { if (!this.config.mergeSimilarDanmaku) { return false } - return this.$refs.renderer.mergeSimilar(content) + return this.$refs.renderer.mergeSimilarText(content) }, mergeSimilarGift(authorName, price) { if (!this.config.mergeSimilarDanmaku) {