squareDancing/js/handle.js

305 lines
8.8 KiB
JavaScript
Raw Normal View History

2021-11-25 10:11:01 +00:00
// 对象uid=>最近弹幕时间
2021-11-22 10:35:45 +00:00
let uidObj = {}
2021-11-25 10:11:01 +00:00
// 歌曲库地址
let musicUrl = 'https://bili.scdxtc.cn'
// 自动删除不说话用户
2021-11-25 11:42:43 +00:00
let autoDel = false
2021-11-25 10:11:01 +00:00
// 多少秒不说话删除
let speakExpireTime = 10*60
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
// 何时添加镜头
var addJingTime = 2000;//2秒
2021-11-20 13:07:24 +00:00
// 事件列表-中文
2021-11-23 14:04:00 +00:00
let eventTextList = ['切歌', '跳', '向左', '向右', '左抖腿', '右抖腿', '抖动', '一起喊', '镜头']
// 领舞者喊的集体舞
2021-11-23 14:04:00 +00:00
let allEventList = ['切歌', '一起跳', '一起向左', '一起向右', '一起左抖腿', '一起右抖腿', '一起抖动', '一起喊', '镜头']
// 镜头列表
2021-11-23 13:54:58 +00:00
let list = ['scale-left', 'scale-right', 'scale-bottom-left', 'scale-bottom-right']
2021-11-20 13:07:24 +00:00
// 根据事件文字 获取事件名
function getEvent(name) {
let map = {
'切歌' : 'change_song',
2021-11-23 14:04:00 +00:00
'跳' : 'dancing-down',
2021-11-20 13:07:24 +00:00
'向左' : '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
}
2021-11-25 02:23:54 +00:00
// 根据事件文字 获取领舞者事件名
function getLeaderEvent(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] : ''
}
2021-11-20 13:07:24 +00:00
// 随机选择镜头
function scale() {
2021-11-24 13:09:21 +00:00
return list[Math.floor(Math.random() * 3)];
2021-11-20 13:07:24 +00:00
}
// 字符串包含的事件 (仅取第一个)
function getFirstEventKeyword(name) {
let event = ''
$.each(eventTextList, function (index, item) {
2021-11-23 13:14:51 +00:00
if (name.toString().indexOf(item) != -1) {
2021-11-20 13:07:24 +00:00
event = item
return false
}
})
return event
}
2021-11-21 09:03:00 +00:00
// 设置领舞 name=姓名 time=有效期 单位秒
function setLeader(name, time) {
2021-11-24 06:37:59 +00:00
sessionStorage.setItem('leader', name)
sessionStorage.setItem('leader_end_at', (new Date).getTime()/1000 + time)
2021-11-21 09:03:00 +00:00
}
// 获取领舞人信息
function getLeader() {
let res = []
2021-11-24 06:37:59 +00:00
res['name'] = sessionStorage.getItem('leader') ? sessionStorage.getItem('leader') : ''
res['time'] = sessionStorage.getItem('leader_end_at') ? sessionStorage.getItem('leader_end_at') : 0
2021-11-21 09:03:00 +00:00
return res
}
2021-11-23 04:15:38 +00:00
function music() {
2021-11-25 10:11:01 +00:00
$.get(musicUrl, function (res) {
2021-11-23 04:15:38 +00:00
musicList = res
if (musicList.length > 0) {
2021-11-25 10:11:01 +00:00
$('#music source').attr('src', musicUrl+'/music/'+musicList[0])
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-25 10:11:01 +00:00
$.get(musicUrl, 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-25 10:11:01 +00:00
$('#music source').attr('src', musicUrl+'/music/'+musicList[index])
2021-11-23 04:54:37 +00:00
$('#music')[0].load()
2021-11-23 04:15:38 +00:00
}
})
}
2021-11-23 13:14:51 +00:00
// 个人喊舞
function peopleHan(val){
return eventTextList.includes(val);
}
// 领舞喊舞
function dancer(val){
return allEventList.includes(val);
}
2021-11-25 10:11:01 +00:00
// 删除久未说话的人
function delExpireDiv() {
if (!autoDel) {
return false
}
let timestamp = parseInt(new Date().getTime()/1000)
let delList = $(".dance-people").filter(function() {
return $(this).attr("data-time") < timestamp - speakExpireTime;
})
console.log(delList.length, speakExpireTime+'秒未说话人数')
delList.remove();
}
2021-11-20 13:07:24 +00:00
// 接受消息处理
function receiveMessage(event)
{
2021-11-24 13:00:13 +00:00
// console.log(event.data, '接受弹幕')
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
2021-11-25 10:11:01 +00:00
// 目前硬编码大航海列表
// let adminList = dataList.admin
adminList = ['内有猛犬小心','热心市民阿狄呐','流域_墨绿绿'];
2021-11-20 13:07:24 +00:00
if (data.length <= 0) {
return false
}
// 当前直播间 自己的弹幕 无法获取到ct和type值 就暂时过滤
if (data.ct === '') {
return false
}
2021-11-25 03:01:26 +00:00
2021-11-21 09:03:00 +00:00
// 大航海列表
if (adminList.length > 0) {
// 获取当前领舞人
2021-11-25 02:39:53 +00:00
let currentLeader = getLeader()
2021-11-21 09:03:00 +00:00
let now = (new Date).getTime()/1000
2021-11-25 02:39:53 +00:00
if (currentLeader['name'] === '') {
2021-11-21 09:03:00 +00:00
setLeader(adminList[0], leaderTime)
} else {
//领舞人存在 检查过期时间
2021-11-25 02:39:53 +00:00
if (currentLeader['time'] === 0 || currentLeader['time'] < now) {
2021-11-21 09:03:00 +00:00
// 切换领舞
// 当前领舞人
2021-11-25 07:25:59 +00:00
let leaderIndex = adminList.indexOf(currentLeader['name'])
2021-11-21 09:03:00 +00:00
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-25 10:11:01 +00:00
let speakTime = uidObj[data.uid];
if (!speakTime) {
2021-11-22 10:35:45 +00:00
isNew = 1
2021-11-25 10:11:01 +00:00
} else if (autoDel && speakTime < parseInt(new Date().getTime()/1000) - speakExpireTime){
//存在 但是最近弹幕时间已在过期时间外。
isNew = 1;
2021-11-22 10:35:45 +00:00
}
2021-11-25 10:11:01 +00:00
// 记录uid最近弹幕时间
uidObj[data.uid] = data.ts
2021-11-22 10:35:45 +00:00
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-21 09:03:00 +00:00
// 获取领舞信息
2021-11-24 06:37:59 +00:00
let leaderInfo = sessionStorage.getItem('leader')
2021-11-21 09:03:00 +00:00
2021-11-25 02:23:54 +00:00
let eventName = getEvent(data.danmaku)
2021-11-21 09:03:00 +00:00
let isLeader = data.uname == leaderInfo ? 1 : 0
let dm = {text: data.danmaku, uid: data.uid, uname: data.uname,
2021-11-25 02:23:54 +00:00
event: eventName,
2021-11-25 10:11:01 +00:00
is_new: isNew, is_admin: 0, is_leader: isLeader,active_time: data.ts}
2021-11-20 13:07:24 +00:00
let leader = {
2021-11-23 07:08:37 +00:00
uid: 0, uname: leaderInfo, text: '', event: ''
2021-11-20 13:07:24 +00:00
}
2021-11-25 02:39:53 +00:00
2021-11-21 09:03:00 +00:00
if (isLeader) {
2021-11-25 02:39:53 +00:00
let leaderEvent = getLeaderEvent(dm.text)
2021-11-21 09:03:00 +00:00
leader.uid = dm.uid
leader.uname = dm.uname
leader.text = dm.text
2021-11-25 02:39:53 +00:00
leader.event = eventName ? eventName : leaderEvent
2021-11-21 09:03:00 +00:00
}
2021-11-25 02:39:53 +00:00
// 新用户
2021-11-23 01:56:09 +00:00
if(dm.is_new==1){
2021-11-24 08:48:50 +00:00
// if(dm.uname!=leader.uname){//如果弹幕的人的名字不等于领舞者的名字,就创建一个人物
2021-11-25 10:11:01 +00:00
peopleObj(dm.uname,dm.uid,dm.text,dm.event,dm.active_time);
2021-11-24 08:48:50 +00:00
// }
2021-11-23 01:56:09 +00:00
} else {
// 老用户
2021-11-23 07:08:37 +00:00
$(`#${dm.uid} .speak`).html(dm.text);
2021-11-25 10:11:01 +00:00
//更新最近时间
$(`#${dm.uid}`).attr('data-time', dm.active_time);
2021-11-23 07:08:37 +00:00
$(`#${dm.uid} .speak`).removeClass("dn");
setTimeout(()=>{
$(`#${dm.uid} .speak`).addClass("dn");
},3000)
// 舞者喊舞
2021-11-23 13:14:51 +00:00
if(peopleHan(dm.text)){
peopleAction(`${dm.uid}`,dm.event);
}
2021-11-22 11:13:38 +00:00
}
2021-11-20 13:07:24 +00:00
num++;
2021-11-25 10:11:01 +00:00
// console.log(Object.keys(uidObj).length, 'uid 长度')
// console.log(uidObj, 'uid')
// console.log(num, '人物渲染个数')
// 显示领舞的昵称
2021-11-20 13:07:24 +00:00
$('.lingname').text(leader.uname);
2021-11-25 03:01:26 +00:00
if (!isLeader) {
return false
}
// console.log(leader, '领舞人信息')
2021-11-20 13:07:24 +00:00
if(leader.event=='scale-left' || leader.event=='scale-right' || leader.event=='scale-bottom-left' || leader.event=='scale-bottom-right'){
// 领舞喊镜头
2021-11-23 13:54:58 +00:00
list.forEach(item=>{
var haveDan = $(`.ground-bg`).is(`.${item}`);
// 如果之前有镜头,删除镜头
2021-11-23 13:54:58 +00:00
if(haveDan) {
$('.ground-bg').removeClass(item);
}
})
// 各几秒重新添加镜头
2021-11-23 13:54:58 +00:00
setTimeout(()=>{
$('.ground-bg').addClass(leader.event);
},addJingTime)
2021-11-20 13:07:24 +00:00
} else {
2021-11-23 13:14:51 +00:00
// 显示领舞说话
if (leader.text.length>0) {
$('.speak-pink').text(leader.text);
$('.speak-pink').fadeIn();
//关闭领舞说话
setTimeout(()=>{
$('.speak-pink').fadeOut();
2021-11-25 03:05:04 +00:00
},3000)
2021-11-23 13:14:51 +00:00
}
// 领舞说切歌
2021-11-23 13:14:51 +00:00
if(leader.text=="切歌"){
changeMusic();
}
// 领舞喊一起喊
2021-11-24 02:57:29 +00:00
// leader.text.slice(0,3);
2021-11-24 07:17:04 +00:00
if (leader.text.length >= 4) {
if(leader.text.slice(0,4)=="一起喊:" || leader.text.slice(0,4)=="一起喊:"){
allSay(leader.text.slice(4));
}
}
// 领舞自己喊跳舞
2021-11-23 13:14:51 +00:00
if(peopleHan(dm.text)){
2021-11-23 13:54:58 +00:00
clearInterval(dancerTimer);
2021-11-23 13:14:51 +00:00
dancerOwn(leader.event);
}
// 领舞喊集体跳舞
2021-11-23 13:14:51 +00:00
if(dancer(leader.text)){
allDancing(leader.event);
}
2021-11-20 13:07:24 +00:00
}
2021-11-20 13:23:59 +00:00
// -------------------- 每条弹幕处理 end -----------------------
2021-11-20 13:07:24 +00:00
}
2021-11-23 04:15:38 +00:00
music();
2021-11-20 13:07:24 +00:00
window.addEventListener("message", receiveMessage, false);