smart player afk detection added
This commit is contained in:
@@ -27,6 +27,51 @@ class TwoWayMap {
|
||||
delete this.reverseMap[value];
|
||||
}
|
||||
}
|
||||
// afk
|
||||
const get_time_in_seconds = () => {
|
||||
return new Date().getTime() / 1000;
|
||||
}
|
||||
|
||||
let lastInputTime
|
||||
let lastInputSendTime = 0
|
||||
let inputSendTimeout = 3
|
||||
let cirrusWebsocket = null
|
||||
let lastOtherPlayerInput = get_time_in_seconds()
|
||||
|
||||
function getOveralLastPlayerInput() {
|
||||
return lastOtherPlayerInput
|
||||
}
|
||||
|
||||
function updateLastInputTime() {
|
||||
lastInputTime = get_time_in_seconds()
|
||||
}
|
||||
updateLastInputTime()
|
||||
|
||||
function updateLastOtherPlayerInput() {
|
||||
//console.log('last player input')
|
||||
lastOtherPlayerInput = get_time_in_seconds()
|
||||
}
|
||||
|
||||
const check_input = () => {
|
||||
//console.log('time from last input: ', get_time_in_seconds() - getOveralLastPlayerInput())
|
||||
if (!cirrusWebsocket)
|
||||
return
|
||||
if (lastInputSendTime == lastInputTime) {
|
||||
return
|
||||
}
|
||||
if ((get_time_in_seconds() - lastInputSendTime) > inputSendTimeout) {
|
||||
lastInputSendTime = lastInputTime
|
||||
cirrusWebsocket.send(JSON.stringify({type:"playerInput"}))
|
||||
}
|
||||
}
|
||||
|
||||
const start_check_input = () => {
|
||||
check_input()
|
||||
setTimeout(() => start_check_input(), 1000)
|
||||
}
|
||||
|
||||
start_check_input()
|
||||
|
||||
|
||||
/**
|
||||
* Frontend logic
|
||||
@@ -1153,6 +1198,7 @@ function createWebRtcOffer() {
|
||||
|
||||
function sendInputData(data) {
|
||||
if (webRtcPlayerObj) {
|
||||
updateLastInputTime();
|
||||
resetAfkWarningTimer();
|
||||
webRtcPlayerObj.send(data);
|
||||
}
|
||||
@@ -2744,6 +2790,7 @@ function connect() {
|
||||
}
|
||||
|
||||
ws.onmessage = function (event) {
|
||||
cirrusWebsocket = ws
|
||||
|
||||
// Check if websocket message is binary, if so, stringify it.
|
||||
if (event.data && event.data instanceof Blob) {
|
||||
@@ -2771,6 +2818,8 @@ function connect() {
|
||||
console.warn(msg.warning);
|
||||
} else if (msg.type === 'peerDataChannels') {
|
||||
onWebRtcSFUPeerDatachannels(msg);
|
||||
} else if (msg.type === 'lastOtherPlayerInput') {
|
||||
updateLastOtherPlayerInput();
|
||||
} else {
|
||||
console.error("Invalid SS message type", msg.type);
|
||||
}
|
||||
@@ -2778,9 +2827,11 @@ function connect() {
|
||||
|
||||
ws.onerror = function (event) {
|
||||
console.log(`WS error: ${JSON.stringify(event)}`);
|
||||
cirrusWebsocket = null
|
||||
};
|
||||
|
||||
ws.onclose = function (event) {
|
||||
cirrusWebsocket = null
|
||||
|
||||
closeStream();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user