-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
245 lines (226 loc) · 7.95 KB
/
index.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
var blessed = require('blessed');
var fs = require('fs');
var path = require('path');
var player = require('play-sound')(opts = {});
var game = require('./game.js');
var AdmZip = require('adm-zip');
var child_process = require('child_process');
var sm_parser = require('sm-parser');
Array.prototype.diff = function(a) {
return this.filter(function(i) {
return a.indexOf(i) < 0;
});
};
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var screen = blessed.screen({
smartCSR: true,
debug: true
});
function getDirectories(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
if (fs.readdirSync('./song_zips').length > 1) {
fs.readdirSync('./song_zips').forEach(function(zip_file) {
if (zip_file.indexOf(".zip") == -1) {
return;
}
var zip = new AdmZip('./song_zips/' + zip_file);
zip.extractAllTo(__dirname + '/songs/', /*overwrite*/ true);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
var dir_name;
zipEntries.forEach(function(zipEntry) {
if (zipEntry.isDirectory) {
dir_name = zipEntry.entryName.match(/([^\/]+)\/$/)[1] || zipEntry.entryName.substring(0, zipEntry.entryName.length - 1);
} else {
zip.extractEntryTo(zipEntry.entryName, './songs/' + dir_name, false, true);
}
});
child_process.execSync('convert -colors 256 -depth 8 +dither png8:./songs/' + dir_name.replace(/ /g,'\\ ') + '/' + dir_name.replace(/ /g,'\\ ') + '.png png8:./songs/' + dir_name.replace(/ /g,'\\ ') + '/' + dir_name.replace(/ /g,'\\ ') + '.png');
child_process.execSync('convert -colors 256 -depth 8 +dither png8:./songs/' + dir_name.replace(/ /g,'\\ ') + '/' + dir_name.replace(/ /g,'\\ ') + '-bg.png png8:./songs/' + dir_name.replace(/ /g,'\\ ') + '/' + dir_name.replace(/ /g,'\\ ') + '-bg.png');
//child_process.execSync('node parser.js ' + __dirname + '/songs/' + dir_name + '/' + dir_name + '.sm');
sm_parser.convert(__dirname + '/songs/' + dir_name + '/' + dir_name + '.sm');
fs.unlink('./song_zips/' + zip_file, function(err) {});
});
}
var songs = getDirectories('./songs');
var song_index = 0;
var music;
fs.stat(__dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {
if(err){
music = player.play('./songs/' + songs[song_index] + '/' + songs[song_index] + '.mp3', function(err) {})
}
else{
music = player.play('./songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {})
}
});
var image = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index] + '/' + songs[song_index] + '.png',
height: '90%',
top: 'center',
left: 'center'
});
var image_left = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index - 1] + '/' + songs[song_index - 1] + '.png',
height: '70%',
top: 'center',
left: '-57%'
});
var image_right = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index + 1] + '/' + songs[song_index + 1] + '.png',
height: '70%',
top: 'center',
left: '95%'
});
screen.render();
screen.key(['left', 'right'], function(ch, key) {
switch (key.name) {
case 'left':
song_index = Math.max(0, song_index - 1);
break;
case 'right':
song_index = Math.min(songs.length - 1, song_index + 1);
break;
default:
break;
}
music.kill();
image.destroy();
image_left.destroy();
image_right.destroy();
fs.stat(__dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {
if(err){
music = player.play('./songs/' + songs[song_index] + '/' + songs[song_index] + '.mp3', function(err) {})
}
else{
music = player.play('./songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {})
}
});
image = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index] + '/' + songs[song_index] + '.png',
height: '90%',
top: 'center',
left: 'center'
});
image_left = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index - 1] + '/' + songs[song_index - 1] + '.png',
height: '70%',
top: 'center',
left: '-57%'
}); //Fix left offset here. Calculate by screen and image width?
image_right = blessed.ansiimage({
parent: screen,
file: './songs/' + songs[song_index + 1] + '/' + songs[song_index + 1] + '.png',
height: '70%',
top: 'center',
left: '95%'
});
screen.render();
});
screen.key(['enter'], function(ch, key) {
var song = require('./songs/' + songs[song_index] + '/' + songs[song_index] + '.json');
var modes = song.notes.map(function(x) {
return x.mode
}).filter(onlyUnique)
var list = blessed.list({
parent: screen,
keys: true,
left: 'center',
top: 'center',
height: '20%',
align: 'center',
width: '40%',
bg: 'green',
items: modes,
interactive: true,
});
list.focus();
screen.render();
list.on('select', function(item, selected) {
this.destroy();
var difficulties = song.notes.filter(function(x) {
return x.mode === modes[selected]
}).map(function(x) {
return x.difficulty
});
var list = blessed.list({
parent: screen,
keys: true,
left: 'center',
top: 'center',
height: '20%',
align: 'center',
width: '40%',
bg: 'green',
items: difficulties,
interactive: true,
});
list.focus();
screen.render();
list.on('select', function(item, selected2) {
var chosen = song.notes.filter(function(x) {
return x.mode === modes[selected] && x.difficulty === difficulties[selected2]
})[0];
start(song.notes.indexOf(chosen));
})
})
function start(mode) {
fs.stat(__dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.avi', function(err) {
if (err) {
fs.stat(__dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {
if(err){
game.play(
data_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.json',
audio_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.mp3',
bg_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '-bg.png',
mode = mode
);
}
else {
game.play(
data_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.json',
audio_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg',
bg_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '-bg.png',
mode = mode
);
}
});
} else {
fs.stat(__dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg', function(err) {
if(err){
game.play(
data_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.json',
audio_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.mp3',
bg_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.avi',
mode = mode
);
}
else {
game.play(
data_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.json',
audio_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.ogg',
bg_file = __dirname + '/songs/' + songs[song_index] + '/' + songs[song_index] + '.avi',
mode = mode
);
}
});
}
image.destroy();
image_left.destroy();
image_right.destroy();
music.kill();
screen.destroy();
});
}
});
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});