squareDancing/js/handle.js

107 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// uid列表 TODO 优化查询性能
let uidList = {}
// 事件列表-中文
let eventTextList = ['切歌', '跳跃', '向左', '向右', '左抖腿', '右抖腿', '抖动', '一起喊', '镜头']
// 根据事件文字 获取事件名
function getEvent(name) {
let map = {
'切歌' : 'change_song',
'跳跃' : 'dancing-down',
'向左' : 'dancing-left',
'向右' : 'dancing-right',
'左抖腿' : 'dancing-wobble',
'右抖腿' : 'dancing-wobble-right',
'抖动' : 'dancing-up',
'一起喊' : 'all_shout',
'镜头' : scale()
}
}
// 随机选择镜头
function scale() {
let list = ['scale-left', 'scale-right', 'scale-bottom-left', 'scale-bottom-right']
return list[Math.floor(Math.random() * list.length)];
}
// 字符串包含的事件 (仅取第一个)
function getFirstEventKeyword(name) {
let event = ''
$.each(eventTextList, function (index, item) {
if (name.indexOf(item) != -1) {
event = item
return false
}
})
return event
}
// 接受消息处理
function receiveMessage(event)
{
console.log(event.data, 'event test')
// 我们能信任信息来源吗?
if (event.origin !== "https://live.bilibili.com") {
return false
}
let data = JSON.parse(event.data)
if (data.length <= 0) {
return false
}
// 当前直播间 自己的弹幕 无法获取到ct和type值 就暂时过滤
if (data.ct === '') {
return false
}
// custom.js中 每条弹幕的处理 放到了此处
// -------------------- 每条弹幕处理 begin -----------------------
let random1 = uid in uidList ? 1 : 0
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 leader = {
uid: '1111', uname: '大帅比', text: '6666', event: ''
}
let actionIndex = Math.floor(Math.random()*actionArr.length);
let moveIndex = Math.floor(Math.random()*moveArr.length);
peopleObj(dm.uname,dm.text,actionArr[actionIndex],moveArr[moveIndex]);
num++;
$('.lingname').text(leader.uname);
if(leader.event=='scale-left' || leader.event=='scale-right' || leader.event=='scale-bottom-left' || leader.event=='scale-bottom-right'){
$('.ground-bg').addClass(leader.event);
} else {
setTimeout(()=>{
console.log(leader.event);
// 显示领舞说话
$('.speak-pink').text(leader.text);
$('.speak-pink').fadeIn();
setTimeout(()=>{//关闭领舞说话
$('.speak-pink').fadeOut();
},3000)
allDancing(leader.event);
},3000)
}
// -------------------- 每条弹幕处理 end -----------------------
// event.source 就当前弹出页的来源页面
// event.data 是 "hello there!"
// 假设你已经验证了所受到信息的origin (任何时候你都应该这样做), 一个很方便的方式就是把event.source
// 作为回信的对象并且把event.origin作为targetOrigin
// event.source.postMessage("hi there yourself! the secret response " +
// "is: rheeeeet!",
// event.origin);
// 数据是否为空
// 弹幕类型判断 事件弹幕、内容弹幕
}
window.addEventListener("message", receiveMessage, false);