forked from sector3studios/r3e-spectator-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr3e.js
237 lines (215 loc) · 5.53 KB
/
r3e.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
window.r3e = (function r3eBridge() {
if (!window.gameClient) {
return console.warn('This depends on the R3E game client');
}
var getRequestPool = (function() {
var requestPool = {
'vehicleInfo': {},
'pitInfo': {},
'ptpInfo': {},
'extendedInfo': {},
'driverInfo': {},
'driversInfo': [],
'sessionInfo': [],
'eventInfo': [],
'resultsUpdate': []
};
return {
'driverInfo': function getdriverInfoPool(data) {
if (!requestPool.driverInfo[data.slotId]) {
requestPool.driverInfo[data.slotId] = [];
}
return requestPool.driverInfo[data.slotId];
},
'vehicleInfo': function getVehicleInfoPool(data) {
if (!requestPool.vehicleInfo[data.slotId]) {
requestPool.vehicleInfo[data.slotId] = [];
}
return requestPool.vehicleInfo[data.slotId];
},
'pitInfo': function getPitInfoPool(data) {
if (!requestPool.pitInfo[data.slotId]) {
requestPool.pitInfo[data.slotId] = [];
}
return requestPool.pitInfo[data.slotId];
},
'ptPInfo': function getPtpInfoPool(data) {
if (!requestPool.ptpInfo[data.slotId]) {
requestPool.ptpInfo[data.slotId] = [];
}
return requestPool.ptpInfo[data.slotId];
},
'extendedInfo': function getExtendedInfo(data) {
if (!requestPool.extendedInfo[data.slotId]) {
requestPool.extendedInfo[data.slotId] = [];
}
return requestPool.extendedInfo[data.slotId];
},
'driversInfo': function getDriversInfoPool() {
return requestPool.driversInfo;
},
'sessionInfo': function getSessionInfoPool() {
return requestPool.sessionInfo;
},
'eventInfo': function getEventInfoPool() {
return requestPool.eventInfo;
},
'resultsUpdate': function getEventInfoPool() {
return requestPool.resultsUpdate;
}
};
})();
window.communicator = function(type, data) {
var poolFetcher = getRequestPool[type];
if (!poolFetcher) {
return;
}
var pool = poolFetcher(data);
var callbacksToTrigger = [];
pool.forEach(function(callback) {
callbacksToTrigger.push(callback);
});
// Reset pool
if (!pool.persistent) {
pool.length = 0;
}
callbacksToTrigger.forEach(function runCallback(callback) {
callback(data);
});
};
function get(opts) {
return function getter(argsOrCallback, callback) {
var args = {};
if (opts.requiresArguments) {
args = argsOrCallback;
if (!callback) {
throw new Error('Callback is not set: '+opts.call);
}
} else {
callback = argsOrCallback;
}
if (typeof callback !== 'function') {
throw new Error('Callback is not a function: '+opts.call);
}
var pool = getRequestPool[opts.pool](args);
pool.push(callback);
if (pool.length > 1) {
return;
}
var gameClientData = {};
gameClientData[opts.call] = args;
var jsonStr = JSON.stringify(gameClientData);
window.r3e.gameClient(null, jsonStr);
};
}
function set(opts) {
return function seter(args) {
if (typeof args === 'undefined' && !opts.noArgs) {
throw new Error('Args are not set: '+opts.call);
}
var gameClientData = {};
gameClientData[opts.call] = args || {};
var jsonStr = JSON.stringify(gameClientData);
window.r3e.gameClient(null, jsonStr);
};
}
function cameraChanger(cameraId) {
return function cameraChangers(slotId) {
if (!Number.isInteger(slotId)) {
throw new Error('slotId is not an integer');
}
window.r3e.gameClient(null, JSON.stringify({
'ChangeCamera': {
'slotId': slotId,
'camera': cameraId
}
}));
};
}
function listener(opts) {
return function listeners(callback) {
var pool = getRequestPool[opts.pool]();
// Set property on array so it doesn't get cleared
pool.persistent = true;
pool.push(callback);
};
}
return {
'getVehicleInfo': get({
'call': 'GetVehicleInfo',
'pool': 'vehicleInfo',
'requiresArguments': true
}),
'getPitInfo': get({
'call': 'GetPitInfo',
'pool': 'pitInfo',
'requiresArguments': true
}),
'getPushToPassInfo': get({
'call': 'GetPtPInfo',
'pool': 'ptPInfo',
'requiresArguments': true
}),
'getExtendedInfo': get({
'call': 'GetExtendedInfo',
'pool': 'extendedInfo',
'requiresArguments': true
}),
'getDriverInfo': get({
'call': 'GetDriverInfo',
'pool': 'driverInfo',
'requiresArguments': true
}),
'getDriversInfo': get({
'call': 'GetDriversInfo',
'pool': 'driversInfo'
}),
'getSessionInfo': get({
'call': 'GetSessionInfo',
'pool': 'sessionInfo'
}),
'getEventInfo': get({
'call': 'GetEventInfo',
'pool': 'eventInfo'
}),
'showCursor': set({
'call': 'ShowCursor'
}),
'waitOnResults': set({
'call': 'WaitForMe'
}),
'goToNextEvent': set({
'call': 'Proceed',
'noArgs': true
}),
'exit': set({
'call': 'Exit',
'noArgs': true
}),
'setCamera': {
'nosecam': cameraChanger('nosecam'),
'cockpit': cameraChanger('cockpit'),
'swingman': cameraChanger('swingman'),
'onboard': cameraChanger('onboard'),
'trackside': cameraChanger('trackside1'),
'onboard1': cameraChanger('onboard_1'),
'onboard2': cameraChanger('onboard_2'),
'frontCam': cameraChanger('front_cam'),
'rearCam': cameraChanger('rear_cam'),
'flFront': cameraChanger('fl_front'),
'frFront': cameraChanger('fr_front'),
'rlRear': cameraChanger('rl_rear'),
'rrRear': cameraChanger('rr_rear'),
'rlFront': cameraChanger('rl_front'),
'rrFront': cameraChanger('rr_front'),
'exhaust': cameraChanger('exhaust'),
'wing': cameraChanger('wing')
},
'on': {
'resultsUpdate': listener({
'pool': 'resultsUpdate'
})
},
'gameClient': window.gameClient
};
})();