Fixes to update friend state

This function is a massive pile of garbage that I don't have time to rewrite
This commit is contained in:
Natsumi
2022-08-14 20:18:32 +12:00
parent ed5a1daf92
commit 270aaeab96
+173 -136
View File
@@ -446,7 +446,7 @@ speechSynthesis.getVoices();
type: 'error' type: 'error'
}); });
$app.avatarDialog.visible = false; $app.avatarDialog.visible = false;
throw new Error(`404: Can't find avatarǃ ${endpoint}`); throw new Error(`404: ${data.error.message} ${endpoint}`);
} }
if ( if (
init.method === 'GET' && init.method === 'GET' &&
@@ -455,7 +455,7 @@ speechSynthesis.getVoices();
this.failedGetRequests.set(endpoint, Date.now()); this.failedGetRequests.set(endpoint, Date.now());
} }
if (status === 404 && endpoint.substring(0, 6) === 'users/') { if (status === 404 && endpoint.substring(0, 6) === 'users/') {
throw new Error(`404: Can't find user! ${endpoint}`); throw new Error(`404: ${data.error.message} ${endpoint}`);
} }
if ( if (
status === 404 && status === 404 &&
@@ -4296,6 +4296,7 @@ speechSynthesis.getVoices();
$app.data.debugUserDiff = false; $app.data.debugUserDiff = false;
$app.data.debugPhotonLogging = false; $app.data.debugPhotonLogging = false;
$app.data.debugGameLog = false; $app.data.debugGameLog = false;
$app.data.debugFriendState = false;
$app.data.APILastOnline = new Map(); $app.data.APILastOnline = new Map();
@@ -6601,7 +6602,7 @@ speechSynthesis.getVoices();
$app.data.updateFriendInProgress = new Map(); $app.data.updateFriendInProgress = new Map();
$app.methods.updateFriend = async function (id, stateInput, origin) { $app.methods.updateFriend = function (id, stateInput, origin) {
var ctx = this.friends.get(id); var ctx = this.friends.get(id);
if (typeof ctx === 'undefined') { if (typeof ctx === 'undefined') {
return; return;
@@ -6611,9 +6612,17 @@ speechSynthesis.getVoices();
stateInput && stateInput &&
ctx.state !== stateInput && ctx.state !== stateInput &&
lastOnlineDate && lastOnlineDate &&
lastOnlineDate > Date.now() - 100 lastOnlineDate > Date.now() - 1000
) { ) {
// crappy double online fix // crappy double online fix
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'userAlreadyOnline',
stateInput
);
}
return; return;
} }
if (stateInput === 'online') { if (stateInput === 'online') {
@@ -6692,144 +6701,172 @@ speechSynthesis.getVoices();
) { ) {
API.getUser({ API.getUser({
userId: id userId: id
}).catch(() => {
this.updateFriendInProgress.delete(id);
}); });
} }
} else { } else if (
var newState = stateInput; ctx.state === 'online' &&
var location = ''; (stateInput === 'active' || stateInput === 'offline')
var $location_at = ''; ) {
if ( // delayed second check to prevent status flapping
typeof ref !== 'undefined' && var date = this.updateFriendInProgress.get(id);
typeof ref.location !== 'undefined' if (date && date > Date.now() - 120000) {
) {
var {location, $location_at} = ref;
}
// prevent status flapping
if (
ctx.state === 'online' &&
(stateInput === 'active' || stateInput === 'offline')
) {
// check if already waiting // check if already waiting
var date = this.updateFriendInProgress.get(id); if (this.debugFriendState) {
if (date && date > Date.now() - 110000) { console.log(
return; ctx.name,
} new Date().toJSON(),
this.updateFriendInProgress.set(id, Date.now()); 'pendingOfflineCheck',
// wait 2minutes then check if user came back online stateInput
await new Promise((resolve) => { );
setTimeout(resolve, 110000);
});
var date1 = this.APILastOnline.get(id);
if (date1 && date1 > Date.now() - 120000) {
this.updateFriendInProgress.delete(id);
return;
} }
return;
} }
try { this.updateFriendInProgress.set(id, Date.now());
var args = await API.getUser({ // wait 2minutes then check if user came back online
userId: id workerTimers.setTimeout(() => {
}); this.updateFriendInProgress.delete(id);
if ( var {location, $location_at} = ref;
typeof args !== 'undefined' && this.updateFriendDelayedCheck(
typeof args.ref !== 'undefined' id,
) { ctx,
newState = args.ref.state; stateInput,
ctx.ref = args.ref; isVIP,
} location,
} catch (err) { $location_at
console.error(err); );
} }, 110000);
if (ctx.state !== newState) { } else {
if ( var {location, $location_at} = ref;
typeof ctx.ref.$offline_for !== 'undefined' && this.updateFriendDelayedCheck(
ctx.ref.$offline_for === '' && id,
(newState === 'offline' || newState === 'active') && ctx,
ctx.state === 'online' stateInput,
) { isVIP,
ctx.ref.$online_for = ''; location,
ctx.ref.$offline_for = Date.now(); $location_at
var ts = Date.now(); );
var time = ts - $location_at;
var worldName = await this.getWorldName(location);
var feed = {
created_at: new Date().toJSON(),
type: 'Offline',
userId: ctx.ref.id,
displayName: ctx.ref.displayName,
location,
worldName,
time
};
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
} else if (newState === 'online') {
ctx.ref.$location_at = Date.now();
ctx.ref.$online_for = Date.now();
ctx.ref.$offline_for = '';
if (
typeof ctx.ref.location !== 'undefined' &&
ctx.ref.location !== 'offline'
) {
var {location} = ctx.ref;
}
var worldName = await this.getWorldName(ctx.ref.location);
var feed = {
created_at: new Date().toJSON(),
type: 'Online',
userId: ctx.ref.id,
displayName: ctx.ref.displayName,
location,
worldName,
time: ''
};
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
}
}
if (ctx.state === 'online') {
if (ctx.isVIP) {
removeFromArray(this.friendsGroup0_, ctx);
removeFromArray(this.friendsGroupA_, ctx);
} else {
removeFromArray(this.friendsGroup1_, ctx);
removeFromArray(this.friendsGroupB_, ctx);
}
} else if (ctx.state === 'active') {
removeFromArray(this.friendsGroup2_, ctx);
removeFromArray(this.friendsGroupC_, ctx);
} else {
removeFromArray(this.friendsGroup3_, ctx);
removeFromArray(this.friendsGroupD_, ctx);
}
if (newState === 'online') {
if (isVIP) {
this.sortFriendsGroup0 = true;
this.friendsGroup0_.push(ctx);
this.friendsGroupA_.unshift(ctx);
} else {
this.sortFriendsGroup1 = true;
this.friendsGroup1_.push(ctx);
this.friendsGroupB_.unshift(ctx);
}
} else if (newState === 'active') {
this.sortFriendsGroup2 = true;
this.friendsGroup2_.push(ctx);
this.friendsGroupC_.unshift(ctx);
} else {
this.sortFriendsGroup3 = true;
this.friendsGroup3_.push(ctx);
this.friendsGroupD_.unshift(ctx);
}
if (ctx.state !== newState) {
this.updateOnlineFriendCoutner();
}
ctx.state = newState;
ctx.name = ctx.ref.displayName;
ctx.isVIP = isVIP;
} }
this.updateFriendInProgress.delete(id); };
$app.methods.updateFriendDelayedCheck = async function (
id,
ctx,
stateInput,
isVIP,
location,
$location_at
) {
var date = this.APILastOnline.get(id);
if (
ctx.state === 'online' &&
(stateInput === 'active' || stateInput === 'offline') &&
date &&
date > Date.now() - 120000
) {
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'falsePositiveOffline',
stateInput
);
}
return;
}
var newState = stateInput;
var args = await API.getUser({
userId: id
});
newState = args.ref.state;
if (this.debugFriendState) {
console.log(
ctx.name,
new Date().toJSON(),
'updateFriendState',
newState,
stateInput
);
}
var newRef = args.ref;
if (ctx.state !== newState) {
if (
(newState === 'offline' || newState === 'active') &&
ctx.state === 'online'
) {
ctx.ref.$online_for = '';
ctx.ref.$offline_for = Date.now();
var ts = Date.now();
var time = ts - $location_at;
var worldName = await this.getWorldName(location);
var feed = {
created_at: new Date().toJSON(),
type: 'Offline',
userId: newRef.id,
displayName: newRef.displayName,
location,
worldName,
time
};
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
} else if (newState === 'online') {
ctx.ref.$location_at = Date.now();
ctx.ref.$online_for = Date.now();
ctx.ref.$offline_for = '';
var worldName = await this.getWorldName(newRef.location);
var feed = {
created_at: new Date().toJSON(),
type: 'Online',
userId: ctx.ref.id,
displayName: ctx.ref.displayName,
location: newRef.location,
worldName,
time: ''
};
this.addFeed(feed);
database.addOnlineOfflineToDatabase(feed);
}
}
if (ctx.state === 'online') {
if (ctx.isVIP) {
removeFromArray(this.friendsGroup0_, ctx);
removeFromArray(this.friendsGroupA_, ctx);
} else {
removeFromArray(this.friendsGroup1_, ctx);
removeFromArray(this.friendsGroupB_, ctx);
}
} else if (ctx.state === 'active') {
removeFromArray(this.friendsGroup2_, ctx);
removeFromArray(this.friendsGroupC_, ctx);
} else {
removeFromArray(this.friendsGroup3_, ctx);
removeFromArray(this.friendsGroupD_, ctx);
}
if (newState === 'online') {
if (isVIP) {
this.sortFriendsGroup0 = true;
this.friendsGroup0_.push(ctx);
this.friendsGroupA_.unshift(ctx);
} else {
this.sortFriendsGroup1 = true;
this.friendsGroup1_.push(ctx);
this.friendsGroupB_.unshift(ctx);
}
} else if (newState === 'active') {
this.sortFriendsGroup2 = true;
this.friendsGroup2_.push(ctx);
this.friendsGroupC_.unshift(ctx);
} else {
this.sortFriendsGroup3 = true;
this.friendsGroup3_.push(ctx);
this.friendsGroupD_.unshift(ctx);
}
if (ctx.state !== newState) {
this.updateOnlineFriendCoutner();
}
ctx.state = newState;
ctx.name = newRef.displayName;
ctx.isVIP = isVIP;
}; };
$app.methods.getWorldName = async function (location) { $app.methods.getWorldName = async function (location) {