aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/appearance/TransitionPick.js
blob: 3a8b11ad060d13b94a4cf0920158ccef60ec217a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.pragma library

// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Which transition plays, tested by test_transitions.js under node.

// The name for a run. `pick` is a name or "random"; `pool` is what random may
// draw; `last` is what played last, skipped while there is anything else.
function choose(pick, pool, last) {
    if (pick !== "random") return pick;
    const from = pool.length > 1 ? pool.filter(n => n !== last) : pool;
    return from.length ? from[Math.floor(Math.random() * from.length)] : "burn";
}

// What was saved, made safe against `names`. Nothing saved ticks everything;
// a pool is kept in the order of `names`, so two can be compared as strings.
function clean(saved, names) {
    const s = saved && typeof saved === "object" ? saved : {};
    return {
        pick: s.pick === "random" || names.indexOf(s.pick) >= 0 ? s.pick : "burn",
        pool: Array.isArray(s.pool) ? names.filter(n => s.pool.indexOf(n) >= 0) : names,
    };
}