-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
398 lines (370 loc) · 11.1 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
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
'use strict'
var MongoClient = require('mongodb').MongoClient,
assert = require('assert'),
gameCode = require('./gameCode'),
chess = require('./chess'),
fs = require('fs'),
imageGenerator = require('./imageGenerator.js');
const express = require('express'),
bodyParser = require('body-parser'),
request = require('request'),
Curl = require('node-libcurl').Curl,
app = express(),
token = process.env.PAGE_ACCESS_TOKEN,
mongoURI = process.env.MONGODB_URI;
MongoClient.connect(mongoURI , function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to Mongo server");
db.close();
});
app.set('port', (process.env.PORT || 5000))
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function(req, res) {
res.send('App Online!')
})
// for Facebook verification
app.get('/webhook/', function(req, res) {
if (req.query['hub.mode'] && req.query['hub.verify_token'] === process.env.VERIFY_TOKEN) {
res.status(200).send(req.query['hub.challenge'])
} else {
res.send('Error, wrong token')
}
})
app.post('/webhook/', function(req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i];
let sender = event.sender.id;
let timestamp = event.timestamp;
if (event.message && event.message.text) {
let text = event.message.text;
messageHandler(sender, text);
// logMessage(sender, text, timestamp);
// repeatLastMessages(sender);
}
}
res.sendStatus(200)
})
function logMessage(sender, text, timestamp){
MongoClient.connect(mongoURI , function(err, db) {
assert.equal(null, err);
var message = { "sender": sender, "text":text, "timestamp":timestamp};
db.collection("datamine").insertOne(message, function(err, res) {
if (err) throw err;
console.log("MESSAGE WAS LOGGED");
db.close();
})
});
}
function repeatLastMessages(sender){
MongoClient.connect(mongoURI , function(err, db) {
assert.equal(null, err);
//Find latest 3 message from this user
var cursor = db.collection("datamine").find({"sender":sender}).sort({"timestamp":-1}).limit(3);
cursor.toArray(function(err, results) {
if (err) throw err;
var message = "";
//Compress messages to one message and send it
for(var i = 0; i < results.length; i++){
message += results[i].text + "\n";
}
sendTextMessage(sender, message);
db.close();
})
});
}
function messageHandler(sender, text){
//array of split terms from the command
let textSplit = text.toLowerCase().split(" ");
switch(textSplit[0]){
case "hey":
sendTextMessage(sender, "Hey!" + sender.toString());
break;
case "create":
var newGameCode = gameCode.genCode();
console.log(newGameCode);
sendTextMessage(sender, newGameCode.toString());
initGame(sender, newGameCode);
break;
case "accept": //accept should have bulletproofing that game with same p1 and p2 already exists
gameCode.acceptGame(text.split(" ")[1], sender.toString()).then(() => {
sendTextMessage(sender, "Game Started!");
//Display shit eh?
}).catch((e) => {
console.log("Unabled ot start game " + e.toString());
sendTextMessage(sender, "Unable to start game: " + e.toString());
})
try {
gameCode.acceptGame(textSplit[1]);
//LIKE DISPLAY GAME OR SOME SHIT?
}
catch (e) {
sendTextMessage(sender, "Unable to start game" + e);
}
break;
case "board":
//sendTestImage(sender);
getGame(sender).then((game) => {
sendBoard(sender, game.board);
}).catch((err) => {
console.log("board", err);
});
break;
case "move":
var movePhrase = textSplit[1];
if(movePhrase === undefined){
sendTextMessage(sender, "Bad move phrase.");
break;
}
getGame(sender).then((game) => {
if(isValidMove(game, movePhrase, sender)){
updateGame(chess.incrementGame(game, movePhrase))
.then(messagePlayers(game, movePhrase))
.catch(error => {
console.log(error.message);
sendTextMessage(sender, "Failed to submit move");
})
} else {
sendTextMessage(sender, "Invalid move");
}
})
.catch(error => {
console.log(error.message);
});
/*
getGameInfo(sender, textSplit[1])
//issue here where i need textsplit[1] and sender
.then(moveIfValid.bind(null, textSplit[1]))
.then(updateGame)
.then(tellBothSides)
.catch(error => {
console.log(error.message);
});*/
break;
case "resign":
getGame(sender).then((game) => {
var resigner = (sender === game.white) ? "White" : "Black";
var resignee = (sender === game.white) ? "Black" : "White";
sendTextMessage(game.white, resigner + " resigned, game over, "+resignee+" wins.");
sendTextMessage(game.black, resigner + " resigned, game over, "+resignee+" wins.");
}).then(deleteGame(sender));
break;
case "draw":
getGame(sender).then((game) => {
game.drawOffered = true;
return game;
}).then(updateGame)
.catch(error =>{
console.log(error.message);
});
break;
case "drawaccept":
var white;
var black;
//gets the game, tries to delete the game, then tells both players of draw accepting
getGame(sender)
.then((game) =>{
white = game.white;
black = game.black;
})
.then(deleteGame(sender))
.then(() => {
sendTextMessage(white, "Draw accepted, game over.");
sendTextMessage(black, "Draw accepted, game over.");
})
.catch(error =>{
console.log(error.message);
});
break;
case "help":
sendHelp(sender);
break;
default:
sendTextMessage(sender, "That's no command, type \"help\" for a list of commands");
break;
}
}
function initGame(sender, gameCode){
MongoClient.connect(mongoURI , function(err, db) {
assert.equal(null, err);
var senderID = sender.toString();
var game = {"white": senderID,
"board": chess.initBoard(),
"gameCode": gameCode,
"turnNum": 0,
"isCheck": false,
"drawOffered": false,
"movePiece": 0,
"moveLocationX": 0,
"moveLocationY": 0}
db.collection("games").insertOne(game, function(err, res) {
if (err) throw err;
console.log("NEW GAME ADDED! CHALLENGER:" + sender.toString);
db.close();
})
});
}
function getGame(sender){
return new Promise((resolve, reject) => {
MongoClient.connect(mongoURI).then((db) => {
var collection = db.collection('games');
collection.findOne({$or: [{"white": sender}, {"black": sender}]})
.then(resolve)
.catch(function(){
reject(new Error('getGame: Game not found'));
});
}).catch((err) => {
console.log("Opening GameDB getGame: ", err);
reject(new Error('getGame: games db not opening'));
})
});
}
function getGameInfo(sender, movePhrase){
return new Promise((resolve, reject) => {
MongoClient.connect(mongoURI, function (err, db) {
if(err){
console.log("Opening GameDB getMoverInfo: ", err);
reject(new Error('getGameInfo: games db not opening'));
}
else {
var collection = db.collection('games');
collection.findOne({$or: [{"white": sender}, {"black": sender}]})
.then(function(gameInfo){
console.log(gameInfo);
resolve({"sender": sender, "movePhrase": movePhrase, "gameInfo": gameInfo});
}).catch(function(){
reject(new Error('getGameinfo: Game not found'));
});
}
});
});
}
function isValidMove(game, movePhrase, sender){
var board = game.board;
var color = (game.turnNum % 2 === 0) ? "w" : "b";
var moveInfo = chess.getMoveInfo(movePhrase, board);
if((color === "b" && game.black !== sender)
|| (color === "w" && game.white !== sender)){
sendTextMessage(sender, "It's not your turn");
return false;
}
if(chess.isValidMove(movePhrase, color, board)){
return true;
} else {
sendTextMessage(sender, "That's an invalid move!");
return false;
}
}
function updateGame(game){
return new Promise((resolve, reject) => {
MongoClient.connect(mongoURI).then((db) => {
var collection = db.collection('games');
collection.updateOne({ $or: [{ "white": game.white }] }, game)
.then(resolve(game))
.catch((err) => {
console.log(err);
reject(err);
})
}).catch((err) => {
console.log("updating GameDB updateGame: ", err);
reject(new Error('getGame: games db not opening'));
})
});
}
function deleteGame(sender){
return new Promise((resolve, reject) => {
MongoClient.connect(mongoURI).then((db) => {
var collection = db.collection('games');
collection.deleteOne({ $or: [{ "white": sender }, { "black": sender }]})
.then(resolve)
.catch((err) => {
console.log(err);
reject(err);
})
}).catch((err) => {
console.log("Deleting game record deleteGame: ", err);
reject(new Error('getGame: games db not opening'));
})
});
}
function messagePlayers(game, movePhrase){
var mover = ((game.turnNum-1) % 2 === 0) ? "White" : "Black";
var movee = ((game.turnNum) % 2 === 0) ? "White" : "Black";
var isCheckPhrase = game.isCheck ? (movee + " is in Check.\n") : "";
sendTextMessage(game["white"],
mover + " move: " + movePhrase + " " + isCheckPhrase);
sendBoard(game["white"], game.board);
sendTextMessage(game["black"],
mover + " move: " + movePhrase + " " + isCheckPhrase);
sendBoard(game["black"], game.board);
}
function sendTextMessage(sender, text) {
let messageData = {
text: text
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: token
},
method: 'POST',
json: {
recipient: {
id: sender
},
message: messageData
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
//TODO better error handling?
function sendImage(sender, image, filename){
var r = request.post({
url:'https://graph.facebook.com/v2.6/me/messages?',
qs:{access_token: token}
},
function (error, response, body) {
if (error) {
console.log('Error sending Image: ', error);
} else if (response.body.error) {
console.log('Error sending Image: ', response.body.error);
}
});
var form = r.form();
form.append('recipient', '{"id":"' + sender + '"}');
form.append('message', '{"attachment":{"type":"image", "payload":{}}}');
form.append('filedata', image, { "filename": filename });
}
function sendBoard(sender, board){
imageGenerator.createImage(board).then((image) => {
sendImage(sender, image, "board.png");
});
}
function sendTestImage(sender) {
imageGenerator.createImage("danny i need a board duh").then(function (image) {
sendImage(sender, image, "test.png");
});
}
function sendHelp(sender){
sendTextMessage(sender, "create - gives you a game code to share with another");
sendTextMessage(sender, "accept - accepts given game code to start game");
sendTextMessage(sender, "board - gives you an image of the current board");
sendTextMessage(sender, "move a2a3 - move a piece, if it is a valid move, format [start][destination] is used for each coordinate");
sendTextMessage(sender, "resign - resigns your game");
sendTextMessage(sender, "draw - offers to make the game a draw to opponent");
}