-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (87 loc) · 2.52 KB
/
index.js
File metadata and controls
94 lines (87 loc) · 2.52 KB
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
const socket = new WebSocket("ws://dyalog_root/");
const table = document.getElementById("tab");
const restart = document.getElementById("restart");
const text = document.getElementById("text");
restart.addEventListener("click", e => {
socket.send(JSON.stringify([2, 0]));
});
let cells = []; let ena = 1;
function init(width, height) {
for (let m = 0; m<height; ++m) {
const r = table.insertRow();
for (let n = 0; n<width; ++n) {
cells.push(r.insertCell());
}
}
for (let i = 0; i < cells.length; ++i) {
const ii = i;
const cell = cells[i];
cell.className = "cell";
cell.innerHTML = " ";
cell.addEventListener("click", e => {
e.stopPropagation();
if (ena===1) {
socket.send(JSON.stringify([0, ii]));
}
});
cell.addEventListener("contextmenu", e => {
e.stopPropagation();
e.preventDefault();
if (ena===1) {
socket.send(JSON.stringify([1, ii]));
}
});
}
}
const colors = {
"1": "#3333cc", "2": "#006600", "3": "#cc0000",
"4": "#660066", "5": "#006666"
}
function render(txt, str) {
text.innerText = txt;
for (let i = 0; i < cells.length; ++i) {
const cell = cells[i];
const c = str.charAt(i);
if (c===".") {
cell.innerHTML = " ";
cell.style.color = "#000000";
cell.classList.remove("open");
}
else if (c===" ") {
cell.innerHTML = " ";
cell.style.color = "#000000";
cell.classList.add("open");
}
else if (c==="?") {
cell.innerText = c;
cell.style.color = "#000000";
cell.classList.remove("open");
}
else if (c==="@") {
cell.innerText = "@";
cell.style.color = "#000000";
cell.classList.add("open");
}
else if (c in colors) {
cell.innerText = c;
cell.style.color = colors[c];
cell.classList.add("open");
}
else {
cell.innerText = c;
cell.style.color = "#000000";
cell.classList.add("open");
}
}
}
socket.addEventListener("message", e => {
if (typeof e.data === "string") {
const json = JSON.parse(e.data);
if (json.k===0) {
init(json.w, json.h);
} else {
ena = json.v;
render(json.t, json.f);
}
}
});