{"size": 1513, "ext": "js", "max_stars_repo_path": "src/reducers/gridReducer.js", "max_stars_repo_name": "BrianLusina/dungeoncrawler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/reducers/gridReducer.js", "max_issues_repo_name": "BrianLusina/dungeoncrawler", "max_issues_repo_issues_event_min_datetime": "2019-01-08T17:32:02.000Z", "max_issues_repo_issues_event_max_datetime": "2020-04-20T19:49:22.000Z", "max_forks_repo_path": "src/reducers/gridReducer.js", "max_forks_repo_name": "BrianLusina/dungeoncrawler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 29.0961538462, "max_line_length": 100, "alphanum_fraction": 0.5915399868}
/**
 * @author lusinabrian on 09/05/17.
 * @notes: Grid reducer handling actions dealing with the grid of the application
 */

import * as types from '../constants/actionTypes';
// import initialState from './initialState';
import update from 'immutability-helper';

const initialState = {
    entities: [[]],
    dungeonLevel: 0,
    playerPosition: []
};

/**
 * Reducer for the grid
 * @param{object} state, current state of the grid in the application
 * @param{String} type, action type to dispatch to the store
 * @param{object} payload, optional payload to dispatch along with the action to the store to update
 * the application state
 * @return{object} next state of the grid in the application
 * */
export default (state = initialState, {type, payload}) => {
    switch (type){
        case types.CHANGE_ENTITY:
            const [x, y] = payload.coords;
            const entities =	update(state.entities, {
                [y]: {
                    [x]: {$set: payload.entity }
                }
            });
            return { ...state, entities };

        case types.CHANGE_PLAYER_POSITION:
            return { ...state, playerPosition: payload };

        case types.CREATE_LEVEL:
            return {
                ...state,
                playerPosition: payload.playerPosition,
                entities: payload.entities
            };

        case types.SET_DUNGEON_LEVEL:
            return { ...state, dungeonLevel: payload };

        default:
            return state;
    }
}
