-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveCode.js
126 lines (107 loc) · 4.19 KB
/
ActiveCode.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
import {getQuickJS} from 'quickjs-emscripten';
import {EventEmitter} from 'events';
export default class ActiveCode extends EventEmitter {
constructor(text, botID, botManager, highlightManager) {
super();
this.text = text;
this.botID = botID;
this.botManager = botManager;
this.highlightManager = highlightManager;
this.running = false;
this.latestLeft = null;
this.latestRight = null;
this.latestServo = null;
this.target = null;
for(const bot of botManager.bots) {
if(bot.botID === botID) {
this.target = bot;
bot.on('disconnect', () => {
this.target = null;
});
break;
}
}
botManager.on('connect', bot => {
if(bot.botID === botID) {
this.target = bot;
if(this.latestLeft !== null && this.latestRight !== null) {
bot.setPower(this.latestLeft, this.latestRight);
}
if(this.latestServo !== null) {
bot.sendServo(this.latestServo);
}
bot.on('disconnect', () => {
this.target = null;
});
}
});
}
async start() {
const QuickJS = await getQuickJS();
const vm = QuickJS.newContext();
vm.newFunction('moveMotors', (leftHandle, rightHandle) => {
const left = vm.getNumber(leftHandle);
const right = vm.getNumber(rightHandle);
this.latestLeft = left;
this.latestRight = right;
if(this.target !== null) {
this.target.setPower(left, right);
}
}).consume((handle) => vm.setProp(vm.global, 'moveMotors', handle));
vm.newFunction('moveServo', (angleHandle) => {
const angle = vm.getNumber(angleHandle);
this.latestServo = angle;
if(this.target !== null) {
this.target.sendServo(angle);
}
}).consume((handle) => vm.setProp(vm.global, 'moveServo', handle));
vm.newFunction('getLidar', () => {
return vm.newNumber(this.target.lidar);
}).consume((handle) => vm.setProp(vm.global, 'getLidar', handle));
vm.newFunction('getIR', (sideHandle) => {
const side = vm.getString(sideHandle);
if(side === 'left') {
return vm.newNumber(this.target.leftIR);
} else {
return vm.newNumber(this.target.rightIR);
}
}).consume((handle) => vm.setProp(vm.global, 'getIR', handle));
vm.newFunction('log', (textHandle) => {
const text = vm.getString(textHandle);
console.log(text);
}).consume((handle) => vm.setProp(vm.global, 'log', handle));
vm.newFunction('highlightBlock', (textHandle) => {
const text = vm.getString(textHandle);
this.highlightManager.highlight(text);
}).consume((handle) => vm.setProp(vm.global, 'highlightBlock', handle));
vm.newFunction('waitSeconds', (secondsHandle) => {
const seconds = vm.getNumber(secondsHandle);
secondsHandle.dispose();
const promise = vm.newPromise();
setTimeout(() => {
promise.resolve();
}, seconds * 1000);
promise.settled.then(vm.runtime.executePendingJobs);
return promise.handle;
}).consume((handle) => vm.setProp(vm.global, 'waitSeconds', handle));
const result = vm.evalCode(this.text);
const promiseHandle = vm.unwrapResult(result);
const resolvedResult = await vm.resolvePromise(promiseHandle);
promiseHandle.dispose();
const resolvedHandle = vm.unwrapResult(resolvedResult);
resolvedHandle.dispose();
this.running = false;
this.emit('stopped');
this.killTarget();
}
killTarget() {
this.latestLeft = 0;
this.latestRight = 0;
if(this.target !== null) {
this.target.setPower(0, 0);
this.target.sendServo(90);
}
this.target = null;
this.botID = null;
}
}