This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
220 lines (179 loc) · 5.92 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
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const isDev = !app.isPackaged;
const DiscordRPC = require("discord-rpc");
// everyone.dance discord client ID
const clientId = '833557751849418764';
DiscordRPC.register(clientId);
const rpc = new DiscordRPC.Client({ transport: 'ipc' });
const startTimestamp = new Date();
let rpc_ready = false;
rpc.on('ready', () => {
rpc_ready = true;
});
rpc.login({ clientId }).catch(console.error);
if (isDev) {
require('electron-reload')(__dirname, {
electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
})
}
let win;
function createWindow() {
win = new BrowserWindow({
width: 1024,
height: 720,
title: 'everyone.dance',
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: true,
preload: path.join(__dirname, 'src', 'js', 'preload.js'),
backgroundThrottling: false
},
icon: path.join(__dirname, 'src', require('os').platform() == 'darwin' ? 'favicon.icns' : 'favicon.ico'),
show: false
// frame: false // Looks nice, but the drag css properties have issues. Might look into later
})
if (!isDev) {
win.removeMenu(); // Only enable in dev mode. Otherwise, it removes the devtools menu
}
win.loadFile('src/index.html');
win.on('ready-to-show', () => {
win.show();
})
win.on('close', () => {
Object.values(popout_windows).forEach((window) => {
window.close();
});
})
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})
ipcMain.on('close-window', (_, message) => {
app.quit();
})
const popout_data = {}
const popout_windows = {}
const window_id_to_player_id = {}
function GetWindowKey(id, p2) {
return `${id}${p2 ? '2' : ''}`
}
ipcMain.on('popout-player', (_, args) => {
const key = GetWindowKey(args.id, args.p2);
// Close popout window if it is open
if (typeof popout_windows[key] != 'undefined') {
popout_windows[key].close();
return;
}
const popout_window = new BrowserWindow({
// parent: win,
title: 'everyone.dance Card',
width: 700,
height: 300,
title: `everyone.dance (${args.name})`,
transparent: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
contextIsolation: true,
preload: path.join(__dirname, 'src', 'js', 'preload.js'),
backgroundThrottling: false
},
icon: path.join(__dirname, 'src', require('os').platform() == 'darwin' ? 'favicon.icns' : 'favicon.ico'),
frame: false,
alwaysOnTop: true,
show: false,
maximizable: false,
fullscreenable: false
})
popout_data[key] = args
if (!isDev) {
popout_window.removeMenu(); // Only enable in dev mode. Otherwise, it removes the devtools menu
}
popout_window.loadFile('src/index.html');
popout_window.on('close', () => {
delete window_id_to_player_id[popout_window.id];
delete popout_windows[key];
})
popout_windows[key] = popout_window;
window_id_to_player_id[popout_window.id] = key;
})
// Pass on data from main window to child windows
ipcMain.on('window-ready', (window_data, ...args) => {
if (window_data.sender.id > 1) {
const player_id = window_id_to_player_id[window_data.sender.id];
const window = popout_windows[player_id];
window.send('update-popout-info', popout_data[player_id]);
window.send('game-data', latest_game_data);
window.show();
}
})
ipcMain.on('update-popout-size', (window_data, args) => {
if (window_data.sender.id > 1) {
const player_id = window_id_to_player_id[window_data.sender.id];
const window = popout_windows[player_id];
const current_size = window.getContentSize()
if (Math.abs(current_size[0] - args.width) > 2 || Math.abs(current_size[1] - args.height) > 2) {
window.setContentSize(args.width, args.height);
}
}
})
let latest_game_data = {};
// Pass on data from main window to child windows
ipcMain.on('game-data', (_, ...args) => {
Object.keys(args[0]).forEach((key) =>
{
latest_game_data[key] = args[0][key];
})
Object.values(popout_windows).forEach((window) => {
window.send('game-data', ...args);
});
})
ipcMain.on('discord-data', (_, ...args) => {
const data = args[0];
latest_game_data.app_state = data.app_state;
latest_game_data.game_code = data.game_room_data.game_code;
UpdateRichPresence();
})
function UpdateRichPresence()
{
const data = latest_game_data;
if (typeof data == 'undefined') {return;}
const details = data.app_state == 1 ? 'In Game Room' : 'In Main Menu';
const activity = {
details: details,
startTimestamp,
largeImageKey: 'favicon',
instance: true,
// The buttons show up but don't do anything when clicked...
// If they worked, people could join with one click!
// buttons: [
// {
// label: 'Join',
// url: 'https://everyone.dance'
// },
// {
// label: 'Spectate',
// url: 'https://everyone.dance'
// }
// ]
}
if (data.app_state == 1 && typeof data.options != 'undefined' && typeof data.players != 'undefined')
{
if (data.options.show_game_code)
{
activity.state = `Game Code: ${data.game_code}`;
}
activity.largeImageText = `${Object.keys(data.players).length} Players`;
}
rpc.setActivity(activity);
}