Skip to content

Commit

Permalink
Adding the Game Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
amra12 committed Nov 4, 2018
1 parent 1d752f0 commit a67d441
Show file tree
Hide file tree
Showing 5 changed files with 1,455 additions and 1 deletion.
153 changes: 153 additions & 0 deletions controllers/GameController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
var listOfUsers = [];
var allUsers = [];
var Q = require("q");

const GameSchema = require('../model/Game')
const UserSchema = require('../model/User')

class GameController {
constructor(apiRouter) {

this.basePath = '/game';

apiRouter.post(this.basePath + "/newGame/", this.create.bind(this));
// apiRouter.put(this.basePath + "/:gameId", this.changeStatus.bind(this));
//apiRouter.get(this.basePath + "/getResults/:gameId", this.changeStatus.bind(this));


}

create(req, res) {
const method = 'GameEndpoint.create ';
const path = 'POST ' + this.basePath + '/';
console.info(method, 'Access to', path);

const body = req.body;
const firstPlayerId = body.FirstPlayerId;

this.searchingForPlayer(firstPlayerId).then((respond) => {
const newGame = new GameSchema({
firstPlayerId: respond.player1,
secondPlayerId: respond.player2,
firstPlayerScore: 0,
secondPlayerScore: 0,
});
newGame.save(function (err) {
if (err) {
res.status(500).json({success: false, msg: 'Error while creating the game'});
}
else {
res.status(201).json({success: true, msg: 'Successful created.'});
}
});
}).catch((error) => {

res.status(500).send(error);
});
listOfUsers = []

};

changeStatus(id, status, callback) {

UserSchema.findOneAndUpdate({
_id: id
}, {$set: {status: status}}, callback);

}

promiseWhile(condition, body) {
var done = Q.defer();

function loop() {
// When the result of calling `condition` is no longer true, we are
// done.
if (!condition()) return done.resolve();
// Use `when`, in case `body` does not return a promise.
// When it completes loop again otherwise, if it fails, reject the
// done promise
Q.when(body(), loop, done.reject);
}

// Start running the loop in the next tick so that this function is
// completely async. It would be unexpected if `body` was called
// synchronously the first time.
Q.nextTick(loop);

// The promise
return done.promise;
}

cleanUsers(firstPlayer, users) {
listOfUsers = [];
for (var i = 0; i < users.length; i++) {
if (firstPlayer != users[i]._id && users[i].status != 'playing') {
listOfUsers.push(users[i]);
}
}

}

findUser(firstPlayer, self) {
UserSchema.find({
status: 'searching for game'
}, function (err, users) {
allUsers = users;
self.cleanUsers(firstPlayer, users)
});
}

searchingForPlayer(firstPlayerId) {
const currentTime = Date.now();

return new Promise((resolve, reject) => {
listOfUsers = [];
let self = this;
self.changeStatus(firstPlayerId, 'searching for game', function (err, user) {
if (err || !user) {
reject({success: false, error: 'UserSchema not found'})
console.log(err + ' and this is the user id '+firstPlayerId);
} else {
self.promiseWhile(function () {
return Date.now() - currentTime < 6000 && listOfUsers.length == 0;
}, function () {
setTimeout(self.findUser, 2000, firstPlayerId,self);
return Q.delay(500);
}).then(function () {
if ((listOfUsers.length == 0)) {
self.changeStatus(firstPlayerId, 'nothing', function (err, user) {
if (err) reject({success: false, msg: 'UserSchema not found'});
else {
reject({success: false, msg: 'Request timeout.'});
}
});
}
else {
self.cleanUsers(firstPlayerId, allUsers);
console.log(listOfUsers);
var secondPlayerId = listOfUsers[0]._id;
self.changeStatus(firstPlayerId, 'playing', function (err, user) {
if (err) reject({success: false, msg: 'UserSchema not found'});
else {
self.changeStatus(secondPlayerId, 'playing', function (err, user) {
if (err) reject({success: false, msg: 'UserSchema not found'});
else {
return resolve({success: true, player1: firstPlayerId, player2: secondPlayerId});
}
});
}
});
}
}).done();
}
});
});

}




}

module.exports = GameController;
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var mongoose = require('mongoose');
var port = process.env.PORT || 4200;
var UserController = require('./controllers/UserController');
var VocabController = require('./controllers/VocabController');

var GameController = require('./controllers/GameController');

var databaseConfig = require('./config/database');
const http = require('http');
Expand Down Expand Up @@ -37,6 +37,7 @@ app.use('', apiRoutes);

new UserController(apiRoutes);
new VocabController(apiRoutes);
new GameController(apiRoutes);

http.createServer(app).listen(appConfig.appPort, '0.0.0.0', function () {
console.log('APIs on port ' + appConfig.appPort);
Expand Down
35 changes: 35 additions & 0 deletions model/Game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt');


// The schema defines how the players are going to be saved in the Mongo database
var GameSchema=new Schema({

firstPlayerId: {
type: String,
unique: true,
required: true
},
secondPlayerId: {
type: String,
unique : true,
required: true
},

firstPlayerScore:{
type: Number,
required: true
},

secondPlayerScore:{
type: Number,
required: true

}

});



module.exports = mongoose.model('Game', GameSchema);
5 changes: 5 additions & 0 deletions model/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ var UserSchema = new Schema({
{
type: [String]
},
status:
{
type: String,
required:true
},

});

Expand Down
Loading

0 comments on commit a67d441

Please sign in to comment.