diff --git a/packages/server/packages/play-node-go/server/controllers/auth.js b/packages/server/packages/play-node-go/server/controllers/auth.js index 09b7f5b..77091ba 100644 --- a/packages/server/packages/play-node-go/server/controllers/auth.js +++ b/packages/server/packages/play-node-go/server/controllers/auth.js @@ -34,14 +34,13 @@ const signup = async (req, res, next) => { } const login = async (req, res, next) => { - checkValidationErrors(req, res); const user = req.body; - + try { const queryResults = await userQueries.findUserByNameOrEmail(user); const savedUser = queryResults[0] || null; - + if (!savedUser) { return res.status(401).send({errors: 'bad credentials'}); } diff --git a/packages/server/packages/play-node-go/server/data/migrations/20200120224032_game.js b/packages/server/packages/play-node-go/server/data/migrations/20200120224032_game.js index db63b06..53dde63 100644 --- a/packages/server/packages/play-node-go/server/data/migrations/20200120224032_game.js +++ b/packages/server/packages/play-node-go/server/data/migrations/20200120224032_game.js @@ -5,7 +5,7 @@ const winType = [ ] const rankArray = [ - 'D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1', + 'D9', 'D8', 'D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1', 'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10', 'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20', 'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR' diff --git a/packages/server/packages/play-node-go/server/data/queries/game.js b/packages/server/packages/play-node-go/server/data/queries/game.js index ad08a07..ca76d00 100644 --- a/packages/server/packages/play-node-go/server/data/queries/game.js +++ b/packages/server/packages/play-node-go/server/data/queries/game.js @@ -1,7 +1,7 @@ const knex = require('../db'); const gameOverviewSelect = [ - 'id', 'board_size', 'komi', 'handicap', + 'id', 'board_size', 'komi', 'handicap', 'open', 'win_type', 'player_black', 'player_black_rank', 'player_white', 'player_white_rank' ] @@ -10,7 +10,7 @@ const findGameById = async (gameId) => { .where({'id': gameId}) .select('*'); - return game; + return game[0]; } const findGameByRoom = async (roomId) => { diff --git a/packages/server/packages/play-node-go/server/data/queries/user.js b/packages/server/packages/play-node-go/server/data/queries/user.js index cc38058..5cd1b90 100644 --- a/packages/server/packages/play-node-go/server/data/queries/user.js +++ b/packages/server/packages/play-node-go/server/data/queries/user.js @@ -2,7 +2,7 @@ const knex = require('../db') const insertUser = async (user) => { return await knex('user') - .returning(['username', 'email']) + .returning(['username', 'email', 'id']) .insert(user) .then(queryResults => { newUser = queryResults[0]; @@ -18,7 +18,7 @@ const findUserByNameOrEmail = async (user) => { return await knex('user') .where({'username': user.username}) .orWhere({'email': user.email}) - .select(['username', 'email', 'password']) + .select(['username', 'email', 'password', 'id']) } module.exports = { diff --git a/packages/server/packages/play-node-go/server/data/seeds/03_user.js b/packages/server/packages/play-node-go/server/data/seeds/03_user.js index e68be10..ed39e33 100644 --- a/packages/server/packages/play-node-go/server/data/seeds/03_user.js +++ b/packages/server/packages/play-node-go/server/data/seeds/03_user.js @@ -1,16 +1,18 @@ const {hashPassword} = require('../../services/bcrypt'); require('dotenv').config(); + const password = process.env.USER_ONE_PASSWORD; -const hashedPassword = hashPassword(password); const email = process.env.USER_ONE_EMAIL; -exports.seed = function(knex) { +exports.seed = async function(knex) { // Deletes ALL existing entries return knex('user').del() - .then(function () { + .then(async function () { + const hashedPassword = await hashPassword(password); // Inserts seed entries return knex('user').insert([ {id: 2, username: 'user-one', email: email, password: hashedPassword, admin: true}, + {id: 3, username: 'user-two', email: `2${email}`, password: hashedPassword, admin: true}, ]); }); }; \ No newline at end of file diff --git a/packages/server/packages/play-node-go/server/data/seeds/04_game.js b/packages/server/packages/play-node-go/server/data/seeds/04_game.js index 65414cf..404a348 100644 --- a/packages/server/packages/play-node-go/server/data/seeds/04_game.js +++ b/packages/server/packages/play-node-go/server/data/seeds/04_game.js @@ -18,6 +18,14 @@ exports.seed = function(knex) { player_black: 'user-one', player_black_rank: 'UR', user_black: 2, room: 1, time_setting: 1, open: true + }, + { + id: 3, date: new Date('1971-05-06'), + application: 'node-go', application_version: '0.1.0', + player_black: 'Ishida Yoshio', player_black_rank: 'D7', + player_white: 'Rin Kaiho', player_white_rank: 'D9', + room: 1, time_setting: 1, open: false, + event: '', round: 2, win_type: 'B+', score: 1.5 } ]); }); diff --git a/packages/server/packages/play-node-go/server/socket.js b/packages/server/packages/play-node-go/server/socket.js index d709ebc..f5b3195 100644 --- a/packages/server/packages/play-node-go/server/socket.js +++ b/packages/server/packages/play-node-go/server/socket.js @@ -2,14 +2,21 @@ const socketIO = require('socket.io'); const io = socketIO({ cookie: false }); +const gameQueries = require('./data/queries/game'); + io.on('connection', ()=> { io.emit('connected', {message: 'socket connected'}); }) enableRoomSocket = (roomId) => { const roomSocket = io.of(roomId); - roomSocket.on('connection', () => { + roomSocket.on('connection', (socket) => { + // socket.emit('connected'); console.log(`Socket connected at room ${roomId}`); + socket.on('join_game_request', async data => { + const gameRequest = await logJoinGameRequest(data); + roomSocket.emit('join_game_request', gameRequest); + }) }); return roomSocket; } @@ -17,4 +24,10 @@ enableRoomSocket = (roomId) => { module.exports = { io, enableRoomSocket +} + +async function logJoinGameRequest (data) { + const {user, game} = data; + const requestedGame = await gameQueries.findGameById(game.id); + return { user, requestedGame } } \ No newline at end of file diff --git a/packages/server/packages/play-node-go/server/test/room/api.room.spec.js b/packages/server/packages/play-node-go/server/test/room/api.room.spec.js index 5de030f..d85f364 100644 --- a/packages/server/packages/play-node-go/server/test/room/api.room.spec.js +++ b/packages/server/packages/play-node-go/server/test/room/api.room.spec.js @@ -8,23 +8,53 @@ const apiRoomSpec = (chai, knex, server) => { description: 'A general place to play Go', language: 'EN' }, - roomGames: [ { - id: 1, - komi: 6.5, - handicap: 0, - board_size: 19, - player_black: 'anon', - player_white: 'anon', - player_black_rank: 'K3', - player_white_rank: 'K2' - } ], + roomGames: [ + { + id: 1, + komi: 6.5, + handicap: 0, + board_size: 19, + player_black: 'anon', + player_white: 'anon', + player_black_rank: 'K3', + player_white_rank: 'K2', + open: false, + win_type: null + }, + { + id: 2, + komi: 6.5, + handicap: 0, + board_size: 19, + player_black: 'user-one', + player_white: null, + player_black_rank: 'UR', + player_white_rank: 'UR', + open: true, + win_type: null + }, + { + id: 3, + komi: 6.5, + handicap: 0, + board_size: 19, + player_black: 'Ishida Yoshio', + player_white: 'Rin Kaiho', + player_black_rank: 'D7', + player_white_rank: 'D9', + open: false, + win_type: 'B+' + } + ], messages: [ { content: 'Hey! Welcome to the general room!', username: 'user-one', admin: true } ] } - + + + it('seeded rooms should be present in db', done => { knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => { if (roomResults[0].name === 'main' && roomResults[1].name === 'private') done();