-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
165 lines (142 loc) · 4.74 KB
/
main.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
const MAX_ANT_COUNT = 10;
// Create a table for entering ants' parameters.
document.addEventListener("DOMContentLoaded", function() {
let antParametersTableBody = document
.getElementById("antParametersTable")
.getElementsByTagName('tbody')[0];
for (let i = 0; i < MAX_ANT_COUNT; i++) {
let row = antParametersTableBody.insertRow(i);
// Enable
let cell0 = row.insertCell(0);
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.id = "ant-enable[" + i + "]";
cell0.appendChild(checkbox);
// X Initial Position
let cell1 = row.insertCell(1);
let xPositionInput = document.createElement("input");
xPositionInput.type = "number";
xPositionInput.id = "ant-xposition[" + i + "]";
cell1.appendChild(xPositionInput);
// Y Initial Position
let cell2 = row.insertCell(2);
let yPositionInput = document.createElement("input");
yPositionInput.type = "number";
yPositionInput.id = "ant-yposition[" + i + "]";
cell2.appendChild(yPositionInput);
// Direction
let cell3 = row.insertCell(3);
let directions = ["east", "west", "north", "south"];
for (let j = 0; j < directions.length; j++) {
let directionInput = document.createElement("input");
directionInput.type = "radio";
directionInput.name = "ant-direction[" + i + "]";
directionInput.value = directions[j];
if (j === 0) {
directionInput.checked = true;
}
let label = document.createElement("label");
label.appendChild(directionInput);
label.appendChild(document.createTextNode(" " + directions[j]));
cell3.appendChild(label);
}
}
});
// Send the request.
function submitForm() {
// Destination URL
const url = document.getElementById("url").value;
if(url == "") {
alert("The URL is blank.");
return;
}
// Rule
let json = {}
const rule = document.getElementById("rule").value;
let ruleNum = 0;
if(rule.length < 1) {
alert("The length of the rule is less than 1.");
return;
}
else {
for(i = 0; i < rule.length; i++) {
let c = rule.charAt(i);
if(c == 'L')
ruleNum = (0 << (i * 2)) | ruleNum;
else if(c == 'R')
ruleNum = (1 << (i * 2)) | ruleNum;
else if(c == 'N')
ruleNum = (2 << (i * 2)) | ruleNum;
else if(c == 'T')
ruleNum = (3 << (i * 2)) | ruleNum;
else {
alert("The rule contains invalid characters.");
return;
}
}
}
const ruleJson = {
"length" : rule.length,
"rule" : ruleNum
}
// Ants' parameters
let antParams = [];
for(i = 0; i < MAX_ANT_COUNT; i++) {
if(false == document.getElementById("ant-enable[" + i + "]").checked) { continue; }
let p = 0;
// Directoin
dir = document.querySelector('input[name="ant-direction[' + i + ']"]:checked').value;
if(dir == "north")
p = 0;
else if(dir == "east")
p = 1;
else if(dir == "south")
p = 2;
else if(dir == "west")
p = 3;
else
return;
// X Initial Position
xpos = parseInt(document.getElementById("ant-xposition[" + i + "]").value);
if(isNaN(xpos)) {
alert("The X initial position is not a number.");
return;
}
else
p = ((xpos & 0xFF) << 2) | p;
// Y Initial Position
ypos = parseInt(document.getElementById("ant-yposition[" + i + "]").value);
if(isNaN(ypos)) {
alert("The Y initial position is not a number.");
return;
}
else
p = ((ypos & 0x1FF) << 10) | p;
antParams.push(p);
}
if(antParams.length == 0) {
alert("The number of enabled ants is zero.");
return;
}
const antsJson = {
"count" : antParams.length,
"params" : antParams
}
// This JSON data will be sent to the Raspberry Pi Pico.
const langtonJson = {
"ants" : antsJson,
"rule" : ruleJson
};
console.log(JSON.stringify(langtonJson));
const request = new Request(url, {
method:"POST",
body: JSON.stringify(langtonJson)
});
request.body = JSON.stringify(langtonJson);
console.log(request);
// Send
fetch(request)
.then(response => response.statusText)
.then(txt => alert(txt))
.catch(error => console.error("Error:", error));
}