From 146a213e7bf6e0b580daaf62ed5c1a56ec86c0d2 Mon Sep 17 00:00:00 2001 From: Andrew Zhang Date: Mon, 20 Feb 2017 15:31:18 +0800 Subject: [PATCH] integration --- server/controllers/guessWordController.js | 2 +- server/controllers/startGameController.js | 2 + src/components/WordChip/WordChip.js | 45 +++-- src/routes/Game/components/Game.js | 2 - src/routes/Game/containers/GameContainer.js | 1 - src/routes/Game/modules/game.js | 183 ++++++++++++-------- src/utilities/makeGuess.js | 44 ++--- 7 files changed, 157 insertions(+), 122 deletions(-) diff --git a/server/controllers/guessWordController.js b/server/controllers/guessWordController.js index 8fc9ddc..898d873 100644 --- a/server/controllers/guessWordController.js +++ b/server/controllers/guessWordController.js @@ -45,7 +45,7 @@ export default (req, res) => { // eg: handleWord('*e**', 'test', t) const handleWord = (result, word, guess) => { - const reg = new RegExp(guess, 'g') + const reg = new RegExp(guess, 'ig') while (reg.exec(word)) { result = result.substr(0, reg.lastIndex - 1) + guess + result.substr(reg.lastIndex); } diff --git a/server/controllers/startGameController.js b/server/controllers/startGameController.js index 89cf586..e34346b 100644 --- a/server/controllers/startGameController.js +++ b/server/controllers/startGameController.js @@ -22,6 +22,8 @@ Response: export default (req, res) => { const body = req.body const playerId = body.playerId + console.log(body) + console.log(req) if (validateEmail(playerId)) { const sessionId = jwt.sign({ email: playerId }, config.secret, { expiresIn: 60 * 60 * 24 * 30 // expires in 30 days diff --git a/src/components/WordChip/WordChip.js b/src/components/WordChip/WordChip.js index 2fd81ed..daa5b6f 100644 --- a/src/components/WordChip/WordChip.js +++ b/src/components/WordChip/WordChip.js @@ -10,43 +10,42 @@ import { connect } from 'react-redux' import './WordChip.scss' const styles = { - chip: { - margin: 4 - } + chip: { + margin: 4 + } } function checkWord(str) { - const reg = /\*/ - return !reg.test(str) + const reg = /\*/ + return !reg.test(str) } -function getWord(data, index) { - let word = data.word - return ( - - { - checkWord(word) ? } /> : - } /> - } - {word} - - ) +function getWord(word, index) { + return ( + + { + checkWord(word) ? } /> : + } /> + } + {word} + + ) } export const WordChip = (props) => { - return getWord(props.word, props.index) + return getWord(props.word, props.index) } WordChip.propTypes = { - word: React.PropTypes.array.object, - index: React.PropTypes.array.number + word: React.PropTypes.string, + index: React.PropTypes.number } const TargetView = connect( - (state, ownProps) => { - //can not add console.log(state) here - return { word: state.game[ownProps.targetId], index: ownProps.targetId } - } + (state, ownProps) => { + //can not add console.log(state) here + return { word: state.game[ownProps.targetId], index: ownProps.targetId } + } )(WordChip); export default TargetView diff --git a/src/routes/Game/components/Game.js b/src/routes/Game/components/Game.js index fe64e1f..1815f4b 100644 --- a/src/routes/Game/components/Game.js +++ b/src/routes/Game/components/Game.js @@ -14,7 +14,6 @@ const styles = { export const Game = (props) => { return (
-

Player Id: {props.playerId}

{ } Game.propTypes = { - playerId: React.PropTypes.number.isRequired, startGame: React.PropTypes.func.isRequired, nextWord: React.PropTypes.func.isRequired, guessWord: React.PropTypes.func.isRequired, diff --git a/src/routes/Game/containers/GameContainer.js b/src/routes/Game/containers/GameContainer.js index a1e3a1e..3ce85cf 100644 --- a/src/routes/Game/containers/GameContainer.js +++ b/src/routes/Game/containers/GameContainer.js @@ -27,7 +27,6 @@ const mapDispatchToProps = { // mapStateToProps (state, ownProps ) const mapStateToProps = (state) => { return { - playerId: state.playerId, allIds: state.game.allIds } } diff --git a/src/routes/Game/modules/game.js b/src/routes/Game/modules/game.js index 6dfe8f8..693dae5 100644 --- a/src/routes/Game/modules/game.js +++ b/src/routes/Game/modules/game.js @@ -1,113 +1,150 @@ import { createAction } from 'redux-actions' -//const url = 'http://www.domain-name.com/game/on' -const url = 'http://localhost:3003/api/' -const GUESS_NUMBER = 80 -let mock_id = 0 +import makeGuess from '../../../utilities/makeGuess' -const gameFetch = async function (payload, route) { - const response = await fetch(url + route, { - method: 'GET', - header: { - 'Content-Type': 'application/json' - }, - //body: JSON.stringify(payload) - }) - const res = await response.json() - return res +const url = 'http://localhost:3000/api/game' +const email = 'test@example.com' + +const ctrlStatus = { + sessionId: '', + curWord: '', + curTotalTimes: 0, + curWrongTimes: 0, + numberOfWordsToGuess: 0, + numberOfGuessAllowedForEachWord: 0 +} + +const gameFetch = async function(payload) { + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(payload) + }) + const res = await response.json() + updateControlStatus(ctrlStatus, payload.action, res) + return res.data +} + +const updateControlStatus = (status, action, update) => { + switch (action) { + case START_GAME: + status.sessionId = update.sessionId + status.numberOfWordsToGuess = update.data.numberOfWordsToGuess + status.numberOfGuessAllowedForEachWord = update.data.numberOfGuessAllowedForEachWord + break + case GUESS_WORD: + status.curWrongTimes = update.data.wrongGuessCountOfCurrentWord + status.curTotalTimes += 1 + break + case NEXT_WORD: + status.curWord = update.data.word + status.curWrongTimes = 0 + status.curTotalTimes = 0 + break + default: + break + } } // ------------------------------------ // Constants // ------------------------------------ -export const START_GAME = 'START_GAME' -export const NEXT_WORD = 'NEXT_WORD' -export const GUESS_WORD = 'GUESS_WORD' -export const GET_RESULT = 'GET_RESULT' -export const AUTO_PLAY = 'AUTO_PLAY' +export const START_GAME = 'startGame' +export const NEXT_WORD = 'nextWord' +export const GUESS_WORD = 'guessWord' +export const GET_RESULT = 'getResult' +export const AUTO_PLAY = 'autoPlay' // ------------------------------------ // Actions // ------------------------------------ -export const startGame = createAction(START_GAME, async playerID => { - const payload = { - playerId: playerID, - action: "startGame" - } - const result = await gameFetch(payload, 'start-game') - return result.data +export const startGame = createAction(START_GAME, async () => { + const payload = { + playerId: email, + action: START_GAME + } + const result = await gameFetch(payload) }) -export const nextWord = createAction(NEXT_WORD, async sessionId => { - const payload = { - sessionId: sessionId, - action: 'nextWord' - } - const result = await gameFetch(payload, 'next-word') - return await result.data +export const nextWord = createAction(NEXT_WORD, async() => { + const payload = { + sessionId: ctrlStatus.sessionId, + action: NEXT_WORD + } + const result = await gameFetch(payload) + return result }) -export const guessWord = createAction(GUESS_WORD, async ({guess, sessionId}) => { - const payload = { - sessionId: sessionId, - action: "guessWord", - guess: guess - } - const result = await gameFetch(payload, 'guess-word') - return result.data +export const guessWord = createAction(GUESS_WORD, async(guess) => { + const payload = { + sessionId: ctrlStatus.sessionId, + action: GUESS_WORD, + guess: guess + } + const result = await gameFetch(payload) + return result }) export const getResult = createAction(GET_RESULT, async sessionId => { - const payload = { - sessionId: sessionId, - action: "getResult", - } - const result = await gameFetch(payload, 'get-result') - return result + const payload = { + sessionId: ctrlStatus.sessionId, + action: GET_RESULT + } + const result = await gameFetch(payload) + return result }) // Thunk is not compatible with redux-actions // redux-action always set the returned function as payload of action ojbect. -export const autoPlay = async function () { - return async dispatch => { - for (let i = 0; i < GUESS_NUMBER; i++) { - await dispatch(nextWord({})) - await dispatch(guessWord({})) +export const autoPlay = async function() { + return async dispatch => { + await dispatch(startGame()) + while (ctrlStatus.numberOfWordsToGuess + 1) { + if (ctrlStatus.curWrongTimes >= ctrlStatus.numberOfGuessAllowedForEachWord || !/\*/.test(ctrlStatus.curWord)) { + await dispatch(nextWord()) + ctrlStatus.numberOfWordsToGuess-- + } else { + const guess = makeGuess(ctrlStatus.curWord, ctrlStatus.curTotalTimes) + await dispatch(guessWord(guess)) + } + } } - } } export const actions = { - startGame, - nextWord, - guessWord, - getResult, - autoPlay + startGame, + nextWord, + guessWord, + getResult, + autoPlay } // ------------------------------------ // Action Handlers // ------------------------------------ const ACTION_HANDLERS = { - [NEXT_WORD]: (state, action) => { - let newObj = { ...state } - mock_id++ - newObj[mock_id] = action.payload - newObj.allIds = [...newObj.allIds, mock_id] - return newObj - }, - [GUESS_WORD]: (state, action) => { - let newObj = { ...state } - newObj[mock_id] = action.payload - return newObj - } + [NEXT_WORD]: (state, action) => { + const payload = action.payload + const newObj = {...state } + newObj[payload.totalWordCount] = payload.word + newObj.allIds = [...newObj.allIds, payload.totalWordCount] + return newObj + }, + [GUESS_WORD]: (state, action) => { + const payload = action.payload + const newObj = {...state } + newObj[payload.totalWordCount] = payload.word + return newObj + } } // ------------------------------------ // Reducer // ------------------------------------ -const initialState = {allIds: []} +const initialState = { allIds: [] } export default function gameReducer(state = initialState, action) { - const handler = ACTION_HANDLERS[action.type] - return handler ? handler(state, action) : state + const handler = ACTION_HANDLERS[action.type] + return handler ? handler(state, action) : state } diff --git a/src/utilities/makeGuess.js b/src/utilities/makeGuess.js index 44f73cf..3842204 100644 --- a/src/utilities/makeGuess.js +++ b/src/utilities/makeGuess.js @@ -1,26 +1,26 @@ const strategy = [ - 'AI', - 'AOEIMHNUSTYB', - 'AEOITSUPRNDBG', - 'AESOIRLTNUDCYP', - 'SEAROILTNUDCYPM', - 'ESARIOLNTDUCMPGH', - 'ESIARNTOLDUCGPMHB', - 'ESIARNTOLDCUGMPHBY', - 'ESIRANTOLCDUGMPHBYF', - 'EISRANTOLCDUGMPHBYFV', - 'EISNARTOLCUDPMGHBYFVK', - 'EISNTAROLCPUMDGHYBVFZK', - 'IENTSAORLCPUMGDHYBVFZXK', - 'IETSNAORLCPUMDHGYBVFZXKW', - 'IETNSOARLCPUMDHGYBVFZXWKQ', - 'IETSNAORLCPUMHDYGBVFZXWQKJ', - 'IETNSOARLCPUMHDGYBVFZXQWJK', - 'ISETONRALCPMUHDGYBVZFXQWK', - 'IETONASRLCPMUHDGYBVFZXKJQW', - 'IOETRSANCLPHUMYDGBZVFKXJQ' + 'AI', + 'AOEIMHNUSTYB', + 'AEOITSUPRNDBG', + 'AESOIRLTNUDCYP', + 'SEAROILTNUDCYPM', + 'ESARIOLNTDUCMPGH', + 'ESIARNTOLDUCGPMHB', + 'ESIARNTOLDCUGMPHBY', + 'ESIRANTOLCDUGMPHBYF', + 'EISRANTOLCDUGMPHBYFV', + 'EISNARTOLCUDPMGHBYFVK', + 'EISNTAROLCPUMDGHYBVFZK', + 'IENTSAORLCPUMGDHYBVFZXK', + 'IETSNAORLCPUMDHGYBVFZXKW', + 'IETNSOARLCPUMDHGYBVFZXWKQ', + 'IETSNAORLCPUMHDYGBVFZXWQKJ', + 'IETNSOARLCPUMHDGYBVFZXQWJK', + 'ISETONRALCPMUHDGYBVZFXQWK', + 'IETONASRLCPMUHDGYBVFZXKJQW', + 'IOETRSANCLPHUMYDGBZVFKXJQ' ] export default function makeGuess(word, times) { - return strategy[word.length - 1][times] -} + return strategy[word.length - 1][times] +} \ No newline at end of file