-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
69 lines (60 loc) · 2.05 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
// imports
var http = require('http');
var firebase = require('firebase');
var express = require('express');
var swig = require('swig');
var chess = require('chess');
var shortid = require('shortid');
// initialize web app
var app = express();
// static files in 'static' directory
app.use('/static', express.static('static'));
// we use the 'swig' template engine for rendering files
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
// setup firebase client
var config = {
apiKey: 'AIzaSyACofSRECHHqlF7SWrQkr48ZaHX7ps7LYY',
serviceAccount: `${__dirname}/chess-game-b00bdb1d898a.json`,
databaseURL: 'https://chess-game-2dce5.firebaseio.com',
};
firebase.initializeApp(config);
// routes go here
app.get('/', function (req, res) {
firebase.database().ref('games/').once('value', snapshot => {
res.render('index.html', {'games': snapshot.val()});
console.log(snapshot.val());
});
});
app.get('/new-game', function (req, res) {
const gameId = shortid.generate();
firebase.database().ref(`games/${gameId}`).set({
'fen': ['rnbqkbnr', 'pppppppp', '8', '8', '8', '8', 'PPPPPPPP', 'RNBQKBNR'],
'id': gameId,
'turn': 'white'
});
res.redirect(`/game/${gameId}_W`);
});
app.get('/games', function (req, res) {
});
app.get('/game/:gameId', function (req, res) {
const fullId = req.params.gameId;
const gameId = fullId.substring(0, fullId.length - 2);
const isWhite = fullId.substring(fullId.length - 2) === '_W';
console.log('getting ', gameId);
firebase.database().ref(`games/${gameId}`).once('value', snapshot => {
console.log('got ', snapshot.val());
res.render('game.html', {
'opponentId': gameId + (isWhite ? '_B' : '_W'),
'fullId': fullId,
'gameId': gameId,
'player': isWhite ? 'white' : 'black',
'gameObj': snapshot.val(),
});
})
});
// server listens on port 8080
app.listen(8080, function () {
console.log('Listening on port 8080...');
});