<commit_before>import promotion from '../shared/offlineRound/promotion';
import ground from '../shared/offlineRound/ground';
import makeData from '../shared/offlineRound/data';
import { setResult } from '../shared/offlineRound';
import sound from '../../sound';
import replayCtrl from '../shared/offlineRound/replayCtrl';
import storage from '../../storage';
import settings from '../../settings';
import actions from './actions';
import engine from './engine';
import helper from '../helper';
import { getRandomArbitrary } from '../../utils';
import { setCurrentAIGame, getCurrentAIGame } from '../../utils/offlineGames';
import m from 'mithril';

export const storageFenKey = 'ai.setupFen';

export default function controller() {

  helper.analyticsTrackView('Offline AI');

  const save = function() {
    setCurrentAIGame({
      data: this.data,
      situations: this.replay.situations,
      ply: this.replay.ply
    });
  }.bind(this);

  const addMove = function(orig, dest, promotionRole) {
    this.replay.addMove(orig, dest, promotionRole);
    engine.addMove(orig, dest, promotionRole);
    this.data.game.fen = engine.getFen();
  }.bind(this);

  this.getOpponent = function() {
    const level = settings.ai.opponent();
    return {
      name: settings.ai.availableOpponents.filter(function(o) {
        return o[1] === level;
      })[0][0],
      level: parseInt(level) || 1
    };
  };

  const engineMove = function() {
    if (this.chessground.data.turnColor !== this.data.player.color) setTimeout(function() {
      engine.setLevel(this.getOpponent().level);
      engine.search(function(move) {
        this.chessground.apiMove(move[0], move[1]);
        addMove(move[0], move[1], move[2]);
      }.bind(this));
    }.bind(this), 500);
  }.bind(this);

  const onPromotion = function(orig, dest, role) {
    addMove(orig, dest, role);
  };

  const userMove = function(orig, dest) {
    if (!promotion.start(this, orig, dest, onPromotion)) {
      addMove(orig, dest);
    }
  }.bind(this);

  const onMove = function(orig, dest, capturedPiece) {
    if (!capturedPiece)
      sound.move();
    else
      sound.capture();
  };

  this.onReplayAdded = function() {
    save();
    m.redraw();
    if (this.replay.situation().finished) {
      setResult(this);
      this.chessground.cancelMove();
      this.chessground.stop();
      setTimeout(function() {
        this.actions.open();
        m.redraw();
      }.bind(this), 1000);
    } else engineMove();
  }.bind(this);

  this.actions = new actions.controller(this);

  this.init = function(data, situations, ply) {
    this.data = data;
    if (!this.chessground)
      this.chessground = ground.make(this.data, this.data.game.fen, userMove, onMove);
    else ground.reload(this.chessground, this.data, this.data.game.fen);
    if (!this.replay) this.replay = new replayCtrl(this, situations, ply);
    else this.replay.init(situations, ply);
    this.replay.apply();
    if (this.actions) this.actions.close();
    engine.init(this.data.game.fen);
    engineMove();
  }.bind(this);

  this.startNewGame = function() {
    const opts = {
      color: getColorFromSettings()
    };
    this.init(makeData(opts));
  }.bind(this);

  this.jump = function(ply) {
    this.chessground.cancelMove();<commit_msg>Fix ai game result data<commit_after>import promotion from '../shared/offlineRound/promotion';
import ground from '../shared/offlineRound/ground';
import makeData from '../shared/offlineRound/data';
import { setResult } from '../shared/offlineRound';
import sound from '../../sound';
import replayCtrl from '../shared/offlineRound/replayCtrl';
import storage from '../../storage';
import settings from '../../settings';
import actions from './actions';
import engine from './engine';
import helper from '../helper';
import { getRandomArbitrary } from '../../utils';
import { setCurrentAIGame, getCurrentAIGame } from '../../utils/offlineGames';
import m from 'mithril';

export const storageFenKey = 'ai.setupFen';

export default function controller() {

  helper.analyticsTrackView('Offline AI');

  const save = function() {
    setCurrentAIGame({
      data: this.data,
      situations: this.replay.situations,
      ply: this.replay.ply
    });
  }.bind(this);

  const addMove = function(orig, dest, promotionRole) {
    this.replay.addMove(orig, dest, promotionRole);
    engine.addMove(orig, dest, promotionRole);
    this.data.game.fen = engine.getFen();
  }.bind(this);

  this.getOpponent = function() {
    const level = settings.ai.opponent();
    return {
      name: settings.ai.availableOpponents.filter(function(o) {
        return o[1] === level;
      })[0][0],
      level: parseInt(level) || 1
    };
  };

  const engineMove = function() {
    if (this.chessground.data.turnColor !== this.data.player.color) setTimeout(function() {
      engine.setLevel(this.getOpponent().level);
      engine.search(function(move) {
        this.chessground.apiMove(move[0], move[1]);
        addMove(move[0], move[1], move[2]);
      }.bind(this));
    }.bind(this), 500);
  }.bind(this);

  const onPromotion = function(orig, dest, role) {
    addMove(orig, dest, role);
  };

  const userMove = function(orig, dest) {
    if (!promotion.start(this, orig, dest, onPromotion)) {
      addMove(orig, dest);
    }
  }.bind(this);

  const onMove = function(orig, dest, capturedPiece) {
    if (!capturedPiece)
      sound.move();
    else
      sound.capture();
  };

  this.onReplayAdded = function() {
    save();
    m.redraw();
    if (this.replay.situation().finished) {
      setResult(this);
      this.chessground.cancelMove();
      this.chessground.stop();
      save();
      setTimeout(function() {
        this.actions.open();
        m.redraw();
      }.bind(this), 1000);
    } else engineMove();
  }.bind(this);

  this.actions = new actions.controller(this);

  this.init = function(data, situations, ply) {
    this.data = data;
    if (!this.chessground)
      this.chessground = ground.make(this.data, this.data.game.fen, userMove, onMove);
    else ground.reload(this.chessground, this.data, this.data.game.fen);
    if (!this.replay) this.replay = new replayCtrl(this, situations, ply);
    else this.replay.init(situations, ply);
    this.replay.apply();
    if (this.actions) this.actions.close();
    engine.init(this.data.game.fen);
    engineMove();
  }.bind(this);

  this.startNewGame = function() {
    const opts = {
      color: getColorFromSettings()
    };
    this.init(makeData(opts));
  }.bind(this);

  this.jump = function(ply) {
    this.chessground.cancelMove();
{'fraction_non_alphanumeric': 0.1635839902971498, 'fraction_numerical': 0.004851425106124924, 'mean_word_length': 3.2397172236503855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3688521', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2076, 'n_words': 444}