-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.js
210 lines (187 loc) · 5.7 KB
/
old.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
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
const etherDOM = document.querySelector('#ether');
const computersDOM = document.querySelector('#computers');
const cableDOM = document.querySelector('#cable');
const CABLE_LENGTH = 32;
const COMPUTER_NUMBER = 8;
// const COLORS = ['#E00', '#0D0', '#00C', '#FC0', '#F0F', '#ACE', '#0AE', '#FA0'];
const WAIT_INTERVAL_LOW = 10;
const WAIT_INTERVAL_HIGH = 200;
const RETRY_INTERVAL_LOW = 10;
const RETRY_INTERVAL_HIGH = 50;
var CLOCK = 50;
// const labelClk = document.querySelector('#labelclk');
// const rangeClk = document.querySelector('#clk');
// rangeClk.addEventListener('change', () => {
// CLOCK = parseInt(rangeClk.value);
// labelClk.innerHTML = CLOCK+'ms';
// });
function generateColors(n) {
var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var result = [];
var tmp = '';
for(var i = 0; i < n; i += 1) {
tmp = '#';
for(var j = 0; j < 3; j += 1) {
tmp += chars[Math.floor(Math.random()*16)];
}
result.push(tmp);
}
return result;
}
const COLORS = generateColors(COMPUTER_NUMBER);
class Cell {
constructor(id, domobj) {
this.id = id;
this.obj = domobj;
this.lifeTime = 0;
this.propagating = false;
}
energyLoss() {
this.lifeTime = this.lifeTime > 0 ? this.lifeTime - 1 : 0;
if(this.lifeTime == 0) {
this.obj.innerText = '';
this.obj.style.backgroundColor = '';
}
}
send(char) {
if(this.obj.innerHTML == '') {
this.obj.innerHTML = char;
this.lifeTime = CABLE_LENGTH;
} else if(this.obj.innerHTML != char) {
this.obj.innerHTML = 'ERR!';
this.lifeTime = CABLE_LENGTH;
}
this.obj.style.backgroundColor = char;
}
}
class Computer {
constructor(id, color, cable) {
this.id = id;
this.color = color;
this.cable = cable;
this.obj = null;
this.waiting = false;
this.messages = [];
this.sent = 0;
this.messagesObj = document.createElement('pre');
this.messagesObj.style.color = this.color;
document.body.appendChild(this.messagesObj);
}
showMessages() {
// if(this.messages.length == 10) {
// this.messages = this.messages.slice(5, this.messages.length);
// }
this.messagesObj.innerHTML = this.messages;
}
send(char) {
if(this.etherEmpty()) {
this.sent += 1;
char = char || this.color;
this.cable[this.id].propagating = true;
this.cable[this.id].send(char);
} else {
this.obj.setAttribute('waiting', 'true');
waitThenSend(RETRY_INTERVAL_LOW, RETRY_INTERVAL_HIGH, this);
}
}
receive() {
var mailbox = this.cable[this.id];
if(mailbox.propagating == true && mailbox.obj.innerText != this.color) {
this.messages.push(mailbox.obj.innerText);
this.showMessages();
}
}
etherEmpty() {
return this.cable[this.id].obj.innerText == '';
}
}
function generateCable(len) {
var result = [];
var td = null;
var cell = null;
for(var i = 0; i < CABLE_LENGTH; i++) {
td = document.createElement('td');
td.innerHTML = '';
cell = new Cell(i, td);
result.push(cell);
}
return result;
}
function drawCable(arr, objcable, objcomputers) {
var th = null;
for(var elt of arr) {
th = document.createElement('th');
th.setAttribute('computer', elt.id+'');
objcomputers.appendChild(th);
objcable.appendChild(elt.obj);
}
}
function appendComputer(pc, obj) {
for(var th of obj.children) {
// debugger
if(th.attributes.computer.value == pc.id) {
pc.obj = th;
pc.obj.innerHTML = '💻';
pc.obj.style.color = pc.color;
}
}
}
function computersListen(arr) {
for(var pc of arr) {
pc.receive();
}
}
function propagate(cells) {
var propagatingCells = [];
for(var cell of cells) {
if(cell.propagating == true) {
propagatingCells.push(cell);
}
cell.energyLoss();
}
var next = null;
var prev = null;
for(var cell of propagatingCells) {
next = cells[cell.id + 1];
prev = cells[cell.id - 1];
if(next !== undefined && next.obj.innerText != cell.obj.innerText) {
next.send(cell.obj.innerText);
next.propagating = true;
}
if(prev !== undefined && prev.obj.innerText != cell.obj.innerText) {
prev.send(cell.obj.innerText);
prev.propagating = true;
}
cell.propagating = false;
}
}
function waitThenSend(low, high, pc) {
if(pc.waiting == false) {
pc.waiting = true;
var randomTimeout = (Math.random()*(high - low) + low)*CLOCK;
setTimeout(function() {
pc.waiting = false;
pc.obj.setAttribute('waiting', 'false');
pc.send();
}, randomTimeout);
}
}
function randomComputerSends(arr, chance) {
var willSend = Math.random() < chance;
if(willSend) {
var randComputer = Math.floor(Math.random()*arr.length);
waitThenSend(WAIT_INTERVAL_LOW, WAIT_INTERVAL_HIGH, arr[randComputer]);
}
}
const cable = generateCable(CABLE_LENGTH);
drawCable(cable, cableDOM, computersDOM);
const computers = [];
for(var i = 0; i < COMPUTER_NUMBER; i++) {
computers.push(new Computer(i*Math.floor(CABLE_LENGTH / COMPUTER_NUMBER), COLORS[i], cable));
appendComputer(computers[i], computersDOM);
}
setInterval(function() {
computersListen(computers);
randomComputerSends(computers, 0.1);
propagate(cable);
}, CLOCK);