Skip to content

Commit

Permalink
feat: check duplicate msg (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Dec 1, 2022
1 parent 679cc9d commit 60f7752
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/listener/index.ts
Expand Up @@ -12,8 +12,8 @@ import {
SUPER_CHAT_MESSAGE, type SuperChatHandler,
WATCHED_CHANGE, type WatchedChangeHandler,
} from '../parser'
import type { Message } from '../types/app'
import type { KeepLiveTCP, KeepLiveWS, Message as WSMessage } from 'tiny-bilibili-ws'
import { normalizeDanmu, checkIsDuplicateDanmuMsg } from '../utils/message'

export type MsgHandler = Partial<
{
Expand All @@ -40,19 +40,6 @@ export type MsgHandler = Partial<
& WatchedChangeHandler
>

const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
const timestamp = Date.now()
const randomText = Math.floor(Math.random() * 10000).toString()
// @ts-ignore
const id = `${timestamp}:${msgType}:${body.user?.uid}:${randomText}`
return {
id,
timestamp,
type: msgType,
body,
}
}

export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, handler?: MsgHandler) => {
if (!handler) return

Expand Down Expand Up @@ -99,14 +86,14 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha

// DANMU_MSG
if (handler[DANMU_MSG.handlerName] || handler[DANMU_MSG_402220.handlerName]) {
instance.on(DANMU_MSG.eventName, (data: WSMessage<any>) => {
const msgCallback = handler[DANMU_MSG.handlerName]!
const handleDanmuMsg = (data: WSMessage<any>) => {
const parsedData = DANMU_MSG.parser(data.data, roomId)
handler[DANMU_MSG.handlerName]?.(normalizeDanmu(DANMU_MSG.eventName, parsedData))
})
instance.on(DANMU_MSG_402220.eventName, (data: WSMessage<any>) => {
const parsedData = DANMU_MSG_402220.parser(data.data, roomId)
handler[DANMU_MSG_402220.handlerName]?.(normalizeDanmu(DANMU_MSG_402220.eventName, parsedData))
})
if (checkIsDuplicateDanmuMsg(parsedData)) return
msgCallback(normalizeDanmu(DANMU_MSG.eventName, parsedData))
}
instance.on(DANMU_MSG.eventName, handleDanmuMsg)
instance.on(DANMU_MSG_402220.eventName, handleDanmuMsg)
}

// GUARD_BUY
Expand Down
46 changes: 46 additions & 0 deletions src/utils/message.ts
@@ -0,0 +1,46 @@
import type { Message } from '../types/app'
import type { DanmuMsg } from '../parser'

type QueueItem = [number, string]
class MsgQueue {
private items: QueueItem[] = []
push = (item: QueueItem) => {
this.items.push(item)
this.debug()
if (this.items.length > 10) {
this.items.shift()
}
}
has = ( [timestamp, content]: QueueItem ) => {
return this.items.some(i => {
const [queueTimestamp, queueContent] = i
return Math.abs(timestamp - queueTimestamp) < 1200 && content === queueContent
})
}
debug = () => console.log(this.items)
}

const msgQueue = new MsgQueue()

export const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
const timestamp = Date.now()
const randomText = Math.floor(Math.random() * 10000).toString()
// @ts-ignore
const id = `${timestamp}:${msgType}:${body.user?.uid}:${randomText}`
return {
id,
timestamp,
type: msgType,
body,
}
}

export const checkIsDuplicateDanmuMsg = (msg: DanmuMsg) => {
const msgIdentifier = `${msg.user.uid}:${msg.content}`
const queueItem: QueueItem = [msg.timestamp, msgIdentifier]
if (msgQueue.has(queueItem)) {
return true
}
msgQueue.push(queueItem)
return false
}

0 comments on commit 60f7752

Please sign in to comment.