|
|
|
@@ -1,5 +1,10 @@
|
|
|
|
|
package tv.orbit.classic
|
|
|
|
|
|
|
|
|
|
/** Local hours, wrapping past midnight, and the titles a channel may use then. No titles means everything in its pool. */
|
|
|
|
|
internal data class DayPart(val fromHour: Int, val toHour: Int, val titles: List<String> = emptyList()) {
|
|
|
|
|
fun covers(hour: Int) = if (fromHour <= toHour) hour in fromHour until toHour else hour >= fromHour || hour < toHour
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** What makes a channel itself. The engine below knows nothing about any particular channel. */
|
|
|
|
|
internal data class ChannelProgramming(val number: Int, val name: String, val searches: List<String>, val seriesCount: Int = 3,
|
|
|
|
|
/** Set for a film channel: how long before a film may air again, as long as the pool allows it. */
|
|
|
|
@@ -7,7 +12,8 @@ internal data class ChannelProgramming(val number: Int, val name: String, val se
|
|
|
|
|
/** A show that finishes waits until every show has finished, so nothing repeats while others have new episodes. */
|
|
|
|
|
val finishTogether: Boolean = false,
|
|
|
|
|
/** Only the exact title counts, for channels whose short names could match something unrelated. */
|
|
|
|
|
val exactTitles: Boolean = false) {
|
|
|
|
|
val exactTitles: Boolean = false,
|
|
|
|
|
val dayParts: List<DayPart> = emptyList()) {
|
|
|
|
|
/** Changes when the list does, so the episode cache is read again. What's already committed stays. */
|
|
|
|
|
val signature get() = "${searches.joinToString("|")}#$seriesCount"
|
|
|
|
|
}
|
|
|
|
@@ -16,9 +22,14 @@ internal val SmartChannels = listOf(
|
|
|
|
|
ChannelProgramming(102, "After Dark", listOf("The Thing", "The Evil Dead", "Re-Animator", "Hellraiser", "Videodrome", "An American Werewolf in London",
|
|
|
|
|
"Halloween", "Day of the Dead", "They Live", "Escape from New York", "The Fog", "Near Dark", "The Lost Boys", "Fright Night", "Society", "Tremors",
|
|
|
|
|
"Basket Case", "Phantasm", "Repo Man", "Donnie Darko", "The Wicker Man", "Suspiria", "Eraserhead", "Blue Velvet", "From Dusk Till Dawn", "Scream",
|
|
|
|
|
"Poltergeist", "The Howling", "Creepshow", "Prince of Darkness"), seriesCount = 40, filmRestMs = 48 * 60 * 60_000L),
|
|
|
|
|
"Poltergeist", "The Howling", "Creepshow", "Prince of Darkness"), seriesCount = 40, filmRestMs = 48 * 60 * 60_000L,
|
|
|
|
|
dayParts = listOf(DayPart(21, 5), DayPart(5, 21, listOf("The 'Burbs", "'Til Death Do Us Part", "Tremors", "The Lost Boys", "Fright Night", "Poltergeist", "Scream",
|
|
|
|
|
"Escape from New York", "They Live", "Repo Man", "Donnie Darko", "From Dusk Till Dawn", "Creepshow", "Near Dark", "Halloween", "The Fog", "Blue Velvet",
|
|
|
|
|
"The Wicker Man", "An American Werewolf in London", "Prince of Darkness")))),
|
|
|
|
|
ChannelProgramming(103, "Vortex", listOf("Dragon Ball Z", "Batman Beyond", "Superman: The Animated Series", "Naruto", "Gundam Wing", "Yu Yu Hakusho", "Samurai Jack"),
|
|
|
|
|
seriesCount = 6, finishTogether = true),
|
|
|
|
|
seriesCount = 6, finishTogether = true,
|
|
|
|
|
dayParts = listOf(DayPart(6, 18, listOf("Batman Beyond", "Superman: The Animated Series", "Samurai Jack")),
|
|
|
|
|
DayPart(18, 6, listOf("Dragon Ball Z", "Naruto", "Yu Yu Hakusho", "Gundam Wing")))),
|
|
|
|
|
ChannelProgramming(140, "Terra", listOf("The Blue Planet", "Blue Planet II", "Planet Earth", "Planet Earth II", "Planet Earth III", "Frozen Planet", "Frozen Planet II",
|
|
|
|
|
"Life on Earth", "The Living Planet", "The Trials of Life", "Our Planet", "Our Planet II", "Dynasties", "Dynasties II", "Seven Worlds, One Planet", "A Perfect Planet",
|
|
|
|
|
"Hostile Planet", "Africa", "Wild China", "South Pacific", "Great Barrier Reef", "The Hunt", "The Life of Mammals", "The Life of Birds", "Nature's Great Events", "Green Planet", "Prehistoric Planet"),
|
|
|
|
@@ -53,6 +64,17 @@ internal const val SCHEDULE_KEEP_PAST_MS = 24 * 60 * 60_000L
|
|
|
|
|
internal fun nextEpisode(series: PoolSeries, last: EpisodeMark?): PoolEpisode =
|
|
|
|
|
last?.let { mark -> series.episodes.firstOrNull { it.mark > mark } } ?: series.episodes.first()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* What a programme starting at [startMs] may use, by the local hour then, so the clocks changing moves blocks with the wall clock.
|
|
|
|
|
* A block with nothing in the pool leaves the whole pool, so a channel never goes off air for lack of a title.
|
|
|
|
|
*/
|
|
|
|
|
internal fun dayPartPool(pool: List<PoolSeries>, parts: List<DayPart>, startMs: Long, zone: java.time.ZoneId): List<PoolSeries> {
|
|
|
|
|
val hour = java.time.Instant.ofEpochMilli(startMs).atZone(zone).hour
|
|
|
|
|
val titles = parts.firstOrNull { it.covers(hour) }?.titles.orEmpty().map(::seriesKey).toSet()
|
|
|
|
|
if (titles.isEmpty()) return pool
|
|
|
|
|
return pool.filter { seriesKey(it.title) in titles }.ifEmpty { pool }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The next show after [last] in the rota that [eligible] allows. */
|
|
|
|
|
internal fun nextInTurn(pool: List<PoolSeries>, last: String?, eligible: (PoolSeries) -> Boolean): PoolSeries {
|
|
|
|
|
val after = pool.indexOfFirst { it.id == last } + 1
|
|
|
|
@@ -70,13 +92,14 @@ internal fun nextFilm(pool: List<PoolSeries>, shown: Map<String, Long>, startMs:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Films by their rest period, back to back from [fromMs] until [untilMs] is covered. */
|
|
|
|
|
internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, restMs: Long): ChannelPlan {
|
|
|
|
|
internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, restMs: Long,
|
|
|
|
|
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan {
|
|
|
|
|
val shown = cursor.filmShown.toMutableMap()
|
|
|
|
|
val entries = mutableListOf<ScheduleEntry>()
|
|
|
|
|
var last = cursor.lastSeriesId
|
|
|
|
|
var start = fromMs
|
|
|
|
|
while (start < untilMs) {
|
|
|
|
|
val film = nextFilm(pool, shown, start, restMs)
|
|
|
|
|
val film = nextFilm(dayPartPool(pool, dayParts, start, zone), shown, start, restMs)
|
|
|
|
|
val copy = film.episodes.first()
|
|
|
|
|
val item = SampleItem(film.title, "Movie", copy.durationMs / 60_000, "", copy.id, copy.durationMs)
|
|
|
|
|
entries += ScheduleEntry(film.id, copy.mark, ScheduledMedia(item, copy.sourceId, copy.durationMs), start)
|
|
|
|
@@ -88,16 +111,19 @@ internal fun extendFilmSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Shows take turns, one episode each, back to back from [fromMs] until [untilMs] is covered. */
|
|
|
|
|
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false): ChannelPlan {
|
|
|
|
|
internal fun extendSchedule(pool: List<PoolSeries>, cursor: ScheduleCursor, fromMs: Long, untilMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
|
|
|
|
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan {
|
|
|
|
|
require(pool.isNotEmpty() && pool.all { it.episodes.isNotEmpty() })
|
|
|
|
|
if (filmRestMs != null) return extendFilmSchedule(pool, cursor, fromMs, untilMs, filmRestMs)
|
|
|
|
|
if (filmRestMs != null) return extendFilmSchedule(pool, cursor, fromMs, untilMs, filmRestMs, dayParts, zone)
|
|
|
|
|
val marks = cursor.marks.toMutableMap()
|
|
|
|
|
val entries = mutableListOf<ScheduleEntry>()
|
|
|
|
|
var last = cursor.lastSeriesId
|
|
|
|
|
var start = fromMs
|
|
|
|
|
while (start < untilMs) {
|
|
|
|
|
if (finishTogether && pool.all { marks[it.id] == it.episodes.last().mark }) marks.clear()
|
|
|
|
|
val series = nextInTurn(pool, last) { !finishTogether || marks[it.id] != it.episodes.last().mark }
|
|
|
|
|
val available = dayPartPool(pool, dayParts, start, zone)
|
|
|
|
|
// Shows only wait for others in the same block, or a daytime show could stand idle for an evening one.
|
|
|
|
|
if (finishTogether && available.all { marks[it.id] == it.episodes.last().mark }) available.forEach { marks.remove(it.id) }
|
|
|
|
|
val series = nextInTurn(pool, last) { it in available && (!finishTogether || marks[it.id] != it.episodes.last().mark) }
|
|
|
|
|
val episode = nextEpisode(series, marks[series.id])
|
|
|
|
|
val media = ScheduledMedia(scheduledItem(series.title, episode), episode.sourceId, episode.durationMs)
|
|
|
|
|
entries += ScheduleEntry(series.id, episode.mark, media, start)
|
|
|
|
@@ -128,12 +154,13 @@ internal fun airedCursor(base: ScheduleCursor, entries: List<ScheduleEntry>, now
|
|
|
|
|
* Only ever appends. Programmes already committed stay as they are, apart from dropping old ones.
|
|
|
|
|
* With no usable plan it starts from now at the aired position.
|
|
|
|
|
*/
|
|
|
|
|
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false): ChannelPlan? {
|
|
|
|
|
internal fun topUpPlan(existing: ChannelPlan?, aired: ScheduleCursor, pool: List<PoolSeries>?, nowMs: Long, filmRestMs: Long? = null, finishTogether: Boolean = false,
|
|
|
|
|
dayParts: List<DayPart> = emptyList(), zone: java.time.ZoneId = java.time.ZoneId.systemDefault()): ChannelPlan? {
|
|
|
|
|
val kept = existing?.let { plan -> plan.copy(entries = plan.entries.dropWhile { it.endMs < nowMs - SCHEDULE_KEEP_PAST_MS }) }
|
|
|
|
|
if (kept != null && kept.endMs >= nowMs + SCHEDULE_TOP_UP_MS) return kept
|
|
|
|
|
if (pool.isNullOrEmpty()) return kept
|
|
|
|
|
val from = if (kept != null && kept.endMs > nowMs) kept.endMs else nowMs
|
|
|
|
|
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether)
|
|
|
|
|
val added = extendSchedule(pool, existing?.tail ?: aired, from, nowMs + SCHEDULE_AHEAD_MS, filmRestMs, finishTogether, dayParts, zone)
|
|
|
|
|
return ChannelPlan(kept?.entries.orEmpty() + added.entries, added.tail)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|