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 f6d4ed49ca
commit a3180b0bde
8 changed files with 75 additions and 23 deletions

View file

@ -34,7 +34,6 @@ const signup = async (req, res, next) => {
} }
const login = async (req, res, next) => { const login = async (req, res, next) => {
checkValidationErrors(req, res); checkValidationErrors(req, res);
const user = req.body; const user = req.body;

View file

@ -5,7 +5,7 @@ const winType = [
] ]
const rankArray = [ 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', 'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10',
'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20', 'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20',
'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR' 'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR'

View file

@ -1,7 +1,7 @@
const knex = require('../db'); const knex = require('../db');
const gameOverviewSelect = [ 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' 'player_black', 'player_black_rank', 'player_white', 'player_white_rank'
] ]
@ -10,7 +10,7 @@ const findGameById = async (gameId) => {
.where({'id': gameId}) .where({'id': gameId})
.select('*'); .select('*');
return game; return game[0];
} }
const findGameByRoom = async (roomId) => { const findGameByRoom = async (roomId) => {

View file

@ -2,7 +2,7 @@ const knex = require('../db')
const insertUser = async (user) => { const insertUser = async (user) => {
return await knex('user') return await knex('user')
.returning(['username', 'email']) .returning(['username', 'email', 'id'])
.insert(user) .insert(user)
.then(queryResults => { .then(queryResults => {
newUser = queryResults[0]; newUser = queryResults[0];
@ -18,7 +18,7 @@ const findUserByNameOrEmail = async (user) => {
return await knex('user') return await knex('user')
.where({'username': user.username}) .where({'username': user.username})
.orWhere({'email': user.email}) .orWhere({'email': user.email})
.select(['username', 'email', 'password']) .select(['username', 'email', 'password', 'id'])
} }
module.exports = { module.exports = {

View file

@ -1,16 +1,18 @@
const {hashPassword} = require('../../services/bcrypt'); const {hashPassword} = require('../../services/bcrypt');
require('dotenv').config(); require('dotenv').config();
const password = process.env.USER_ONE_PASSWORD; const password = process.env.USER_ONE_PASSWORD;
const hashedPassword = hashPassword(password);
const email = process.env.USER_ONE_EMAIL; const email = process.env.USER_ONE_EMAIL;
exports.seed = function(knex) { exports.seed = async function(knex) {
// Deletes ALL existing entries // Deletes ALL existing entries
return knex('user').del() return knex('user').del()
.then(function () { .then(async function () {
const hashedPassword = await hashPassword(password);
// Inserts seed entries // Inserts seed entries
return knex('user').insert([ return knex('user').insert([
{id: 2, username: 'user-one', email: email, password: hashedPassword, admin: true}, {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', player_black: 'user-one', player_black_rank: 'UR',
user_black: 2, user_black: 2,
room: 1, time_setting: 1, open: true 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 socketIO = require('socket.io');
const io = socketIO({ cookie: false }); const io = socketIO({ cookie: false });
const gameQueries = require('./data/queries/game');
io.on('connection', ()=> { io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'}); io.emit('connected', {message: 'socket connected'});
}) })
enableRoomSocket = (roomId) => { enableRoomSocket = (roomId) => {
const roomSocket = io.of(roomId); const roomSocket = io.of(roomId);
roomSocket.on('connection', () => { roomSocket.on('connection', (socket) => {
// socket.emit('connected');
console.log(`Socket connected at room ${roomId}`); 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; return roomSocket;
} }
@ -18,3 +25,9 @@ module.exports = {
io, io,
enableRoomSocket enableRoomSocket
} }
async function logJoinGameRequest (data) {
const {user, game} = data;
const requestedGame = await gameQueries.findGameById(game.id);
return { user, requestedGame }
}

View file

@ -8,7 +8,8 @@ const apiRoomSpec = (chai, knex, server) => {
description: 'A general place to play Go', description: 'A general place to play Go',
language: 'EN' language: 'EN'
}, },
roomGames: [ { roomGames: [
{
id: 1, id: 1,
komi: 6.5, komi: 6.5,
handicap: 0, handicap: 0,
@ -16,8 +17,35 @@ const apiRoomSpec = (chai, knex, server) => {
player_black: 'anon', player_black: 'anon',
player_white: 'anon', player_white: 'anon',
player_black_rank: 'K3', player_black_rank: 'K3',
player_white_rank: 'K2' 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: [ { messages: [ {
content: 'Hey! Welcome to the general room!', content: 'Hey! Welcome to the general room!',
username: 'user-one', username: 'user-one',
@ -25,6 +53,8 @@ const apiRoomSpec = (chai, knex, server) => {
} ] } ]
} }
it('seeded rooms should be present in db', done => { it('seeded rooms should be present in db', done => {
knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => { knex('room').where('id', 1).orWhere('id', 2).select('name').then(roomResults => {
if (roomResults[0].name === 'main' && roomResults[1].name === 'private') done(); if (roomResults[0].name === 'main' && roomResults[1].name === 'private') done();