stub games/:id endpoint to join game socket room
This commit is contained in:
parent
f0c0b9a0f6
commit
5a2d5648e0
11 changed files with 130 additions and 15 deletions
|
@ -3,7 +3,7 @@ import { Link } from 'react-router-dom';
|
||||||
import './Game.scss';
|
import './Game.scss';
|
||||||
|
|
||||||
const GameButton = (props) => {
|
const GameButton = (props) => {
|
||||||
const { game, dispatch } = props;
|
const { game, dispatch, user } = props;
|
||||||
|
|
||||||
const requestJoinGame = () => {
|
const requestJoinGame = () => {
|
||||||
console.log(`request to Join Game ${game.id}!`)
|
console.log(`request to Join Game ${game.id}!`)
|
||||||
|
@ -29,7 +29,8 @@ const GameButton = (props) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const renderInProgressGame = () => {
|
const renderInProgressGame = () => {
|
||||||
const gameLinkText = game.winType ? 'Study Game' : 'Watch Game'
|
const gameLinkText = game.winType ? 'Study Game'
|
||||||
|
: user ? 'Rejoin Game' : 'Watch Game'
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Link to={`/games/${game.id}`}>{gameLinkText}</Link>
|
<Link to={`/games/${game.id}`}>{gameLinkText}</Link>
|
||||||
|
|
|
@ -78,6 +78,7 @@ const Room = (props) => {
|
||||||
key={`game-${gameData.id}`}
|
key={`game-${gameData.id}`}
|
||||||
game={gameData}
|
game={gameData}
|
||||||
dispatch={dispatch}
|
dispatch={dispatch}
|
||||||
|
user={gameData.playerBlack === state.user.username || gameData.playerWhite === state.user.username}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
24
packages/play-node-go/server/controllers/api/apiGame.js
Normal file
24
packages/play-node-go/server/controllers/api/apiGame.js
Normal 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
|
||||||
|
}
|
|
@ -1,14 +1,30 @@
|
||||||
const knex = require('../db');
|
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 = [
|
const gameOverviewSelect = [
|
||||||
'id', 'board_size', 'komi', 'handicap', 'open', 'win_type',
|
'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'
|
||||||
]
|
]
|
||||||
|
|
||||||
const findGameById = async (gameId) => {
|
const findGameById = async function (gameId) {
|
||||||
const game = await knex('game')
|
const selection = gameDetailSelect.concat(timeSettingSelect);
|
||||||
.where({'id': gameId})
|
|
||||||
.select('*');
|
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];
|
return game[0];
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@ exports.seed = function(knex) {
|
||||||
{
|
{
|
||||||
id: 1, date: new Date(),
|
id: 1, date: new Date(),
|
||||||
application: 'node-go', application_version: '0.1.0',
|
application: 'node-go', application_version: '0.1.0',
|
||||||
player_black: 'anon', player_white: 'anon',
|
player_black: 'user-one', player_white: 'user-two',
|
||||||
player_black_rank: 'K3', player_white_rank: 'K2',
|
player_black_rank: 'UR', player_white_rank: 'UR',
|
||||||
|
user_black: 2, user_white: 3,
|
||||||
room: 1, time_setting: 1, open: false
|
room: 1, time_setting: 1, open: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,8 +2,10 @@ const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const apiIndexController = require('../controllers/api/apiIndex');
|
const apiIndexController = require('../controllers/api/apiIndex');
|
||||||
const apiRoomRouter = require('./api/room');
|
const apiRoomRouter = require('./api/room');
|
||||||
|
const apiGameRouter = require('./api/game');
|
||||||
|
|
||||||
router.use('/rooms', apiRoomRouter);
|
router.use('/rooms', apiRoomRouter);
|
||||||
|
router.use('/games', apiGameRouter);
|
||||||
|
|
||||||
router.get('/', apiIndexController.apiIndex);
|
router.get('/', apiIndexController.apiIndex);
|
||||||
|
|
||||||
|
|
7
packages/play-node-go/server/routes/api/game.js
Normal file
7
packages/play-node-go/server/routes/api/game.js
Normal 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;
|
|
@ -8,7 +8,7 @@ io.on('connection', ()=> {
|
||||||
io.emit('connected', {message: 'socket connected'});
|
io.emit('connected', {message: 'socket connected'});
|
||||||
})
|
})
|
||||||
|
|
||||||
enableRoomSocket = (roomId) => {
|
const enableRoomSocket = (roomId) => {
|
||||||
const roomSocket = io.of(roomId);
|
const roomSocket = io.of(roomId);
|
||||||
roomSocket.on('connection', (socket) => {
|
roomSocket.on('connection', (socket) => {
|
||||||
|
|
||||||
|
@ -27,9 +27,22 @@ enableRoomSocket = (roomId) => {
|
||||||
return roomSocket;
|
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 = {
|
module.exports = {
|
||||||
io,
|
io,
|
||||||
enableRoomSocket
|
enableRoomSocket,
|
||||||
|
enableGameSocket
|
||||||
}
|
}
|
||||||
|
|
||||||
async function logJoinGameRequest (data) {
|
async function logJoinGameRequest (data) {
|
||||||
|
|
48
packages/play-node-go/server/test/api.game.spec.js
Normal file
48
packages/play-node-go/server/test/api.game.spec.js
Normal 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;
|
|
@ -14,10 +14,10 @@ const apiRoomSpec = (chai, knex, server) => {
|
||||||
komi: 6.5,
|
komi: 6.5,
|
||||||
handicap: 0,
|
handicap: 0,
|
||||||
board_size: 19,
|
board_size: 19,
|
||||||
player_black: 'anon',
|
player_black: 'user-one',
|
||||||
player_white: 'anon',
|
player_white: 'user-two',
|
||||||
player_black_rank: 'K3',
|
player_black_rank: 'UR',
|
||||||
player_white_rank: 'K2',
|
player_white_rank: 'UR',
|
||||||
open: false,
|
open: false,
|
||||||
win_type: null
|
win_type: null
|
||||||
},
|
},
|
|
@ -11,7 +11,8 @@ const should = chai.should();
|
||||||
const authSignupSpec = require('./auth.signup.spec');
|
const authSignupSpec = require('./auth.signup.spec');
|
||||||
const authLoginSpec = require('./auth.login.spec');
|
const authLoginSpec = require('./auth.login.spec');
|
||||||
const apiIndexSpec = require('./api.index.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);
|
chai.use(chaiHttp);
|
||||||
// ! to run tests from other testing modules
|
// ! to run tests from other testing modules
|
||||||
|
@ -42,5 +43,6 @@ describe('API Routes', function() {
|
||||||
|
|
||||||
apiIndexSpec(chai, knex, server)
|
apiIndexSpec(chai, knex, server)
|
||||||
apiRoomSpec(chai, knex, server)
|
apiRoomSpec(chai, knex, server)
|
||||||
|
apiGameSpec(chai, knex, server)
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue