connect join_game_request socket at client and server

This commit is contained in:
Sorrel Bri 2020-01-23 21:25:08 -08:00 committed by sorrelbri
parent d882fe9bcf
commit eeca5480ec
8 changed files with 75 additions and 23 deletions

View file

@ -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'});
}

View file

@ -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'

View file

@ -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) => {

View file

@ -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 = {

View file

@ -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},
]);
});
};

View file

@ -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
}
]);
});

View file

@ -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 }
}

View file

@ -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();