IT
apps/winzik / src / Model / Stats.php · commit 6576d6f
<?phpnamespace App\Model;/** * What the user thinks of each song: how often it was listened to (heard past half of it, or 4 minutes), how often it * was skipped (changed before a quarter), when it was last heard, and the stars given (1..5, 0: none). Kept in * var/stats.json, apart from the settings (resetting the options does not erase them). * * Two marks come of it: the listening mark (0..100: listened often and rarely skipped = high) and the preference * (the stars first, the listening mark next): the play list can be sorted by it. */final class Stats{    /** @var array<string, array{p: int, s: int, l: int, r: int}> per song (its full path) */    public array $songs = [];    private bool $dirty = false;    public function __construct(private string $file)    {        // written by WinZik::writeJsonFile(): a Latin-1 file name (not UTF-8) comes back as it was        $data = \App\Core\WinZik::readJsonFile($file);        if (is_array($data)) {            foreach ($data as $path => $v) {                if (is_string($path) && is_array($v)) {                    $this->songs[$path] = ['p' => (int)($v['p'] ?? 0), 's' => (int)($v['s'] ?? 0), 'l' => (int)($v['l'] ?? 0), 'r' => max(0, min(5, (int)($v['r'] ?? 0)))];                }            }        }    }    /** @return array{p: int, s: int, l: int, r: int} */    private function of(string $path): array    {        return $this->songs[$path] ?? ['p' => 0, 's' => 0, 'l' => 0, 'r' => 0];    }    /** Heard (past half of it, or 4 minutes). */    public function listened(string $path): void    {        $s = $this->of($path);        $s['p']++;        $s['l'] = time();        $this->songs[$path] = $s;        $this->dirty = true;    }    /** Changed before a quarter of it. */    public function skipped(string $path): void    {        $s = $this->of($path);        $s['s']++;        $this->songs[$path] = $s;        $this->dirty = true;    }    /** The stars given (0: none). */    public function rate(string $path, int $stars): void    {        $s = $this->of($path);        $s['r'] = max(0, min(5, $stars));        $this->songs[$path] = $s;        $this->dirty = true;    }    public function rating(string $path): int    {        return $this->of($path)['r'];    }    public function plays(string $path): int    {        return $this->of($path)['p'];    }    public function skips(string $path): int    {        return $this->of($path)['s'];    }    /**     * The listening mark, 0..100: the share of the times it was kept to the end (a song skipped every time: low), worth     * more the more it was heard (10 listens: its full weight). Never heard: 0.     */    public function score(string $path): int    {        $s = $this->of($path);        if ($s['p'] + $s['s'] === 0) {            return 0;        }        $kept = ($s['p'] + 1) / ($s['p'] + $s['s'] + 2);        return (int)round(100 * $kept * (0.35 + 0.65 * min(1.0, $s['p'] / 10)));    }    /** The order of "par préférence": the stars first, then the listening mark, then the last heard. */    public function preference(string $path): float    {        $s = $this->of($path);        return $s['r'] * 1000 + $this->score($path) + min(0.99, $s['l'] / 1e10);    }    public function save(): void    {        if (!$this->dirty) {            return;        }        // a name that is not UTF-8 kept (b64:); a value JSON cannot hold never empties the file (tried again later)        if (\App\Core\WinZik::writeJsonFile($this->file, $this->songs, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) {            $this->dirty = false;        }    }}