-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelectron.js
129 lines (111 loc) · 3.58 KB
/
electron.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
const {app, BrowserWindow, ipcMain, ipcRenderer} = require('electron')
const path = require('path')
const express = require('express')
var bodyParser = require('body-parser')
const fs = require("fs")
let serverStarted = false;
let mainWindow;
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
var configData = {
port: 4444,
scene: null,
password: null,
covers: {
draft: null,
minimap: null,
},
};
const configFile = `${app.getAppPath()}/config.json`
function createWindow() {
mainWindow = new BrowserWindow({
width: 400,
height: 775,
resizable: false,
fullscreenable: false,
backgroundColor: '#1f1f1f',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
webSecurity: false
},
})
mainWindow.loadURL('http://127.0.0.1:8000');
//mainWindow.webContents.openDevTools()
loadConfig()
}
function loadConfig() {
fs.exists(configFile, exists => {
if (exists) {
console.log(`Loaded config from: ${configFile}`)
fs.readFile(configFile, 'utf8', (err, data) => {
if (err) {
return console.error(err);
}
configData = JSON.parse(data);
})
} else {
console.log("Config file not exists! creating one.")
fs.writeFile(configFile, JSON.stringify(configData), (err, data) => {
if (err) {
return console.error(err)
}
})
}
})
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
ipcMain.on('update_config', (_, config) => {
configData = config;
fs.writeFile(configFile, JSON.stringify(config), (err, data) => {
if (err) {
return console.error(err)
}
});
})
ipcMain.on('config', () => {
mainWindow.webContents.send('config_loaded', {config: configData})
});
ipcMain.on('start-server', (_, config) => {
//console.log(config);
if (!serverStarted) {
const httpApp = express()
httpApp.use(bodyParser.json());
httpApp.get('/', (req, res) => {
const data = req.body;
if (data.map.name === "start") {
switch (data.map.game_state) {
case "DOTA_GAMERULES_STATE_HERO_SELECTION":
console.log("Choosing hero...")
mainWindow.webContents.send('toggle_drafting_cover', {visible: true});
mainWindow.webContents.send('toggle_minimap_cover', {visible: false});
break
case "DOTA_GAMERULES_STATE_PLAYING":
mainWindow.webContents.send('toggle_drafting_cover', {visible: false});
mainWindow.webContents.send('toggle_minimap_cover', {visible: true});
break
case "DOTA_GAMERULES_STATE_POST_GAME":
mainWindow.webContents.send('toggle_drafting_cover', {visible: false});
mainWindow.webContents.send('toggle_minimap_cover', {visible: false});
break
default:
mainWindow.webContents.send('toggle_drafting_cover', {visible: false})
mainWindow.webContents.send('toggle_minimap_cover', {visible: false});
break
}
}
res.sendStatus(200)
})
httpApp.listen(31558, () => console.log('Server running on port 31558!'))
serverStarted = true;
}
})
app.on('window-all-closed', () => {
if(process.platform !== 'darwin') app.quit()
})
});