-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
110 lines (91 loc) · 4.34 KB
/
script.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
const blueprintTemplate = {"blueprint":{"description": "Generated by: combe15.github.io/factorio-text-to-constant-combinator","icons":[{"signal":{"type":"item","name":"constant-combinator"},"index":1}],"entities":[],"item":"blueprint","version":281479274823680}}
const entityTemplate = {"entity_number":1,"name":"constant-combinator","position":{"x":0.5,"y":0.5},"control_behavior":{"filters":[{"signal":{"type":"virtual","name":"signal-A"},"count":1,"index":1}]}}
const conversionTable = {'A':"signal-A",'B':"signal-B",'C':"signal-C",'D':"signal-D",'E':"signal-E",'F':"signal-F",'G':"signal-G",'H':"signal-H",'I':"signal-I",'J':"signal-J",'K':"signal-K",'L':"signal-L",'M':"signal-M",'N':"signal-N",'O':"signal-O",'P':"signal-P",'Q':"signal-Q",'R':"signal-R",'S':"signal-S",'T':"signal-T",'U':"signal-U",'V':"signal-V",'W':"signal-W",'X':"signal-X",'Y':"signal-Y",'Z':"signal-Z",'0':"signal-0",'1':"signal-1",'2':"signal-2",'3':"signal-3",'4':"signal-4",'5':"signal-5",'6':"signal-6",'7':"signal-7",'8':"signal-8",'9':"signal-9",'.':"signal-dot",'?':"signal-dot",'!':"signal-dot"};
function convertText() {
var wordArray = wordwrap()
let blueprintJSON = JSON.parse(JSON.stringify(blueprintTemplate))
for (let row = 0; row < wordArray.length; row++) {
for (let column = 0; column < wordArray[row].length; column++) {
currentEntity = JSON.parse(JSON.stringify(entityTemplate))
currentEntity.entity_number = column+1
currentEntity.position.x = column+0.5
currentEntity.position.y = row+0.5
if (wordArray[row][column].toUpperCase() in conversionTable) {
currentEntity.control_behavior.filters[0].signal.name = conversionTable[wordArray[row][column].toUpperCase()]
}
else if (wordArray[row][column] === ' ') {
continue;
}
else {
delete currentEntity.control_behavior
}
blueprintJSON.blueprint["entities"].push(currentEntity)
}
}
compressJSON(blueprintJSON)
}
function copyBlueprint() {
/* Get the text field */
var copyText = document.getElementById("blueprintOutput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
navigator.clipboard.writeText(copyText.value);
}
function compressJSON (blueprintJSON) {
var json = JSON.stringify(blueprintJSON);
console.log("json", json)
var enc = new TextEncoder("utf-8").encode(json);
console.log("enc", enc);
var data = pako.deflate(enc, {level: 9});
console.log("zip", data);
var encoded = toBase64(data);
console.log(encoded)
document.getElementById("blueprintOutput").value = "0" + encoded;
}
function toBase64(buf) {
var binaryString = Array.prototype.map.call(buf, function (ch) {
return String.fromCharCode(ch);
}).join('');
return btoa(binaryString);
}
function syncLengthBox() {
document.getElementById("wrapLengthBox").value = document.getElementById("wrapLengthRange").value
}
function syncLengthRange() {
document.getElementById("wrapLengthRange").value = document.getElementById("wrapLengthBox").value
}
function wordwrap() {
wrapValue = document.getElementById("wrapLengthBox").value
var text = document.getElementById("inputText").value
if (wrapValue === 0) {
return text.split("\n");
}
if (text.length === 0) {
text = document.getElementById("inputText").placeholder
}
var textSplit = text.split("\n")
var textArray = []
var finalArray = [[]]
var rowCounter = 0
for (let i = 0; i < textSplit.length; i++){
textArray.push(textSplit[i].split(" "))
}
for (let arrayNum = 0; arrayNum < textArray.length; arrayNum++) {
for (let wordNum = 0; wordNum < textArray[arrayNum].length; wordNum++) {
finalArray[rowCounter] += textArray[arrayNum][wordNum] + " "
if (textArray[arrayNum][wordNum+1] === undefined) {
break
}
else if (finalArray[rowCounter].length + textArray[arrayNum][wordNum+1].length > wrapValue) {
rowCounter++
finalArray[rowCounter] = []
}
}
rowCounter++
finalArray[rowCounter] = []
}
console.log(finalArray)
return finalArray;
}