优化领舞

master
yin5th 2021-11-21 17:03:00 +08:00
parent 65597767c7
commit c24bb8200d
1 changed files with 82 additions and 9 deletions

View File

@ -1,5 +1,8 @@
// uid列表 TODO 优化查询性能 let uidList = []
let uidList = {}
// 领舞人领舞时间 秒
let leaderTime = 10
// 事件列表-中文 // 事件列表-中文
let eventTextList = ['切歌', '跳跃', '向左', '向右', '左抖腿', '右抖腿', '抖动', '一起喊', '镜头'] let eventTextList = ['切歌', '跳跃', '向左', '向右', '左抖腿', '右抖腿', '抖动', '一起喊', '镜头']
@ -16,6 +19,8 @@ function getEvent(name) {
'一起喊' : 'all_shout', '一起喊' : 'all_shout',
'镜头' : scale() '镜头' : scale()
} }
return map[name] ? map[name] : ''
} }
// 随机选择镜头 // 随机选择镜头
@ -36,16 +41,33 @@ function getFirstEventKeyword(name) {
return event return event
} }
// 设置领舞 name=姓名 time=有效期 单位秒
function setLeader(name, time) {
localStorage.setItem('leader', name)
localStorage.setItem('leader_end_at', (new Date).getTime()/1000 + time)
}
// 获取领舞人信息
function getLeader() {
let res = []
res['name'] = localStorage.getItem('leader') ? localStorage.getItem('leader') : ''
res['time'] = localStorage.getItem('leader_end_at') ? localStorage.getItem('leader_end_at') : 0
return res
}
// 接受消息处理 // 接受消息处理
function receiveMessage(event) function receiveMessage(event)
{ {
console.log(event.data, 'event test') // console.log(event.data, 'event test')
// 我们能信任信息来源吗? // 我们能信任信息来源吗?
if (event.origin !== "https://live.bilibili.com") { if (event.origin !== "https://live.bilibili.com") {
return false return false
} }
let data = JSON.parse(event.data) let dataList = JSON.parse(event.data)
let data = dataList.dm
let adminList = dataList.admin
if (data.length <= 0) { if (data.length <= 0) {
return false return false
@ -56,16 +78,65 @@ function receiveMessage(event)
return false return false
} }
// 大航海列表
if (adminList.length > 0) {
// 获取当前领舞人
let leader = getLeader()
let now = (new Date).getTime()/1000
if (leader['name'] === '') {
setLeader(adminList[0], leaderTime)
} else {
//领舞人存在 检查过期时间
if (leader['time'] === 0 || leader['time'] < now) {
// 切换领舞
// 当前领舞人
let leaderIndex = adminList.indexOf(leader['name'])
let newLeader
if (leaderIndex == -1) {
newLeader = adminList[0]
} else {
let newIndex = leaderIndex + 1
if (newIndex >= adminList.length) {
newIndex = 0
}
newLeader = adminList[newIndex]
}
setLeader(newLeader, leaderTime)
}
}
}
// custom.js中 每条弹幕的处理 放到了此处 // custom.js中 每条弹幕的处理 放到了此处
// -------------------- 每条弹幕处理 begin ----------------------- // -------------------- 每条弹幕处理 begin -----------------------
let random1 = data.uid in uidList ? 1 : 0 let random1 = data.uid in uidList ? 1 : 0
let random2 = Math.floor(Math.random() + 0.5) let random2 = Math.floor(Math.random() + 0.5)
let dm = {text: data.danmaku, uid: data.uid, uname: data.uname,
event: getFirstEventKeyword(data.danmaku), is_new: random1, is_admin: 0, is_exit: random2}
// 获取领舞信息
let leaderInfo = localStorage.getItem('leader')
let isLeader = data.uname == leaderInfo ? 1 : 0
// 给弹幕加上随机事件
let mathIndex = Math.floor(Math.random()*15)
console.log(mathIndex, '随机因子')
let randomEvent = eventTextList[mathIndex] ? eventTextList[mathIndex] : ''
let dm = {text: data.danmaku, uid: data.uid, uname: data.uname,
event: getEvent(getFirstEventKeyword(data.danmaku+randomEvent)),
is_new: random1, is_admin: 0, is_exit: random2, is_leader: isLeader}
let leader = { let leader = {
uid: '1111', uname: '大帅比', text: '6666', event: '' uid: 0, uname: leaderInfo, text: '', event: ''
} }
if (isLeader) {
leader.uid = dm.uid
leader.uname = dm.uname
leader.text = dm.text
leader.event = dm.event
}
console.log(dm, '弹幕')
console.log(leader, '当前领舞')
console.log(leaderInfo, '本地缓存领舞')
let actionIndex = Math.floor(Math.random()*actionArr.length); let actionIndex = Math.floor(Math.random()*actionArr.length);
let moveIndex = Math.floor(Math.random()*moveArr.length); let moveIndex = Math.floor(Math.random()*moveArr.length);
@ -79,8 +150,10 @@ function receiveMessage(event)
setTimeout(()=>{ setTimeout(()=>{
console.log(leader.event); console.log(leader.event);
// 显示领舞说话 // 显示领舞说话
$('.speak-pink').text(leader.text); if (leader.text.length>0) {
$('.speak-pink').fadeIn(); $('.speak-pink').text(leader.text);
$('.speak-pink').fadeIn();
}
setTimeout(()=>{//关闭领舞说话 setTimeout(()=>{//关闭领舞说话
$('.speak-pink').fadeOut(); $('.speak-pink').fadeOut();
},3000) },3000)