-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2snesbridge.js
119 lines (105 loc) · 3.68 KB
/
2snesbridge.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
const WebSocket = require('ws');
class QUSB2SNESConnection {
constructor(url, eventCallbacks) {
this.url = url;
this.connection = null;
this.answers = [];
this.lastValue = {}; // Store last values for monitored addresses
this.eventCallbacks = eventCallbacks; // Object containing various event callbacks
this.monitorConfig = {
'F50071': {
condition: value => value === 9,
action: () => this.triggerEvent('death')
},
'F50100': {
condition: value => value === 11,
action: () => {
this.triggerEvent('timerStart');
}
},
'F51F2E': {
condition: value => value,
action: value => this.triggerEvent('exitUpdate', value)
}
};
}
triggerEvent(eventName, value) {
if (this.eventCallbacks && typeof this.eventCallbacks[eventName] === 'function') {
this.eventCallbacks[eventName](value); // Call the callback, passing value if provided
}
}
connect() {
console.log('Connecting...');
this.connection = new WebSocket(this.url);
this.connection.on('open', () => this.onOpen());
this.connection.on('error', error => this.onError(error));
this.connection.on('close', () => this.onClose());
this.connection.on('message', message => this.onMessage(message));
}
onOpen() {
console.log('Connected.');
this.requestDeviceList();
}
onError(error) {
console.error('WebSocket Error:', error);
}
onClose() {
console.log('Connection closed.');
}
onMessage(message) {
if (this.answers.length) {
const callback = this.answers.shift();
callback(message);
}
}
send(data) {
this.connection.send(JSON.stringify(data));
}
ask(question, answer) {
if (answer) {
this.answers.push(answer);
}
this.send(question);
}
requestDeviceList() {
this.ask({ Opcode: 'DeviceList', Space: 'SNES' }, response => {
console.log(JSON.parse(response.toString()).Results);
const devices = JSON.parse(response.toString()).Results;
if (devices.length) {
this.attachToDevice(devices[0]);
} else {
console.log('No SNES found. Connect SNES and reload page.');
}
});
}
attachToDevice(device) {
this.ask({ Opcode: 'Attach', Space: 'SNES', Operands: [device] });
this.ask({ Opcode: 'Name', Space: "SNES", Operands: ['SMW Tracker'] });
this.monitorAddress('F50071');
this.monitorAddress('F50100');
this.monitorAddress('F51F2E');
}
monitorAddress(address) {
const checkAddress = () => {
this.ask({
Opcode: 'GetAddress',
Space: 'SNES',
Flags: null,
Operands: [address, '1']
}, messageData => {
const value = messageData[0]; // Assuming messageData is already a buffer
if (value !== this.lastValue[address] && this.monitorConfig[address]) {
console.log(`Address ${address} changed to ${value}`);
this.lastValue[address] = value;
const config = this.monitorConfig[address];
if (config.condition(value)) {
config.action(value);
}
}
setTimeout(checkAddress, 10); // Check again after a delay
});
};
checkAddress();
}
}
module.exports = QUSB2SNESConnection;