Skip to content

Instantly share code, notes, and snippets.

View cazala's full-sized avatar
🤸‍♂️

Juan Cazala cazala

🤸‍♂️
View GitHub Profile
case CHECKMATE: {
if (state.status === STATUS.started) {
return Object.assign({}, state, {
status: STATUS.checkmate
})
}
return state
}
case UNREGISTER_PLAYER: {
const { playerWhite, playerBlack } = state
if (playerWhite === action.playerId || playerBlack === action.playerId) {
return initialState // one of the players playing left, so we reset the game
}
}
case REGISTER_PLAYER: {
let { playerWhite, playerBlack, status } = state
if (playerWhite && playerBlack) {
return state // both players already registered
}
if (playerWhite === action.playerId || playerBlack === action.playerId) {
return state // this player is already registered
}
// src/modules/match/reducer.js
import { INIT_SQUARES } from '../squares/actions'
import { REGISTER_PLAYER, UNREGISTER_PLAYER, CHECKMATE } from './actions'
const STATUS = {
idle: 'idle',
started: 'started',
checkmate: 'checkmate'
}
// src/modules/match/actions.js
export const REGISTER_PLAYER = 'chess/match/register_player'
export const UNREGISTER_PLAYER = 'chess/match/unregister_player'
export const CHECKMATE = 'chess/match/checkmate'
export const registerPlayer = (playerId, isWhite) => ({
type: REGISTER_PLAYER,
playerId,
isWhite
})
sceneDidMount() {
this.eventSubscriber.on('click', event => {
const { elementId } = event.data
if (elementId != null) {
const squareId = getSquareId(elementId)
const square = store
.getState()
.squares.find((square: any) => square.id === squareId)
store.dispatch(squareClick(square.id, square.pieceId, square.color))
}
const getSquareId = (elementId: string) => elementId.split('-')[0]
const { elementId } = event.data
if (elementId != null) {
// dispatch redux action
}
sceneDidMount() {
  this.eventSubscriber.on('click', event => {
-   setState({ isDoorClosed: !getState().isDoorClosed })
  })
}
const tileSize = { x: 1, y: 0.1, z: 1 }
return (
<entity position={position}>
<box color={color} scale={tileSize} id={`${square.id}-tile`} />
{square.pieceId in modelsById ? (
<gltf-model src={modelsById[square.pieceId]} id={`${square.id}-piece`} />
) : null}
</entity>
)