Skip to content

Commit

Permalink
feat: 主播天选时刻抽奖消息
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Jan 26, 2023
1 parent 8c26a01 commit 734bf40
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 0 deletions.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ export interface SuperChatMsg {
| onGuardBuy | 舰长上舰消息 |
| onRedPocketStart | 红包抽奖开始 |
| onRedPocketEnd | 红包抽奖结果 |
| onAnchorLotteryStart | 主播天选时刻抽奖开启 |
| onAnchorLotteryEnd | 主播天选时刻抽奖结果 |

<details>
<summary>Type Definitions</summary>
Expand Down Expand Up @@ -617,6 +619,106 @@ interface RedPocketEndAward {
award_price: number
}
```

##### handler.onAnchorLotteryStart

主播天选时刻抽奖开启

```ts
export type Handler = {
/** 主播天选时刻抽奖开启 */
onAnchorLotteryStart: (msg: Message<AnchorLotteryStartMsg>) => void
}

type msgType = 'ANCHOR_LOT_START'

export interface AnchorLotteryStartMsg {
/** 天选抽奖id */
id: number
/** 开始时间,秒级时间戳 */
start_time: number
/** 持续时间,秒 */
duration: number
/** 天选奖品信息 */
award: {
/** 奖品图片 */
image: string
/** 奖品名称 */
name: string
/** 奖品数量 */
num: number
/** 是否为虚拟礼物奖品 */
virtual: boolean
/** 虚拟奖品价值描述,实物奖品为空 */
price_text: string
}
/** 抽奖要求 */
require: {
/** 口令弹幕内容,无需弹幕为空字符串 */
danmu: string
/** 需送主播礼物,无需送礼为空 */
gift: {
/** 礼物id */
id: string
/** 礼物名称 */
name: string
/** 礼物数量 */
num: number
/** 单个礼物价值,除以1000为RMB */
price: number
} | null
/** 抽奖参与人群要求,无要求为空 */
user: {
/** 参与人群限制(关注/粉丝勋章/大航海) */
type: 'follow' | 'medal' | 'guard'
/** 参与人群限制等级,如粉丝勋章等级 */
value: number
/** 参与人群限制描述 */
text: string
} | null
}
}
```

##### handler.onAnchorLotteryEnd

主播天选时刻抽奖结果

```ts
export type Handler = {
/** 主播天选时刻抽奖结果 */
onAnchorLotteryEnd: (msg: Message<AnchorLotteryEndMsg>) => void
}

type msgType = 'ANCHOR_LOT_AWARD'

export interface AnchorLotteryEndMsg {
/** 天选抽奖id */
id: number
/** 天选奖品信息 */
award: {
/** 奖品图片 */
image: string
/** 奖品名称 */
name: string
/** 是否为虚拟礼物奖品 */
virtual: boolean
}
/** 中奖用户列表 */
winner: ({
/** 用户uid */
uid: number
/** 用户昵称 */
uname: string
/** 用户头像 */
face: number
/** 用户粉丝勋章等级 */
level: number
/** 中奖数量 */
num: number
})[]
}
```
</details>

#### 房间管理相关
Expand Down
24 changes: 24 additions & 0 deletions src/listener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
HEARTBEAT, type AttentionChangeMsgHandler,
LIVE, type LiveStartMsgHandler,
PREPARING, type LiveStopMsgHandler,
ANCHOR_LOT_AWARD, type AnchorLotteryEndMsgHandler,
ANCHOR_LOT_START, type AnchorLotteryStartMsgHandler,
DANMU_MSG, type DanmuMsgHandler,
GUARD_BUY, type GuardBuyHandler,
INTERACT_WORD, ENTRY_EFFECT, LIKE_INFO_V3_CLICK, type UserActionMsgHandler,
Expand Down Expand Up @@ -34,6 +36,8 @@ export type MsgHandler = Partial<
& AttentionChangeMsgHandler
& LiveStartMsgHandler
& LiveStopMsgHandler
& AnchorLotteryEndMsgHandler
& AnchorLotteryStartMsgHandler
& DanmuMsgHandler
& GuardBuyHandler
& UserActionMsgHandler
Expand Down Expand Up @@ -109,6 +113,26 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
})
}

// ANCHOR_LOT_AWARD
if (handler[ANCHOR_LOT_AWARD.handlerName] || rawHandlerNames.has(ANCHOR_LOT_AWARD.eventName)) {
rawHandlerNames.delete(ANCHOR_LOT_AWARD.eventName)
instance.on(ANCHOR_LOT_AWARD.eventName as any, (data: WSMessage<any>) => {
isHandleRaw && rawHandler[ANCHOR_LOT_AWARD.eventName]?.(data.data)
const parsedData = ANCHOR_LOT_AWARD.parser(data.data, roomId)
handler[ANCHOR_LOT_AWARD.handlerName]?.(normalizeDanmu(ANCHOR_LOT_AWARD.eventName, parsedData, data.data))
})
}

// ANCHOR_LOT_START
if (handler[ANCHOR_LOT_START.handlerName] || rawHandlerNames.has(ANCHOR_LOT_START.eventName)) {
rawHandlerNames.delete(ANCHOR_LOT_START.eventName)
instance.on(ANCHOR_LOT_START.eventName as any, (data: WSMessage<any>) => {
isHandleRaw && rawHandler[ANCHOR_LOT_START.eventName]?.(data.data)
const parsedData = ANCHOR_LOT_START.parser(data.data, roomId)
handler[ANCHOR_LOT_START.handlerName]?.(normalizeDanmu(ANCHOR_LOT_START.eventName, parsedData, data.data))
})
}

// DANMU_MSG
if (handler[DANMU_MSG.handlerName] || rawHandlerNames.has(DANMU_MSG.eventName)) {
rawHandlerNames.delete(DANMU_MSG.eventName)
Expand Down
59 changes: 59 additions & 0 deletions src/parser/ANCHOR_LOT_AWARD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Message } from '../types/app'

export interface AnchorLotteryEndMsg {
/** 天选抽奖id */
id: number
/** 天选奖品信息 */
award: {
/** 奖品图片 */
image: string
/** 奖品名称 */
name: string
/** 是否为虚拟礼物奖品 */
virtual: boolean
}
/** 中奖用户列表 */
winner: ({
/** 用户uid */
uid: number
/** 用户昵称 */
uname: string
/** 用户头像 */
face: number
/** 用户粉丝勋章等级 */
level: number
/** 中奖数量 */
num: number
})[]
}

const parser = (data: any, roomId: number): AnchorLotteryEndMsg => {
const rawData = data.data

return {
id: rawData.id,
award: {
image: rawData.award_image,
name: rawData.award_name,
virtual: rawData.award_type === 1,
},
winner: rawData.award_users.map((user: any) => ({
uid: user.uid,
uname: user.uname,
face: user.face,
level: user.level,
num: user.num,
})),
}
}

export const ANCHOR_LOT_AWARD = {
parser,
eventName: 'ANCHOR_LOT_AWARD' as const,
handlerName: 'onAnchorLotteryEnd' as const,
}

export type Handler = {
/** 主播天选时刻抽奖结果 */
onAnchorLotteryEnd: (msg: Message<AnchorLotteryEndMsg>) => void
}
90 changes: 90 additions & 0 deletions src/parser/ANCHOR_LOT_START.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Message } from '../types/app'

export interface AnchorLotteryStartMsg {
/** 天选抽奖id */
id: number
/** 开始时间,秒级时间戳 */
start_time: number
/** 持续时间,秒 */
duration: number
/** 天选奖品信息 */
award: {
/** 奖品图片 */
image: string
/** 奖品名称 */
name: string
/** 奖品数量 */
num: number
/** 是否为虚拟礼物奖品 */
virtual: boolean
/** 虚拟奖品价值描述,实物奖品为空 */
price_text: string
}
/** 抽奖要求 */
require: {
/** 口令弹幕内容,无需弹幕为空字符串 */
danmu: string
/** 需送主播礼物,无需送礼为空 */
gift: {
/** 礼物id */
id: string
/** 礼物名称 */
name: string
/** 礼物数量 */
num: number
/** 单个礼物价值,除以1000为RMB */
price: number
} | null
/** 抽奖参与人群要求,无要求为空 */
user: {
/** 参与人群限制(关注/粉丝勋章/大航海) */
type: 'follow' | 'medal' | 'guard'
/** 参与人群限制等级,如粉丝勋章等级 */
value: number
/** 参与人群限制描述 */
text: string
} | null
}
}

const parser = (data: any, roomId: number): AnchorLotteryStartMsg => {
const rawData = data.data

return {
id: rawData.id,
start_time: rawData.current_time,
duration: rawData.max_time,
award: {
image: rawData.award_image,
name: rawData.award_name,
num: rawData.award_num,
virtual: rawData.award_type === 1,
price_text: rawData.award_price_text || '',
},
require: {
danmu: rawData.danmu || '',
gift: rawData.gift_id ? {
id: rawData.gift_id,
name: rawData.gift_name,
num: rawData.gift_num,
price: rawData.gift_price,
} : null,
user: rawData.require_type ? {
type: (['follow', 'medal', 'guard'] as const)[rawData.require_type - 1],
value: rawData.require_value,
text: rawData.require_text,
} : null,
},
}
}

export const ANCHOR_LOT_START = {
parser,
eventName: 'ANCHOR_LOT_START' as const,
handlerName: 'onAnchorLotteryStart' as const,
}

export type Handler = {
/** 主播天选时刻抽奖开启 */
onAnchorLotteryStart: (msg: Message<AnchorLotteryStartMsg>) => void
}
2 changes: 2 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 { ANCHOR_LOT_AWARD, type Handler as AnchorLotteryEndMsgHandler, type AnchorLotteryEndMsg } from './ANCHOR_LOT_AWARD'
export { ANCHOR_LOT_START, type Handler as AnchorLotteryStartMsgHandler, type AnchorLotteryStartMsg } from './ANCHOR_LOT_START'
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, LIKE_INFO_V3_CLICK, type Handler as UserActionMsgHandler, type UserActionMsg } from './INTERACT_WORD_ENTRY_EFFECT'
Expand Down

0 comments on commit 734bf40

Please sign in to comment.