-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gift.ts
139 lines (133 loc) · 3.94 KB
/
Gift.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { EventInfo } from '../types/event'
import type { CoinType, Message, User } from '../types/message'
import { normalizeMessage } from '../utils/message'
export interface GiftData {
/** 用户信息 */
user: User
/** 礼物Id */
giftId: number
/** 礼物名称 */
giftName: string
/** 礼物瓜子类型;1金瓜子=1/1000元=1/100电池;银瓜子无金钱价值 */
coinType: CoinType
/** 礼物价格(单价);单位:1金瓜子=1/1000元=1/100电池 */
price: number
/** 礼物数量 */
num: number
/** 赠送动作(例如:投喂) */
action: string
/** 礼物连击 */
combo?: {
/** 连击Id */
id: number
/** 连击次数 */
num: number
/** 连击总价;单位:1金瓜子=1/1000元=1/100电池 */
totalPrice: number
}
/** 时间戳 */
timestamp: number
}
function dataProcessorGift(rawData: any): Message<GiftData> {
const { data } = rawData
const { medal_info } = data
const newData: GiftData = {
user: {
uid: data.uid,
uname: data.uname,
face: data.face,
fansMedal: medal_info && medal_info.medal_name
? {
name: medal_info.medal_name,
level: medal_info.medal_level,
guardType: medal_info.guard_level,
color: {
original: medal_info.medal_color,
border: medal_info.medal_color_border,
start: medal_info.medal_color_start,
end: medal_info.medal_color_end,
},
isLighted: medal_info.is_lighted,
anchor: {
uid: medal_info.target_id,
uname: medal_info.anchor_uname, // ""
roomId: medal_info.anchor_roomid, // 0
},
}
: undefined,
},
giftId: data.giftId,
giftName: data.giftName,
coinType: data.coin_type,
price: data.price,
num: data.num,
action: data.action,
combo: data.batch_combo_id
? {
id: data.batch_combo_id,
num: data.batch_combo_num,
totalPrice: data.combo_total_coin,
}
: undefined,
timestamp: data.timestamp,
}
return normalizeMessage(rawData.cmd, newData, rawData)
}
function dataProcessorRedPocket(rawData: any): Message<GiftData> {
const { data } = rawData
const { medal_info } = data
const newData: GiftData = {
user: {
uid: data.uid,
uname: data.uname,
fansMedal: medal_info && medal_info.medal_name
? {
name: medal_info.medal_name,
// 没有 face 字段
level: medal_info.medal_level,
guardType: medal_info.guard_level,
color: {
original: medal_info.medal_color,
border: medal_info.medal_color_border,
start: medal_info.medal_color_start,
end: medal_info.medal_color_end,
},
isLighted: medal_info.is_lighted,
anchor: {
uid: medal_info.target_id,
uname: medal_info.anchor_uname, // ""
roomId: medal_info.anchor_roomid, // 0
},
}
: undefined,
},
giftId: data.gift_id,
giftName: data.gift_name,
coinType: 'gold',
price: data.price * 100, // 原单位是电池,为单位统一,转换为金瓜子
num: data.num,
action: data.action,
combo: data.batch_combo_id
? {
id: data.batch_combo_id,
num: data.batch_combo_num,
totalPrice: data.combo_total_coin,
}
: undefined,
timestamp: data.current_time,
}
return normalizeMessage(rawData.cmd, newData, rawData)
}
function dataProcessor(rawData: any): Message<GiftData> {
switch (rawData.cmd) {
case 'POPULARITY_RED_POCKET_NEW':
return dataProcessorRedPocket(rawData)
default:
return dataProcessorGift(rawData)
}
}
export const GiftEvent: EventInfo<GiftData> = {
cmdName: ['SEND_GIFT', 'POPULARITY_RED_POCKET_NEW'],
handlerName: 'onGift',
dataProcessor,
}