-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
416 lines (353 loc) · 12.1 KB
/
app.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Chessbot
Copyright (C) 2017 Subtixx (Dominic H.)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var restify = require('restify');
var builder = require('botbuilder');
var Jimp = require("jimp");
var Chess = require('./chess.js').Chess;
var ChessAI = null;
var botEnabled = true;
var botUseAI = true;
if(botUseAI)
{
ChessAI = require('./chess-ai.js');
ChessAI.setSearchTimeLimit(1000);
}
// TODO: Make group conversations into a group chessgame.
//=========================================================
// Bot Setup
//=========================================================
if(botEnabled)
{
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
bot.beginDialogAction('newGame', '/newGame', { matches: /^new game/i });
bot.beginDialogAction('endGame', '/endGame', { matches: /(give up|quit|goodbye)/i });
bot.beginDialogAction('help', '/help', { matches: /(help|what)/i });
bot.beginDialogAction('status', '/status', { matches: /^status/i });
bot.beginDialogAction('move', '/move', { matches: /^move/i });
}
//=========================================================
// Chess Setup
//=========================================================
var rebuildChessboard = false;
var chessboardImg;
var boardWhite;
var boardBlack;
var boardCorner = [];
var boardFont;
var boardFont2;
/*
white = White
- p -> Pawn / Bauer
- r -> Rook / Turm
- n -> Knight / Pferd
- b -> Bishop / Springer
- q -> Queen / Dame
- k -> King / König
black = Black
- p -> Pawn / Bauer
- r -> Rook / Turm
- n -> Knight / Pferd
- b -> Bishop / Springer
- q -> Queen / Dame
- k -> King / König
*/
var pieces = [];
//var chess = new Chess();
var currentBoard;
function RebuildChessboard()
{
if(rebuildChessboard)
{
// Build a 10 x 10 chessboard
new Jimp(320, 320, 0x000, function (err, image) {
//image.blit( src, x, y[, srcx, srcy, srcw, srch] );
image.blit(boardCorner["LT"], 0, 0);
image.blit(boardCorner["RT"], 288, 0);
for(var i = 1; i < 9; i++)
{ // Fill borders
image.blit(boardCorner["MT"], i*32, 0);
image.blit(boardCorner["LM"], 0, i*32);
image.blit(boardCorner["RM"], 288, i*32);
image.blit(boardCorner["MB"], i*32, 288);
}
var white = true;
for(var x = 0; x < 8; x++){
for(var y = 0; y < 8; y++){
if(white)
image.blit(boardWhite, 32+x*32, 32+y*32);
else
image.blit(boardBlack, 32+x*32, 32+y*32);
white = !white;
}
white = !white;
}
image.blit(boardCorner["LB"], 0, 288);
image.blit(boardCorner["RB"], 288, 288);
chessboardImg = image.clone();
image.write("chessboard.png");
});
}
}
function DrawCurrentBoard(session = null, send = false)
{
session.sendTyping(); // Let the user know we're generating his image right now.
new Jimp(352, 352, Jimp.rgbaToInt(0, 0, 0, 0), function (err, image) {
image.blit(chessboardImg, 32, 32);
var chess = new Chess(session.userData.chess);
var board = chess.board();
for(var x = 0; x < board.length; x++)
{
// TODO: This is so stupid. We should have an already made texture with labeling.
image.print(boardFont2, 72+x*32, 1, String.fromCharCode('a'.charCodeAt()+x));
image.print(boardFont, 71+x*32, 0, String.fromCharCode('a'.charCodeAt()+x));
for(var y = 0; y < board[x].length; y++)
{
image.print(boardFont2, 9, 65+y*32, (8-y).toString());
image.print(boardFont, 8, 64+y*32, (8-y).toString());
if(board[y][x] == null) // Skip empty places
continue;
//console.log("X: " + x + " Y: " + y);
//console.log(board[y][x]);
if(board[y][x]["color"] == "w")
image.composite(pieces["white"][board[y][x]["type"]], 64+x*32, 32+y*32);
else
image.composite(pieces["black"][board[y][x]["type"]], 64+x*32, 32+y*32);
}
}
image.getBase64(Jimp.MIME_PNG, function (err, encodedImg) {
if(send)
{
var msg = new builder.Message(session)
.attachments([{
contentType: "image/jpeg",
contentUrl: encodedImg
}]);
session.send(msg);
}
});
//image.write("temp/chess.jpg");
});
}
function ValidateSession(session)
{
if(session == undefined || session == null ||
session.userData == undefined || session.userData == null ||
session.userData.chess == undefined || session.userData.chess == null ||
session.userData.chess == "" || !(new Chess().validate_fen(session.userData.chess)["valid"]))
{
session.send("No game running! Type 'new game' to start a new game!");
session.endDialog();
return false;
}
return true;
}
function Move(session = null, send = false, move = null)
{
if(!ValidateSession(session))
return;
var chess = new Chess(session.userData.chess);
var moveRes = chess.move({ from: move[0], to: move[1] }); // Move user choice!
if(moveRes != null)
{
if(botUseAI)
{
session.sendTyping(); // Let the user know the bot is working...
var botMove = ChessAI.search(chess);
var move = chess.move(botMove);
session.send("I'm moving from " + move["from"] + " to " + move["to"]);
}else{
// Make bot move here
var moves = chess.moves({ verbose: true });
var move = moves[Math.floor(Math.random() * moves.length)];
session.send("I'm moving from " + move["from"] + " to " + move["to"]);
chess.move(move);
}
// Save fen to usersession
session.userData.chess = chess.fen();
DrawCurrentBoard(session, true);
}else{
if(chess.get(move[0])["color"] == "b")
{
session.send("This piece doesn't belong to you! (You're white. Piece is black)");
session.endDialog();
return;
}
var availableMoves = chess.moves({square: move[0]});
if(availableMoves != null && availableMoves != undefined && availableMoves >= 1){
var movesAvailable = availableMoves[0];
if(availableMoves.length > 1){
for(var i = 1; i < availableMoves.length; i++)
movesAvailable = movesAvailable + "," + availableMoves[i];
}
session.send("This would be an illegal move. You could try to move from " + move[0] + " to one of the following: " + movesAvailable);
}else{
session.send("This would be an illegal move. This piece doesn't have any move option.");
}
}
if(chess.game_over())
{
session.send("Game over! "+ (chess.turn() == "b") ? "You won":"I won");
session.userData.chess = "";
}
session.endDialog();
}
function PiecesLoaded()
{
RebuildChessboard();
if(botEnabled)
{
bot.on('contactRelationUpdate', function (message) {
if (message.action === 'add') {
var name = message.user ? message.user.name : null;
var reply = new builder.Message()
.address(message.address)
.text("Hello %s... Thanks for adding me. Type 'new game' to start a new game of chess!", name || 'there');
bot.send(reply);
} else {
session.userData.chess = "";
// delete their data
}
});
bot.dialog('/', function(session){
if(session.userData.chess != "")
{
var moves = session.message.text.split(" ");
if(moves.length == 2)
{
Move(session, true, moves);
}else{
session.send("I don't understand sorry. (Type for example 'e2 e3' to move from e2 to e3");
}
}else{
session.send("Hello! I'm Chessbot written by Subtixx! The following commands are available:");
session.send("'new game', 'move', 'status', 'from to'");
}
session.endDialog();
});
bot.dialog('/help', function(session){
session.send("For more help please visit my Github page!");
var msg = new builder.Message(session)
.textFormat(builder.TextFormat.xml)
.attachments([
new builder.HeroCard(session)
.title("ChessBot Github Page")
.subtitle("Written by Subtixx")
.text("Chessbot is a bot written in Javascript for Node.JS and uses the BotBuilder SDK. It was written to play chess in skype. It generates a visual layout of the chessboard using jimp")
.images([
builder.CardImage.create(session, "https://raw.githubusercontent.com/Subtixx/Chessbot/master/logo.png")
])
.tap(builder.CardAction.openUrl(session, "https://github.com/Subtixx/Chessbot"))
]);
session.send(msg);
});
bot.dialog('/newGame', function(session){
session.userData.chess = new Chess().fen();
DrawCurrentBoard(session, true);
session.endDialog();
});
bot.dialog('/endGame', function(session){
session.userData.chess = "";
session.send("HaHa that means I win! Thank you for playing!");
session.endDialog();
});
bot.dialog('/status', function (session) {
if(!ValidateSession(session))
return;
DrawCurrentBoard(session, true);
session.endDialog();
});
bot.dialog('/move', [
function(session) {
if(!ValidateSession(session))
return;
if(new Chess(session.userData.chess).turn() == "w")
builder.Prompts.text(session, 'How do you want to move?');
else{
session.send("Not your turn!");
session.endDialog();
}
},
function(session, results) {
var moves = results.response.split(" ");
if(moves.length == 2)
{
Move(session, true, moves);
}else{
session.send("I didn't understand, sorry.");
}
session.endDialog();
console.log(results);
}
]);
}else{
DrawCurrentBoard();
}
}
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(function (font) {
boardFont2 = font;
Jimp.loadFont(Jimp.FONT_SANS_32_WHITE).then(function (font) {
boardFont = font;
Jimp.read("images/chess_pieces.png", function (err, lenna) {
if (err) throw err;
pieces["white"] = [];
pieces["white"]["p"] = lenna.clone().crop(0, 0, 32, 64);
pieces["white"]["r"] = lenna.clone().crop(32, 0, 32, 64);
pieces["white"]["n"] = lenna.clone().crop(64, 0, 32, 64);
pieces["white"]["b"] = lenna.clone().crop(96, 0, 32, 64);
pieces["white"]["q"] = lenna.clone().crop(128, 0, 32, 64);
pieces["white"]["k"] = lenna.clone().crop(160, 0, 32, 64);
pieces["black"] = [];
pieces["black"]["p"] = lenna.clone().crop(0, 64, 32, 64);
pieces["black"]["r"] = lenna.clone().crop(32, 64, 32, 64);
pieces["black"]["n"] = lenna.clone().crop(64, 64, 32, 64);
pieces["black"]["b"] = lenna.clone().crop(96, 64, 32, 64);
pieces["black"]["q"] = lenna.clone().crop(128, 64, 32, 64);
pieces["black"]["k"] = lenna.clone().crop(160, 64, 32, 64);
if(rebuildChessboard)
{
Jimp.read("images/chess_background.png", function (err, lenna) {
boardWhite = lenna.clone().crop(0, 0, 32, 32);
boardBlack = lenna.clone().crop(0, 32, 32, 32);
boardCorner["LT"] = lenna.clone().crop(32, 0, 32, 32);
boardCorner["MT"] = lenna.clone().crop(64, 0, 32, 32);
boardCorner["RT"] = lenna.clone().crop(96, 0, 32, 32);
boardCorner["LM"] = lenna.clone().crop(32, 32, 32, 32);
boardCorner["MM"] = lenna.clone().crop(64, 32, 32, 32);
boardCorner["RM"] = lenna.clone().crop(96, 32, 32, 32);
boardCorner["LB"] = lenna.clone().crop(32, 64, 32, 32);
boardCorner["MB"] = lenna.clone().crop(64, 64, 32, 32);
boardCorner["RB"] = lenna.clone().crop(96, 64, 32, 32);
PiecesLoaded();
});
}else{
Jimp.read("images/chessboard.png", function (err, lenna) {
chessboardImg = lenna.clone();
PiecesLoaded();
});
}
});
});
});