-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
209 lines (176 loc) · 6.28 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="Online tool to artfully visualize Factorio Blueprints.">
<title>Factorio Blueprint Visualizer</title>
<link rel='shortcut icon' type='image/x-icon' href='website/favicon.ico'/>
<script src="https://cdn.jsdelivr.net/pyodide/v0.23.1/full/pyodide.js"></script>
<script defer data-domain="piebro.github.io/factorio-blueprint-visualizer" src="https://plausible.io/js/plausible.js"></script>
</head>
<style>
img, svg {
width:auto;
height:auto;
max-width: 92vw;
max-height: 86vh;
margin: auto;
display: block;
padding-top: 10px;
}
.button-container {
text-align: center;
float:center;
}
.btn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.btn:hover{
background-color: #3e8e41;
}
</style>
<script>
async function setup_pyodide(){
pyodide = await loadPyodide({indexURL: "https://cdn.jsdelivr.net/pyodide/v0.23.1/pyc/"});
await pyodide.loadPackage(['numpy', 'micropip']);
await pyodide.runPythonAsync(`
import micropip
await micropip.install('website/factorioBlueprintVisualizer-1.1.0-py2.py3-none-any.whl')
import factorioBlueprintVisualizer as bv
settings_list = [bv.EXAMPLE_SETTINGS]
current_setting_index = 0
`)
}
async function get_init_blueprint(params){
const blueprint_id = params.get("source").split("/").at(-1);
const cors_proxy_url = "https://api.allorigins.win/get?url="
const init_blueprint_json = await fetch(cors_proxy_url + encodeURIComponent("https://www.factorio.school/api/blueprintString/" + blueprint_id)).then(r => r.json());
return init_blueprint_json["contents"]
}
function get_current_blueprint_entity_count(){
pyodide.runPython(`
current_blueprint_entity_count = {}
for e in current_blueprint["entities"]:
if e["name"] not in current_blueprint_entity_count:
current_blueprint_entity_count[e["name"]] = 0
else:
current_blueprint_entity_count[e["name"]] += 1
current_blueprint_entity_count = sorted(current_blueprint_entity_count.items(), key=lambda item: item[1], reverse=True)
`);
console.log(pyodide.globals.get('current_blueprint_entity_count').toJs())
}
function new_blueprint_string(blueprint_string){
pyodide.runPython(`
blueprint_names, blueprint_jsons = bv.get_blueprint_list(` + blueprint_string + `)
current_blueprint = bv.get_blueprint(blueprint_jsons[0])
`);
get_current_blueprint_entity_count()
blueprint_names = pyodide.globals.get('blueprint_names').toJs();
prev_selection = document.getElementById('blueprint_selection');
if (prev_selection != null) {
prev_selection.remove();
}
if (blueprint_names.length > 1){
selection = document.createElement("select")
selection.classList.add("btn")
selection.setAttribute("id", "blueprint_selection")
selection.setAttribute("style", "width: 160px;")
selection.setAttribute("onChange", "blueprintSelectionOnChange(this)")
for (i in blueprint_names){
option = document.createElement("option")
option.setAttribute("value", i)
option.innerHTML = blueprint_names[i]
selection.appendChild(option)
}
document.getElementById("button-container").appendChild(selection)
}
}
async function setup(){
let params = (new URL(document.location)).searchParams;
if (params.has("source") && params.get("source").substring(0,28) == "https://www.factorio.school/"){
let [, init_blueprint] = await Promise.all([setup_pyodide(), get_init_blueprint(params)]);
new_blueprint_string("\"" + init_blueprint + "\"")
} else {
const elem = document.createElement("img");
elem.setAttribute("src", "website/start.svg");
document.getElementById("svg-container").appendChild(elem);
await setup_pyodide();
new_blueprint_string("bv.EXAMPLE_BLUEPRINT")
}
draw_blueprint()
document.getElementById("uploadBtn").onclick = uploadBtnClick
document.getElementById("uploadBtn").innerText = "Upload Blueprint"
document.getElementById("saveBtn").onclick = saveBtnClick
document.getElementById("saveBtn").innerText = "Save"
document.getElementById("nextSettingBtn").onclick = nextSetting
document.getElementById("nextSettingBtn").innerText = "Random Drawing Setting"
document.addEventListener('keyup', onKeyUp);
console.log("finished setup")
}
function draw_blueprint(){
document.getElementById("svg-container").innerHTML = pyodide.runPython(`
bv.draw_blueprint(current_blueprint, settings_list[current_setting_index], svg_width_in_mm=300)
`)
}
async function saveBtnClick(){
svg_str = document.getElementById("svg-container").innerHTML
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(svg_str));
element.setAttribute('download', "blueprint.svg");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function blueprintSelectionOnChange(selectObject){
pyodide.runPython("current_blueprint = bv.get_blueprint(blueprint_jsons[" + selectObject.value + "])");
draw_blueprint()
}
function uploadBtnClick(){
input = window.prompt("paste blueprint here")
if (input != null && input != ""){
new_blueprint_string("\"" + input + "\"")
draw_blueprint()
}
}
function nextSetting(){
pyodide.runPython(`
current_setting_index += 1
if len(settings_list) == current_setting_index:
settings_list.append(bv.get_random_settings())
`)
draw_blueprint()
}
function previousSetting(){
pyodide.runPython(`
if current_setting_index > 0:
current_setting_index -= 1
`)
draw_blueprint()
}
function onKeyUp(e){
if (e.code == "ArrowRight"){
nextSetting()
} else if (e.code == "ArrowLeft"){
previousSetting()
}
}
settings_list = []
current_setting_index = 0
pyodide = null
</script>
<body onload="setup()">
<div class="button-container" id="button-container">
<button class="btn" id="nextSettingBtn">Loading</button>
<button class="btn" id="saveBtn">Loading</button>
<button class="btn" id="uploadBtn">Loading</button>
<button class="btn" id="infoBtn" onclick="location.href='https://github.com/piebro/factorio-blueprint-visualizer';">Infos</button>
</div>
<div class="svg-container" id="svg-container"></div>
</body>
</html>