-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
224 lines (221 loc) · 7.22 KB
/
main.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
console.log("Running MOJOWHEEL [@ngiano]! Close this window or press Ctrl+C when you are finished");
//node-modules
const http = require('http'),
//fs = require('fs'),
WebSocket = require('ws'),
path = require("path"),
fs = require('fs-extra'),
opn = require("opn"),
glob = require("glob"),
jsonfile = require("jsonfile");
const wss = new WebSocket.Server({ port: 8080 });
//paths for required files
var index = path.join(__dirname, '/index.html')
var result = path.join(__dirname, '/result.html')
var bitsImage = path.join(__dirname, '/bit.png')
var spinSound = path.join(__dirname, '/spinningsound.ogg')
var config = path.join(__dirname, '/config-1.3.JSON')
var logo = path.join(__dirname, '/logo.svg')
var wheelBase = path.join(__dirname, '/wheel-string-storage.array')
//app workspace
const baseFolder = process.env.APPDATA+'\\MojoWheel';
const dataFolder = process.env.APPDATA+'\\MojoWheel\\data';
//create folders for workspace, if they don't exist already
if (!fs.existsSync(baseFolder)){
fs.mkdirSync(baseFolder);
}
if (!fs.existsSync(dataFolder)){
fs.mkdirSync(dataFolder);
}
//regex
var pattern1 = "^addtogoalwheel (.*)";
var re1 = new RegExp(pattern1,'i');
var pattern2 = "^removefromwheel (.*)";
var re2 = new RegExp(pattern2,'i');
var pattern3 = "^sendconfig ([a-z]+) (.*)";
var re3 = new RegExp(pattern3,'i');
//send local copies of files to workspace
fs.copySync(result,baseFolder+'/result.html');
fs.copySync(spinSound,baseFolder+'/spinningsound.ogg');
fs.copySync(bitsImage,baseFolder+'/bit.png');
function restoreToDefault() {
fs.copySync(config,baseFolder+'/config-1.4.JSON'); //protect file from overwriting each time exe is ran
}
function updateConfigFile() {
jsonfile.readFile(baseFolder+"/config-1.3.JSON", function (err,obj) {
try {
obj[9] = JSON.parse('{"config":"repeatmode","theOption":"true"}');
obj[10] = JSON.parse('{"config":"volume","theOption":"100"}');
jsonfile.writeFileSync(baseFolder+"/config-1.4.JSON", obj);
//fs.unlink(baseFolder+"/config-1.3.JSON"); Don't Delete ATM for backwards compatability
console.log("Config file updated for new release");
} catch(err) {
}
})
}
if(!fs.existsSync(baseFolder+"/config-1.4.JSON") && fs.existsSync(baseFolder+"/config-1.3.JSON")) { //check if config file exists, and if legacy config file exists
updateConfigFile()
} else if(!fs.existsSync(baseFolder+"/config-1.4.JSON")) {
restoreToDefault()
}
if(!fs.existsSync(dataFolder+"/wheel-string-storage.array")) {
fs.copySync(wheelBase,dataFolder+'/wheel-string-storage.array');
console.log("No wheel array file located! Creating a new one. If you were using a previous version of MojoWheel and would like to transfer your options, get the conversion tool on github. A link can be found in the corner of the dashboard")
}
//hosting index.html on localhost:8000 instead of also moving it to workspace
fs.readFile(index, function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
//hosting result.html on localhost:8000 instead of also moving it to workspace
fs.readFile(result, function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8001);
});
//open localhost:8000 in default browser
opn('http://localhost:8000');
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
if (re1.test(message)) {
fs.readFile(dataFolder+"/wheel-string-storage.array", function (err, arrayFile) {
var matches = message.match(pattern1);
arrayFile = JSON.parse(arrayFile);
if(!err) {
arrayFile.push(matches[1]);
fs.writeFile(dataFolder+"/wheel-string-storage.array", JSON.stringify(arrayFile), function (err) {
if (err) console.log('Error: ', err);
});
}
});
};
if (re2.test(message)) {
var matches = message.match(pattern2);
var textToRemove = matches[1]
console.log(textToRemove);
fs.readFile(dataFolder+"/wheel-string-storage.array", function (err, arrayFile) {
if(!err) {
arrayFile = JSON.parse(arrayFile);
var spliceIndex = arrayFile.indexOf(textToRemove);
if(spliceIndex != -1) {
arrayFile.splice(spliceIndex, 1);
fs.writeFile(dataFolder+"/wheel-string-storage.array", JSON.stringify(arrayFile), function (err) {
if (err) console.log('Error: ', err);
});
}
}
});
};
if(message == "requesting") {
fs.readFile(dataFolder+"/wheel-string-storage.array", function (err, arrayFile) {
if(!err) {
wss.clients.forEach(function each(client) {
client.send("sendtowheel "+arrayFile);
});
wss.clients.forEach(function each(client) {
client.send("location "+baseFolder);
});
}
});
}
if(message == "dash-requesting") {
sendConfig();
fs.readFile(dataFolder+"/wheel-string-storage.array", function (err, arrayFile) {
if(!err) {
wss.clients.forEach(function each(client) {
client.send("sendtodash "+arrayFile);
});
}
});
wss.clients.forEach(function each(client) {
client.send("location "+baseFolder);
});
}
if(message == "wheel-requesting") {
sendConfig();
fs.readFile(dataFolder+"/wheel-string-storage.array", function (err, arrayFile) {
if(!err) {
wss.clients.forEach(function each(client) {
client.send("sendtowheel "+arrayFile);
});
wss.clients.forEach(function each(client) {
client.send("location "+baseFolder);
});
}
});
wss.clients.forEach(function each(client) {
client.send("location "+baseFolder);
});
}
if(message == "refresh-wheel") {
wss.clients.forEach(function each(client) {
client.send("refresh-wheel");
});
}
if(message.startsWith("wheelselected")) {
wss.clients.forEach(function each(client) {
client.send(message);
});
}
if(message == "spin") {
wss.clients.forEach(function each(client) {
client.send("spin");
});
}
if(message == "restoretodefault") {
restoreToDefault();
sendConfig();
}
if(message == "config-request") {
sendConfig();
}
if(re3.test(message)) {
var matches = message.match(pattern3);
replaceConfigString(matches[1],matches[2]);
}
if(message == "openDataFolder") {
opn(dataFolder);
}
//wss.clients.forEach(function each(client) {
// if (client !== ws && client.readyState === WebSocket.OPEN) {
// client.send(message);
// }
//});
});
function replaceConfigString(configOption, message) {
jsonfile.readFile(baseFolder+"/config-1.4.JSON", function (err,obj) {
try {
for (var i=0; i< obj.length; i++) {
if (obj[i]['config'] == configOption) {
obj[i]['theOption'] = message;
jsonfile.writeFileSync(baseFolder+"/config-1.4.JSON", obj);
break;
}
}
} catch(err) {
}
});
}
function sendConfig() {
jsonfile.readFile(baseFolder+"/config-1.4.JSON", function (err,obj) {
wss.clients.forEach(function each(client) {
for (var i=0; i< obj.length; i++) {
var configOption = obj[i]['config'];
var message = obj[i]['theOption'];
client.send("config "+configOption+" "+message);
}
});
});
}
});