forked from ynoproject/forest-orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends.js
105 lines (87 loc) · 4.31 KB
/
friends.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
let playerFriendsCache = [];
let pendingOfflineFriendUuids = [];
function updatePlayerFriends() {
if (loginToken)
sendSessionCommand('pf');
else
onUpdatePlayerFriends([]);
}
function onUpdatePlayerFriends(playerFriends) {
const friendsPlayerList = document.getElementById('friendsPlayerList');
const friendsPlayerListScrollTop = friendsPlayerList.scrollTop;
const oldPlayerUuids = Array.from(friendsPlayerList.querySelectorAll('.listEntry')).map(e => e.dataset.uuid);
const removedPlayerUuids = oldPlayerUuids.filter(uuid => !playerFriends.find(m => m.uuid === uuid));
let newIncomingCount = 0;
for (let playerUuid of removedPlayerUuids)
removePlayerListEntry(friendsPlayerList, playerUuid);
Array.from(friendsPlayerList.querySelectorAll('.listEntryCategoryHeader')).map(h => h.remove());
for (let playerFriend of playerFriends) {
const uuid = playerFriend.uuid;
const oldFriendData = playerFriendsCache.find(m => m.uuid === uuid);
if (oldFriendData) {
const pendingOfflineFriendIndex = pendingOfflineFriendUuids.indexOf(uuid);
if (playerFriend.online !== oldFriendData.online) {
if (playerFriend.online) {
if (pendingOfflineFriendIndex > -1)
pendingOfflineFriendUuids.splice(pendingOfflineFriendIndex, 1);
else
showFriendsToastMessage('playerOnline', 'friend', playerFriend);
} else
pendingOfflineFriendUuids.push(uuid);
} else if (!playerFriend.online) {
if (pendingOfflineFriendIndex > -1) {
showFriendsToastMessage('playerOffline', 'friend', playerFriend);
pendingOfflineFriendUuids.splice(pendingOfflineFriendIndex, 1);
}
}
} else if (!playerFriend.accepted && playerFriend.incoming) {
showFriendsToastMessage('incoming', 'friend', playerFriend, true);
newIncomingCount++;
}
if (playerFriend.badge === 'null')
playerFriend.badge = null;
globalPlayerData[playerFriend.uuid] = {
name: playerFriend.name,
systemName: playerFriend.systemName,
rank: playerFriend.rank,
account: playerFriend.account,
badge: playerFriend.badge || null,
medals: playerFriend.medals
};
}
playerFriendsCache = playerFriends || [];
for (let playerFriend of playerFriends) {
const entry = addOrUpdatePlayerListEntry(friendsPlayerList, playerFriend, true);
entry.classList.toggle('offline', playerFriend.accepted && !playerFriend.online);
entry.dataset.categoryId = playerFriend.accepted ? playerFriend.online ? 'online' : 'offline' : playerFriend.incoming ? 'incoming' : 'outgoing';
addOrUpdatePlayerListEntryLocation(true, playerFriend, entry);
}
sortPlayerListEntries(friendsPlayerList);
[ 'incoming', 'outgoing', 'online', 'offline' ].forEach(c => updatePlayerListEntryHeader(friendsPlayerList, 'friends', c));
friendsPlayerList.scrollTop = friendsPlayerListScrollTop;
if (!playerFriendsCache.length)
document.getElementById('incomingFriendRequestCountContainer').classList.add('hidden');
const playersTabFriends = document.getElementById('playersTabFriends');
const incomingFriendRequestCountContainer = document.getElementById('incomingFriendRequestCountContainer');
const incomingFriendRequestCountLabel = incomingFriendRequestCountContainer.querySelector('.notificationCountLabel');
if (incomingFriendRequestCountContainer.classList.contains('hidden'))
incomingFriendRequestCountLabel.textContent = '0';
let incomingCount = parseInt(incomingFriendRequestCountLabel.textContent) + newIncomingCount;
if (incomingCount) {
incomingFriendRequestCountContainer.classList.toggle('hidden', !incomingCount);
if (newIncomingCount)
playersTabFriends.classList.toggle('unread', !!incomingCount);
incomingFriendRequestCountLabel.textContent = incomingCount < 9 ? incomingCount : `${incomingCount}+`;
}
}
function showFriendsToastMessage(key, icon, player, persist) {
if (!notificationConfig.friends.all || !notificationConfig.friends[key])
return;
let message = getMassagedLabel(localizedMessages.toast.friends[key], true);
if (player)
message = message.replace('{PLAYER}', getPlayerName(player, true, false, true));
showToastMessage(message, icon, true, null, persist);
}
(function () {
addSessionCommandHandler('pf', args => onUpdatePlayerFriends(JSON.parse(args[0]) || []));
})();