stub games/:id endpoint to join game socket room

This commit is contained in:
Sorrel Bri 2020-01-24 00:07:40 -08:00
parent 4a960c784b
commit f4b8e79a3a
11 changed files with 130 additions and 15 deletions

View file

@ -3,7 +3,7 @@ import { Link } from 'react-router-dom';
import './Game.scss';
const GameButton = (props) => {
const { game, dispatch } = props;
const { game, dispatch, user } = props;
const requestJoinGame = () => {
console.log(`request to Join Game ${game.id}!`)
@ -29,7 +29,8 @@ const GameButton = (props) => {
}
const renderInProgressGame = () => {
const gameLinkText = game.winType ? 'Study Game' : 'Watch Game'
const gameLinkText = game.winType ? 'Study Game'
: user ? 'Rejoin Game' : 'Watch Game'
return (
<>
<Link to={`/games/${game.id}`}>{gameLinkText}</Link>

View file

@ -78,6 +78,7 @@ const Room = (props) => {
key={`game-${gameData.id}`}
game={gameData}
dispatch={dispatch}
user={gameData.playerBlack === state.user.username || gameData.playerWhite === state.user.username}
/>
))
}

View file

@ -0,0 +1,24 @@
const roomQueries = require('../../data/queries/room');
const messageQueries = require('../../data/queries/message');
const gameQueries = require('../../data/queries/game');
const moveQueries = require('../../data/queries/move');
const { enableGameSocket } = require('../../socket');
const show = async (req, res, next) => {
try {
const gameId = req.params.id;
if (!gameId) throw('missing game parameter')
const game = await gameQueries.findGameById(gameId);
enableGameSocket(game.room, game.id);
const record = await moveQueries.findGameRecord(gameId);
res.status(200).json({game, record})
}
catch (err) {
res.status(500).json(err);
}
}
module.exports = {
show
}

View file

@ -1,15 +1,31 @@
const knex = require('../db');
const gameDetailSelect = [
'game.id', 'application', 'application_version',
'board_size', 'komi', 'handicap', 'open', 'win_type',
'player_black', 'player_black_rank', 'player_white', 'player_white_rank',
'black_captures', 'white_captures', 'score', 'win_type',
'description', 'event', 'round', 'name', 'room'
]
const timeSettingSelect = [
'main_time', 'time_period', 'period_length', 'overtime', 'overtime_period', 'overtime_length'
]
const gameOverviewSelect = [
'id', 'board_size', 'komi', 'handicap', 'open', 'win_type',
'player_black', 'player_black_rank', 'player_white', 'player_white_rank'
]
const findGameById = async (gameId) => {
const game = await knex('game')
.where({'id': gameId})
.select('*');
const findGameById = async function (gameId) {
const selection = gameDetailSelect.concat(timeSettingSelect);
const game = await knex
.from('game')
.select(selection)
.where({'game.id': gameId})
.leftJoin('time_setting', function() { this.on('time_setting.id', '=', 'game.time_setting')})
return game[0];
}

View file

@ -8,8 +8,9 @@ exports.seed = function(knex) {
{
id: 1, date: new Date(),
application: 'node-go', application_version: '0.1.0',
player_black: 'anon', player_white: 'anon',
player_black_rank: 'K3', player_white_rank: 'K2',
player_black: 'user-one', player_white: 'user-two',
player_black_rank: 'UR', player_white_rank: 'UR',
user_black: 2, user_white: 3,
room: 1, time_setting: 1, open: false
},
{

View file

@ -2,8 +2,10 @@ const express = require('express');
const router = express.Router();
const apiIndexController = require('../controllers/api/apiIndex');
const apiRoomRouter = require('./api/room');
const apiGameRouter = require('./api/game');
router.use('/rooms', apiRoomRouter);
router.use('/games', apiGameRouter);
router.get('/', apiIndexController.apiIndex);

View file

@ -0,0 +1,7 @@
const express = require('express');
const router = express.Router();
const apiGameController = require('../../controllers/api/apiGame');
router.get('/:id', apiGameController.show);
module.exports = router;

View file

@ -8,7 +8,7 @@ io.on('connection', ()=> {
io.emit('connected', {message: 'socket connected'});
})
enableRoomSocket = (roomId) => {
const enableRoomSocket = (roomId) => {
const roomSocket = io.of(roomId);
roomSocket.on('connection', (socket) => {
@ -27,9 +27,22 @@ enableRoomSocket = (roomId) => {
return roomSocket;
}
const enableGameSocket = (roomId, gameId) => {
const gameSocket = io.of(roomId);
gameSocket.on('connection', (socket) => {
socket.join(gameId);
socket.to(gameId).emit(`joined room ${gameId}`)
console.log(socket)
});
return gameSocket;
}
module.exports = {
io,
enableRoomSocket
enableRoomSocket,
enableGameSocket
}
async function logJoinGameRequest (data) {

View file

@ -0,0 +1,48 @@
const apiRoomSpec = (chai, knex, server) => {
const gameEndpoint = '/api/v1/games';
const gameOne = {
id: 1,
room: 1,
application: 'node-go',
application_version: '0.1.0',
board_size: 19,
komi: 6.5,
handicap: 0,
open: false,
win_type: null,
player_black: 'user-one',
player_black_rank: 'UR',
player_white: 'user-two',
player_white_rank: 'UR',
black_captures: null,
white_captures: null,
score: null,
description: null,
event: null,
round: null,
name: null,
main_time: 'untimed',
time_period: 1,
period_length: 0,
overtime: 'untimed',
overtime_period: 0,
overtime_length: 0
}
const recordOne = [
// {}
]
it('request to api games/1 should return 1 room game information with moves', done => {
chai.request(server)
.get(`${gameEndpoint}/1`)
.end((err,res)=> {
if(err) done(err);
res.body.should.eql({ game: gameOne, record: recordOne });
done();
});
})
}
module.exports = apiRoomSpec;

View file

@ -14,10 +14,10 @@ const apiRoomSpec = (chai, knex, server) => {
komi: 6.5,
handicap: 0,
board_size: 19,
player_black: 'anon',
player_white: 'anon',
player_black_rank: 'K3',
player_white_rank: 'K2',
player_black: 'user-one',
player_white: 'user-two',
player_black_rank: 'UR',
player_white_rank: 'UR',
open: false,
win_type: null
},

View file

@ -11,7 +11,8 @@ const should = chai.should();
const authSignupSpec = require('./auth.signup.spec');
const authLoginSpec = require('./auth.login.spec');
const apiIndexSpec = require('./api.index.spec');
const apiRoomSpec = require('./room/api.room.spec');
const apiRoomSpec = require('./api.room.spec');
const apiGameSpec = require('./api.game.spec');
chai.use(chaiHttp);
// ! to run tests from other testing modules
@ -42,5 +43,6 @@ describe('API Routes', function() {
apiIndexSpec(chai, knex, server)
apiRoomSpec(chai, knex, server)
apiGameSpec(chai, knex, server)
});