aboutsummaryrefslogtreecommitdiffstats
path: root/src/VideoList.hx
blob: eb3e67de97f55b7dc021a1b0903c87f93a0c6d72 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package;

import Types.VideoItem;

class VideoList {
	public var length(get, never):Int;
	public var pos(default, null) = 0;
	public var isOpen = true;

	final items:Array<VideoItem> = [];

	public function new() {}

	inline function get_length():Int {
		return items.length;
	}

	public var currentItem(get, never):Null<VideoItem>;

	inline function get_currentItem():Null<VideoItem> {
		return items[pos];
	}

	public inline function getItem(i:Int):Null<VideoItem> {
		return items[i];
	}

	public inline function setItem(i:Int, item:VideoItem):Void {
		items[i] = item;
	}

	public inline function getItems():Array<VideoItem> {
		return items;
	}

	public function setItems(items:Array<VideoItem>):Void {
		clear();
		for (item in items)
			this.items.push(item);
	}

	public function setPos(i:Int):Void {
		if (i < 0 || i > length - 1) i = 0;
		pos = i;
	}

	public function hasItem(i:Int):Bool {
		return items[i] != null;
	}

	public function exists(f:(item:VideoItem) -> Bool):Bool {
		return items.exists(f);
	}

	public function findIndex(f:(item:VideoItem) -> Bool):Int {
		var i = 0;
		for (v in items) {
			if (f(v)) return i;
			i++;
		}
		return -1;
	}

	public function addItem(item:VideoItem, atEnd:Bool):Void {
		if (atEnd) items.push(item);
		else items.insert(pos + 1, item);
	}

	public function setNextItem(nextPos:Int):Void {
		final next = items[nextPos];
		items.remove(next);
		if (nextPos < pos) pos--;
		items.insert(pos + 1, next);
	}

	public function toggleItemType(pos:Int):Void {
		items[pos].isTemp = !items[pos].isTemp;
	}

	public function removeItem(index:Int):Void {
		if (index < pos) pos--;
		items.remove(items[index]);
		if (pos >= items.length) pos = 0;
	}

	public function skipItem():Void {
		final item = items[pos];
		if (!item.isTemp) pos++;
		else items.remove(item);
		if (pos >= items.length) pos = 0;
	}

	public function itemsByUser(client:Client):Int {
		var i = 0;
		for (item in items) {
			if (item.author == client.name) i++;
		}
		return i;
	}

	public inline function clear():Void {
		items.resize(0);
		pos = 0;
	}

	public function shuffle() {
		final current = items[pos];
		items.remove(current);
		shuffleArray(items);
		items.insert(pos, current);
	}

	function shuffleArray<T>(arr:Array<T>):Void {
		for (i => a in arr) {
			final n = Std.random(arr.length);
			final b = arr[n];
			arr[i] = b;
			arr[n] = a;
		}
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage