forked from greggman/hft-clean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
189 lines (167 loc) · 4.82 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
"use strict";
const isOSX = process.platform === 'darwin';
const isDevMode = process.env.NODE_ENV === 'development';
var optionSpec = {
options: [
{ option: 'port', alias: 'p', type: 'Int', description: 'port. Default 18679'},
{ option: 'dns', type: 'Boolean', description: 'enable dns server'},
{ option: 'address', type: 'String', description: 'ip address for dns and controller url conversion'},
{ option: 'help', alias: 'h', type: 'Boolean', description: 'displays help'},
{ option: 'private-server', type: 'Boolean', description: 'do not inform happyfuntimes.net about this server. Users will not be able to use happyfuntimes.net to connect to your games'},
{ option: 'debug', type: 'Boolean', description: 'check more things'},
{ option: 'verbose', type: 'Boolean', description: 'print more stuff'},
{ option: 'system-name', type: 'String', description: 'name used if multiple happyFunTimes servers are running on the same network. Default = computer name'},
],
helpStyle: {
typeSeparator: '=',
descriptionSeparator: ' : ',
initialIndent: 4,
},
};
const optionator = require('optionator')(optionSpec);
try {
var args = optionator.parse(process.argv);
} catch (e) {
console.error(e);
process.exit(1); // eslint-disable-line
}
function printHelp() {
console.log(optionator.generateHelp());
process.exit(0); // eslint-disable-line
}
if (args.help) {
printHelp();
}
const happyfuntimes = require('happyfuntimes');
const electron = require('electron');
const querystring = require('querystring');
const webContents = electron.webContents;
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let gameWindow = null;
let setupSteps = 2;
let server;
const state = {};
args.baseDir = __dirname;
happyfuntimes.start(args)
.then((srv) => {
server = srv;
const ports = server.ports;
console.log("Listening on ports:", ports);
state.ports = ports;
state.port = ports[0];
startIfReady();
})
.catch((err) => {
console.error("error starting server:", err);
});
function createWindow() {
const {width: screenWidth, height: screenHeight} = electron.screen.getPrimaryDisplay().workAreaSize;
const space = 50;
const x = space;
const y = space;
const width = screenWidth - space * 2;
const height = screenHeight - space * 2;
gameWindow = new BrowserWindow({
// setting to true doesn't work in Windows
// https://github.com/electron/electron/issues/6036
// fullscreen: false,
fullscreenable: true,
defaultEncoding: "utf8",
x: x,
y: y,
width: width,
height: height,
});
const settings = {
hftUrl: 'ws://localhost:' + state.port,
};
const settingsStr = querystring.stringify(settings);
gameWindow.loadURL(`file://${__dirname}/game.html?${settingsStr}`);
if (isDevMode) {
gameWindow.webContents.openDevTools();
}
// open links in browser
const webContents = gameWindow.webContents;
const handleRedirect = (e, url) => {
if(url != webContents.getURL()) {
e.preventDefault();
electron.shell.openExternal(url);
}
};
webContents.on('will-navigate', handleRedirect);
webContents.on('new-window', handleRedirect);
webContents.on('dom-ready', () => {
if (!isDevMode) {
gameWindow.setFullScreen(true);
}
});
}
function startIfReady() {
--setupSteps;
if (setupSteps === 0) {
setupMenus();
createWindow();
}
}
app.on('ready', () => {
startIfReady();
});
app.on('window-all-closed', () => {
server.close();
app.quit();
});
function setupMenus() {
const menuTemplate = [
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click(item, focusedWindow) {
if (focusedWindow) focusedWindow.reload();
}
},
{
label: 'Toggle Full Screen',
accelerator: isOSX ? 'Ctrl+Command+F' : 'F11',
click(item, focusedWindow) {
if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
},
{
label: 'Toggle Developer Tools',
accelerator: isOSX ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(item, focusedWindow) {
if (focusedWindow)
focusedWindow.webContents.toggleDevTools();
}
},
]
},
];
if (isOSX) {
const name = electron.app.getName();
menuTemplate.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click() { app.quit(); }
},
]
});
}
const menu = electron.Menu.buildFromTemplate(menuTemplate);
electron.Menu.setApplicationMenu(menu);
}