-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiliver.ts
96 lines (78 loc) · 2.27 KB
/
biliver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import say from 'say'
import type { GiftMsg, Message, MsgHandler } from 'blive-message-listener'
import type { BasicMessage } from './types'
import { BiliverView } from './view'
import { debouceSpeak, generateBullet, generateGift } from './utils'
import type { RoomInfo } from './fetchs'
import { getRoomInfo } from './fetchs'
import type Listener from './listener'
import { basicListenerHandler } from './listener'
export interface Options {
roomId: string
isCanSay?: boolean
speakAtSameTime?: boolean
}
export class Biliver {
roomId: number
// speak the bullet
isCanSay: boolean
currBulletContent = ''
// Make a sound at the same time
speakAtSameTime = false
bullets: Map<string, BasicMessage> = new Map()
handler: MsgHandler = {}
view: BiliverView
roomInfo: RoomInfo | undefined
constructor(options: Options, private listener: Listener) {
this.roomId = Number(options.roomId)
this.isCanSay = options.isCanSay || false
this.speakAtSameTime = options.speakAtSameTime || false
this.listener = listener
this.view = new BiliverView(this.roomId)
this.initHandler()
this.getRoomInfo()
}
async getRoomInfo() {
const data = await getRoomInfo(this.roomId)
// for debugging
// fs.writeFile('test.json', JSON.stringify({ data }, null, 4), () => {
// process.exit(0)
// })
if (data)
this.view.updateRoomInfo(data)
}
start() {
this.listener.startListen(this.handler)
}
add(msg: BasicMessage) {
this.bullets.set(msg.id, msg)
this.fire(msg)
}
notice(msg: string) {
this.view.roomMsgList.addListItem(msg)
}
fire(msg: BasicMessage) {
this.view.bulletList.addListItem(this.createBulletStr(msg))
}
say(msg: BasicMessage) {
if (!this.isCanSay)
return
if (!this.speakAtSameTime)
debouceSpeak(msg.body.content)
else
say.speak(msg.body.content)
}
noticeGift(msg: Message<GiftMsg>) {
this.notice(this.createGiftStr(msg))
}
initHandler(handler?: MsgHandler) {
const basicHandler = basicListenerHandler(this)
this.handler = Object.assign(basicHandler, handler || {})
}
public createBulletStr(msg: BasicMessage): string {
return generateBullet(msg)
}
public createGiftStr(msg: Message<GiftMsg>): string {
return generateGift(msg)
}
}