stub game connection via socket room

This commit is contained in:
Sorrel Bri 2020-01-29 23:22:50 -08:00 committed by sorrelbri
parent 857e9e0fd5
commit 1d8cabf5ee
6 changed files with 89 additions and 39 deletions

View file

@ -23,6 +23,11 @@ const launch = (nsp, dispatch) => {
dispatch({ type: 'ROOMS', message: 'NEW_USER', body: data })
})
socket.on('game_connected', (data) => {
console.log(data)
console.log('game_connected received')
})
return socket;
}

View file

@ -10,12 +10,9 @@ import Development from '../../components/Display/Development/Development';
const Game = (props) => {
const { state, dispatch } = props;
const gameId = parseInt(useParams().id) || 0;
const [ socket, setSocket ] = useState(false)
const fetchGameAPI = async () => {
console.log(gameId)
const response = await gamesServices.getGameService(gameId);
console.log(response)
if (response) {
const action = {
type: 'GAMES',
@ -32,36 +29,20 @@ const Game = (props) => {
// ! [start] gameSocket
const roomSocketConnect = (roomSocket) => {
roomSocket.on('connect', socket => {
roomSocket.emit('joined game', gameId)
roomSocket.on('success', () => setSocket(true))
});
roomSocket.on('connect_error', err => {
setSocket(false)
console.log(err);
});
roomSocket.on('error', err => {
setSocket(false)
console.log(err);
});
const roomSocketConnect = () => {
const game = state.active.game;
const user = state.user;
const action = {
type: 'SOCKET',
message: 'CONNECT_GAME',
body: { game, user, dispatch }
}
return dispatch(action);
}
useEffect(() => {
if (!state.active) return;
const roomSocket = socketIOClient(`${config.socketAddress}/${state.active.game.room}`)
roomSocketConnect(roomSocket);
}, [state.active])
useEffect(() => {
const data = {
user: state.user,
game: state.joinGame
};
console.log('emitting request')
console.log(data)
// socket.emit('join_game_request', data)
}, [state.joinGame])
roomSocketConnect();
}, [state.active] )
// ! [end]
@ -74,7 +55,7 @@ const Game = (props) => {
<span
className="Game__socket-flag"
>{socket ? '✓' : ' ⃠'}</span>
>{state.socket ? '✓' : ' ⃠'}</span>
<Development />

View file

@ -8,7 +8,7 @@ const state = initState();
test('renders Game without crashing', () => {
const { getByTestId } = render(
<Router>
<Game state={state} />
<Game state={state} dispatch={()=>{}}/>
</Router>
);
const GameDiv = getByTestId('Game');

View file

@ -5,22 +5,44 @@ const socket = require('../../io');
export const initState = (): state => {
return {
user: { username: '', email: '', id: 0 },
active: {
game: {
id: 0, room: 0,
mainTime: '', timePeriod: 0, periodLength: 0, overtime: '', overtimePeriod: 0, overtimeLength: 0,
application: '', applicationVersion: '', open: false, description: null,
event: null, round: null, name: null,
winType: null, capturesBlack: null, capturesWhite: null, score: null,
boardSize: 0, komi: 0.0, handicap: 0,
playerBlack: '', playerBlackRank: '', playerWhite: '', playerWhiteRank: '',
},
record: []
},
connect: { location: '', type: '' },
currentRoom: {
description: '', id: 0, language: '', name: ''
},
errors: {},
currentRoom: { description: '', id: 0, language: '', name: '' },
messages: [ {
admin: false, content: '', username: ''
} ],
games: [ {
boardSize: 0, handicap: 0, id: 0, komi: 0.0, open: false,
playerBlack: '', playerBlackRank: '', playerWhite: '',
playerWhiteRank: '', winType: null
} ],
joinGame: {},
messages: [ {
admin: false, content: '', username: ''
} ],
socket: {
connected: false,
nsp: ''
},
connect: { location: '', type: '' }
user: { username: '', email: '', id: 0 }
};
}

View file

@ -30,7 +30,30 @@ export const socketReducer = (state: state, action: action):state => {
return {...state, socket}
}
case 'CONNECT_GAME': {
return connectGame(state, action);
}
default:
return state;
}
}
function connectGame (state, action) {
const { user, game, dispatch } = action.body;
const priorSocket = state.socket;
let updatedState;
if ( !priorSocket.nsp || priorSocket.nsp !== `/${game.room}` ) {
const connectRoomAction = {
type: 'SOCKET',
message: 'CONNECT_ROOM',
body: { user, room: game.room, dispatch}
}
updatedState = stateReducer(state, connectRoomAction);
}
if (!updatedState) updatedState = {...state};
const socket = updatedState.socket;
socket.emit('connect_game', {user, game});
return {...updatedState};
}

View file

@ -14,6 +14,25 @@ const getGameService = async (gameIndex) => {
)
.then(res => res.text())
.then(text => JSON.parse(text))
.then(obj => {
// Reformat from SQL coluumn name convention to JS
delete Object.assign(obj.game, {applicationVersion: obj.game.application_version }).application_version;
delete Object.assign(obj.game, {boardSize: obj.game.board_size }).board_size;
delete Object.assign(obj.game, {playerBlack: obj.game.player_black }).player_black;
delete Object.assign(obj.game, {playerBlackRank: obj.game.player_black_rank }).player_black_rank;
delete Object.assign(obj.game, {playerWhite: obj.game.player_white }).player_white;
delete Object.assign(obj.game, {playerWhiteRank: obj.game.player_white_rank }).player_white_rank;
delete Object.assign(obj.game, {capturesWhite: obj.game.captures_white }).captures_white;
delete Object.assign(obj.game, {capturesBlack: obj.game.captures_black }).captures_black;
delete Object.assign(obj.game, {mainTime: obj.game.main_time }).main_time;
delete Object.assign(obj.game, {timePeriod: obj.game.time_period }).time_period;
delete Object.assign(obj.game, {periodLength: obj.game.period_length }).period_length;
delete Object.assign(obj.game, {overtimePeriod: obj.game.overtime_period }).overtime_period;
delete Object.assign(obj.game, {overtimeLength: obj.game.overtime_length }).overtime_length;
delete Object.assign(obj.game, {winType: obj.game.win_type }).win_type;
return obj;
})
.catch(err => err);
return response;