-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_script2.js
409 lines (355 loc) · 11.8 KB
/
main_script2.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
var C1 = []
var C2 = []
var C3 = []
var script = ["Message 1", "Message 2", "Message 3", "Message 4", "Message 5", "Message 6", "Message 7"]
var hint_count = 2
var timestamp = 0
var message_number_C1 = 0
var message_number_C2 = 0
var message_number_C3 = 0
var original_cooldown = []
// Class definition for a Node. In Javascript, this is how we define blueprints of objects (class), as functions!
function Node(node_id) {
var node = document.createElement("BUTTON")
node.setAttribute("id", node_id)
var node_text = document.createTextNode(node_id)
node.appendChild(node_text)
document.body.appendChild(node)
node.message = '' // default message.
node.cooldown = 0
}
function cansend(node) {
if (node.cooldown == 0 && node.message != '')
return true
else
return false
}
function canreceive(node) {
if (node.message == '')
return true
else
return false
}
function send(node, next_node){
if (level.options[level.selectedIndex].value == "PlainText"){
receive(next_node, node.message)
node.message = ''
}
else{
node.message = genRndString()
next_node.message = genRndString()
} // If professor want's the encrypted messages to flow like the plaintext version, then remove the else condition. Else, let it be and so, whenever a send action takes place, both nodes have new messages.
if (node.id == '1')
node.cooldown = original_cooldown[0]
else if (node.id == '2')
node.cooldown = original_cooldown[1]
else if (node.id == '3')
node.cooldown = original_cooldown[2]
else if (node.id == '4')
node.cooldown = original_cooldown[3]
else if (node.id == '5')
node.cooldown = original_cooldown[4]
else if (node.id == '6')
node.cooldown = original_cooldown[5]
else if (node.id == '7')
node.cooldown = original_cooldown[6]
else if (node.id == '8')
node.cooldown = original_cooldown[7]
else if (node.id == '9')
node.cooldown = original_cooldown[8]
else if (node.id == 'A')
node.cooldown = original_cooldown[9]
else if (node.id == 'B')
node.cooldown = original_cooldown[10]
else if (node.id == 'C')
node.cooldown = original_cooldown[11]
}
function receive(node,received_message) {
node.message = received_message
}
// This is how a new object is instantiated.
var one = new Node('1')
var two = new Node('2')
var three = new Node('3')
var four = new Node('4')
var five = new Node('5')
var six = new Node('6')
var seven = new Node('7')
var eight = new Node('8')
var nine = new Node('9')
var A = new Node('A')
var B = new Node('B')
var C = new Node('C')
var X = new Node('X')
var Y = new Node('Y')
var Z = new Node('Z')
// The server nodes don't need to reset their cooldowns because they are never decreased.
document.getElementById('X').cooldown = getRndInteger(5,1)
document.getElementById('Y').cooldown = getRndInteger(5,1)
document.getElementById('Z').cooldown = getRndInteger(5,1)
var Node1_timeout
var list1 = document.getElementById('Node1')
var prev_state_N1 = list1.options[list1.selectedIndex].value
var curr_state_N1
/*
var pA = document.getElementById('A').message
var pB = document.getElementById('B').message
var pC = document.getElementById('C').message
var cA
var cB
var cC*/
function Log_Node1(){
curr_state_N1 = list1.options[list1.selectedIndex].value
if (curr_state_N1 != prev_state_N1){
document.getElementById('TA1').innerHTML = ""
clearTimeout(Node1_timeout)
prev_state_N1 = curr_state_N1
}
if (document.getElementById(curr_state_N1).message == '')
document.getElementById('TA1').innerHTML += '...' + '\n'
else
document.getElementById('TA1').innerHTML += document.getElementById(curr_state_N1).message + '\n'
Node1_timeout = setTimeout(Log_Node1,2000)
}
var Node2_timeout
var list2 = document.getElementById('Node2')
var prev_state_N2 = list2.options[list2.selectedIndex].value
var curr_state_N2
function Log_Node2(){
curr_state_N2 = list2.options[list2.selectedIndex].value
if (curr_state_N2 != prev_state_N2){
document.getElementById('TA2').innerHTML = ""
clearTimeout(Node2_timeout)
prev_state_N2 = curr_state_N2
}
if (document.getElementById(curr_state_N2).message == '')
document.getElementById('TA2').innerHTML += '...' + '\n'
else
document.getElementById('TA2').innerHTML += document.getElementById(curr_state_N2).message + '\n'
Node2_timeout = setTimeout(Log_Node2,2000)
}
function getRndInteger(max,min) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function genRndString() {
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var numbers = "0123456789"
var punctuation = "!@#$%^&*()_+="
return letters[getRndInteger(25,0)] + numbers[getRndInteger(9,0)] + punctuation[getRndInteger(12,0)] + letters[getRndInteger(25,0)] + punctuation[getRndInteger(12,0)]
}
function generate_circuits() {
// Generating the circuit for client A.
C1[0] = 'A'
C1[1] = String(getRndInteger(3,1))
C1[2] = String(getRndInteger(6,4))
C1[3] = String(getRndInteger(9,7))
var end_node = String(getRndInteger(3,1))
if (end_node == 1)
C1[4] = 'X'
else if (end_node == 2)
C1[4] = 'Y'
else
C1[4] = 'Z'
// Generating the circuit for client B.
var temp
C2[0] = 'B'
while (true){
temp = String(getRndInteger(3,1))
if (!C1.includes(temp)){
C2[1] = temp
break
}
}
while (true){
temp = String(getRndInteger(6,4))
if (!C1.includes(temp)){
C2[2] = temp
break
}
}
while (true){
temp = String(getRndInteger(9,7))
if (!C1.includes(temp)){
C2[3] = temp
break
}
}
while (true){
temp = String(getRndInteger(3,1))
if (temp == '1')
temp = 'X'
else if(temp == '2')
temp = 'Y'
else
temp = 'Z'
if (!C1.includes(temp)){
C2[4] = temp
break
}
}
// Generating the circuit for client C.
var temp
C3[0] = 'C'
while (true){
temp = String(getRndInteger(3,1))
if (!C1.includes(temp) && !C2.includes(temp)){
C3[1] = temp
break
}
}
while (true){
temp = String(getRndInteger(6,4))
if (!C1.includes(temp) && !C2.includes(temp)){
C3[2] = temp
break
}
}
while (true){
temp = String(getRndInteger(9,7))
if (!C1.includes(temp) && !C2.includes(temp)){
C3[3] = temp
break
}
}
while (true){
temp = String(getRndInteger(3,1))
if (temp == '1')
temp = 'X'
else if(temp == '2')
temp = 'Y'
else
temp = 'Z'
if (!C1.includes(temp) && !C2.includes(temp)){
C3[4] = temp
break
}
}
// Populating the original countdowns here.
for(var i = 0; i < 12; i++)
original_cooldown[i] = getRndInteger(5,1)
alert("TOR Circuits Setup! Good Luck!")
}
// This function just moves the message along the circuit.
var simulation_timer
var level = document.getElementById('Diff')
function start_sim() {
if (level.options[level.selectedIndex].value == "PlainText"){
if (timestamp == 0){
document.getElementById(C1[0]).message = script[message_number_C1]
document.getElementById(C2[0]).message = script[message_number_C2]
document.getElementById(C3[0]).message = script[message_number_C3]
}
else{
// Special check on the server nodes because they are the only nodes which don't 'send' messages. If this wasn't done, they would not be able to receive any more messages beyond the first one.
if (timestamp % document.getElementById(C1[4]).cooldown == 0)
document.getElementById(C1[4]).message = ''
if (timestamp % document.getElementById(C2[4]).cooldown == 0)
document.getElementById(C2[4]).message = ''
if (timestamp % document.getElementById(C3[4]).cooldown == 0)
document.getElementById(C3[4]).message = ''
// At every timestamp mark, reduce cooldown by 1 for all nodes (except the server node since they don't 'send' messages) provided they are not 0.
for(var i = 3; i >= 0; i--){
if (document.getElementById(C1[i]).cooldown != 0)
document.getElementById(C1[i]).cooldown--
if (document.getElementById(C2[i]).cooldown != 0)
document.getElementById(C2[i]).cooldown--
if (document.getElementById(C3[i]).cooldown != 0)
document.getElementById(C3[i]).cooldown--
}
// Cycle through all the nodes to see whether they can send or not, in reverse order because otherwise a node which just received a message could send to the next node in the same timestamp and this is not allowed.
for(var i = 4; i > 0; i--){
if (cansend(document.getElementById(C1[i-1])) && canreceive(document.getElementById(C1[i]))){
send(document.getElementById(C1[i-1]),document.getElementById(C1[i]))
if (i-1 == 0){
message_number_C1++
document.getElementById(C1[i-1]).message = script[message_number_C1 % 7]
}
}
if (cansend(document.getElementById(C2[i-1])) && canreceive(document.getElementById(C2[i]))){
send(document.getElementById(C2[i-1]),document.getElementById(C2[i]))
if (i-1 == 0){
message_number_C2++
document.getElementById(C2[i-1]).message = script[message_number_C2 % 7]
}
}
if (cansend(document.getElementById(C3[i-1])) && canreceive(document.getElementById(C3[i]))){
send(document.getElementById(C3[i-1]),document.getElementById(C3[i]))
if (i-1 == 0){
message_number_C3++
document.getElementById(C3[i-1]).message = script[message_number_C3 % 7]
}
}
}
}
}
else{
if (timestamp == 0){
document.getElementById(C1[0]).message = genRndString()
document.getElementById(C2[0]).message = genRndString()
document.getElementById(C3[0]).message = genRndString()
}
else{
// Special check on the server nodes because they are the only nodes which don't 'send' messages. If this wasn't done, they would not be able to receive any more messages beyond the first one.
if (timestamp % document.getElementById(C1[4]).cooldown == 0)
document.getElementById(C1[4]).message = ''
if (timestamp % document.getElementById(C2[4]).cooldown == 0)
document.getElementById(C2[4]).message = ''
if (timestamp % document.getElementById(C3[4]).cooldown == 0)
document.getElementById(C3[4]).message = ''
// At every timestamp mark, reduce cooldown by 1 for all nodes (except the server node since they don't 'send' messages) provided they are not 0.
for(var i = 3; i >= 0; i--){
if (document.getElementById(C1[i]).cooldown != 0)
document.getElementById(C1[i]).cooldown--
if (document.getElementById(C2[i]).cooldown != 0)
document.getElementById(C2[i]).cooldown--
if (document.getElementById(C3[i]).cooldown != 0)
document.getElementById(C3[i]).cooldown--
}
// Cycle through all the nodes to see whether they can send or not, in reverse order because otherwise a node which just received a message could send to the next node in the same timestamp and this is not allowed.
for(var i = 4; i > 0; i--){
if (cansend(document.getElementById(C1[i-1])) && canreceive(document.getElementById(C1[i]))){
send(document.getElementById(C1[i-1]),document.getElementById(C1[i]))
if (i-1 == 0){
message_number_C1++
document.getElementById(C1[i-1]).message = genRndString()
}
}
if (cansend(document.getElementById(C2[i-1])) && canreceive(document.getElementById(C2[i]))){
send(document.getElementById(C2[i-1]),document.getElementById(C2[i]))
if (i-1 == 0){
message_number_C2++
document.getElementById(C2[i-1]).message = genRndString()
}
}
if (cansend(document.getElementById(C3[i-1])) && canreceive(document.getElementById(C3[i]))){
send(document.getElementById(C3[i-1]),document.getElementById(C3[i]))
if (i-1 == 0){
message_number_C3++
document.getElementById(C3[i-1]).message = genRndString()
}
}
}
}
}
timestamp++
simulation_timer = setTimeout(start_sim,2000)
}
function stop_sim() {
clearTimeout(simulation_timer)
clearTimeout(Node1_timeout)
clearTimeout(Node2_timeout)
}
function show_hints() {
if (hint_count > 0) {
alert("You have " + hint_count + " hints remaining!")
alert("Node " + C1[hint_count] + " lies on the circuit starting from client A!")
hint_count--
}
else
alert("You've run out of hints! Come on chum, you can do it!")
}
function test() {
console.log(C1)
console.log(C2)
console.log(C3)
}