-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
173 lines (142 loc) · 4.35 KB
/
index.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
var path = require('path');
var { app, BrowserWindow, session } = require('electron');
var { autoUpdater } = require('electron-updater');
var log = require('electron-log');
var { ElectronBlocker } = require('@cliqz/adblocker-electron');
var fetch = require('cross-fetch');
const cors = require('cors');
const express = require('express');
const stockfish = require("./scripts/stockfish");
const engine = stockfish();
class AppUpdater {
constructor() {
log.transports.file.level = 'info';
autoUpdater.logger = log;
autoUpdater.checkForUpdatesAndNotify();
}
}
let mainWindow = null;
ElectronBlocker.fromPrebuiltAdsAndTracking(fetch)
// eslint-disable-next-line promise/always-return
.then((blocker) => {
blocker.enableBlockingInSession(session.defaultSession);
})
.catch((err) => {
console.log(err);
});
if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
}
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
require('electron-debug')();
}
const installExtensions = async () => {
const installer = require('electron-devtools-installer');
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS'];
return Promise.all(
extensions.map((name) => installer.default(installer[name], forceDownload))
).catch(console.log);
};
const createWindow = async () => {
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
await installExtensions();
}
mainWindow = new BrowserWindow({
width: 1024,
height: 728,
// fullscreen: true,
// frame: true,
transparent: false,
webPreferences: {
webSecurity: true,
nodeIntegration: false,
// allowRunningInsecureContent: true,
devTools: true,
preload: path.join(__dirname, 'preload/preload.js'),
},
});
mainWindow.webContents.openDevTools();
mainWindow.loadURL(`https://chess.com/live`, {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
});
mainWindow.webContents.on('did-finish-load', function() {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
});
mainWindow.on('closed', () => {
mainWindow = null;
});
new AppUpdater();
};
/**
* Add event listeners...
*/
app.on('window-all-closed', () => {
// Respect the OSX convention of having the application in memory even
// after all windows have been closed
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('ready', createWindow);
app.on('activate', () => {
// 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 (mainWindow === null) createWindow();
});
const expressApp = express();
expressApp.listen(3000);
expressApp.use(express.urlencoded());
expressApp.use(cors({credentials: true, origin: 'https://www.chess.com'}));
let previousResponse;
let firstMove = true;
expressApp.get('/stockfish', (req, res) => {
const uciMessage = decodeURIComponent(req.query.uci);
function send(str)
{
console.log(`Sending: ${str}`)
engine.postMessage(str);
}
if (process.argv[2] === "--help") {
console.log("Usage: node simple_node.js [FEN OR move1 move2 ...moveN]");
console.log("");
console.log("Examples:");
console.log(" node simple_node.js");
console.log(" node simple_node.js \"rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2\"");
console.log(" node simple_node.js g1f3 e7e5");
process.exit();
}
engine.onmessage = function(line){
console.log(`Line: ${line}`)
if (typeof line !== "string") {
console.log("Got line:");
console.log(typeof line);
console.log(line);
}
if(line.includes('bestmove') || line.includes('bestmove') && firstMove){
previousResponse = line;
res.end(line);
}
}
if(uciMessage === 'position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'){
console.log('first message true')
firstMove = true;
} else {
console.log('first message false')
firstMove = false;
}
send(uciMessage);
if(uciMessage.includes('position')){
send('go depth 15');
// send("d");
}
})