diff --git a/packages/play-node-go/play-node-go/src/components/Button/Game/Game.js b/packages/play-node-go/play-node-go/src/components/Button/Game/Game.js index e0258ca..02c28f8 100644 --- a/packages/play-node-go/play-node-go/src/components/Button/Game/Game.js +++ b/packages/play-node-go/play-node-go/src/components/Button/Game/Game.js @@ -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 ( <> {gameLinkText} diff --git a/packages/play-node-go/play-node-go/src/pages/Room/Room.js b/packages/play-node-go/play-node-go/src/pages/Room/Room.js index 421bbbf..5ef0547 100644 --- a/packages/play-node-go/play-node-go/src/pages/Room/Room.js +++ b/packages/play-node-go/play-node-go/src/pages/Room/Room.js @@ -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} /> )) } diff --git a/packages/play-node-go/server/controllers/api/apiGame.js b/packages/play-node-go/server/controllers/api/apiGame.js new file mode 100644 index 0000000..d828830 --- /dev/null +++ b/packages/play-node-go/server/controllers/api/apiGame.js @@ -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 +} \ No newline at end of file diff --git a/packages/play-node-go/server/data/queries/game.js b/packages/play-node-go/server/data/queries/game.js index ca76d00..c8bebe1 100644 --- a/packages/play-node-go/server/data/queries/game.js +++ b/packages/play-node-go/server/data/queries/game.js @@ -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]; } diff --git a/packages/play-node-go/server/data/seeds/04_game.js b/packages/play-node-go/server/data/seeds/04_game.js index 404a348..698633c 100644 --- a/packages/play-node-go/server/data/seeds/04_game.js +++ b/packages/play-node-go/server/data/seeds/04_game.js @@ -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 }, { diff --git a/packages/play-node-go/server/routes/api.js b/packages/play-node-go/server/routes/api.js index ac8c265..1f2d226 100644 --- a/packages/play-node-go/server/routes/api.js +++ b/packages/play-node-go/server/routes/api.js @@ -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); diff --git a/packages/play-node-go/server/routes/api/game.js b/packages/play-node-go/server/routes/api/game.js new file mode 100644 index 0000000..bdf2096 --- /dev/null +++ b/packages/play-node-go/server/routes/api/game.js @@ -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; diff --git a/packages/play-node-go/server/socket.js b/packages/play-node-go/server/socket.js index 579423a..be916f0 100644 --- a/packages/play-node-go/server/socket.js +++ b/packages/play-node-go/server/socket.js @@ -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) { diff --git a/packages/play-node-go/server/test/api.game.spec.js b/packages/play-node-go/server/test/api.game.spec.js new file mode 100644 index 0000000..bb3160d --- /dev/null +++ b/packages/play-node-go/server/test/api.game.spec.js @@ -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; \ No newline at end of file diff --git a/packages/play-node-go/server/test/room/api.room.spec.js b/packages/play-node-go/server/test/api.room.spec.js similarity index 94% rename from packages/play-node-go/server/test/room/api.room.spec.js rename to packages/play-node-go/server/test/api.room.spec.js index d85f364..5205198 100644 --- a/packages/play-node-go/server/test/room/api.room.spec.js +++ b/packages/play-node-go/server/test/api.room.spec.js @@ -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 }, diff --git a/packages/play-node-go/server/test/spec.js b/packages/play-node-go/server/test/spec.js index 43fdfc8..c8940c6 100644 --- a/packages/play-node-go/server/test/spec.js +++ b/packages/play-node-go/server/test/spec.js @@ -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) });