Skip to content

Commit

Permalink
Create getNextMovable function (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsveron23 committed Mar 3, 2019
1 parent 963288c commit 30e9209
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 104 deletions.
49 changes: 24 additions & 25 deletions src/actions/ingame.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { compose, ifElse, reject, thunkify, flip, prop, identity } from 'ramda'
import { compose, ifElse, reject, thunkify, flip, identity } from 'ramda'
import * as types from '~/actions'
import { OPPONENT } from '~/chess/constants'
import {
getMovableAxis,
getNextMovable,
getNextSnapshot,
applySpecialActions,
createTimeline
Expand All @@ -11,8 +11,6 @@ import {
getSpecial,
parseSelected,
replaceSnapshot,
parseCode,
findCode,
createSelected
} from '~/chess/helpers'
import { isEmpty, isExist } from '~/utils'
Expand Down Expand Up @@ -50,31 +48,32 @@ export function setSnapshot (snapshot) {
}
}

export function setMovable (tile) {
export function setNext (snapshot) {
return (dispatch) => {
dispatch(setSnapshot(snapshot))
dispatch(setMovableAxis())
dispatch(toggleTurn())
}
}

export function setNextMovableAxis (tile) {
return (dispatch, getState) => {
const { ingame } = getState()
const { present } = ingame
const { turn, snapshot } = present
const nextSelected = createSelected(tile, turn)

const flippedGetMovableAxis = compose(
flip,
getMovableAxis
)(tile)
const nextSelected = createSelected(tile, turn)

const movableAxis = compose(
flippedGetMovableAxis(turn),
prop('piece'),
parseCode,
findCode(snapshot)
)(tile)
const nextMovableAxis = getNextMovable('axis', () => {
return { tile, timeline: [snapshot], ...present }
})

dispatch(setMovableAxis(movableAxis))
dispatch(setMovableAxis(nextMovableAxis))
dispatch(setSelected(nextSelected))
}
}

export function setNext (tile) {
export function setNextSnapshot (tile) {
return (dispatch, getState) => {
const { ingame } = getState()
const { present, past } = ingame
Expand All @@ -93,13 +92,15 @@ export function setNext (tile) {
getNextSnapshot(selected, tile)
)(snapshot)

dispatch(setSnapshot(nextSnapshot))
dispatch(setMovableAxis())
dispatch(toggleTurn())
dispatch(setNext(nextSnapshot))
}
}

export function setCapturedNext ({ capturedTile, selectedTile, nextCode }) {
export function setNextCapturedSnapshot ({
capturedTile,
selectedTile,
nextCode
}) {
return (dispatch, getState) => {
const { ingame } = getState()
const { present } = ingame
Expand All @@ -110,8 +111,6 @@ export function setCapturedNext ({ capturedTile, selectedTile, nextCode }) {
replaceSnapshot('', capturedTile)
)(present.snapshot)

dispatch(setSnapshot(capturedSnapshot))
dispatch(setMovableAxis())
dispatch(toggleTurn())
dispatch(setNext(capturedSnapshot))
}
}
4 changes: 2 additions & 2 deletions src/chess/core/getMovableAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ function createMapCb (tile, turn) {
/**
* Get movable axis from movements of piece (no invalid axis filter here)
* @param {string} tile
* @param {string} piece
* @param {string} turn
* @param {string} piece
* @return {Array}
*/
function getMovableAxis (tile, piece, turn) {
function getMovableAxis (tile, turn, piece) {
const mapCb = createMapCb(tile, turn)

return compose(
Expand Down
44 changes: 44 additions & 0 deletions src/chess/core/getNextMovable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { compose, curry, ifElse, thunkify, prop } from 'ramda'
import {
getMovableAxis,
getMovableTiles,
appendSpecialAxis,
rejectBlocked,
groupByDirection
} from '~/chess/core'
import { parseCode, findCode } from '~/chess/helpers'
import { isExist } from '~/utils'

/**
* Get next movable
* @param {string} type 'tiles', 'axis'
* @param {Function} getFlatArgs
* @return {Array}
*/
function getNextMovable (type, getFlatArgs) {
const { turn, movableAxis, timeline, special, side, tile } = getFlatArgs()
const [snapshot] = timeline

if (type === 'axis') {
return compose(
getMovableAxis(tile, turn),
prop('piece'),
parseCode,
findCode(snapshot)
)(tile)
}

const getSpecialAxisFn = appendSpecialAxis(side, special, tile, timeline)

const getRegularAxisFn = compose(
rejectBlocked(turn, snapshot),
groupByDirection
)

return compose(
getMovableTiles,
ifElse(thunkify(isExist)(special), getSpecialAxisFn, getRegularAxisFn)
)(movableAxis)
}

export default curry(getNextMovable)
1 change: 1 addition & 0 deletions src/chess/core/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as getMovableAxis } from './getMovableAxis'
export { default as getMovableTiles } from './getMovableTiles'
export { default as getNextMovable } from './getNextMovable'
export { default as appendSpecialAxis } from './appendSpecialAxis'
export { default as applySpecialActions } from './applySpecialActions'
export { default as getNextSnapshot } from './getNextSnapshot'
Expand Down
16 changes: 8 additions & 8 deletions src/chess/enhancePiece.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ function enhancePiece (WrappedComponent, staticKey, staticTurn) {
selectedFile: PropTypes.string,
selectedRank: PropTypes.string,
isMovable: PropTypes.bool,
setMovable: PropTypes.func,
setCapturedNext: PropTypes.func
setNextMovableAxis: PropTypes.func,
setNextCapturedSnapshot: PropTypes.func
}

static defaultProps = {
isMovable: false,
setMovable: noop,
setCapturedNext: noop
setNextMovableAxis: noop,
setNextCapturedSnapshot: noop
}

getSelectedTile = memoize(createTile)
Expand All @@ -53,22 +53,22 @@ function enhancePiece (WrappedComponent, staticKey, staticTurn) {
selectedSide,
selectedFile,
selectedRank,
setMovable,
setCapturedNext
setNextMovableAxis,
setNextCapturedSnapshot
} = this.props

const selectedTile = this.getSelectedTile(selectedFile, selectedRank)
const isTurn = getSide(staticTurn) === turn
const isCapturable = isMovable && !isTurn

if (isTurn) {
setMovable(tile)
setNextMovableAxis(tile)
}

if (isCapturable) {
const code = `${selectedSide}${selectedPiece}${tile}`

setCapturedNext({
setNextCapturedSnapshot({
selectedTile,
capturedTile: tile,
nextCode: code
Expand Down
24 changes: 12 additions & 12 deletions src/components/Diagram/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const Diagram = (props) => {
ranks,
files,
movableTiles,
setCapturedNext,
setMovable,
setNext
setNextCapturedSnapshot,
setNextMovableAxis,
setNextSnapshot
} = props

if (!isDoingMatch) {
Expand All @@ -40,9 +40,9 @@ const Diagram = (props) => {
files={files}
rankName={rankName}
movableTiles={movableTiles}
setCapturedNext={setCapturedNext}
setMovable={setMovable}
setNext={setNext}
setNextCapturedSnapshot={setNextCapturedSnapshot}
setNextMovableAxis={setNextMovableAxis}
setNextSnapshot={setNextSnapshot}
/>
)
})}
Expand All @@ -61,15 +61,15 @@ Diagram.propTypes = {
selectedFile: PropTypes.string,
selectedRank: PropTypes.string,
movableTiles: PropTypes.array,
setCapturedNext: PropTypes.func,
setMovable: PropTypes.func,
setNext: PropTypes.func
setNextCapturedSnapshot: PropTypes.func,
setNextMovableAxis: PropTypes.func,
setNextSnapshot: PropTypes.func
}

Diagram.defaultProps = {
setCapturedNext: noop,
setMovable: noop,
setNext: noop
setNextCapturedSnapshot: noop,
setNextMovableAxis: noop,
setNextSnapshot: noop
}

export default Diagram
24 changes: 12 additions & 12 deletions src/components/File/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ class File extends Component {
selectedRank: PropTypes.string,
movableTiles: PropTypes.array,
children: PropTypes.func,
setCapturedNext: PropTypes.func,
setMovable: PropTypes.func,
setNext: PropTypes.func
setNextCapturedSnapshot: PropTypes.func,
setNextMovableAxis: PropTypes.func,
setNextSnapshot: PropTypes.func
}

static defaultProps = {
movableTiles: [],
setCapturedNext: noop,
setMovable: noop,
setNext: noop
setNextCapturedSnapshot: noop,
setNextMovableAxis: noop,
setNextSnapshot: noop
}

isMovable = memoize(includes)
Expand All @@ -50,14 +50,14 @@ class File extends Component {
handleClick (evt) {
evt.preventDefault()

const { tile, children, movableTiles, setNext } = this.props
const { tile, children, movableTiles, setNextSnapshot } = this.props
const shouldSetNext = compose(
and(isEmpty(children)),
this.isMovable(tile)
)(movableTiles)

if (shouldSetNext) {
setNext(tile)
setNextSnapshot(tile)
}
}

Expand All @@ -72,8 +72,8 @@ class File extends Component {
selectedFile,
selectedRank,
movableTiles,
setCapturedNext,
setMovable
setNextCapturedSnapshot,
setNextMovableAxis
} = this.props

const isMovable = this.isMovable(tile, movableTiles)
Expand All @@ -86,8 +86,8 @@ class File extends Component {
selectedRank,
tile,
isMovable,
setCapturedNext,
setMovable
setNextCapturedSnapshot,
setNextMovableAxis
}

const blankProps = {
Expand Down
24 changes: 12 additions & 12 deletions src/components/Rank/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const Rank = (props) => {
selectedFile,
selectedRank,
movableTiles,
setCapturedNext,
setMovable,
setNext
setNextCapturedSnapshot,
setNextMovableAxis,
setNextSnapshot
} = props
const cls = cx(css.rank, 'l-flex-row')
const parseCodeByTile = findCodeByTile(snapshot)
Expand All @@ -42,9 +42,9 @@ const Rank = (props) => {
selectedFile={selectedFile}
selectedRank={selectedRank}
movableTiles={movableTiles}
setCapturedNext={setCapturedNext}
setMovable={setMovable}
setNext={setNext}
setNextCapturedSnapshot={setNextCapturedSnapshot}
setNextMovableAxis={setNextMovableAxis}
setNextSnapshot={setNextSnapshot}
>
{getPiece(side, piece)}
</File>
Expand All @@ -64,16 +64,16 @@ Rank.propTypes = {
selectedFile: PropTypes.string,
selectedRank: PropTypes.string,
movableTiles: PropTypes.array,
setCapturedNext: PropTypes.func,
setMovable: PropTypes.func,
setNext: PropTypes.func
setNextCapturedSnapshot: PropTypes.func,
setNextMovableAxis: PropTypes.func,
setNextSnapshot: PropTypes.func
}

Rank.defaultProps = {
setSelected: noop,
setCapturedNext: noop,
setMovable: noop,
setNext: noop
setNextCapturedSnapshot: noop,
setNextMovableAxis: noop,
setNextSnapshot: noop
}

export default Rank
Loading

0 comments on commit 30e9209

Please sign in to comment.