Skip to content

Commit

Permalink
ft(Fixed):Renamed player model and controller to game model and contr…
Browse files Browse the repository at this point in the history
…oller
  • Loading branch information
kenware committed Jul 10, 2018
1 parent 93b7019 commit 08e3faf
Show file tree
Hide file tree
Showing 17 changed files with 4,083 additions and 3,459 deletions.
4 changes: 0 additions & 4 deletions .nodemonignore

This file was deleted.

9 changes: 4 additions & 5 deletions app/controllers/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ const Game = mongoose.model('Game');
const gameResult = (req, res) => {
let gameId = req.params.id;
gameId = parseInt(gameId, 10);
const { gameStarter, gameWinner, players } = req.body;
if (!gameStarter || !players) {
return res.status(4000).json({ message: 'Game have not started' });
const { gameWinner, players } = req.body;
if (!players) {
return res.status(401).json({ message: 'Players have not joined the game' });
}
const payload = {
gameStarter,
gameWinner,
players,
gameId
};
const game = new Game(payload);
game.save((err) => {
if (err) {
return res.status(401).json({ message: 'Error saving game' });
return res.status(500).json({ message: 'Error saving game' });
}
return res.status(201).json(game);
});
Expand Down
17 changes: 15 additions & 2 deletions app/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import 'babel-polyfill';
import jwt from 'jsonwebtoken';

/**
* @function verifyToken
* @function auth
* @param {string} data - A token
* @returns { null } returns Unauthorized Access if token is undfefined
* @returns { expired } returns Please login
* @description used to access authenticated route
* @description if token is valid, decode the payload and pass it controller
*/

const auth = (req, res, next) => {
Expand All @@ -21,10 +24,20 @@ const auth = (req, res, next) => {
next();
});
};

/**
* @function verifyToken
* @param {string} data - A token
* @returns { null } returns Unauthorized Access if token is undfefined
* @returns { expired } returns Please login
* @returns { valid } returns the token and user info
* @description used to check if user seesion/token is expired
*/

const verifyToken = (req, res) => {
const token = req.headers.authorization || req.headers['x-access-token'];
if (!token) {
return res.status(401).json({ message: 'Unauthorized Access' });
return res.status(402).json({ message: 'Unauthorized Access' });
}

jwt.verify(token, process.env.SECRET_KEY, (err, result) => {
Expand Down
11 changes: 3 additions & 8 deletions app/models/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Module dependencies.
*/
import mongoose from 'mongoose';
// import config from '../../config/config';

const { Schema } = mongoose;

/**
* Player Schema
* Game Schema
*/
const GameSchema = new Schema({
id: {
Expand All @@ -15,18 +15,13 @@ const GameSchema = new Schema({
gameId: {
type: Number
},
gameStarter: {
type: String,
default: '',
trim: true
},
gameWinner: {
type: String,
default: '',
trim: true
},
players: {
type: [Number]
type: [String]
}
});

Expand Down
3 changes: 1 addition & 2 deletions backend-test/startGame/startGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import app from '../../server';
const User = mongoose.model('User');

const mockPlayers = {
gameStarter: 'Kevin',
gameWinner: 'kevin',
players: [1, 2, 3]
};
Expand Down Expand Up @@ -48,7 +47,7 @@ describe('Player endpoints', () => {
.end((err, res) => {
if (err) return done(err);
expect(res.statusCode).to.equal(201);
expect(res.body.gameStarter).to.equal('Kevin');
expect(res.body.gameWinner).to.equal('kevin');
done();
});
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default (router, passport, app) => {

// Setting up the game api
api.post('/game/:id/start', middleware.auth, game);

// Donation Routes
router.post('/donations', users.addDonation);

Expand Down
3 changes: 2 additions & 1 deletion config/socket/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Game.prototype.payload = function() {
avatar: player.avatar,
premium: player.premium,
socketID: player.socket.id,
color: player.color
color: player.color,
userId: player.userId
});
});
return {
Expand Down
2 changes: 1 addition & 1 deletion config/socket/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Player(socket) {
this.username = null;
this.premium = 0;
this.avatar = null;
this.userID = null;
this.userId = null;
this.color = null;
}

Expand Down
8 changes: 4 additions & 4 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ module.exports = function(io) {
var joinGame = function(socket,data) {
var player = new Player(socket);
data = data || {};
player.userID = data.userID || 'unauthenticated';
if (data.userID !== 'unauthenticated') {
player.userId = data.userId || 'unauthenticated';
if (data.userId !== 'unauthenticated') {
User.findOne({
_id: data.userID
_id: data.userId
}).exec(function(err, user) {
if (err) {
console.log('err',err);
Expand All @@ -92,7 +92,7 @@ module.exports = function(io) {
player.username = 'Guest';
player.avatar = avatars[Math.floor(Math.random()*4)+12];
} else {
player.userID = user.id;
player.userId = user.id;
player.username = user.name;
player.premium = user.premium || 0;
player.avatar = user.avatar || avatars[Math.floor(Math.random()*4)+12];
Expand Down
Loading

0 comments on commit 08e3faf

Please sign in to comment.