263 lines
8.0 KiB
JavaScript
263 lines
8.0 KiB
JavaScript
let uidObj = {}
|
||
// 歌曲列表
|
||
var musicList = []
|
||
|
||
// 领舞人领舞时间 秒
|
||
let leaderTime = 15*60
|
||
// 何时添加镜头
|
||
var addJingTime = 2000;//2秒
|
||
// 事件列表-中文
|
||
let eventTextList = ['切歌', '跳', '向左', '向右', '左抖腿', '右抖腿', '抖动', '一起喊', '镜头']
|
||
// 领舞者喊的集体舞
|
||
let allEventList = ['切歌', '一起跳', '一起向左', '一起向右', '一起左抖腿', '一起右抖腿', '一起抖动', '一起喊', '镜头']
|
||
// 镜头列表
|
||
let list = ['scale-left', 'scale-right', 'scale-bottom-left', 'scale-bottom-right']
|
||
// 根据事件文字 获取事件名
|
||
function getEvent(name) {
|
||
let map = {
|
||
'切歌' : 'change_song',
|
||
'跳' : 'dancing-down',
|
||
'向左' : 'dancing-left',
|
||
'向右' : 'dancing-right',
|
||
'左抖腿' : 'dancing-wobble',
|
||
'右抖腿' : 'dancing-wobble-right',
|
||
'抖动' : 'dancing-up',
|
||
'一起喊' : 'all_shout',
|
||
'镜头' : scale()
|
||
}
|
||
|
||
return map[name] ? map[name] : ''
|
||
}
|
||
|
||
// 随机选择镜头
|
||
function scale() {
|
||
return list[Math.floor(Math.random() * list.length)];
|
||
}
|
||
|
||
// 字符串包含的事件 (仅取第一个)
|
||
function getFirstEventKeyword(name) {
|
||
let event = ''
|
||
$.each(eventTextList, function (index, item) {
|
||
if (name.toString().indexOf(item) != -1) {
|
||
event = item
|
||
return false
|
||
}
|
||
})
|
||
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 music() {
|
||
$.get('https://bili.scdxtc.cn', function (res) {
|
||
musicList = res
|
||
if (musicList.length > 0) {
|
||
$('#music source').attr('src', 'https://bili.scdxtc.cn/music/'+musicList[15])
|
||
}
|
||
})
|
||
}
|
||
|
||
// 切歌
|
||
function changeMusic() {
|
||
$.get('https://bili.scdxtc.cn', function (res) {
|
||
musicList = res
|
||
if (musicList.length > 0) {
|
||
let index = Math.floor(Math.random()*(musicList.length-1))
|
||
$('#music').data('song', musicList[index])
|
||
$('#music source').attr('src', 'https://bili.scdxtc.cn/music/'+musicList[index])
|
||
$('#music')[0].load()
|
||
}
|
||
})
|
||
}
|
||
// 个人喊舞
|
||
function peopleHan(val){
|
||
return eventTextList.includes(val);
|
||
}
|
||
// 领舞喊舞
|
||
function dancer(val){
|
||
return allEventList.includes(val);
|
||
}
|
||
// 接受消息处理
|
||
function receiveMessage(event)
|
||
{
|
||
// console.log(event.data, 'event test')
|
||
// 我们能信任信息来源吗?
|
||
if (event.origin !== "https://live.bilibili.com") {
|
||
return false
|
||
}
|
||
|
||
let dataList = JSON.parse(event.data)
|
||
let data = dataList.dm
|
||
let adminList = dataList.admin
|
||
|
||
if (data.length <= 0) {
|
||
return false
|
||
}
|
||
|
||
// 当前直播间 自己的弹幕 无法获取到ct和type值 就暂时过滤
|
||
if (data.ct === '') {
|
||
return false
|
||
}
|
||
adminList = ['内有猛犬小心','热心市民阿狄呐','流域_墨绿绿'];
|
||
// adminList = ['饺子吃肉肉'];
|
||
// 大航海列表
|
||
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)
|
||
}
|
||
}
|
||
}
|
||
|
||
// uid是否存在 不存在则存入uidObj
|
||
let isNew = 0
|
||
// console.log(uidObj[data.uid],'uidObj[data.uid]');
|
||
if (!uidObj[data.uid]) {
|
||
isNew = 1
|
||
uidObj[data.uid] = data.uname
|
||
}
|
||
|
||
// console.log(isNew, data.uname + ' 是否存在')
|
||
|
||
// custom.js中 每条弹幕的处理 放到了此处
|
||
// -------------------- 每条弹幕处理 begin -----------------------
|
||
let random2 = Math.floor(Math.random() + 0.5)
|
||
|
||
// 获取领舞信息
|
||
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] : ''
|
||
// TODO 测试后 删除弹幕随机附加事件
|
||
let dm = {text: data.danmaku, uid: data.uid, uname: data.uname,
|
||
event: getEvent(getFirstEventKeyword(data.danmaku)),
|
||
is_new: isNew, is_admin: 0, is_exit: random2, is_leader: isLeader}
|
||
// TODO 测试后 删除领舞随机
|
||
// let leaderRandomEvent = getEvent(getFirstEventKeyword(data.danmaku+randomEvent))
|
||
let leader = {
|
||
// uid: 0, uname: leaderInfo, text: getEvent(getFirstEventKeyword(data.danmaku+randomEvent)), event: leaderRandomEvent
|
||
uid: 0, uname: leaderInfo, text: '', event: ''
|
||
}
|
||
if (isLeader) {
|
||
leader.uid = dm.uid
|
||
leader.uname = dm.uname
|
||
leader.text = dm.text
|
||
leader.event = dm.event
|
||
}
|
||
// 新用户
|
||
if(dm.is_new==1){
|
||
// if(dm.uname!=leader.uname){//如果弹幕的人的名字不等于领舞者的名字,就创建一个人物
|
||
peopleObj(dm.uname,dm.uid,dm.text,dm.event);
|
||
// }
|
||
} else {
|
||
// 老用户
|
||
$(`#${dm.uid} .speak`).html(dm.text);
|
||
$(`#${dm.uid} .speak`).removeClass("dn");
|
||
setTimeout(()=>{
|
||
$(`#${dm.uid} .speak`).addClass("dn");
|
||
},3000)
|
||
// 舞者喊舞
|
||
if(peopleHan(dm.text)){
|
||
peopleAction(`${dm.uid}`,dm.event);
|
||
}
|
||
}
|
||
num++;
|
||
// 显示领舞的昵称
|
||
$('.lingname').text(leader.uname);
|
||
if(leader.event=='scale-left' || leader.event=='scale-right' || leader.event=='scale-bottom-left' || leader.event=='scale-bottom-right'){
|
||
// 领舞喊镜头
|
||
list.forEach(item=>{
|
||
var haveDan = $(`.ground-bg`).is(`.${item}`);
|
||
// 如果之前有镜头,删除镜头
|
||
if(haveDan) {
|
||
$('.ground-bg').removeClass(item);
|
||
}
|
||
})
|
||
// 各几秒重新添加镜头
|
||
setTimeout(()=>{
|
||
$('.ground-bg').addClass(leader.event);
|
||
},addJingTime)
|
||
} else {
|
||
// 显示领舞说话
|
||
if (leader.text.length>0) {
|
||
$('.speak-pink').text(leader.text);
|
||
$('.speak-pink').fadeIn();
|
||
//关闭领舞说话
|
||
setTimeout(()=>{
|
||
$('.speak-pink').fadeOut();
|
||
},1000)
|
||
}
|
||
// 领舞说切歌
|
||
if(leader.text=="切歌"){
|
||
changeMusic();
|
||
}
|
||
// 领舞喊一起喊
|
||
// leader.text.slice(0,3);
|
||
if(leader.text.slice(0,4)=="一起喊:" || leader.text.slice(0,4)=="一起喊:"){
|
||
allSay(leader.text.slice(4));
|
||
}
|
||
// 领舞自己喊跳舞
|
||
if(peopleHan(dm.text)){
|
||
clearInterval(dancerTimer);
|
||
dancerOwn(leader.event);
|
||
}
|
||
// 领舞喊集体跳舞
|
||
if(dancer(leader.text)){
|
||
allDancing(leader.event);
|
||
}
|
||
}
|
||
// -------------------- 每条弹幕处理 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);
|
||
|
||
// 数据是否为空
|
||
// 弹幕类型判断 事件弹幕、内容弹幕
|
||
|
||
}
|
||
|
||
music();
|
||
window.addEventListener("message", receiveMessage, false); |