Repository navigation
Expand file tree
/
Copy pathhelpers.php
More file actions
62 lines (49 loc) · 1.43 KB
/
Copy pathhelpers.php
File metadata and controls
62 lines (49 loc) · 1.43 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
function e(mixed $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}
function renderView(string $path, array $vars = []): string
{
extract($vars);
ob_start();
require $path;
return ob_get_clean();
}
/**
* @param array<array{id: int, queued_by: string}> $rows Queued songs ordered by their current sort.
* @return int[] Song ids in fair, round-robin order across users.
*/
function fairQueueOrder(array $rows): array
{
$songIdsGroupedByUser = [];
foreach ($rows as $row) {
$songIdsGroupedByUser[$row['queued_by']][] = $row['id'];
}
$userNames = array_keys($songIdsGroupedByUser);
shuffle($userNames);
$nextIndexByUser = array_fill_keys($userNames, 0);
$ordered = [];
while ($userNames !== []) {
foreach ($userNames as $i => $user) {
$ordered[] = $songIdsGroupedByUser[$user][$nextIndexByUser[$user]++];
if ($nextIndexByUser[$user] >= count($songIdsGroupedByUser[$user])) {
unset($userNames[$i]);
}
}
}
return $ordered;
}
function formatDuration(int $seconds): string
{
$h = intdiv($seconds, 3600);
$m = intdiv($seconds % 3600, 60);
$s = $seconds % 60;
$s = str_pad((string) $s, 2, '0', STR_PAD_LEFT);
if ($h > 0) {
$m = str_pad((string) $m, 2, '0', STR_PAD_LEFT);
return $h . ':' . $m . ':' . $s;
} else {
return $m . ':' . $s;
}
}