jukebox
All repositories: gitoria
3.1 KB
// lib/queue.hl — THE SHUFFLED QUEUE, kept on the server. A shuffle never sends its songs to the browser: the browser holds a small context `{ kind: 'shuffle', seed, spec }`// and asks for the next items by index. The server makes the order from the seed (the same seed gives the same order, so a restarted server or a second tab finds the same queue),// every song of the pool comes once before any comes again (a new round gets a new order from seed + round), and the last orders are kept in memory.// spec = { kind: 'favourites', minPlays } (every song played at least N times, file count + jukebox count)// | { kind: 'list', ctx } (the list a screen shows: songs with a minimum, an album, an artist's songs, a search)import { floor } from 'hl:math'import { orderList, songsPage, searchAll } from './browse.hl'import { cache } from './state.hl'static keepOrders = 6static roundStep = 7919// the ids of a pool, in list orderstatic poolFor = (spec) => {out = []if (spec.kind == 'favourites') {for (r of orderList('title')) {if (r.pc >= spec.minPlays) { out.push(r.id) }}return out}ctx = spec.ctxif (ctx.kind == 'songs') {min = ctx.minPlays == null ? 0 : ctx.minPlaysfor (r of orderList('title')) {if (r.pc >= min) { out.push(r.id) }}return out}let off = 0let total = 1while (off < total) {let page = nullif (ctx.kind == 'album') { page = songsPage('artist', off, 200, null, null, ctx.album) }else if (ctx.kind == 'artist-songs') { page = songsPage('artist', off, 200, null, ctx.artist, null) }else if (ctx.kind == 'search') { page = searchAll(ctx.q, off, 200).songs }else { return out }total = page.totalif (page.items.length == 0) { break }for (s of page.items) { out.push(s.id) }off = off + page.items.length}return out}// the pool in a random order made from the seed (Fisher-Yates with a Park-Miller generator: numbers stay far below 2^53)static shuffled = (spec, seed) => {ids = poolFor(spec)let x = seed % 2147483646 + 1let i = ids.length - 1while (i > 0) {x = x * 48271 % 2147483647j = floor(x / 2147483647 * (i + 1))t = ids[i]ids[i] = ids[j]ids[j] = ti = i - 1}return ids}static orderKey = (spec, seed) => { return seed + ':' + JSON.stringify(spec) }// the order of one round, from the memory or made nowstatic roundOrder = (spec, seed) => {k = orderKey(spec, seed)q = cache.queues[k]if (q != null) { return q }ids = shuffled(spec, seed)qs = cache.queuesqs[k] = idslet names = cache.queueNamesnames.push(k)if (names.length > keepOrders) {old = names[0]names = names.slice(1)qs[old] = null}cache.queues = qscache.queueNames = namesreturn ids}// the song id at a place of the endless queue (round after round), or null when the pool is emptystatic idAt = (ctx, index) => {first = roundOrder(ctx.spec, ctx.seed)n = first.lengthif (n == 0) { return null }round = floor(index / n)ids = round == 0 ? first : roundOrder(ctx.spec, ctx.seed + round * roundStep)return ids[index - round * n]}// how many songs the pool holdsstatic poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed).length }
Branches
Latest commits
- d56e5054Sync theme and shared tokens from layouts 17e0f03mre
- b1362547Re-vendor to Hybriel set day35mre
- 89e10f08Records: mission 002 rollback wordingmre
- 9485dee7Records: mission 002 brief (live step)mre
- 9ba68fe7Records: S1b reviewedmre
- 193dfff1Records: S1 result, S1b startedmre
- 028ed776Records: mission 001 continuation S1bmre
- 1838d7e8Records: S1 startedmre
- a43fca3fRecords: mission 001 decided pointsmre
- 41773828Records: statusmre
- a82c0805Records: mission 001 brief (Style form)mre
- a4bce6d1Records: ticket push subscriptionmre
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 0433e9a8jukebox: records: gitoria remote added and pushedmre
- 1722d707jukebox: records: repo creation handed to Gitoriamre
- b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
- 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
- b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
- 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
- 06c548d6jukebox JB15: report, log linemre