-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
273 lines (210 loc) · 6.06 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//var ___prodution = true;
var ___prodution = false;
var fs = require('fs');
var Menu = require('menu');
var Tray = require('tray');
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var tray = null;
var app = require('app'); // Module to control application life.
// Report crashes to our server.
require('crash-reporter').start();
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is GCed.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
var timeout = null;
var RAFFLES = {};
var NICKNAME = null;
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
tray = new Tray(__dirname+'/favicon_64x64.png');
tray.setToolTip('CSGORage - BOT');
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1280,
height: 720,
show: !___prodution,
'node-integration': false
});
mainWindow.loadUrl('http://csgorage.com/free-raffles/current');
if (!___prodution){
mainWindow.openDevTools();
}
mainWindow.on('closed', function() {
mainWindow = null;
});
mainWindow.webContents.on('did-finish-load', function () {
var url = this.getUrl();
if (url.indexOf('steamcommunity.com/openid/login') !== -1) {
mainWindow.show();
clearTimeout(timeout);
timeout = null;
} else if (url === 'http://csgorage.com/free-raffles/current') {
if (___prodution){
mainWindow.hide();
}
this.executeJavaScript('(function () {'+ fs.readFileSync(__dirname+'/login.bundle.js') +'})()');
timeout = setTimeout(function () {
mainWindow.loadUrl('http://csgorage.com/free-raffles/current');
}, 60 * 1000);
}
});
});
function destroyRaffle(id) {
closeRaffle(id);
if (RAFFLES[id]) {
delete RAFFLES[id];
}
}
function closeRaffle(id) {
if (RAFFLES[id]) {
if (RAFFLES[id]['window']) {
RAFFLES[id]['window'].close();
}
RAFFLES[id]['window'] = null;
}
}
function renderTray() {
var template = [
{ label: NICKNAME, icon: __dirname+'/profile.png', },
{ type: 'separator' }
];
Object.keys(RAFFLES).forEach(function(id) {
var raffle = RAFFLES[id];
var progress = Math.floor((raffle.progressValue / raffle.progressMax) * 100);
var label = raffle.label + ' '+progress+'%';
if (raffle.slots.length > 0) {
label = '#'+raffle.slots.join(', #') + ' ' + label;
}
var icon;
if (raffle.lock) {
icon = __dirname+'/lock.png';
} else {
if (raffle.ribbon) {
icon = __dirname+'/checked-yes.png';
} else {
icon = __dirname+'/checked-no.png';
}
}
template.push({ icon: icon, label: label });
});
if (Object.keys(RAFFLES).length > 0) {
template.push({ type: 'separator' });
}
template.push({
icon: __dirname+'/exit.png',
label: 'Exit',
click: function () {
app.quit();
}
});
var contextMenu = Menu.buildFromTemplate(template);
tray.setContextMenu(contextMenu);
}
function createRaffle(data) {
var id = data.raffle;
console.log('------ createRaffle('+id+') ------');
RAFFLES[id] = data;
R = RAFFLES[id];
R.slots = [];
R.lock = false;
R['window'] = null;
requestRaffle(id);
}
function updateRaffle(id, data) {
console.log('------ updateRaffle('+id+') ------');
console.log(JSON.stringify(data));
var R = RAFFLES[id];
Object.keys(data).forEach(function (key) {
R[key] = data[key];
});
if (!R.lock && !R.ribbon) {
requestRaffle(id);
}
}
function requestRaffle(id) {
if (RAFFLES[id]['window']) {
return;
}
console.log('------ requestRaffle('+id+') ------');
var win = new BrowserWindow({
width: 1280,
height: 720,
show: !___prodution,
'node-integration': false
});
RAFFLES[id]['window'] = win;
var url = 'http://csgorage.com'+RAFFLES[id].href;
win.loadUrl(url);
win.on('closed', function() {
if (RAFFLES[id]) {
RAFFLES[id]['window'] = null;
}
win = null;
});
win.webContents.on('did-finish-load', function () {
this.executeJavaScript('(function () {'+ fs.readFileSync(__dirname+'/raffle.bundle.js') +'})()');
});
}
var io = require('socket.io')();
io.on('connection', function(socket){
socket.on('raffles', function (payload) {
NICKNAME = payload.nickname;
payload.raffles.forEach(function(raffle) {
var R = RAFFLES[raffle.raffle];
if (!R) {
createRaffle(raffle);
} else {
updateRaffle(raffle.raffle, raffle);
}
});
Object.keys(RAFFLES).forEach(function (id) {
var test = false;
payload.raffles.forEach(function (raffle) {
test = test || (raffle.raffle == id);
});
if (!test) {
destroyRaffle(id);
}
});
renderTray();
});
socket.on('raffle update', function (payload) {
closeRaffle(payload.raffle);
updateRaffle(payload.raffle, payload);
renderTray();
});
socket.on('raffle error', function (payload) {
closeRaffle(payload.raffle);
console.log('-----------------ERROR 224-------------------');
});
socket.on('raffle register', function (payload) {
closeRaffle(payload.raffle);
console.log('-----------------raffle register-------------------');
if (payload.error) {
console.log('-----------------ERROR 231-------------------');
console.log(payload.error);
return;
}
var R = RAFFLES[payload.raffle];
updateRaffle(payload.raffle, {
ribbon: true,
slots: payload.slots
});
tray.displayBalloon({
title: 'CSGORage - BOT',
content: 'Raffle '+R.label+' #'+payload.raffle+'\nslots: #'+payload.slots.join(', #')
});
renderTray();
});
});
io.listen(3000);
// document.body.scrollTop = document.body.scrollHeight