diff --git a/controller/ajax.js b/controller/ajax.js index b2de8eb..4b6923c 100644 --- a/controller/ajax.js +++ b/controller/ajax.js @@ -1,9 +1,9 @@ -var methods = { - checkGames: function(req, res) { +const methods = { + checkGames: (req, res) => { req.session.me.getActiveGames(function(rooms) { - var data = []; + const data = []; - for(var i = 0, x = rooms.length; i < x ; i++) { + for(let i = 0, x = rooms.length; i < x ; i++) { data.push(rooms[i]._id); } diff --git a/controller/create.js b/controller/create.js index 8434f22..639c67a 100644 --- a/controller/create.js +++ b/controller/create.js @@ -8,7 +8,7 @@ module.exports = function(app, req, res, params) { dbUser2.countActiveGames(function(gamesp2) { if ((gamesp1 < 10) || (gamesp2 < 10)) { - var room = new app.models.room({ + const room = new app.models.room({ requestId: params[0], players: { p1: req.session.me._id, diff --git a/controller/game.js b/controller/game.js index 00e207f..1a705fa 100644 --- a/controller/game.js +++ b/controller/game.js @@ -1,11 +1,11 @@ -var moment = require('moment'); +const moment = require('moment'); module.exports = function(app, req, res, params) { app.models.room.findOne({_id: params[0]}).populate('players.p1 players.p2 log.player chat.player').exec(function(err, room) { - var player1 = null; - var player2 = null; - var playerIdx = 1; + const player1 = null; + const player2 = null; + const playerIdx = 1; if (req.session.meFB.id == room.players.p1.facebookId) { player1 = room.players.p1; player2 = room.players.p2; @@ -16,7 +16,7 @@ module.exports = function(app, req, res, params) { } if (player1) { - var data = { + const data = { roomId: params[0], room: room, moment: moment, diff --git a/controller/home.js b/controller/home.js index 19947fb..0ec0341 100644 --- a/controller/home.js +++ b/controller/home.js @@ -1,4 +1,4 @@ -var moment = require('moment'); +const moment = require('moment'); module.exports = function(app, req, res) { app.checkUser(req.session.meFB.id, function(dbUser) { @@ -7,16 +7,16 @@ module.exports = function(app, req, res) { req.session.me.getWinsAndLoses(function(wins ,loses) { - var getWinsLosesIds = []; + const getWinsLosesIds = []; - for(var i = 0, x = req.session.meFB.friends.data.length; i < x ; i++) { + for(let i = 0, x = req.session.meFB.friends.data.length; i < x ; i++) { getWinsLosesIds.push({facebookId: req.session.meFB.friends.data[i].id}) } app.models.user.find({$or: getWinsLosesIds}, function(err, friendsResult) { - var friendsInfo = {}; + const friendsInfo = {}; - for(var i = 0, x = friendsResult.length; i < x ; i++) { + for(let i = 0, x = friendsResult.length; i < x ; i++) { friendsInfo[friendsResult[i].facebookId] = friendsResult[i]; } diff --git a/index.js b/index.js index faf03d0..942854e 100644 --- a/index.js +++ b/index.js @@ -3,9 +3,9 @@ //! authors : Thiago Genuino, Luiz Felipe //! contributor : Trendi Relevance Builders http://trendi.com.br //! license : MIT -(function(undefined) { +((undefined) => { - var express = require('express') + const express = require('express') , app = module.exports = express() , config = require('./config.json') , server = require('http').createServer(app) diff --git a/lib/app.js b/lib/app.js index daa9daf..a229e49 100644 --- a/lib/app.js +++ b/lib/app.js @@ -1,6 +1,6 @@ -var app = module.parent.exports; +let app = module.parent.exports; -app.getControl = function(control, req, res, params) { +app.getControl = (control, req, res, params) => { try { return require(app.path + '/controller/' + control + '.js')(app, req, res, params); } catch(e) { @@ -8,10 +8,10 @@ app.getControl = function(control, req, res, params) { } }; -app.checkUser = function(facebookId, cb) { - app.models.user.findOne({facebookId: facebookId}, function(err, dbUser) { +app.checkUser = (facebookId, cb) => { + app.models.user.findOne({facebookId: facebookId}, (err, dbUser) => { if (!dbUser) { - app.facebook.api('/'+facebookId+'?fields=id,name,picture', function(err, user) { + app.facebook.api('/'+facebookId+'?fields=id,name,picture', (err, user) => { dbUser = new app.models.user({ facebookId: user.id, name: user.name, @@ -28,12 +28,12 @@ app.checkUser = function(facebookId, cb) { }); }; -app.getRoom = function(id, cb) { - app.models.room.findOne({_id: id}).populate('players.p1 players.p2').exec(function(err, room) { +app.getRoom = (id, cb) => { + app.models.room.findOne({_id: id}).populate('players.p1 players.p2').exec((err, room) => { cb(room); }); }; -app.facebookLogin = function() { +app.facebookLogin = () => { return app.fbClass.loginRequired({scope:'user_friends, email, publish_actions'}); }; \ No newline at end of file diff --git a/lib/events.js b/lib/events.js index 6a58464..33bdbf2 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1,5 +1,5 @@ -var app = module.parent.exports; -var events = require("events") +let app = module.parent.exports; +const events = require("events") , util = require("util"); // Emitter Class - Responsible for all the events on the game @@ -9,7 +9,7 @@ function Emitter() { util.inherits(Emitter, events.EventEmitter); -Emitter.prototype.connect = function() { +Emitter.prototype.connect = () => { this.emit('connect', {}); }; @@ -17,7 +17,7 @@ Emitter.prototype.connect = function() { // Events app.emitter = new Emitter(); -app.emitter.on('connect', function() { +app.emitter.on('connect', () => { }); diff --git a/lib/models.js b/lib/models.js index 7309952..d1cfe34 100644 --- a/lib/models.js +++ b/lib/models.js @@ -1,6 +1,6 @@ -var app = module.parent.exports; +let app = module.parent.exports; -var loadModel = function(name) { +const loadModel = (name) => { return require(app.path+'/model/'+name+'.js'); }; diff --git a/lib/routes.js b/lib/routes.js index abfc425..848c496 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,19 +1,19 @@ -var app = module.parent.exports; -var emitter = app.emitter; +let app = module.parent.exports; +const emitter = app.emitter; -(function() { +(()=> { // Index - app.get(/^\/chess\/?$/, function(req, res) { + app.get(/^\/chess\/?$/, (req, res) => { app.getControl('index', req, res); }); // Ajax - app.post('/chess/_/:action', function(req, res) { - app.checkUser(req.session.meFB.id, function(dbUser) { + app.post('/chess/_/:action', (req, res) => { + app.checkUser(req.session.meFB.id, (dbUser) => { req.session.me = dbUser; - var params = req.body; + const params = req.body; params.action = req.params.action; app.getControl('ajax', req, res, params); @@ -21,29 +21,29 @@ var emitter = app.emitter; }); // Views - app.get(/^\/chess\/?(.*)/, app.facebookLogin(), function(req, res) { + app.get(/^\/chess\/?(.*)/, app.facebookLogin(), (req, res) => { if (req.params[0] === undefined) { - var params = []; + const params = []; } else { - var params = req.params[0].split('/'); + const params = req.params[0].split('/'); if (params[0] === '') params = params.slice(1); if (params[params.length-1] === '') params = params.slice(0,params.length-1); } - var control = 'index'; + const control = 'index'; if (params[0]) control = params[0]; - req.facebook.api('/me?fields=id,name,picture,friends.fields(id,name,picture)', function(err, me) { + req.facebook.api('/me?fields=id,name,picture,friends.fields(id,name,picture)', (err, me) => { if (!me.friends) me.friends = {data:[]}; req.session.meFB = me; - app.checkUser(req.session.meFB.id, function(dbUser) { + app.checkUser(req.session.meFB.id, (dbUser) => { req.session.me = dbUser; app.getControl(control, req, res, params.slice(1)); diff --git a/lib/sockets.js b/lib/sockets.js index 3feedfa..b4cfa18 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -1,15 +1,15 @@ -var app = module.parent.exports; -var moment = require('moment'); +let app = module.parent.exports; +const moment = require('moment'); -(function() { - var statuses = {}; +(()=> { + const statuses = {}; app.io.of('/chess') - .on('connection', function(socket) { + .on('connection', (socket) => { // User joined game room - socket.on('join-room', function(data) { - app.models.user.findOne({_id: data.user}, function(err, user) { - app.getRoom(data.room, function(room) { + socket.on('join-room', (data) => { + app.models.user.findOne({_id: data.user}, (err, user) => { + app.getRoom(data.room, (room) => { room.log.push({ player: user._id, date: new Date(), @@ -21,7 +21,7 @@ var moment = require('moment'); data.user = user; - socket.set('me', data, function() { + socket.set('me', data, () => { socket.join(data.room); if (!statuses[data.room]) @@ -47,17 +47,17 @@ var moment = require('moment'); }); // New chat message - socket.on('chat-msg', function(data) { - socket.get('me', function(err, me) { - app.getRoom(me.room, function(room) { + socket.on('chat-msg', (data) => { + socket.get('me', (err, me) => { + app.getRoom(me.room, (room) => { room.chat.push({ player: me.user._id, message: data.message, date: new Date() }); - room.save(function(err, r) { - var chatMsg = r.chat.reverse()[0]; - var send = { + room.save((err, r)=> { + const chatMsg = r.chat.reverse()[0]; + const send = { player: chatMsg.player, message: chatMsg.message, date: moment(chatMsg.date).format('DD/MM/YY HH:mm') @@ -71,35 +71,35 @@ var moment = require('moment'); }); - socket.on('make-move', function(data) { + socket.on('make-move', (data) => { if (!data.from || !data.to) return; - var from = { + const from = { line: data.from.substring(0,1), col: data.from.substring(1,2) }; - var to = { + const to = { line: data.to.substring(0,1), col: data.to.substring(1,2) }; - socket.get('me', function(err, me) { + socket.get('me', (err, me) => { if (!me) { socket.emit('reload', {msg: 'Server down, refresh to rejoin'}); return; } - app.getRoom(me.room, function(room) { + app.getRoom(me.room, (room) => { if (room.winner !== null) { socket.emit('game-done', {msg: 'Game already finished'}); return; } - var result = app.validate.move(room, me.user, from, to); + const result = app.validate.move(room, me.user, from, to); if (result.status === true) { - var initial = room.board[from.line][from.col]; - var destination = room.board[to.line][to.col]; + const initial = room.board[from.line][from.col]; + const destination = room.board[to.line][to.col]; if (result.extra.capture == 1) { if (destination.piece == 'king') { @@ -129,7 +129,7 @@ var moment = require('moment'); data: {} }); - room.save(function(err, r) { + room.save((err, r) => { result.from = from; result.to = to; @@ -151,21 +151,21 @@ var moment = require('moment'); // Disconnect - socket.on('disconnect', function() { - socket.get('me', function(err, me) { + socket.on('disconnect', () => { + socket.get('me', (err, me) => { if (!me) return; statuses[me.room][me.user._id] = 'offline'; - app.getRoom(me.room, function(room) { + app.getRoom(me.room, (room) => { room.log.push({ player: me.user._id, date: new Date(), action: 'exit-room', data: {} }); - room.save(function(err, r) { - var log = r.log.reverse()[0]; + room.save((err, r) => { + const log = r.log.reverse()[0]; socket.volatile.broadcast.to(me.room).emit('exit-room', { player: { @@ -181,7 +181,7 @@ var moment = require('moment'); // Get game information - socket.on('game-info', function(data) { + socket.on('game-info', (data) => { console.log(data); }); diff --git a/lib/validate.js b/lib/validate.js index 60bc031..c1bc32e 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -1,14 +1,14 @@ -var app = module.parent.app; -var lineIndex = { +let app = module.parent.app; +const lineIndex = { a: 1, b:2, c:3, d:4, e:5, f:6, g:7, h:8 }; -var lineIndexInverse = { +const lineIndexInverse = { '1': 'a', '2':'b', '3':'c', '4':'d', '5':'e', '6':'f', '7':'g', '8':'h' }; -var checkMoviment = function(from, to, room) { +const checkMoviment = (from, to, room) => { - var idxLineFrom = parseInt(lineIndex[from.line]), + const idxLineFrom = parseInt(lineIndex[from.line]), idxLineTo = parseInt(lineIndex[to.line]), idxColFrom = parseInt(from.col), idxColTo = parseInt(to.col), @@ -23,7 +23,7 @@ var checkMoviment = function(from, to, room) { msg = 'Movimento inválido'; return { - 'pawn' : function(checkKing) { + 'pawn' : (checkKing) => { moveRange = 1; // Já se moveu alguma vez? @@ -33,10 +33,10 @@ var checkMoviment = function(from, to, room) { // Same collumn? if ((!checkKing) && (to.col == from.col)) { - var lineMoved = 0; + const lineMoved = 0; - for(var i=1;i<=moveRange;i++) { - var check = lineIndex[from.line] + (i * side); + for(let i=1;i<=moveRange;i++) { + const check = lineIndex[from.line] + (i * side); lineMoved++; @@ -81,14 +81,14 @@ var checkMoviment = function(from, to, room) { }; }, - 'rook' : function() { + 'rook' : () => { if (from.col == to.col) { moveRange = (idxLineFrom > idxLineTo ? idxLineFrom - idxLineTo : idxLineTo - idxLineFrom)*side; if (idxLineFrom < idxLineTo) { - for(var i = (idxLineFrom+1), x = idxLineTo; i <= x; i++) { - var check = room.board[lineIndexInverse[i]][from.col]; + for(let i = (idxLineFrom+1), x = idxLineTo; i <= x; i++) { + let check = room.board[lineIndexInverse[i]][from.col]; if (lineIndexInverse[i] == to.line) { valid = true; @@ -109,8 +109,8 @@ var checkMoviment = function(from, to, room) { } else { - for(var i = (idxLineFrom-1), x = idxLineTo; i >= x; i--) { - var check = room.board[lineIndexInverse[i]][from.col]; + for(let i = (idxLineFrom-1), x = idxLineTo; i >= x; i--) { + let check = room.board[lineIndexInverse[i]][from.col]; if (lineIndexInverse[i] == to.line) { valid = true; @@ -136,8 +136,8 @@ var checkMoviment = function(from, to, room) { if (idxColFrom < idxColTo) { - for(var i = (idxColFrom+1), x = idxColTo; i <= x; i++) { - var check = room.board[from.line][i]; + for(let i = (idxColFrom+1), x = idxColTo; i <= x; i++) { + let check = room.board[from.line][i]; if (i == to.col) { valid = true; @@ -158,8 +158,8 @@ var checkMoviment = function(from, to, room) { } else { - for(var i = (idxColFrom-1), x = idxColTo; i >= x; i--) { - var check = room.board[from.line][i]; + for(let i = (idxColFrom-1), x = idxColTo; i >= x; i--) { + let check = room.board[from.line][i]; if (i == to.col) { valid = true; @@ -191,11 +191,11 @@ var checkMoviment = function(from, to, room) { }; }, - 'bishop' : function() { + 'bishop' : () => { moveRange = (idxLineFrom > idxLineTo ? idxLineFrom - idxLineTo : idxLineTo - idxLineFrom)*side; - var diffCol = Math.abs(parseInt(from.col - to.col)); - var diffLine = Math.abs(parseInt(lineIndex[from.line] - lineIndex[to.line])); + const diffCol = Math.abs(parseInt(from.col - to.col)); + const diffLine = Math.abs(parseInt(lineIndex[from.line] - lineIndex[to.line])); if ((diffLine != diffCol) || ((from.col == to.col) || (from.line == to.line))) { msg = 'Movimentação inválida'; @@ -205,10 +205,10 @@ var checkMoviment = function(from, to, room) { if (idxLineFrom < idxLineTo) { - var fix = (from.col - to.col) > 0 ? -1 : 1; + const fix = (from.col - to.col) > 0 ? -1 : 1; - for (var i = 1, x = Math.abs(moveRange); i <= x; i++) { - var check = room.board[lineIndexInverse[lineIndex[from.line]+i]][parseInt(from.col)+(i*fix)]; + for (let i = 1, x = Math.abs(moveRange); i <= x; i++) { + let check = room.board[lineIndexInverse[lineIndex[from.line]+i]][parseInt(from.col)+(i*fix)]; if (i == x) { valid = true; @@ -229,10 +229,10 @@ var checkMoviment = function(from, to, room) { } else { - var fix = (from.col - to.col) > 0 ? -1 : 1; + const fix = (from.col - to.col) > 0 ? -1 : 1; - for (var i = 1, x = Math.abs(moveRange); i <= x; i++) { - var check = room.board[lineIndexInverse[lineIndex[from.line]-i]][parseInt(from.col)+(i*fix)]; + for (let i = 1, x = Math.abs(moveRange); i <= x; i++) { + let check = room.board[lineIndexInverse[lineIndex[from.line]-i]][parseInt(from.col)+(i*fix)]; if (i == x) { valid = true; @@ -262,16 +262,16 @@ var checkMoviment = function(from, to, room) { error: msg }; }, - 'queen' : function() { - var check = checkMoviment(from, to, room); + 'queen' : () => { + let check = checkMoviment(from, to, room); - var resultBishop = check.bishop(); + const resultBishop = check.bishop(); if ((resultBishop.extra.movimentType == 'diagonal') && (resultBishop.status === true)) { valid = resultBishop.status; extra.capture = resultBishop.extra.capture; } else { - var resultRook = check.rook(); + const resultRook = check.rook(); valid = resultRook.status; extra.capture = resultRook.extra.capture; } @@ -283,11 +283,11 @@ var checkMoviment = function(from, to, room) { }; }, - 'horse' : function() { + 'horse' : () => { moveRange = (idxLineFrom > idxLineTo ? idxLineFrom - idxLineTo : idxLineTo - idxLineFrom)*side; - var diffCol = Math.abs(parseInt(from.col - to.col)); - var diffLine = Math.abs(parseInt(lineIndex[from.line] - lineIndex[to.line])); + const diffCol = Math.abs(parseInt(from.col - to.col)); + const diffLine = Math.abs(parseInt(lineIndex[from.line] - lineIndex[to.line])); if (Math.abs(moveRange) == 2) { @@ -318,13 +318,13 @@ var checkMoviment = function(from, to, room) { }; }, - 'king' : function() { + 'king' : () => { moveRangeLine = (idxLineFrom > idxLineTo ? idxLineFrom - idxLineTo : idxLineTo - idxLineFrom)*side; moveRangeCol = (idxColFrom > idxColTo ? idxColFrom - idxColTo : idxColTo - idxColFrom)*side; if ((Math.abs(moveRangeLine) == 1) || (Math.abs(moveRangeCol) == 1)) { valid = true; - var isSafe = isKingSafe(from, to, room); + const isSafe = isKingSafe(from, to, room); if (!isSafe) { valid = false; @@ -348,15 +348,15 @@ var checkMoviment = function(from, to, room) { } }; -var isKingSafe = function(from, to, room, onCheck) { +const isKingSafe = (from, to, room, onCheck) => { safe = true; onCheck = onCheck || false; - for (var line in room.board) { - for (var col in room.board[line]) { + for (let line in room.board) { + for (let col in room.board[line]) { if ((!onCheck) && ((from.col == col) && (from.line == line))) continue; - var square = { + const square = { line: line, col: col, data: room.board[line][col], @@ -369,7 +369,7 @@ var isKingSafe = function(from, to, room, onCheck) { //console.log(square); - var result = checkMoviment(square, to, room)[square.data.piece](1); + const result = checkMoviment(square, to, room)[square.data.piece](1); if (result.status === true) { safe = false; @@ -382,10 +382,10 @@ var isKingSafe = function(from, to, room, onCheck) { return safe; }; -var getEnemyKing = function(room, player) { - for (var line in room.board) { - for (var col in room.board[line]) { - var square = room.board[line][col]; +const getEnemyKing = (room, player) => { + for (let line in room.board) { + for (let col in room.board[line]) { + let square = room.board[line][col]; if ((square.piece == 'king') && (player._id.toString() != room.players['p'+square.player]._id.toString())) { return { @@ -398,7 +398,7 @@ var getEnemyKing = function(room, player) { } }; -var validate = function(room, player, from, to) { +const validate = (room, player, from, to) => { from.data = room.board[from.line][from.col]; to.data = room.board[to.line][to.col]; @@ -422,7 +422,7 @@ var validate = function(room, player, from, to) { } if (from.data.playerData._id.toString() == player._id) { - var result = checkMoviment(from, to, room)[from.data.piece](); + const result = checkMoviment(from, to, room)[from.data.piece](); result.enemyKing = getEnemyKing(room, player); return result; diff --git a/model/room.js b/model/room.js index a7e924c..7218f79 100644 --- a/model/room.js +++ b/model/room.js @@ -1,6 +1,6 @@ -var app = module.parent.exports.app; -var mongoose = require('mongoose'); -var schema = new mongoose.Schema({ +let app = module.parent.exports.app; +const mongoose = require('mongoose'); +const schema = new mongoose.Schema({ winner: { type: Number, default: null }, endDate: { type: Date, default: null }, players: { @@ -113,21 +113,21 @@ var schema = new mongoose.Schema({ updated: { type: Date, default: Date.now } }); -schema.statics.countActiveGames = function(cb) { +schema.statics.countActiveGames = (cb) => { return this.model('room') - .count({endDate: null}, function(err, count) { + .count({endDate: null}, (err, count) => { cb(count); }); }; -schema.statics.countAllGames = function(cb) { +schema.statics.countAllGames = (cb)=> { return this.model('room') - .count(function(err, count) { + .count((err, count)=> { cb(count); }); }; -schema.pre('save', function(next) { +schema.pre('save', (next) => { this.updated = new Date(); next(); }); diff --git a/model/user.js b/model/user.js index 91af593..82bfd43 100644 --- a/model/user.js +++ b/model/user.js @@ -1,6 +1,6 @@ -var app = module.parent.exports.app; -var mongoose = require('mongoose'); -var schema = new mongoose.Schema({ +let app = module.parent.exports.app; +const mongoose = require('mongoose'); +const schema = new mongoose.Schema({ facebookId: Number, name: String, picture: String, @@ -10,58 +10,58 @@ var schema = new mongoose.Schema({ updated: { type: Date, default: Date.now } }); -schema.statics.countAllUsers = function(cb) { +schema.statics.countAllUsers = (cb) => { return this.model('user') - .count(function(err, count) { + .count((err, count) => { cb(count); }); }; -schema.methods.countActiveGames = function(cb) { +schema.methods.countActiveGames = (cb) => { return this.model('room') - .count({winner: null, $or: [{'players.p1': this._id}, {'players.p2': this._id}]}, function(err, count) { + .count({winner: null, $or: [{'players.p1': this._id}, {'players.p2': this._id}]}, (err, count) =>{ cb(count); }); }; -schema.methods.getActiveGames = function(cb) { +schema.methods.getActiveGames = (cb) => { return this.model('room') .find({endDate: null, $or: [{'players.p1': this._id}, {'players.p2': this._id}]}) .populate('players.p1 players.p2') - .exec(function(err, games) { + .exec((err, games) => { cb(games); }); }; -schema.methods.getWins = function(cb) { +schema.methods.getWins = (cb) => { return this.model('room') .find({endDate: {$ne: null}, $or: [{'players.p1': this._id, winner: 1}, {'players.p2': this._id, winner:2}]}) .populate('players.p1 players.p2') - .exec(function(err, wins) { + .exec((err, wins) => { cb(wins); }); }; -schema.methods.getLoses = function(cb) { +schema.methods.getLoses = (cb) => { return this.model('room') .find({endDate: {$ne: null}, $or: [{'players.p1': this._id, winner: 2}, {'players.p2': this._id, winner:1}]}) .populate('players.p1 players.p2') - .exec(function(err, loses) { + .exec((err, loses) => { cb(loses); }); }; -schema.methods.getWinsAndLoses = function(cb) { - var self = this; - self.getWins(function(wins) { - self.getLoses(function(loses) { +schema.methods.getWinsAndLoses = (cb) => { + const self = this; + self.getWins((wins)=> { + self.getLoses((loses) => { cb(wins, loses); }); }); }; -schema.pre('save', function(next) { +schema.pre('save', (next) => { this.updated = new Date(); next(); });