summaryrefslogtreecommitdiff
path: root/trumagic.js
blob: 14bc2f38cdb1ec0eecb4f476a18df96cc5c2fe38 (plain) (blame)
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
const electron = require('electron');
const url = require('url');
const path = require('path');
const exec = require('child_process').exec;
const fs = require('fs');

const {app, BrowserWindow, Menu, ipcMain} = electron;

// SET ENV
process.env.NODE_ENV = 'production';

let mainWindow;

// Listen for app to be ready
app.on('ready', function() {
  // Create new window
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    frame: false,
    resizable: false,
    titleBarStyle: 'hidden-inset'
  });
  // Load html into window
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'mainWindow.html'),
    protocol: 'file:',
    slashes: true
  }));

  // Build menu from template
  const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  // Insert Menu
  Menu.setApplicationMenu(mainMenu)

});

// Handle creating the path window
function createChangeWindow() {
  pathWindow = new BrowserWindow({
    width: 400,
    height: 280,
    frame: false,
    titleBarStyle: 'hidden-inset',
    resizable: false,
    title: 'Specify Path'
  });
  // Load html
  pathWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'addWindow.html'),
    protocol: 'file:',
    slashes: true
  }));
  pathWindow.on('close', function() {
    pathWindow = null;
  });
}

// Path configuration
let config;

reloadConfig = (callback) => {
  fs.readFile('./config.json', 'utf-8', (err, data) => {
    if (err) throw err;
    config = JSON.parse(data);
    if (callback) {
      callback();
    }
  });
}

saveConfig = (configSettings) => {
  let content = JSON.stringify(configSettings);
  fs.writeFile("./config.json", content, 'utf8', function (err) {
    if (err) {
      return console.log(err);
    }
    console.log("Config updated");
  });
  reloadConfig(() => {
    console.log("Reloading config");
  });
}

// Initially load config
reloadConfig(() => {
  if (!config.customPath) {
    config.customPath = process.platform == 'darwin' ? config.macDefault : config.winDefault;
    saveConfig(config);
  }
});

// Catch start game
ipcMain.on('game:start', function (e) {
  console.log('starting shit');
  let command;
  if (process.platform == 'darwin') {
    command = `cd ${config.customPath}/Bin && wine WizardGraphicalClient.exe -L login.us.wizard101.com 12000 -A English`;
  }
  else {
    command = `${config.customPath}\Bin\WizardGraphicalClient.exe -L login.us.wizard101.com 12000 -A English`
  }

  mainWindow.webContents.send('started');

  exec(command,
    (error, stdout, stderr) => {
      console.log(`${stdout}`);
      console.log(`${stderr}`);
      if (error !== null) {
        console.log(`exec error: ${error}`);
      }
    }
  );
});

// Path change
ipcMain.on('path:change', function(e, newPath) {
  config.customPath = newPath; 
  saveConfig(config);
});

// Path request
ipcMain.on('request:reset', function(e) {
  let resetPath = process.platform == 'darwin' ? config.macDefault : config.winDefault;
  pathWindow.webContents.send('reset:path', resetPath);
});

// Path request
ipcMain.on('request:current', function(e) {
  let currentPath = config.customPath;
  pathWindow.webContents.send('current:path', currentPath);
});

// Catch quit
ipcMain.on('app:quit', function(e) {
  app.quit();
});

// Catch minimize
ipcMain.on('app:minimize', function(e) {
  mainWindow.minimize();
});


// Create menu template
const mainMenuTemplate = [
  {
    label: 'File',
    submenu: [
      {
        label: 'Specify Path',
        accelerator: process.platform == 'darwin' ? 'Command+M' : 'Ctrl+M',
        click() {
          createChangeWindow();
        }
      },
      {
        label: 'Quit',
        accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
        click(){
          app.quit();
        }
      }
    ]
  }
];

// If mac, add empty object to menu
if (process.platform == 'darwin') {
  mainMenuTemplate.unshift({});
}

let prod = true;
// Add developer tools item if not in production
if (prod) {
  mainMenuTemplate.push({
    label: 'Developer Tools',
    submenu: [
      {
        label: 'Toggle DevTools',
        accelerator: process.platform == 'darwin' ? 'Command+I' : 'Ctrl+I',
        click(item, focusedWindow){
          focusedWindow.toggleDevTools();
        }
      },
      {
        role: 'reload'
      }
    ]
  });
}