Skip to main content

Request queue

A request queue next to the catalogue browser. Press + next to an item in the browser to queue it, or click an item, then Queue. An ST Queue on the browser's object holds the requests, and the queue video player plays the first one. Each row has a remove button: players can remove their own request, hosts and moderators any (the queue enforces this). Clear works only for hosts and moderators.

Each entry's key is the item's row in the browser's table, and its text is the item's video name. The names come from a second ST Data Table, videoNames, with one name per row (a tab-separated file without a header). Items without a video name can't be queued. Keeping the link in the entry's text means the video player script doesn't need the catalogue tables.

SongQueue.cs
using StageTime.SDK;
using TMPro;
using UnityEngine.UI;

/// <summary>
/// A request queue display over an <see cref="STQueue"/> on the same GameObject. "Queue" adds the item selected
/// in the <see cref="SongBrowser"/>, and each browser row's + adds that row's item. Each queue row's X removes that
/// entry: players can remove their own request, hosts and moderators any (the queue enforces this). Clear is for hosts
/// and moderators only. Each entry's key is the item's row and its text the item's video name from
/// <see cref="videoNames"/> (one name per row), which a <see cref="QueueVideoPlayer"/> on the video player turns
/// into the link of the first entry.
/// </summary>
public class SongQueue : StageTimeBehaviour
{
public SongBrowser browser;
public TMP_Text status;
public TMP_Text[] rows;
public Button clearButton;
public STDataTable videoNames;

private STQueue queue;

private void Start()
{
queue = GetComponent<STQueue>();
Show();
}

public void QueueSelected()
{
RequestItem(browser.selectedRow);
}

public void Add0() => Add(0);
public void Add1() => Add(1);
public void Add2() => Add(2);
public void Add3() => Add(3);
public void Add4() => Add(4);
public void Add5() => Add(5);
public void Add6() => Add(6);
public void Add7() => Add(7);

private void Add(int line)
{
RequestItem(browser.RowOnPage(line));
}

private void RequestItem(int row)
{
if (row < 0) return;
string video = row < videoNames.rowCount ? videoNames.GetString(row, 0).Trim() : "";
if (video.Length == 0 || video == "-")
{
status.text = videoNames.isLoaded ? "No video for " + browser.ItemName(row) : "Still loading the video list...";
return;
}
queue.Request(row, video);
}

public void Remove0() => queue.Remove(0);
public void Remove1() => queue.Remove(1);
public void Remove2() => queue.Remove(2);
public void Remove3() => queue.Remove(3);
public void Remove4() => queue.Remove(4);
public void Remove5() => queue.Remove(5);
public void Remove6() => queue.Remove(6);
public void Remove7() => queue.Remove(7);

public void ClearQueue() => queue.Clear();

public override void OnQueueChanged() => Show();

private bool IsManager()
{
StageTimePlayer me = Networking.LocalPlayer;
return me.isRoomHost || me.isModerator;
}

private void Show()
{
StageTimePlayer me = Networking.LocalPlayer;
bool manager = IsManager();
clearButton.interactable = manager;

int mine = queue.IndexOf(me);
string roles = (me.isRoomHost ? " host" : "") + (me.isModerator ? " moderator" : "") + (me.isInstanceOwner ? " owner" : "");
status.text = queue.count + "/" + queue.capacity + " queued" + (mine >= 0 ? " | you are #" + (mine + 1) : "")
+ "\nYou:" + (roles.Length > 0 ? roles : " guest") + (manager ? " | you can remove any request" : " | you can remove your own request");
for (int i = 0; i < rows.Length; i++)
{
if (i >= queue.count)
{
rows[i].text = "";
continue;
}
StageTimePlayer player = queue.GetPlayer(i);
string who = player != null ? player.displayName : "?";
rows[i].text = (i == 0 ? "<b>Now:</b> " : (i + 1) + ". ") + browser.ItemName(queue.GetKey(i)) + " <alpha=#99>(" + who + ")";
}
}
}