-
{state.currentRoom ? state.currentRoom.name : 'Loading'}
+
+
{state.currentRoom ? state.currentRoom.name : 'Loading'}
+
{socketData ? '✓' : ' ⃠'}
+ {state.errors.joinGame ?
: <>>}
+
{renderGames()}
diff --git a/packages/play-node-go/play-node-go/src/reducers/err/stateReducer.err.js b/packages/play-node-go/play-node-go/src/reducers/err/stateReducer.err.js
index 876d1c4..d563dd4 100644
--- a/packages/play-node-go/play-node-go/src/reducers/err/stateReducer.err.js
+++ b/packages/play-node-go/play-node-go/src/reducers/err/stateReducer.err.js
@@ -8,6 +8,9 @@ export const errorReducer = (state: state, action: action):state => {
case 'JOIN_ROOM_ERROR':
return joinRoomErrorReducer(state, action);
+
+ case 'JOIN_GAME_ERROR':
+ return joinGameErrorReducer(state, action);
default:
return state;
@@ -22,4 +25,9 @@ function authErrorReducer(state: state, action: action): state {
function joinRoomErrorReducer(state: state, action: action): state {
const joinRoom = action.body.joinRoomError;
return { ...state, errors: {joinRoom} }
+}
+
+function joinGameErrorReducer(state: state, action: action): state {
+ const joinGame = action.body.joinGameError;
+ return { ...state, errors: {joinGame} }
}
\ No newline at end of file
diff --git a/packages/play-node-go/play-node-go/src/reducers/games/stateReducer.games.js b/packages/play-node-go/play-node-go/src/reducers/games/stateReducer.games.js
index 47d462e..3427d4d 100644
--- a/packages/play-node-go/play-node-go/src/reducers/games/stateReducer.games.js
+++ b/packages/play-node-go/play-node-go/src/reducers/games/stateReducer.games.js
@@ -6,11 +6,51 @@ export const gamesReducer = (state: state, action: action):state => {
switch(action.message) {
case 'SET_GAMES':
- const games = action.body;
+ const games = formatGames(action);;
return {...state, games};
+
+ case 'JOIN_REQUEST':
+ if (!Object.entries(state.user).length) {
+ const errAction = {
+ type: 'ERR',
+ message: 'JOIN_GAME_ERROR',
+ body: {joinGameError: 'user not logged in'}
+ }
+ return stateReducer(state, errAction)
+ }
+ const id = action.body;
+ return {...state, joinGame: id};
default:
return state;
}
+}
+
+function parseRank(rank: string): string {
+ switch(rank[0]) {
+ case 'D':
+ return `${rank.slice(1)}${rank[0].toLowerCase()}`
+ case 'K':
+ return `${rank.slice(1)}${rank[0].toLowerCase()}`
+ case 'U':
+ return '?'
+ }
+}
+
+function formatGames(action: action): Array<{}> {
+ const games = [...action.body].map(game => {
+
+ if (game.playerBlackRank) {
+ game.playerBlackRank = parseRank(game.playerBlackRank)
+ }
+
+ if (game.playerWhiteRank) {
+ game.playerWhiteRank = parseRank(game.playerWhiteRank)
+ }
+
+ return game;
+ })
+
+ return games;
}
\ No newline at end of file
diff --git a/packages/play-node-go/play-node-go/src/reducers/init/stateReducer.init.js b/packages/play-node-go/play-node-go/src/reducers/init/stateReducer.init.js
index 1780d36..60d3e01 100644
--- a/packages/play-node-go/play-node-go/src/reducers/init/stateReducer.init.js
+++ b/packages/play-node-go/play-node-go/src/reducers/init/stateReducer.init.js
@@ -5,6 +5,10 @@ import type { state } from '../stateReducer';
export const initState = (): state => {
return {
user: {},
- errors: {}
+ errors: {},
+ currentRoom: {},
+ messages: {},
+ games: {},
+ joinGame: {}
};
}
\ No newline at end of file
diff --git a/packages/play-node-go/play-node-go/src/services/api/roomsServices.js b/packages/play-node-go/play-node-go/src/services/api/roomsServices.js
index 92ca915..89d78f6 100644
--- a/packages/play-node-go/play-node-go/src/services/api/roomsServices.js
+++ b/packages/play-node-go/play-node-go/src/services/api/roomsServices.js
@@ -32,6 +32,7 @@ const getRoomService = async (roomIndex) => {
delete Object.assign(game, {playerBlackRank: game.player_black_rank }).player_black_rank;
delete Object.assign(game, {playerWhite: game.player_white }).player_white;
delete Object.assign(game, {playerWhiteRank: game.player_white_rank }).player_white_rank;
+ delete Object.assign(game, {winType: game.win_type }).win_type;
return game;
})
return obj;
diff --git a/packages/play-node-go/server/controllers/api/apiRoom.js b/packages/play-node-go/server/controllers/api/apiRoom.js
index 74d8195..7a108e4 100644
--- a/packages/play-node-go/server/controllers/api/apiRoom.js
+++ b/packages/play-node-go/server/controllers/api/apiRoom.js
@@ -18,6 +18,8 @@ const getAll = async (req, res, next) => {
const show = async (req, res, next) => {
try {
const roomId = req.params.id;
+ if (!roomId) throw('missing room parameter')
+
// TODO eventually add check for user's private rooms
enableRoomSocket(roomId);
diff --git a/packages/play-node-go/server/socket.js b/packages/play-node-go/server/socket.js
index f5b3195..579423a 100644
--- a/packages/play-node-go/server/socket.js
+++ b/packages/play-node-go/server/socket.js
@@ -11,12 +11,18 @@ io.on('connection', ()=> {
enableRoomSocket = (roomId) => {
const roomSocket = io.of(roomId);
roomSocket.on('connection', (socket) => {
- // socket.emit('connected');
- console.log(`Socket connected at room ${roomId}`);
+
+ //! Join Game Request queries db for game, ensures unique player joining
socket.on('join_game_request', async data => {
const gameRequest = await logJoinGameRequest(data);
+
+ if (gameRequest.err) {
+ roomSocket.emit('join_game_request_error', gameRequest.err);
+ }
+
roomSocket.emit('join_game_request', gameRequest);
- })
+ });
+
});
return roomSocket;
}
@@ -29,5 +35,12 @@ module.exports = {
async function logJoinGameRequest (data) {
const {user, game} = data;
const requestedGame = await gameQueries.findGameById(game.id);
- return { user, requestedGame }
+
+ if (requestedGame.user_black === user.id) {
+ return { err: 'players must be unique' }
+ }
+
+ const requestingUser = {...user};
+ delete requestingUser.email;
+ return { requestingUser, requestedGame }
}
\ No newline at end of file