Skip to content

Commit

Permalink
feat: remove DANMU_MSG:4:0:2:2:2:0 judge
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Dec 22, 2022
1 parent 9da40d1 commit dc371ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 76 deletions.
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 9 additions & 19 deletions src/listener/index.ts
Expand Up @@ -2,7 +2,7 @@ import {
HEARTBEAT, type AttentionChangeMsgHandler,
LIVE, type LiveStartMsgHandler,
PREPARING, type LiveStopMsgHandler,
DANMU_MSG, DANMU_MSG_402220, type DanmuMsgHandler,
DANMU_MSG, type DanmuMsgHandler,
GUARD_BUY, type GuardBuyHandler,
INTERACT_WORD, ENTRY_EFFECT, type UserActionMsgHandler,
LIKE_INFO_V3_UPDATE, type LikedChangeMsgHandler,
Expand All @@ -13,7 +13,7 @@ import {
WATCHED_CHANGE, type WatchedChangeHandler,
} from '../parser'
import type { KeepLiveTCP, KeepLiveWS, Message as WSMessage } from 'tiny-bilibili-ws'
import { normalizeDanmu, checkIsDuplicateDanmuMsg } from '../utils/message'
import { normalizeDanmu } from '../utils/message'

export type MsgHandler = Partial<
{
Expand Down Expand Up @@ -82,7 +82,7 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
// LIVE
if (handler[LIVE.handlerName] || rawHandlerNames.has(LIVE.eventName)) {
rawHandlerNames.delete(LIVE.eventName)
instance.on(LIVE.eventName, (data: WSMessage<any>) => {
instance.on(LIVE.eventName as any, (data: WSMessage<any>) => {
isHandleRaw && rawHandler[LIVE.eventName]?.(data.data)
const parsedData = LIVE.parser(data.data)
handler[LIVE.handlerName]?.(normalizeDanmu(LIVE.eventName, parsedData, data.data))
Expand All @@ -92,30 +92,20 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
// PREPARING
if (handler[PREPARING.handlerName] || rawHandlerNames.has(PREPARING.eventName)) {
rawHandlerNames.delete(LIVE.eventName)
instance.on(PREPARING.eventName, (data: WSMessage<any>) => {
instance.on(PREPARING.eventName as any, (data: WSMessage<any>) => {
isHandleRaw && rawHandler[PREPARING.eventName]?.(data.data)
const parsedData = PREPARING.parser(data.data)
handler[PREPARING.handlerName]?.(normalizeDanmu(PREPARING.eventName, parsedData, data.data))
})
}

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

Expand Down Expand Up @@ -168,7 +158,7 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
// ROOM_CHANGE
if (handler[ROOM_CHANGE.handlerName] || rawHandlerNames.has(ROOM_CHANGE.eventName)) {
rawHandlerNames.delete(ROOM_CHANGE.eventName)
instance.on(ROOM_CHANGE.eventName, (data: WSMessage<any>) => {
instance.on(ROOM_CHANGE.eventName as any, (data: WSMessage<any>) => {
isHandleRaw && rawHandler[ROOM_CHANGE.eventName]?.(data.data)
const parsedData = ROOM_CHANGE.parser(data.data)
handler[ROOM_CHANGE.handlerName]?.(normalizeDanmu(ROOM_CHANGE.eventName, parsedData, data.data))
Expand Down Expand Up @@ -208,7 +198,7 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
// Rest raw events
rawHandlerNames.forEach((eventName) => {
console.log('rest', eventName)
instance.on(eventName, (data: WSMessage<any>) => {
instance.on(eventName as any, (data: WSMessage<any>) => {
rawHandler[eventName](data.data)
})
})
Expand Down
6 changes: 0 additions & 6 deletions src/parser/DANMU_MSG.ts
Expand Up @@ -65,12 +65,6 @@ export const DANMU_MSG = {
handlerName: 'onIncomeDanmu' as const,
}

export const DANMU_MSG_402220 = {
parser,
eventName: 'DANMU_MSG:4:0:2:2:2:0' as const,
handlerName: 'onIncomeDanmu' as const,
}

export type Handler = {
/** 收到普通弹幕消息 */
onIncomeDanmu: (msg: Message<DanmuMsg>) => void
Expand Down
2 changes: 1 addition & 1 deletion src/parser/index.ts
@@ -1,7 +1,7 @@
export { HEARTBEAT, type Handler as AttentionChangeMsgHandler, type AttentionChangeMsg } from './HEARTBEAT'
export { LIVE, type Handler as LiveStartMsgHandler, type LiveStartMsg } from './LIVE'
export { PREPARING, type Handler as LiveStopMsgHandler, type LiveEndMsg } from './PREPARING'
export { DANMU_MSG, DANMU_MSG_402220, type Handler as DanmuMsgHandler, type DanmuMsg } from './DANMU_MSG'
export { DANMU_MSG, type Handler as DanmuMsgHandler, type DanmuMsg } from './DANMU_MSG'
export { GUARD_BUY, type Handler as GuardBuyHandler, type GuardBuyMsg } from './GUARD_BUY'
export { INTERACT_WORD, ENTRY_EFFECT, type Handler as UserActionMsgHandler, type UserActionMsg } from './INTERACT_WORD_ENTRY_EFFECT'
export { LIKE_INFO_V3_UPDATE, type Handler as LikedChangeMsgHandler, type LikedChangeMsg } from './LIKE_INFO_V3_UPDATE'
Expand Down
30 changes: 0 additions & 30 deletions src/utils/message.ts
@@ -1,24 +1,4 @@
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)
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
})
}
}

const msgQueue = new MsgQueue()

export const normalizeDanmu = <T>(msgType: string, body: T, rawBody: any): Message<T> => {
const timestamp = Date.now()
Expand All @@ -33,13 +13,3 @@ export const normalizeDanmu = <T>(msgType: string, body: T, rawBody: any): Messa
raw: rawBody,
}
}

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 dc371ca

Please sign in to comment.