-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.js
178 lines (147 loc) · 4.19 KB
/
tiles.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
let selected = undefined;
let to = undefined;
let domlistener = false;
let latest_board = undefined;
var observeDOM = (function () {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
return function (objs, callback) {
if (MutationObserver) {
// define a new observer
var mutationObserver = new MutationObserver(callback);
// have the observer observe foo for changes in children
for (const o of objs) {
mutationObserver.observe(o, { childList: true, subtree: true, attributes: true });
}
return mutationObserver;
}
// browser support fallback
else if (window.addEventListener) {
for (const o of objs) {
o.addEventListener("DOMNodeInserted", callback, false);
o.addEventListener("DOMNodeRemoved", callback, false);
}
}
};
})();
Object.defineProperty(Object.prototype, "board", {
set: x => {
this._board = x;
latest_board = x;
const nl = x[0].layers.length;
const iter = () => {
console.log(latest_board);
console.log("WS", selected);
const match = (i, j, blacklist) => {
if (i === j) return [false, undefined];
let ok = false;
let matches = [];
for (let k = 0; k < nl; k++) {
if (
latest_board[i].layers[k].id === latest_board[j].layers[k].id &&
!latest_board[i].layers[k].hidden &&
!latest_board[j].layers[k].hidden
) {
const id = latest_board[i].layers[k].id;
if (blacklist && blacklist.indexOf(id) >= 0) continue;
matches.push(id);
ok = true;
}
}
return [ok, matches];
};
const nhidden = layers => layers.reduce((a, c) => a + c.hidden, 0);
const greaterthan = (a, b, cutoff) => {
// prioritize making it to the cutoff
if (a >= cutoff) {
return a > b;
} else if (b >= cutoff) {
return false;
} else {
// otherwise, prioritize most clears
return a % 1 > b % 1;
}
};
const cutoff = 5;
const eval = (ind, visited, blacklist) => {
if (visited.length > cutoff) {
return 0;
} else {
let max = 0;
let imax = -1;
let idmax = [];
for (let i = 0; i < 30; i++) {
if (visited.indexOf(i) >= 0) continue;
let cur = 0;
const [ok, ids] = match(i, ind, blacklist);
if (ok) {
cur += eval(i, [...visited, ind], [...blacklist, ...ids])[0] + 1;
if (nhidden(latest_board[i].layers) === nl - ids.length) {
cur += 2.002;
}
if (nhidden(latest_board[ind].layers) === nl - ids.length) {
cur += 0.001;
}
}
if (greaterthan(cur, max, cutoff - visited.length)) {
max = cur;
imax = i;
idmax = ids;
}
}
return [max, imax, idmax];
}
};
let best = -1;
let bestid = -1;
let bestlinks = [];
if (selected === undefined) {
for (let i = 0; i < 30; i++) {
// none selected; ensure nonempty
if (nhidden(latest_board[i].layers) === nl) continue;
const [max, imax, idmax] = eval(i, [], []);
if (greaterthan(max, best, cutoff)) {
best = max;
bestid = imax;
bestlinks = idmax;
}
}
} else {
const [bestscore, bestnext, bestnextlinks] = eval(selected, [], []);
best = bestscore;
bestid = bestnext;
bestlinks = bestnextlinks;
}
console.log("B", best, bestid);
if (bestid < 0) {
console.log("Abort");
return;
}
console.log("C", bestid);
if (selected === undefined || nhidden(latest_board[bestid].layers) < nl - bestlinks.length) {
selected = bestid;
} else {
selected = undefined;
}
const tiles = document.getElementsByClassName("tls-tile");
tiles[bestid].click();
};
if (!domlistener) {
domlistener = true;
setTimeout(() => {
const els = document.getElementsByClassName("tls-tile");
for (let i = 0; i < 30; i++) {
observeDOM([els[i].children[0], els[i].children[2]], e => {
console.log("U", i);
clearTimeout(to);
to = setTimeout(() => {
iter();
}, 10);
});
}
iter();
}, 2000);
}
},
get: () => this._board,
configurable: true,
});