Skip to content

Commit

Permalink
Fix sorting bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Jan 1, 2025
1 parent 352998e commit d173538
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
1 change: 0 additions & 1 deletion friend.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ async function SortRoles() {
role: r.role.id,
position: positionCount,
});
console.log(positionCount, r.role.name, r.score);
positionCount++;
}
await guild.roles.setPositions(rolePositions);
Expand Down
43 changes: 19 additions & 24 deletions huddles.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,23 @@ async function UpdateHuddles() {

// A comparator for Discord rooms. Controls the sort order of the rooms.
function CompareRooms(a, b) {
// Never sort rooms that have a parent.
if (a.parent || b.parent) {
return 0;
}
// Empty rooms sort down.
if (a.members.size === 0 && b.members.size > 0) {
if (a.memberCount === 0 && b.memberCount > 0) {
return 1;
}
if (a.members.size > 0 && b.members.size === 0) {
if (a.memberCount > 0 && b.memberCount === 0) {
return -1;
}
// Friend rooms sort down.
const aIsFriendRoom = a.id in fc.friendRoomCache;
const bIsFriendRoom = b.id in fc.friendRoomCache;
if (aIsFriendRoom && !bIsFriendRoom) {
if (a.ownerScore === 0 && b.ownerScore > 0) {
return 1;
}
if (!aIsFriendRoom && bIsFriendRoom) {
if (a.ownerScore > 0 && b.ownerScore === 0) {
return -1;
}
// Tie-breaker between friend rooms is rank.
if (aIsFriendRoom && bIsFriendRoom) {
const aid = fc.friendRoomCache[a.id];
const bid = fc.friendRoomCache[b.id];
const au = UserCache.GetCachedUserByCommissarId(aid);
const bu = UserCache.GetCachedUserByCommissarId(bid);
if (au && !bu) {
return -1;
}
if (!au && bu) {
return 1;
}
return parseFloat(b.rank_score) - parseFloat(a.rank_score);
if (a.ownerScore > 0 && b.ownerScore) {
return b.ownerScore - a.ownerScore;
}
// Should all other criteria fail to break the tie, then alphabetic ordering is the last resort.
return a.name.localeCompare(b.name);
Expand All @@ -105,15 +89,26 @@ async function SortVoiceRooms() {
const sortableChannels = [];
for (const [id, channel] of allChannels) {
if (channel.type === 2 && !channel.parent) {
sortableChannels.push(channel);
let ownerScore = 0;
if (channel.id in fc.friendRoomCache) {
const ownerId = fc.friendRoomCache[channel.id];
const cu = UserCache.GetCachedUserByCommissarId(ownerId);
ownerScore = parseFloat(cu.rank_score);
}
sortableChannels.push({
channel,
memberCount: channel.members.size,
name: channel.name,
ownerScore,
});
}
}
sortableChannels.sort(CompareRooms);
const channelPositions = [];
let positionCount = 0;
for (const channel of sortableChannels) {
channelPositions.push({
channel: channel.id,
channel: channel.channel.id,
position: positionCount,
});
positionCount++;
Expand Down

0 comments on commit d173538

Please sign in to comment.