forked from Berny23/LD-ToyPad-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
306 lines (279 loc) · 10.9 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!--
Copyright © 2021 Berny23
This file is part of "ToyPad Emulator for Lego Dimensions" which is released under the "MIT" license.
See file "LICENSE" or go to "https://choosealicense.com/licenses/mit" for full license details.
-->
<html>
<header>
<style>
html {
text-align: center;
}
input {
margin-bottom: 1em;
font-size: 1.2em;
}
button {
margin-top: 1em;
padding: 1em;
font-weight: bold;
}
.container {
width: fit-content;
margin: auto;
background-color: aliceblue;
box-shadow: 0 4px 8px 0 rgb(0 0 0 / 20%), 0 6px 20px 0 rgb(0 0 0 / 19%);
padding: 2em;
margin-bottom: 1.5em;
}
#active-tokens>div {
padding: 0.5em;
margin: auto;
margin-bottom: 0.5em;
background-color: blanchedalmond;
}
#active-tokens>.removeable {
margin-bottom: -0.5em;
padding: 1em;
}
#active-tokens>.removeable:hover {
background-color: burlywood;
}
#warnings {
color: ghostwhite;
background-color: darkslateblue;
padding: 0.5em;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</header>
<body>
<div>
<h1>LD ToyPad Emulator</h1>
<p id="warnings">
→ Please don't spam click, or things may break and you'll have to restart the server! ←<br>
After changing a position, you have to wait for about 2 seconds, otherwise the game can't keep up.<br>
If nothing works anymore, stop the emulator via ssh by pressing Ctrl + C and restart it.<br>
Make sure not to exceed the item limit on each position when switching or placing objects (left/right: 3, middle: 1)!!!
</p>
<form id="character-select" class="container">
<label>
Choose a character:
<input list="character-list" id="character-name" />
<datalist id="character-list"></datalist>
</label>
<br>
Position:
<input type="radio" id="character_pos2" name="position" value="2" checked />
<label for="character_pos2">Left</label>
<input type="radio" id="character_pos1" name="position" value="1" />
<label for="character_pos1">Middle</label>
<input type="radio" id="character_pos3" name="position" value="3" />
<label for="character_pos3">Right</label>
<br>
<button type="submit">Place Character</button>
</form>
<form id="vehicle-select" class="container">
<label>
Choose a vehicle:
<input list="vehicle-list" id="vehicle-name" />
<datalist id="vehicle-list"></datalist>
</label>
<br>
Position:
<input type="radio" id="vehicle_pos2" name="position" value="2" checked />
<label for="vehicle_pos2">Left</label>
<input type="radio" id="vehicle_pos1" name="position" value="1" />
<label for="vehicle_pos1">Middle</label>
<input type="radio" id="vehicle_pos3" name="position" value="3" />
<label for="vehicle_pos3">Right</label>
<br>
<button type="submit">Place Vehicle</button>
</form>
<div class="container">
<h2>Currently Placed</h2>
<p>Click an entry to remove it from the ToyPad.</p>
<div id="active-tokens">
</div>
<button id="remove-all" disabled>Remove all</button>
</div>
</div>
<script>
$(function() {
//const index_max = 7;
//var availableIndices = [0, 1, 2, 3, 4, 5, 6];
const MAX_INDICES = 8;
const MAX_LEFT = 3;
const MAX_RIGHT = 3;
const MAX_MIDDLE = 1;
var availableIndices = [1, 2, 3, 4, 5, 6, 7];
function filterById(jsonObject, id) {return jsonObject.filter(function(jsonObject) {return (jsonObject['id'] == id);})[0];}
function filterByName(jsonObject, name) {return jsonObject.filter(function(jsonObject) {return (jsonObject['name'] == name);})[0];}
function nameValid(jsonObject, name) {
var found = false;
$.each(jsonObject, function(i, item) {
if (item.name == name) {
found = true;
return;
}
});
return found;
}
function checkPositionLimits() {
// Count all objects in the 3 positions and disable radio buttons if needed:
if ($('#active-tokens').find('input[type="radio"][value="2"]:checked').length >= MAX_LEFT) $('body').find('input[type="radio"][value="2"]').attr('disabled', 'disabled'); else $('body').find('input[type="radio"][value="2"]').removeAttr('disabled');
if ($('#active-tokens').find('input[type="radio"][value="1"]:checked').length >= MAX_MIDDLE) $('body').find('input[type="radio"][value="1"]').attr('disabled', 'disabled'); else $('body').find('input[type="radio"][value="1"]').removeAttr('disabled');
if ($('#active-tokens').find('input[type="radio"][value="3"]:checked').length >= MAX_RIGHT) $('body').find('input[type="radio"][value="3"]').attr('disabled', 'disabled'); else $('body').find('input[type="radio"][value="3"]').removeAttr('disabled');
}
var characters;
$.getJSON('https://raw.githubusercontent.com/Berny23/node-ld/master/data/charactermap.json', function(data) {
characters = data;
}).done(function() {
$.each(characters, function(i, item) {
if(item.name != 'Unknown') $('#character-list').append('<option value="' + item.name + '">');
});
});
var vehicles;
$.getJSON('https://raw.githubusercontent.com/Berny23/node-ld/master/data/tokenmap.json', function(data) {
vehicles = data;
}).done(function() {
$.each(vehicles, function(i, item) {
if(item.name != 'unknown') $('#vehicle-list').append('<option value="' + item.name + '">');
});
});
$('#character-select').submit(function(e) {
e.preventDefault();
if ($('input[name="position"]:checked', '#character-select').attr('disabled')) {
alert("This position is already full, please reposition or remove your active characters/vehicles below!");
return;
}
var name = $('#character-name').val();
var position = $('input[name="position"]:checked', '#character-select').val();
var index = availableIndices[0];
if (nameValid(characters, name)) {
if (availableIndices.length > 0) {
$.ajax({
method: 'POST',
contentType: 'application/json',
url: '/character',
data: JSON.stringify({ id: filterByName(characters, name).id, position: position, index: index })
}).done(function(uid) {
$('#active-tokens').append('<div class="removeable" id="' + uid + '" data-type="character" data-index="' + index + '">' + name + '</div>');
$('#active-tokens').append('<div><input type="radio" id="' + uid + '_pos2" name="' + uid + '" value="2" /><label for="' + uid + '_pos2">Left</label><input type="radio" id="' + uid + '_pos1" name="' + uid + '" value="1" /><label for="' + uid + '_pos1">Middle</label><input type="radio" id="' + uid + '_pos3" name="' + uid + '" value="3" /><label for="' + uid + '_pos3">Right</label></div>');
$('#active-tokens').find('#' + uid + '_pos' + position).attr('checked', true);
availableIndices.shift();
checkPositionLimits();
});
}
else {
alert("The ToyPad is full, please remove some objects below!");
}
}
});
$('#vehicle-select').submit(function(e) {
e.preventDefault();
if ($('input[name="position"]:checked', '#vehicle-select').attr('disabled')) {
alert("This position is already full, please reposition or remove your active characters/vehicles below!");
return;
}
var name = $('#vehicle-name').val();
var position = $('input[name="position"]:checked', '#vehicle-select').val();
var index = availableIndices[0];
if (nameValid(vehicles, name)) {
if (availableIndices.length > 0) {
$.ajax({
method: 'POST',
contentType: 'application/json',
url: '/vehicle',
data: JSON.stringify({ id: filterByName(vehicles, name).id, position: position, index: index })
}).done(function(uid) {
$('#active-tokens').append('<div class="removeable" id="' + uid + '" data-type="vehicle" data-index="' + index + '">' + name + '</div>');
$('#active-tokens').append('<div id=""><input type="radio" id="' + uid + '_pos2" name="' + uid + '" value="2" /><label for="' + uid + '_pos2">Left</label><input type="radio" id="' + uid + '_pos1" name="' + uid + '" value="1" /><label for="' + uid + '_pos1">Middle</label><input type="radio" id="' + uid + '_pos3" name="' + uid + '" value="3" /><label for="' + uid + '_pos3">Right</label></div>');
$('#active-tokens').find('#' + uid + '_pos' + position).attr('checked', true);
availableIndices.shift();
checkPositionLimits();
});
}
else {
alert("The ToyPad is full, please remove some objects below!");
}
}
});
$('#active-tokens').on('change', 'input', function() {
var uid = $(this).attr('name');
var position = $(this).val();
var index = parseInt($('#active-tokens').find('#' + uid).attr('data-index'));
console.log(index);
var name = $('#active-tokens').find('#' + uid).text();
$('html').find("input").not('#remove-all').prop('disabled', true);
setTimeout(function() {
$('html').find("input").not('#remove-all').prop('disabled', false);
checkPositionLimits();
}, 2000);
$.ajax({
method: 'DELETE',
contentType: 'application/json',
url: '/remove',
data: JSON.stringify({ index: index })
}).done(function() {
setTimeout(function() {
switch($('#active-tokens').find('#' + uid).attr('data-type')) {
case "character":
$.ajax({
method: 'PUT',
contentType: 'application/json',
url: '/character',
data: JSON.stringify({ id: filterByName(characters, name).id, position: position, index: index, uid: uid })
});
break;
case "vehicle":
$.ajax({
method: 'PUT',
contentType: 'application/json',
url: '/vehicle',
data: JSON.stringify({ id: filterByName(vehicles, name).id, position: position, index: index, uid: uid })
});
break;
}
}, 500);
});
});
$('#remove-all').click(function() {
for (let i = 0; i < MAX_INDICES; i++) {
$.ajax({
method: 'DELETE',
contentType: 'application/json',
url: '/remove',
data: JSON.stringify({ index: i }),
context: this,
success: (json) => {
//availableIndices = [0, 1, 2, 3, 4, 5, 6];
availableIndices = [1, 2, 3, 4, 5, 6, 7];
$('body').find('input[type="radio"]').removeAttr('disabled');
$('.removeable').next().remove();
$('.removeable').remove();
}
});
}
});
$('#active-tokens').on('click', '.removeable', function() {
var index = parseInt($(this).attr('data-index'));
$.ajax({
method: 'DELETE',
contentType: 'application/json',
url: '/remove',
data: JSON.stringify({ index: index }),
context: this,
success: (json) => {
availableIndices.push(index);
$(this).next().remove();
$(this).remove();
checkPositionLimits();
}
});
});
});
</script>
</body>
</html>