forked from yusitnikov/fpuzzles-fow-bulb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpuzzles-fow-bulb.user.js
105 lines (86 loc) · 3.18 KB
/
fpuzzles-fow-bulb.user.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
// ==UserScript==
// @name Fpuzzles-FOW
// @version 1.2
// @description Adds the Fog of War Bulb constraint to f-puzzles.
// @author Chameleon
// @updateURL https://github.com/yusitnikov/fpuzzles-fow-bulb/raw/main/fpuzzles-fow-bulb.user.js
// @match https://*.f-puzzles.com/*
// @match https://f-puzzles.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(() => {
const id = 'fogofwarbulb';
const name = 'Fog of War Bulb';
const doShim = () => {
const origExportPuzzle = exportPuzzle;
exportPuzzle = function(includeCandidates) {
const compressed = origExportPuzzle(includeCandidates);
const puzzle = JSON.parse(compressor.decompressFromBase64(compressed));
if (puzzle[id]) {
if (puzzle[id].length) {
puzzle.fogofwar = {cells: puzzle[id].map(({cell}) => cell)};
}
delete puzzle[id];
}
return compressor.compressToBase64(JSON.stringify(puzzle));
}
const origImportPuzzle = importPuzzle;
importPuzzle = function(string, clearHistory) {
const puzzle = JSON.parse(compressor.decompressFromBase64(string));
if (puzzle.fogofwar) {
if (puzzle.fogofwar.cells && puzzle.fogofwar.cells.length) {
puzzle[id] = puzzle.fogofwar.cells.map(cell => ({cell}));
}
delete puzzle.fogofwar;
}
string = compressor.compressToBase64(JSON.stringify(puzzle));
origImportPuzzle(string, clearHistory);
}
const origDrawConstraints = drawConstraints;
drawConstraints = (...args) => {
origDrawConstraints(...args);
for (const c of constraints[id] || []) {
c.show();
}
}
window[id] = function(cells) {
if (cells && cells.length) {
this.cell = cells[0];
}
this.show = () => {
if (!this.cell) {
return;
}
ctx.save();
const size = 0.7;
ctx.translate(this.cell.x + cellSL / 2, this.cell.y + cellSL / 2);
ctx.font = (cellSL * 0.8 * size) + 'px Arial';
ctx.fillText('💡', 0, cellSL * 0.3 * size);
ctx.restore();
};
};
const origCategorizeTools = categorizeTools;
categorizeTools = () => {
origCategorizeTools();
toolConstraints.push(name);
perCellConstraints.push(name);
oneCellAtATimeTools.push(name);
tools.push(name);
};
if (window.boolConstraints) {
window.onload();
}
};
const intervalId = setInterval(() => {
if (typeof grid === 'undefined' ||
typeof exportPuzzle === 'undefined' ||
typeof importPuzzle === 'undefined' ||
typeof drawConstraints === 'undefined' ||
typeof categorizeTools === 'undefined') {
return;
}
clearInterval(intervalId);
doShim();
}, 16);
})();