{"size": 1621, "ext": "js", "max_stars_repo_path": "src/client/actions/GameActions.js", "max_stars_repo_name": "igotlegs/skipbo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/client/actions/GameActions.js", "max_issues_repo_name": "igotlegs/skipbo", "max_issues_repo_issues_event_min_datetime": "2021-03-10T11:58:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T01:41:29.000Z", "max_forks_repo_path": "src/client/actions/GameActions.js", "max_forks_repo_name": "igotlegs/skipbo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.2625, "max_line_length": 54, "alphanum_fraction": 0.6002467613}
import API from '../api/Api'
import { 
  CREATE_GAME, 
  SETUP_GAME, 
  START_GAME,
  NEW_PLAYER, 
  SET_PLAYER_DECK,
  INIT_PLAYER_TABLE_CARDS,
  SET_PLAYER_IDENTITY,
} from '../constants/ActionTypes'
import { dealCardsToMyHand, } from './GamePlayActions'

export const createNewGame = (playerCount) => {
  return (dispatch) => {
    API.createGame(playerCount)
      .then((data) => {
        dispatch({
          type: CREATE_GAME,
          ...data,
        })
      })
      .catch((e) => {
        console.log(e)
      })
  }
}

export const setupGame = (data) => {
  return (dispatch) => {
    dispatch({type: SETUP_GAME})
    dispatch(setPlayerDeck(data))
    dispatch(initPlayerTableCards(data.id))

    // MOCK, should be linked to game turn
    dispatch(dealCardsToMyHand(5))
  }
}

export const setPlayerDeck = (data) => ({
  type: SET_PLAYER_DECK,
  playerId: data.id,
  topMostCard: data.deckTopMostCard,
  size: data.deckSize,
})

export const initPlayerTableCards = (playerId) => ({
  type: INIT_PLAYER_TABLE_CARDS,
  playerId,
})

export const startGame = () => ({
  type: START_GAME,
})

export const joinGame = (playerName) => {
  return (dispatch, getState) => {
    const gameId = getState().game.get('id')

    API.addPlayerToGame(playerName, gameId)
      .then((data) => {
        dispatch(setWhoAmI(data.id))
        dispatch({
          type: NEW_PLAYER,
          id: data.id,
          name: data.name,
        })

        dispatch(setupGame(data))
      })
      .catch((e) => {
        console.log(e)
      })
  }
}

export const setWhoAmI = (id) => ({
  type: SET_PLAYER_IDENTITY,
  id,
})
