Skip to content

Commit

Permalink
Document queue vars & fix rare failure case
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 29, 2024
1 parent 0e2de41 commit 3602234
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@ namespace ButterSTT.MessageSystem
{
public class MessageQueue
{
/// <summary>
/// The maximum length of the message. Default is 144.
/// </summary>
public int MessageLength = 144;

/// <summary>
/// The maximum number of words to dequeue at once, regardless of their expiration time. Default is 6.
/// </summary>
public int MaxWordsDequeued = 6;

/// <summary>
/// The number of characters to allow between the current paragraph and the message length. Default is 36.
/// </summary>
public int RealtimeQueuePadding = 36;

/// <summary>
/// The amount of time before a word will expire.
/// </summary>
public TimeSpan WordTime = TimeSpan.FromSeconds(3);

public Paragraph CurParagraph;
Expand Down Expand Up @@ -100,10 +116,15 @@ public void QueueParagraphToFit(int padding = 0)

private void ProgressWordQueue()
{
// Make sure there is enough room to fit a new word in the message
// Make sure there is enough room to fit a new word in the message,
// if the word is too long then just give up and pass it in anyways
// TODO: Maybe handle long words better? Or maybe it's not worthwhile
while (
WordQueue.TryPeek(out var newWord)
&& CurMessageLength + newWord.Length < MessageLength
&& (
CurMessageLength + newWord.Length <= MessageLength
|| newWord.Length > MessageLength
)
)
{
var word = WordQueue.Dequeue();
Expand Down Expand Up @@ -136,10 +157,7 @@ public string GetCurrentMessage()
}

if (CurParagraph.Length >= MessageLength)
{
// Queue with a padding of the max words times the average word length
QueueParagraphToFit(MaxWordsDequeued * 6);
}
QueueParagraphToFit(RealtimeQueuePadding);

ProgressWordQueue();

Expand Down

0 comments on commit 3602234

Please sign in to comment.