-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (191 loc) · 8.28 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const _CMD_PREFIX = '/coord';
const _ECHO_PREFIX = '/e';
const _MAX_LINES_PER_MACRO = 15;
let lastClickedButton = null;
window.onload = () => {
var warnDiv = document.getElementById('warn');
document.getElementById('echoMarksAndFlags').addEventListener('change', function() {
var isChecked = this.checked;
['includeZoneName', 'includeMarkName'].forEach((id) => {
var checkbox = document.getElementById(id);
checkbox.disabled = isChecked;
if (isChecked)
checkbox.checked = false;
});
});
['includeZoneName', 'includeMarkName'].forEach((id) => {
document.getElementById(id).addEventListener('change', function() {
var isOtherChecked = document.getElementById(id === 'includeZoneName' ? 'includeMarkName' : 'includeZoneName').checked;
var echoMarksAndFlags = document.getElementById('echoMarksAndFlags');
echoMarksAndFlags.disabled = this.checked || isOtherChecked;
if (this.checked || isOtherChecked) {
echoMarksAndFlags.disabled = true;
warnDiv.style.color = 'lightgrey';
echoMarksAndFlags.checked = false;
} else {
warnDiv.style.color = 'red';
echoMarksAndFlags.disabled = false;
}
});
});
}
function generateMacro() {
clearOutput();
linesPerMacro = document.getElementById('linesPerMacro').value ?? _MAX_LINES_PER_MACRO;
const regex = {
siren: /\(Maybe:\s*(?<mark>[^)]+)\)\s*.(?<zone>[\w\'\- ]+)\s*\(\s*(?<x>[0-9\.]+)\s*,\s*(?<y>[0-9\.]+)\s*\)/,
prime: /(?<mark>[^(]+)\s+\((?<x>[0-9\.]+)\s*,\s*(?<y>[0-9\.]+)\s*\)/,
primeZoneOnly: /^(?<zone>[\w\'\- ]+)$/,
bear: /(?<zone>[\w\'\- ]+)\s+\(\s*(?<x>[0-9\.]+)\s*,\s*(?<y>[0-9\.]+)\s*\)\s*(?<mark>[\w\'\- ]+)/,
}
const scoutSelect = document.querySelector('input[name="tool"]:checked');
if (!scoutSelect) {
displayError('Must select scouting tool.');
return;
}
// Turtle uses the same export format as PrimeHunts
const scoutSrc = scoutSelect.value === 'turtle' ? 'prime' : scoutSelect.value;
const scoutRegex = regex[scoutSrc];
if (!scoutRegex) {
displayError('Invalid scout source');
return;
}
const scoutMarks = document.getElementById('marks').value.split('\n');
if (scoutMarks.length < 2) {
displayError('Not enough marks (min. 2)');
return;
}
const echoMarksAndFlags = document.getElementById('echoMarksAndFlags').checked;
const includeZoneName = document.getElementById('includeZoneName').checked;
const includeMarkName = document.getElementById('includeMarkName').checked;
let trainName = document.getElementById('trainName').value;
trainName = trainName === '' ? 'My Train' : trainName;
const trainHeader = `/e ${trainName.toUpperCase()} `
const output = [trainHeader];
let zoneName = '';
let zoneMarkCount = 0;
let zoneMarkCoordCache = [];
let zoneMarkNameCache = [];
// TODO: Add support for multiple instances (need to confirm output format from scout tools).
for (const mark of scoutMarks) {
if (mark.length < 3) // probably a newline
continue;
// if using prime, new zones will appear on a line by themselves
// so we need to use a different regex to detect the change in zone,
// and based on that, dump the previous zone's cached marks into `output`.
if (scoutSrc === 'prime') {
const zoneTestMatch = mark.match(regex.primeZoneOnly);
if (zoneTestMatch) {
// this could be the beginning of a new zone, or it could be
// an inadvertent header row that was copied (e.g. 'Endwalker').
// If MarkCount=0, that means we cached no marks for the last zone, so just
// set the new zone and move on. If we do have cached marks, process the prior zone.
if (zoneMarkCount > 0) {
const zoneHeader = getHeaderLine(zoneName, includeZoneName, zoneMarkNameCache, includeMarkName);
if (zoneHeader)
output.push(zoneHeader);
output.push(...zoneMarkCoordCache);
}
zoneMarkCount = 0;
zoneMarkCoordCache = [];
zoneMarkNameCache = [];
zoneName = zoneTestMatch.groups.zone;
continue;
}
}
const match = mark.match(scoutRegex);
const markName = (match?.groups?.mark ?? '').trim();
const x = match?.groups?.x;
const y = match?.groups?.y;
const nonPrimeZoneName = match?.groups?.zone;
// handle output of previous zone here for non-prime scouts
if (
scoutSrc !== 'prime' &&
zoneName !== '' &&
nonPrimeZoneName &&
zoneName !== nonPrimeZoneName
) {
const zoneHeader = getHeaderLine(zoneName, includeZoneName, zoneMarkNameCache, includeMarkName);
if (zoneHeader)
output.push(zoneHeader);
output.push(...zoneMarkCoordCache);
zoneMarkCount = 0;
zoneMarkCoordCache = [];
zoneMarkNameCache = [];
}
if (scoutSrc !== 'prime')
zoneName = nonPrimeZoneName;
if (!markName || !x || !y || !zoneName) {
displayError(`Invalid mark format: ${mark}. Is the correct scouting tool selected?`);
continue;
}
zoneMarkCount++;
zoneMarkNameCache.push(markName);
zoneMarkCoordCache.push(`${_CMD_PREFIX} ${x} ${y} : ${zoneName}`);
if (echoMarksAndFlags)
zoneMarkCoordCache.push(`${_ECHO_PREFIX} ${markName} <flag>`);
}
// push the final zone's marks, since the loop has ended
if (zoneMarkCount > 0) {
const zoneHeader = getHeaderLine(zoneName, includeZoneName, zoneMarkNameCache, includeMarkName);
if (zoneHeader)
output.push(zoneHeader);
output.push(...zoneMarkCoordCache);
}
displayMacro(output);
}
function getHeaderLine(zoneName, includeZone, markNames, includeMarks) {
if (!includeZone && !includeMarks)
return;
let headerEcho = `${_ECHO_PREFIX} `;
if (includeZone)
headerEcho += includeMarks ? `${zoneName}: ` : `${zoneName} `;
if (includeMarks)
headerEcho += `[${markNames.join(' → ')}] `;
return headerEcho;
}
function displayMacro(output) {
if (output.length <= 1) {
displayError('No macro generated.');
return;
}
document.getElementById('macroHeader').style.visibility = 'visible';
document.getElementById('macroContainer').innerHTML = getFormattedMacro(output);
}
function getFormattedMacro(output) {
let brokenOutput = '';
for (let i = 0; i < output.length; i++) {
if (i % linesPerMacro === 0) {
if (i !== 0) {
brokenOutput += '</div>';
}
brokenOutput += `<div class="macroText"><button class="copyButton" onclick="copyToClipboard(this)">Copy to Clipboard</button>`;
}
brokenOutput += output[i];
if ((i + 1) % linesPerMacro !== 0 || i !== output.length - 1)
brokenOutput += '<br />\n';
}
brokenOutput += '</div>';
return brokenOutput;
}
function copyToClipboard(button) {
if (lastClickedButton)
lastClickedButton.innerHTML = 'Copy to Clipboard';
let text = button.parentNode.textContent.replace('Copy to Clipboard', '');
navigator.clipboard.writeText(text).then(function () {
button.innerHTML = '<span style="color:green;">✔</span> Copied!';
lastClickedButton = button;
}, function (err) {
displayError('Could not copy text: ', err);
});
}
function clearOutput() {
document.getElementById('errorText').innerHTML = '';
document.getElementById('macroContainer').innerHTML = '';
document.getElementById('errorHeader').style.visibility = 'hidden';
document.getElementById('macroHeader').style.visibility = 'hidden';
}
function displayError(msg) {
document.getElementById('errorHeader').style.visibility = 'visible';
document.getElementById('errorText').innerHTML += `Error: ${msg}<br />`;
}