Skip to content

Instantly share code, notes, and snippets.

@camelgod
Last active February 14, 2023 18:10
Show Gist options
  • Save camelgod/a712ce7d43f21260ae5b4971b9a58dfb to your computer and use it in GitHub Desktop.
Save camelgod/a712ce7d43f21260ae5b4971b9a58dfb to your computer and use it in GitHub Desktop.
[DUMMY CODE, NOT TESTED] Utopiah BestRoom script with scenes
// Warning this is pretty hacky as you need to change the URL for the room to end with the scene ID. Maybe there is a better way to query the scene ID based on the url, but the spoke only allows to set URL so this is what I imagined late in the evening.
// open-media-button.js
} else if (await isHubsRoomUrl(this.src)) {
// Get the 6 last letters of the name of the url (that you set up as the Scene ID)
let lastSix = this.src.substr(this.src.length - 6); // Should be something like "avC5IO"
// use modified Utopiah script
let bestRoom = getBestRoom(lastSix);
await exitImmersive();
location.href = bestRoom; // because getBestRoom returns the bestRoom.url already
} else {
// Wherever you want, probably somewhere above the code in the same open-media-button, or in its own file (remember to import it!)
// Get the URL of a Hubs room that is not empty nor at room capacity
let getBestRoom = async (sceneID) => {
// by https://twitter.com/jamesckane for @paracreative worked on the #ApartPosters show.
// let endpoint = "https://show.apartposters.com/api/v1/media/search?source=rooms&filter=public";
let endpoint = "/api/v1/media/search?source=rooms&filter=public";
let response = await fetch(endpoint);
let json = await response.json();
let rooms = json.entries;
let filteredRooms = rooms.filter(function(sceneID) {
return rooms.scene_id = sceneID;
});
let bestRoom = {};
filteredRooms.forEach(room => {
if (Object.keys(bestRoom).length === 0) bestRoom = room;
let memberCount = room.member_count;
let lobbyCount = room.lobby_count;
let totalCount = memberCount + lobbyCount;
let buffer = 5;
let roomCapacity = room.room_size - buffer;
if ((totalCount >= 0 && totalCount < roomCapacity)) {
bestRoom = room;
}
});
return bestRoom.url;
// window.location.href = bestRoom.url; // to directly redirect and balance your server load
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment