<commit_before>/*
 * Copyright 2018 The flamecoals-boardgame.io Authors.
 *
 * Use of this source code is governed by a MIT-style
 * license that can be found in the LICENSE file or at
 * https://opensource.org/licenses/MIT.
 */

import React from 'react';

import Chess from './chessjswrapper';
import { Checkerboard, IAlgebraicCoords, IColorMap } from './checkerboard';
import { Token } from 'flamecoals-boardgame.io/ui';
import { IGameArgs } from '../../App/Game/GameBoardWrapper';
import { GameLayout } from '../../App/Game/GameLayout';
import { GameMode } from '../../App/Game/GameModePicker';
import Bishop from './pieces/bishop';
import King from './pieces/king';
import Knight from './pieces/knight';
import Pawn from './pieces/pawn';
import Queen from './pieces/queen';
import Rook from './pieces/rook';
import AlertLayer from '../../App/Game/AlertLayer';
import ReactGA from 'react-ga';

const COL_NAMES = 'abcdefgh';
const SELECTED_COLOR = 'green';
const MOVABLE_COLOR = 'palegreen';

interface IBoardProps {
  G: any;
  ctx: any;
  moves: any;
  playerID: string;
  isActive: boolean;
  isConnected: boolean;
  gameArgs?: IGameArgs;
}

export class Board extends React.Component<IBoardProps, {}> {
  chess = Chess();
  state = {
    selected: '',
  };
  _click = this.click.bind(this);

  componentWillReceiveProps(nextProps: IBoardProps) {
    if (nextProps.G.pgn) {
      this.chess.load_pgn(nextProps.G.pgn);
      this.setState({
        ...this.state,
        selected: '',
      });
    }
  }

  render() {
    if (this.props.ctx.gameover) {
      return (
        <GameLayout gameOver={this._getGameOver()} />
      );
    }
    return (
      <GameLayout>
        <div>
          <h2 style={{ textAlign: 'center' }}>
            {this._getStatus()}
          </h2>
          <Checkerboard
            invert={this.getPlayer() === 'b'}
            highlightedSquares={this._getHighlightedSquares()}
            onClick={this._click}
          >
            {this._getPieces()}
          </Checkerboard>
        </div>
      </GameLayout>
    );
  }

  getPlayer(): 'w' | 'b' {
    if (this.props.playerID === '1') {
      return 'b';
    } else {
      return 'w';
    }
  }

  click(coords: IAlgebraicCoords) {
    ReactGA.event({
      category: 'ChessGame',
      action: 'click',
    });
    const { square } = coords;
    if (!this.props.isActive) {
      return;
    }

    if (!this.state.selected && this._isSelectable(square)) {
      this.setState({
        ...this.state,
        selected: square,
      });
    }

    if (this.state.selected) {
      const moves = this._getMoves();
      const move = moves.find((m: any) => {
        return m.from === this.state.selected && m.to === square;
      });
      if (move) {
        this.props.moves.move(move.san);
      } else {
        this.setState({
          ...this.state,
          selected: '',
        });
      }
    }
  }

  _getHighlightedSquares() {
    const result = {} as IColorMap;
    if (this.state.selected) {
      result[this.state.selected] = SELECTED_COLOR;
    }
    for (const move of this._getMoves()) {
      result[move.to] = MOVABLE_COLOR;
    }
    return result;
  }

  _getPieces() {
    const result = [];
    for (let y = 1; y <= 8; y++) {
      for (let x = 0; x < 8; x++) {
        const square = COL_NAMES[x] + y;
        const p = this.chess.get(square);<commit_msg>Fixing chess not loading state issue<commit_after>/*
 * Copyright 2018 The flamecoals-boardgame.io Authors.
 *
 * Use of this source code is governed by a MIT-style
 * license that can be found in the LICENSE file or at
 * https://opensource.org/licenses/MIT.
 */

import React from 'react';

import Chess from './chessjswrapper';
import { Checkerboard, IAlgebraicCoords, IColorMap } from './checkerboard';
import { Token } from 'flamecoals-boardgame.io/ui';
import { IGameArgs } from '../../App/Game/GameBoardWrapper';
import { GameLayout } from '../../App/Game/GameLayout';
import { GameMode } from '../../App/Game/GameModePicker';
import Bishop from './pieces/bishop';
import King from './pieces/king';
import Knight from './pieces/knight';
import Pawn from './pieces/pawn';
import Queen from './pieces/queen';
import Rook from './pieces/rook';
import AlertLayer from '../../App/Game/AlertLayer';
import ReactGA from 'react-ga';

const COL_NAMES = 'abcdefgh';
const SELECTED_COLOR = 'green';
const MOVABLE_COLOR = 'palegreen';

interface IBoardProps {
  G: any;
  ctx: any;
  moves: any;
  playerID: string;
  isActive: boolean;
  isConnected: boolean;
  gameArgs?: IGameArgs;
}

export class Board extends React.Component<IBoardProps, {}> {
  chess = Chess();
  state = {
    selected: '',
  };
  _click = this.click.bind(this);

  render() {
    if (this.props.G.pgn) {
      this.chess.load_pgn(this.props.G.pgn);
    }
    if (this.props.ctx.gameover) {
      return (
        <GameLayout gameOver={this._getGameOver()} />
      );
    }
    return (
      <GameLayout>
        <div>
          <h2 style={{ textAlign: 'center' }}>
            {this._getStatus()}
          </h2>
          <Checkerboard
            invert={this.getPlayer() === 'b'}
            highlightedSquares={this._getHighlightedSquares()}
            onClick={this._click}
          >
            {this._getPieces()}
          </Checkerboard>
        </div>
      </GameLayout>
    );
  }

  getPlayer(): 'w' | 'b' {
    if (this.props.playerID === '1') {
      return 'b';
    } else {
      return 'w';
    }
  }

  click(coords: IAlgebraicCoords) {
    ReactGA.event({
      category: 'ChessGame',
      action: 'click',
    });
    const { square } = coords;
    if (!this.props.isActive) {
      return;
    }

    if (!this.state.selected && this._isSelectable(square)) {
      this.setState({
        ...this.state,
        selected: square,
      });
    }

    if (this.state.selected) {
      const moves = this._getMoves();
      const move = moves.find((m: any) => {
        return m.from === this.state.selected && m.to === square;
      });
      if (move) {
        this.props.moves.move(move.san);
      }
      this.setState({
        ...this.state,
        selected: '',
      });
    }
  }

  _getHighlightedSquares() {
    const result = {} as IColorMap;
    if (this.state.selected) {
      result[this.state.selected] = SELECTED_COLOR;
    }
    for (const move of this._getMoves()) {
      result[move.to] = MOVABLE_COLOR;
    }
    return result;
  }

  _getPieces() {
    const result = [];
    for (let y = 1; y <= 8; y++) {
      for (let x = 0; x < 8; x++) {
        const square = COL_NAMES[x] + y;
        const p = this.chess.get(square);
{'fraction_non_alphanumeric': 0.15651387213510254, 'fraction_numerical': 0.0033172496984318458, 'mean_word_length': 2.2934458788480634, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '7429052', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2191, 'n_words': 502}