Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correção do commit passado que fiz na branch "nova-branche". #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions controller/ajax.js
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion controller/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions controller/game.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +16,7 @@ module.exports = function(app, req, res, params) {
}

if (player1) {
var data = {
const data = {
roomId: params[0],
room: room,
moment: moment,
Expand Down
10 changes: 5 additions & 5 deletions controller/home.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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];
}

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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) {
console.log(e);
}
};

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,
Expand All @@ -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'});
};
8 changes: 4 additions & 4 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,15 +9,15 @@ function Emitter() {

util.inherits(Emitter, events.EventEmitter);

Emitter.prototype.connect = function() {
Emitter.prototype.connect = () => {
this.emit('connect', {});
};


// Events
app.emitter = new Emitter();

app.emitter.on('connect', function() {
app.emitter.on('connect', () => {

});

Expand Down
4 changes: 2 additions & 2 deletions lib/models.js
Original file line number Diff line number Diff line change
@@ -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');
};

Expand Down
26 changes: 13 additions & 13 deletions lib/routes.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
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);
});
});

// 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));
Expand Down
60 changes: 30 additions & 30 deletions lib/sockets.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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])
Expand All @@ -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')
Expand All @@ -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') {
Expand Down Expand Up @@ -129,7 +129,7 @@ var moment = require('moment');
data: {}
});

room.save(function(err, r) {
room.save((err, r) => {
result.from = from;
result.to = to;

Expand All @@ -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: {
Expand All @@ -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);
});

Expand Down
Loading