forked from jmoenig/Snap
-
Notifications
You must be signed in to change notification settings - Fork 13
/
rw-parser.js
45 lines (37 loc) · 1.33 KB
/
rw-parser.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
/* eslint-disable prefer-const */
// has parsing for all previous versions
let parser = {};
parser.parse = function(opts, ref, wheelNumber, sprite) {
if (opts === undefined) {
return alert('Could not parse: Undefined parameter');
}
if (opts.string === undefined) {
return alert('Could not parse: Empty string');
}
let lines = opts.string.split('\n');
if (parser[lines[0]] === undefined) {
return alert(
'Could not parse: Unsupported version and/or invalid format'
);
}
parser[lines[0]](opts, ref, lines, wheelNumber, sprite);
};
parser['rw v0.0.1'] = function(opts, ref, lines, wheelNumber, sprite) {
throw new Error("Cannot import v0.0.1, please import v0.0.2!");
};
parser['rw v0.0.2'] = function(opts, ref, lines, wheelNumber, sprite) {
let data = JSON.parse(lines[1]);
let soundArray = sprite.sounds.asArray()
// check if wheel Number is valid
if (wheelNumber < 1 || wheelNumber > data['wheelCount']){
throw new Error("Wheel Number is invalid! Please enter positive number or valid wheel number (can't be more than 3)");
}
let chosenWheel = data['wheels'][wheelNumber-1];
for (let i = 0; i < chosenWheel['size']; ++i){
let nodeName = chosenWheel['nodes'][i];
let sound = detect(soundArray, function(s){return s.name == nodeName});
if (sound){
ref.push(sound);
}
}
};