<commit_before>/** @module */
const { ACTION_TYPES, BOARD_TYPES, FACTION_TYPES, PARAMETERS, STYLES } = require('../immutable/constants');
const { JOB_IDS } = require('../immutable/jobs');
const { computeTick, findOneSquareFromBoardsByPlacement } = require('../state-models/complex-apis');
const { areSamePlace, isPlacedOnBoard } = require('../state-models/placement');
const { findSquareByCoordinate, parseMapText } = require('../state-models/square-matrix');
const { createNewUnitState } = require('../state-models/unit');
const { createNewUnitCollectionState, findUnitsByPlacement } = require('../state-models/unit-collection');


const clearCursor = () => {
  return {
    type: ACTION_TYPES.CLEAR_CURSOR,
  };
};

const moveCursor = (placement) => {
  return {
    type: ACTION_TYPES.MOVE_CURSOR,
    placement,
  };
};

const extendGameStatus = (extension) => {
  return {
    type: ACTION_TYPES.EXTEND_GAME_STATUS,
    extension,
  };
};

const updateAlly = (ally) => {
  return {
    type: ACTION_TYPES.UPDATE_ALLY,
    ally,
  };
};

const updateAllies = (allies) => {
  return {
    type: ACTION_TYPES.UPDATE_ALLIES,
    allies,
  };
};

const updateEnemies = (enemies) => {
  return {
    type: ACTION_TYPES.UPDATE_ENEMIES,
    enemies,
  };
};

const tick = (tickId, enemies) => {
  return {
    type: ACTION_TYPES.TICK,
    tickId,
    enemies,
  };
};


/**
 * @param {State~Placement} placement
 */
const touchSquare = (newPlacement) => {
  return (dispatch, getState) => {
    const { cursor, sortieBoard, allies, battleBoard } = getState();
    const currentPlacement = cursor.placement;
    const isCurrentPlacementPlacedOnBoard = isPlacedOnBoard(currentPlacement);
    const currentSquare = findOneSquareFromBoardsByPlacement(currentPlacement, sortieBoard, battleBoard);
    const newSquare = findOneSquareFromBoardsByPlacement(newPlacement, sortieBoard, battleBoard);
    const currentCursorHittingAlly = findUnitsByPlacement(allies, currentPlacement)[0] || null;
    const newCursorHittingAlly = findUnitsByPlacement(allies, newPlacement)[0] || null;

    // TODO: Probably, it becomes very verbose...

    // Make an ally sortie
    if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.BATTLE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Make an ally retreat
    } else if (
      currentPlacement.boardType === BOARD_TYPES.BATTLE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Move the position of an ally in the sortie-board
    } else if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Exchange the positions of allies in the sortie-board
    } else if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      newCursorHittingAlly
    ) {
      const movedAllyA = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      const movedAllyB = Object.assign({}, newCursorHittingAlly, {
        placement: currentPlacement,
      });
      dispatch(updateAlly(movedAllyA));
      dispatch(updateAlly(movedAllyB));
      dispatch(clearCursor());

    // Just the cursor disappears
    } else if (areSamePlace(newPlacement, currentPlacement)) {
      dispatch(clearCursor());

    // Just move the cursor
    } else {
      dispatch(moveCursor(newPlacement));
    }
  };
};

/**
 * TODO: Probably, that is not correct as an Action Creator.
 *       I think that is an "Action Creator Creator".
 */
const startGame = () => {
  return (dispatch, getState) => {
    dispatch(extendGameStatus({ tickId: 0 }));

    const reserveTickTask = () => {
      setTimeout(() => {
        const { allies, enemies, gameStatus } = getState();

        if (gameStatus.isPaused) {
          reserveTickTask();
          return;
        }

        const newState = computeTick({ allies, enemies, gameStatus });

        dispatch(tick(
          gameStatus.tickId + 1,
          newState.enemies,
        ));

        reserveTickTask();
      }, PARAMETERS.TICK_INTERVAL);
    };
    reserveTickTask();
  };
};

/**
 * @return {Function}
 */
const initializeApp = () => {
  const mapText = [
    '..... .',
    '.F     ',
    '. ... .',
    '. ... .',
    '. ... .',
    '. ... .',
    '. .F. .',
    '.C    .',
    '.......',
  ].join('\n');
  const squareMatrixExtension = parseMapText(mapText);

  const allies = createNewUnitCollectionState().concat([
    Object.assign(createNewUnitState(), {
      factionType: FACTION_TYPES.ALLY,
      jobId: JOB_IDS.FIGHTER,
      placement: { boardType: BOARD_TYPES.SORTIE_BOARD, coordinate: [0, 0] },<commit_msg>Fix a syntax error<commit_after>/** @module */
const { ACTION_TYPES, BOARD_TYPES, FACTION_TYPES, PARAMETERS, STYLES } = require('../immutable/constants');
const { JOB_IDS } = require('../immutable/jobs');
const { computeTick, findOneSquareFromBoardsByPlacement } = require('../state-models/complex-apis');
const { areSamePlace, isPlacedOnBoard } = require('../state-models/placement');
const { findSquareByCoordinate, parseMapText } = require('../state-models/square-matrix');
const { createNewUnitState } = require('../state-models/unit');
const { createNewUnitCollectionState, findUnitsByPlacement } = require('../state-models/unit-collection');


const clearCursor = () => {
  return {
    type: ACTION_TYPES.CLEAR_CURSOR,
  };
};

const moveCursor = (placement) => {
  return {
    type: ACTION_TYPES.MOVE_CURSOR,
    placement,
  };
};

const extendGameStatus = (extension) => {
  return {
    type: ACTION_TYPES.EXTEND_GAME_STATUS,
    extension,
  };
};

const updateAlly = (ally) => {
  return {
    type: ACTION_TYPES.UPDATE_ALLY,
    ally,
  };
};

const updateAllies = (allies) => {
  return {
    type: ACTION_TYPES.UPDATE_ALLIES,
    allies,
  };
};

const updateEnemies = (enemies) => {
  return {
    type: ACTION_TYPES.UPDATE_ENEMIES,
    enemies,
  };
};

const tick = (tickId, enemies) => {
  return {
    type: ACTION_TYPES.TICK,
    tickId,
    enemies,
  };
};


/**
 * @param {State~Placement} placement
 */
const touchSquare = (newPlacement) => {
  return (dispatch, getState) => {
    const { cursor, sortieBoard, allies, battleBoard } = getState();
    const currentPlacement = cursor.placement;
    const isCurrentPlacementPlacedOnBoard = isPlacedOnBoard(currentPlacement);
    const currentSquare = findOneSquareFromBoardsByPlacement(currentPlacement, sortieBoard, battleBoard);
    const newSquare = findOneSquareFromBoardsByPlacement(newPlacement, sortieBoard, battleBoard);
    const currentCursorHittingAlly = findUnitsByPlacement(allies, currentPlacement)[0] || null;
    const newCursorHittingAlly = findUnitsByPlacement(allies, newPlacement)[0] || null;

    // TODO: Probably, it becomes very verbose...

    // Make an ally sortie
    if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.BATTLE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Make an ally retreat
    } else if (
      currentPlacement.boardType === BOARD_TYPES.BATTLE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Move the position of an ally in the sortie-board
    } else if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      !newCursorHittingAlly
    ) {
      const movedAlly = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      dispatch(updateAlly(movedAlly));
      dispatch(clearCursor());

    // Exchange the positions of allies in the sortie-board
    } else if (
      currentPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      newPlacement.boardType === BOARD_TYPES.SORTIE_BOARD &&
      currentCursorHittingAlly &&
      newCursorHittingAlly
    ) {
      const movedAllyA = Object.assign({}, currentCursorHittingAlly, {
        placement: newPlacement,
      });
      const movedAllyB = Object.assign({}, newCursorHittingAlly, {
        placement: currentPlacement,
      });
      dispatch(updateAlly(movedAllyA));
      dispatch(updateAlly(movedAllyB));
      dispatch(clearCursor());

    // Just the cursor disappears
    } else if (areSamePlace(newPlacement, currentPlacement)) {
      dispatch(clearCursor());

    // Just move the cursor
    } else {
      dispatch(moveCursor(newPlacement));
    }
  };
};

/**
 * TODO: Probably, that is not correct as an Action Creator.
 *       I think that is an "Action Creator Creator".
 */
const startGame = () => {
  return (dispatch, getState) => {
    dispatch(extendGameStatus({ tickId: 0 }));

    const reserveTickTask = () => {
      setTimeout(() => {
        const { allies, enemies, gameStatus } = getState();

        if (gameStatus.isPaused) {
          reserveTickTask();
          return;
        }

        const newState = computeTick({ allies, enemies, gameStatus });

        dispatch(tick(
          gameStatus.tickId + 1,
          newState.enemies
        ));

        reserveTickTask();
      }, PARAMETERS.TICK_INTERVAL);
    };
    reserveTickTask();
  };
};

/**
 * @return {Function}
 */
const initializeApp = () => {
  const mapText = [
    '..... .',
    '.F     ',
    '. ... .',
    '. ... .',
    '. ... .',
    '. ... .',
    '. .F. .',
    '.C    .',
    '.......',
  ].join('\n');
  const squareMatrixExtension = parseMapText(mapText);

  const allies = createNewUnitCollectionState().concat([
    Object.assign(createNewUnitState(), {
      factionType: FACTION_TYPES.ALLY,
      jobId: JOB_IDS.FIGHTER,
      placement: { boardType: BOARD_TYPES.SORTIE_BOARD, coordinate: [0, 0] },
{'fraction_non_alphanumeric': 0.1344027551205365, 'fraction_numerical': 0.001087547580206634, 'mean_word_length': 3.0540044085231446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7420855', 'n_tokens_mistral': 3501, 'n_tokens_neox': 3504, 'n_words': 633}