<commit_before>import { combineReducers } from "redux";

import master from "./master";
import player from "./player";

import { PICK_CARD, EMPTY_CARD } from "../../constants/actionTypes";
import { shuffle, replace } from "../../helpers/utils";

import { getId, getCardCoord } from "../selectors";
import cardList, { cardSearcher } from "../static";

function matrix(state = generateMatrix(cardList, 6), action) {
  const { type } = action;
  if (type === PICK_CARD) {
    console.log("matrix", state);
    const from = getCardCoord(state)(action.payload.from);
    const to = getCardCoord(state)(action.payload.card);

    const toCard = cardSearcher(action.payload.card);

    const cards = getCardsBetweenCoords(from, to);

    cards.forEach(coord =>
      console.log(cardSearcher(state[coord.y][coord.x]).name)
    );

    //alter matrix, empty card between master and picked card
    const row = state[from.y];
    const newRow = replace(row, action.payload.from, EMPTY_CARD);

    console.log(from, to);
    return replace(state, row, newRow);
  }
  return state;
}

function generateMatrix(cardList, matrixSize) {
  return shuffle(cardList).reduce((matrix, card, index) => {
    if (index % matrixSize === 0) {
      //new row
      matrix.push([getId(card)]);
    } else {
      //current row
      matrix[matrix.length - 1].push(getId(card));
    }
    return matrix;
  }, []);
}

function getCardsBetweenCoords(a, b, matrix) {
  const cards = [];
  //cards.push(a);

  for (let i = a.x; i <= b.x; i++) {
    for (let j = a.y; j <= b.y; j++) {
      console.log(i, j);

      cards.push({
        x: i,
        y: j
      });
    }
  }

  //cards.push(b);
  return cards;
}

export default combineReducers({
  matrix,
  master,
  player<commit_msg>WIP pick card computation logic<commit_after>import { combineReducers } from "redux";

import master from "./master";
import player from "./player";

import { PICK_CARD, EMPTY_CARD } from "../../constants/actionTypes";
import { shuffle, replace } from "../../helpers/utils";

import { getId, getCardCoord, getFamilyId } from "../selectors";
import cardList, { cardSearcher, familySearcher } from "../static";

function matrix(state = generateMatrix(cardList, 6), action) {
  const { type } = action;
  if (type === PICK_CARD) {
    const from = getCardCoord(state)(action.payload.from);
    const to = getCardCoord(state)(action.payload.card);

    // console.log(from, to);
    const pickedCard = cardSearcher(action.payload.card);
    const pickedFamily = getId(familySearcher(getFamilyId(pickedCard)));

    //select all cards between coordinates and filter them by chosen family
    const cards = getPathBetweenCoords(from, to)
      .map(coord => cardSearcher(state[coord.y][coord.x]))
      .filter(card => getFamilyId(card) === pickedFamily);

    cards.forEach(c => console.log(c));

    return state.map(row => {
      let newRow = row;
      cards.forEach(c => {
        newRow = replace(newRow, getId(c), EMPTY_CARD);
      });
      console.log(newRow);
      return newRow;
    });

    // //alter matrix, empty cards between master and picked card
    // const row = state[from.y];
    // const newRow = replace(row, action.payload.from, EMPTY_CARD);

    // return replace(state, row, newRow);
  }
  return state;
}

function generateMatrix(cardList, matrixSize) {
  return shuffle(cardList).reduce((matrix, card, index) => {
    if (index % matrixSize === 0) {
      //new row
      matrix.push([getId(card)]);
    } else {
      //current row
      matrix[matrix.length - 1].push(getId(card));
    }
    return matrix;
  }, []);
}

function getPathBetweenCoords(from, to, matrix) {
  const coords = [];

  let start = from;
  let end = to;

  //case movement is going backwards
  if (from.x > to.x || from.y > to.y) {
    start = to;
    end = from;
  }

  //double loop but as movement is unidirectional, it will be o(n)
  for (let x = start.x; x <= end.x; x++) {
    for (let y = start.y; y <= end.y; y++) {
      coords.push({
        x,
        y
      });
    }
  }

  return coords;
}

export default combineReducers({
  matrix,
  master,
  player
{'fraction_non_alphanumeric': 0.15358527131782945, 'fraction_numerical': 0.0014534883720930232, 'mean_word_length': 2.951196172248804, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '7362569', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1347, 'n_words': 346}