Skip to content

Commit

Permalink
Fix wait balance when it subtracts from playtime
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcooper committed Aug 9, 2024
1 parent 2944bfc commit c341833
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
40 changes: 29 additions & 11 deletions client/src/main/Player.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@
)
}
const rebalanceWaits = () => {
if ($config.WAIT_INTERVAL_SUBTRACTS_FROM_STOPSET_PLAYTIME) {
// Avoid active waits and the first wait
for (let i = 1; i < items.length; i++) {
const stopset = items[i - 1]
const wait = items[i]
if (!wait.active && wait.type === "wait" && stopset.type === "stopset") {
wait.duration = Math.max(
$config.WAIT_INTERVAL - stopset.remainingAfterQueuedSkips,
$config.WAIT_INTERVAL_SUBTRACTS_FROM_STOPSET_PLAYTIME_MIN_LENGTH
)
updateUI()
}
}
}
}
const addStopset = () => {
// Prepend a full wait interval IF:
// There's nothing in the items list, or the previous item is a stopset
Expand All @@ -90,17 +107,9 @@
// Always add a stopset AND THEN a wait interval
items.push(generatedStopset)
if ($config.WAIT_INTERVAL > 0) {
let duration = $config.WAIT_INTERVAL
if ($config.WAIT_INTERVAL_SUBTRACTS_FROM_STOPSET_PLAYTIME) {
duration = Math.max(
duration - generatedStopset.duration,
$config.WAIT_INTERVAL_SUBTRACTS_FROM_STOPSET_PLAYTIME_MIN_LENGTH
)
}
if (duration > 0) {
items.push(new Wait(duration, doneWaiting, updateUI))
}
items.push(new Wait($config.WAIT_INTERVAL, doneWaiting, updateUI))
}
rebalanceWaits()
} else {
console.warn("Couldn't generate a stopset!")
}
Expand Down Expand Up @@ -218,6 +227,10 @@
}
}
if (success) {
rebalanceWaits()
}
updateUI()
messageServer("ack-action", {
connection_id,
Expand Down Expand Up @@ -246,6 +259,7 @@
generatedStopset.loadAudio()
items[nextStopset].done(true, true) // Mark swap out one as done and don't log
items[nextStopset] = generatedStopset
rebalanceWaits()
updateUI()
}
}
Expand All @@ -265,6 +279,7 @@
} else {
console.warn(`Item at index ${index} is not a stopset. Can't regenerate!`)
}
rebalanceWaits()
updateUI()
}
Expand Down Expand Up @@ -296,7 +311,9 @@
if (stopset.destroyed) {
alert(`Stop set ${stopset.name} no longer active in the playlist. Can't perform swap!`, "warning")
} else {
if (!stopset.swapAsset(subindex, asset, swapAsset.rotator)) {
if (stopset.swapAsset(subindex, asset, swapAsset.rotator)) {
rebalanceWaits()
} else {
alert(`Asset in stop set ${stopset.name}'s index ${subindex + 1} can no longer be swapped.`, "warning")
}
updateUI()
Expand Down Expand Up @@ -421,6 +438,7 @@
{addStopset}
{processItem}
{regenerateStopsetAsset}
{rebalanceWaits}
{showSwapUI}
{pause}
{skip}
Expand Down
2 changes: 2 additions & 0 deletions client/src/main/player/List.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
export let skip
export let regenerateStopsetAsset
export let showSwapUI
export let rebalanceWaits
export let numStopsetsToDisableAddMoreAt
Expand Down Expand Up @@ -217,6 +218,7 @@
skip() // Skip as usual if item is active
} else {
asset.queueForSkip = !asset.queueForSkip
rebalanceWaits()
}
}}
><Icon
Expand Down
3 changes: 3 additions & 0 deletions client/src/stores/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ export class GeneratedStopset {
get remaining() {
return this.playableItems.reduce((s, item) => s + item.remaining, 0)
}
get remainingAfterQueuedSkips() {
return this.playableItems.filter((item) => !item.queueForSkip).reduce((s, item) => s + item.remaining, 0)
}

get playableNonErrorItems() {
return this.playableItems.filter((item) => !item.error)
Expand Down

0 comments on commit c341833

Please sign in to comment.