squareDancing/js/handle.js

232 lines
7.2 KiB
JavaScript
Raw Normal View History

2021-11-22 10:35:45 +00:00
let uidObj = {}
2021-11-23 04:15:38 +00:00
// 歌曲列表
var musicList = []
2021-11-21 09:03:00 +00:00
// 领舞人领舞时间 秒
2021-11-23 06:44:56 +00:00
let leaderTime = 15*60
2021-11-21 09:03:00 +00:00
2021-11-20 13:07:24 +00:00
// 事件列表-中文
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()
}
2021-11-21 09:03:00 +00:00
return map[name] ? map[name] : ''
2021-11-20 13:07:24 +00:00
}
// 随机选择镜头
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
}
2021-11-21 09:03:00 +00:00
// 设置领舞 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
}
2021-11-23 04:15:38 +00:00
function music() {
2021-11-23 06:53:02 +00:00
$.get('https://bili.scdxtc.cn', function (res) {
2021-11-23 04:15:38 +00:00
musicList = res
if (musicList.length > 0) {
2021-11-23 06:53:02 +00:00
$('#music source').attr('src', 'https://bili.scdxtc.cn/music/'+musicList[15])
2021-11-23 04:15:38 +00:00
}
})
}
2021-11-23 04:54:37 +00:00
// 切歌
2021-11-23 04:15:38 +00:00
function changeMusic() {
2021-11-23 04:54:37 +00:00
console.log('切歌')
2021-11-23 06:53:02 +00:00
$.get('https://bili.scdxtc.cn', function (res) {
2021-11-23 04:15:38 +00:00
musicList = res
if (musicList.length > 0) {
2021-11-23 04:54:37 +00:00
let index = Math.floor(Math.random()*(musicList.length-1))
$('#music').data('song', musicList[index])
2021-11-23 06:53:02 +00:00
$('#music source').attr('src', 'https://bili.scdxtc.cn/music/'+musicList[index])
2021-11-23 04:54:37 +00:00
$('#music')[0].load()
2021-11-23 04:15:38 +00:00
}
})
}
2021-11-20 13:07:24 +00:00
// 接受消息处理
function receiveMessage(event)
{
2021-11-21 09:03:00 +00:00
// console.log(event.data, 'event test')
2021-11-20 13:07:24 +00:00
// 我们能信任信息来源吗?
if (event.origin !== "https://live.bilibili.com") {
return false
}
2021-11-21 09:03:00 +00:00
let dataList = JSON.parse(event.data)
let data = dataList.dm
let adminList = dataList.admin
2021-11-20 13:07:24 +00:00
if (data.length <= 0) {
return false
}
// 当前直播间 自己的弹幕 无法获取到ct和type值 就暂时过滤
if (data.ct === '') {
return false
}
2021-11-21 09:03:00 +00:00
// 大航海列表
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)
}
}
}
2021-11-22 10:35:45 +00:00
// uid是否存在 不存在则存入uidObj
let isNew = 0
2021-11-23 01:56:09 +00:00
// console.log(uidObj[data.uid],'uidObj[data.uid]');
2021-11-22 10:35:45 +00:00
if (!uidObj[data.uid]) {
isNew = 1
uidObj[data.uid] = data.uname
}
2021-11-23 01:56:09 +00:00
// console.log(isNew, data.uname + ' 是否存在')
2021-11-22 10:35:45 +00:00
2021-11-20 13:23:59 +00:00
// custom.js中 每条弹幕的处理 放到了此处
// -------------------- 每条弹幕处理 begin -----------------------
2021-11-20 13:07:24 +00:00
let random2 = Math.floor(Math.random() + 0.5)
2021-11-21 09:03:00 +00:00
// 获取领舞信息
let leaderInfo = localStorage.getItem('leader')
let isLeader = data.uname == leaderInfo ? 1 : 0
// 给弹幕加上随机事件
let mathIndex = Math.floor(Math.random()*15)
2021-11-23 01:56:09 +00:00
// console.log(mathIndex, '随机因子')
2021-11-21 09:03:00 +00:00
let randomEvent = eventTextList[mathIndex] ? eventTextList[mathIndex] : ''
2021-11-21 09:14:27 +00:00
// TODO 测试后 删除弹幕随机附加事件
2021-11-21 09:03:00 +00:00
let dm = {text: data.danmaku, uid: data.uid, uname: data.uname,
2021-11-23 09:50:22 +00:00
event: getEvent(getFirstEventKeyword(data.danmaku)),
2021-11-22 10:35:45 +00:00
is_new: isNew, is_admin: 0, is_exit: random2, is_leader: isLeader}
2021-11-23 09:29:35 +00:00
// console.log(dm,"")//
2021-11-21 09:14:27 +00:00
// TODO 测试后 删除领舞随机
let leaderRandomEvent = getEvent(getFirstEventKeyword(data.danmaku+randomEvent))
2021-11-20 13:07:24 +00:00
let leader = {
2021-11-23 07:08:37 +00:00
// uid: 0, uname: leaderInfo, text: getEvent(getFirstEventKeyword(data.danmaku+randomEvent)), event: leaderRandomEvent
uid: 0, uname: leaderInfo, text: '', event: ''
2021-11-20 13:07:24 +00:00
}
2021-11-21 09:03:00 +00:00
if (isLeader) {
leader.uid = dm.uid
leader.uname = dm.uname
leader.text = dm.text
leader.event = dm.event
}
2021-11-23 01:56:09 +00:00
// console.log(leader, '当前领舞')
// console.log(leaderRandomEvent, '领舞随机事件')
2021-11-23 09:50:22 +00:00
// let actionIndex = Math.floor(Math.random()*actionArr.length);
// let moveIndex = Math.floor(Math.random()*moveArr.length);
2021-11-23 01:56:09 +00:00
if(dm.is_new==1){
2021-11-23 09:50:22 +00:00
peopleObj(dm.uname,dm.uid,dm.text,dm.event);
console.log(dm.event, '当前触发事件')
2021-11-23 01:56:09 +00:00
} else {
2021-11-23 07:08:37 +00:00
$(`#${dm.uid} .speak`).html(dm.text);
$(`#${dm.uid} .speak`).removeClass("dn");
setTimeout(()=>{
$(`#${dm.uid} .speak`).addClass("dn");
},3000)
2021-11-23 09:50:22 +00:00
console.log(dm.event, '当前触发事件')
2021-11-23 07:08:37 +00:00
peopleAction(`${dm.uid}`,dm.event);
2021-11-22 11:13:38 +00:00
}
2021-11-20 13:07:24 +00:00
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(()=>{
2021-11-23 01:56:09 +00:00
// console.log(leader.event);
2021-11-20 13:07:24 +00:00
// 显示领舞说话
2021-11-21 09:03:00 +00:00
if (leader.text.length>0) {
$('.speak-pink').text(leader.text);
$('.speak-pink').fadeIn();
}
2021-11-23 06:12:54 +00:00
if(leader.text=="切歌"){
changeMusic();
}
2021-11-20 13:07:24 +00:00
setTimeout(()=>{//关闭领舞说话
$('.speak-pink').fadeOut();
},3000)
allDancing(leader.event);
},3000)
}
2021-11-20 13:23:59 +00:00
// -------------------- 每条弹幕处理 end -----------------------
2021-11-20 13:07:24 +00:00
// 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);
// 数据是否为空
// 弹幕类型判断 事件弹幕、内容弹幕
}
2021-11-23 04:15:38 +00:00
music();
2021-11-20 13:07:24 +00:00
window.addEventListener("message", receiveMessage, false);