diff --git a/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt b/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt index a7bf1d14..211ccc3f 100644 --- a/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt +++ b/android/app/src/main/java/tv/orbit/classic/ChannelSchedule.kt @@ -772,7 +772,8 @@ internal fun extendWeighted(pool: List, cursor: ScheduleCursor, from } val open = unclashed.ifEmpty { available }.let { shows -> if (stories) shows.filter(::storyFits).ifEmpty { shows } else shows } // Inside a shorts slot only the shorts that fit it take part; none fits, and the slot closes where it is. - val inSlot = slot?.let { s -> unclashed.filter { it.id != last && s.takes(start, following(it).durationMs) } }.orEmpty() + fun slotShows(s: ShortsSlot) = unclashed.filter { it.id != last && s.takes(start, following(it).durationMs) } + val inSlot = slot?.let { s -> slotShows(s).ifEmpty { if (s.stretchIfShort(start)) slotShows(s) else emptyList() } }.orEmpty() if (inSlot.isEmpty()) slot = null val clear = if (continuing == null && inSlot.isNotEmpty()) inSlot else open val rules = listOf<(PoolSeries) -> Boolean>( @@ -783,7 +784,8 @@ internal fun extendWeighted(pool: List, cursor: ScheduleCursor, from val low = eligible.minOf { tally[it.id]!! } clear.filter { it.id != last && it !in eligible }.forEach { tally[it.id] = maxOf(tally[it.id]!!, low) } val byTally = compareBy { tally[it.id]!! }.thenBy { pool.indexOf(it) } - val preferred = eligible.minWith(byTally) + // In a shorts slot, a show it hasn't had yet goes first when it's close behind in turn. + val preferred = slot?.takeIf { continuing == null && inSlot.isNotEmpty() }?.varied(eligible.sortedWith(byTally)) { it.id } ?: eligible.minWith(byTally) // Before an appointment, the eligible show that leaves it on time, then any clear one, in tally order. val appointment = if (part?.onTime == true) null else nextAppointment(start, zone, partAt = ::partAt) val fitted = if (continuing != null) null else appointment?.let { at -> fun ends(show: PoolSeries) = start + lengthOf(show) @@ -919,7 +921,8 @@ internal fun extendSchedule(pool: List, cursor: ScheduleCursor, from // A block carries on from its own last show. Starting from the channel's last show would begin each block at the same place every day. val after = if (blockRotation) blockLast[block] ?: last else last // Inside a shorts slot, the next short in turn that fits it; none fits, and the slot closes where it is. - val inSlot = slot?.let { open -> turnOrder(pool, after).firstOrNull { show -> show.id != last && allowed(show) && open.takes(start, following(show).durationMs) && !avoid(show, start, start + following(show).durationMs) } } + fun slotPick(open: ShortsSlot) = open.varied(turnOrder(pool, after).filter { show -> show.id != last && allowed(show) && open.takes(start, following(show).durationMs) && !avoid(show, start, start + following(show).durationMs) }) { it.id } + val inSlot = slot?.let { open -> slotPick(open) ?: if (open.stretchIfShort(start)) slotPick(open) else null } if (inSlot == null) slot = null val picked = inSlot ?: turnOrder(pool, after).firstOrNull { show -> when { diff --git a/android/app/src/main/java/tv/orbit/classic/Shorts.kt b/android/app/src/main/java/tv/orbit/classic/Shorts.kt index 05bdc91a..522bbb27 100644 --- a/android/app/src/main/java/tv/orbit/classic/Shorts.kt +++ b/android/app/src/main/java/tv/orbit/classic/Shorts.kt @@ -24,6 +24,8 @@ import java.time.ZoneId internal const val SHORTS_MAX_MS = 8 * 60_000L internal const val SHORTS_SLOT_MIN_MS = 15 * 60_000L internal const val SHORTS_OVERRUN_MS = 2 * 60_000L +/** How far down the turn order a slot looks for a show it hasn't had yet, before taking a show back. */ +internal const val SHORTS_VARIETY_TURNS = 1 /** A channel's shorts slots: what each is called at a given time. */ internal class ShortsRule(val label: (startMs: Long, zone: ZoneId) -> String) @@ -35,8 +37,21 @@ internal val ToonZShorts = ShortsRule { _, _ -> "Cartoon Shorts" } internal fun isShort(durationMs: Long) = durationMs in 1..SHORTS_MAX_MS /** One slot being filled: when it aims to end, and the tag its shorts carry. */ -internal class ShortsSlot(val startMs: Long, val targetMs: Long, val label: String) { +internal class ShortsSlot(val startMs: Long, var targetMs: Long, val label: String) { val tag get() = shortsTag(startMs, label) + /** The shows already in this slot: a show comes back only when no other short fits (Paul, 6 November 2026: variety first). */ + val shows = mutableSetOf() + /** + * Variety without jumping the queue: [first] in turn order stands, unless it's already in the slot and a show that isn't + * is close behind it (within [SHORTS_VARIETY_TURNS] places of [ordered]). + */ + fun varied(ordered: List, id: (T) -> String): T? = ordered.firstOrNull()?.let { first -> + if (id(first) !in shows) first else ordered.take(SHORTS_VARIETY_TURNS + 1).firstOrNull { id(it) !in shows } ?: first } + /** + * A slot still under [SHORTS_SLOT_MIN_MS] that can't fit its next short by its target aims for the half hour after + * instead, rather than closing as a fragment with another slot straight after it. + */ + fun stretchIfShort(atMs: Long): Boolean = (atMs - startMs < SHORTS_SLOT_MIN_MS).also { if (it) targetMs += 30 * 60_000L } /** Whether a short of [durationMs] starting at [atMs] belongs in this slot. */ fun takes(atMs: Long, durationMs: Long) = isShort(durationMs) && atMs < targetMs && atMs + durationMs <= targetMs + SHORTS_OVERRUN_MS } @@ -80,6 +95,7 @@ internal fun shortsBlocks(entries: List): List = bui internal fun shortsTagFor(rule: ShortsRule?, slot: ShortsSlot?, series: PoolSeries, episode: PoolEpisode, inBlock: Boolean, startMs: Long, zone: ZoneId): Pair { if (rule == null || series.isFilm || !inBlock || !isShort(episode.durationMs)) return "" to null val open = slot?.takeIf { it.takes(startMs, episode.durationMs) } ?: openShortsSlot(rule, startMs, zone) + open.shows += series.id return open.tag to open } diff --git a/android/app/src/test/java/tv/orbit/classic/ShortsReplayTest.kt b/android/app/src/test/java/tv/orbit/classic/ShortsReplayTest.kt index d4b1f14f..8b594e34 100644 --- a/android/app/src/test/java/tv/orbit/classic/ShortsReplayTest.kt +++ b/android/app/src/test/java/tv/orbit/classic/ShortsReplayTest.kt @@ -89,12 +89,19 @@ class ShortsReplayTest { val loose = after.count { isShort(it.media.durationMs) && it.group.isEmpty() } val lengths = blocks.map { it.endMs - it.startMs }.sorted() val repeats = blocks.sumOf { b -> b.entries.zipWithNext().count { (a, c) -> a.seriesId == c.seriesId } } + assertEquals("${programming.name}: no show twice in a row in a slot", 0, repeats) + val returns = blocks.sumOf { b -> b.entries.size - b.entries.map { it.seriesId }.distinct().size } + val couldVary = blocks.count { b -> b.entries.size > b.entries.map { it.seriesId }.distinct().size } + val shortSlots = blocks.filter { it.endMs - it.startMs < SHORTS_SLOT_MIN_MS } out.append("\n${programming.name} ($number)\n") out.append(" Programmes booked: ${before.size} before, ${after.size} after; shorts among them: $shortsBefore before, $shortsAfter after\n") out.append(" Shorts slots: ${blocks.size}, holding ${blocks.sumOf { it.entries.size }} shorts; $loose short(s) left on their own (a slot of one, or the block changed)\n") if (lengths.isNotEmpty()) out.append(" Slot length: shortest ${mins(lengths.first())} min, median ${mins(lengths[lengths.size / 2])}, longest ${mins(lengths.last())}; " + "${blocks.count { onHalfHour(it.endMs) }} of ${blocks.size} end within 2 min of :00 or :30\n") - out.append(" Shows per slot: average ${"%.1f".format(blocks.map { it.shows.size }.average())}; the same show twice in a row inside a slot: $repeats time(s)\n") + out.append(" Shows per slot: average ${"%.1f".format(blocks.map { it.shows.size }.average())}; the same show twice in a row inside a slot: $repeats time(s); a show back later in the same slot: $returns time(s), in $couldVary slot(s)\n") + out.append(" Slots under 15 minutes: ${shortSlots.size}${if (shortSlots.isEmpty()) "" else ": " + shortSlots.take(6).joinToString { blk -> + val next = after.firstOrNull { it.startMs == blk.endMs } + "${clock(blk.startMs)} ${mins(blk.endMs - blk.startMs)} min, then ${next?.let(::name) ?: "?"} (${next?.let { mins(it.media.durationMs) } ?: "?"} min)" }}\n") out.append(" Programmes starting within 2 min of :00 or :30: ${before.count { onHalfHour(it.startMs) }} of ${before.size} before, ${after.count { onHalfHour(it.startMs) }} of ${after.size} after\n") out.append(" Labels: ${blocks.groupingBy { it.label }.eachCount()}\n") val airtime = { run: List -> run.groupBy(::name).mapValues { (_, v) -> v.sumOf { it.media.durationMs } / 60_000 } }