<commit_before>var compact = require('lodash-node/modern/arrays/compact');
var utils = require('../utils');
var xhr = require('../xhr');
var settings = require('../settings');
var iScroll = require('iscroll');
var session = require('../session');
var i18n = require('../i18n');
var formWidgets = require('./widget/form');
var session = require('../session');
var moment = window.moment;
var backbutton = require('../backbutton');

// iScroll instance
var scroller = null;

var gamesMenu = {};

gamesMenu.isOpen = false;
var newGameCardSwapped = false;

var doOpen = function() {
  window.analytics.trackView('Games Menu');
  backbutton.stack.push(gamesMenu.close);
  gamesMenu.isOpen = true;
  if (utils.hasNetwork() && session.isConnected()) session.refresh();
};

gamesMenu.openNewGame = function() {
  doOpen();
  newGameCardSwapped = true;
};

gamesMenu.openNewGameCorrespondence = function() {
  settings.game.selected('human');
  settings.game.human.timeMode('2');
  gamesMenu.openNewGame();
};

gamesMenu.openCurrentGames = function() {
  doOpen();
  setTimeout(function() {
    if (scroller) scroller.goToPage(1, 0);
  }, 200);
};

gamesMenu.open = function() {
  if (session.nowPlaying().length)
    gamesMenu.openCurrentGames();
  else
    gamesMenu.openNewGame();
};

gamesMenu.close = function(fromBB) {
  if (fromBB !== 'backbutton' && gamesMenu.isOpen) backbutton.stack.pop();
  gamesMenu.isOpen = false;
};

gamesMenu.lastJoined = null;

gamesMenu.joinGame = function(g) {
  gamesMenu.lastJoined = g;
  gamesMenu.close();
  m.route('/play/' + g.fullId);
  m.redraw();
};

function startAIGame() {
  return xhr.newAiGame().then(function(data) {
    m.route('/play' + data.url.round);
  }, function(error) {
    utils.handleXhrError(error);
    throw error;
  });
}

function seekHumanGame() {
  if (settings.game.human.timeMode() === '1') m.route('/seek');
  else {
    xhr.seekGame();
    m.route('/seeks');
  }
}

function swapCard() {
  newGameCardSwapped = !newGameCardSwapped;
}

function tupleOf(x) {
  return [x, x];
}

function renderForm(formName, action, settingsObj, variants, timeModes) {
  var timeMode = settingsObj.timeMode();
  var hasClock = timeMode === '1';
  var hasDays = timeMode === '2';
  var allowWhite = !settingsObj.mode ||
    settingsObj.mode() === '0' || ['5', '6', '7'].indexOf(settingsObj.variant()) === -1 ||
    settings.game.selected() !== 'human';
  var generalFieldset = [
    m('div.select_input', [
      formWidgets.renderSelect('variant', formName + 'variant', variants, settingsObj.variant),
    ])
  ];

  if (settingsObj.color) {
    var colors = compact([
      ['randomColor', 'random'],
      allowWhite ? ['white', 'white'] : null, ['black', 'black']
    ]);
    generalFieldset.unshift(
      m('div.select_input', [
        formWidgets.renderSelect('side', formName + 'color', colors, settingsObj.color),
      ])
    );
  }
  if (settingsObj.level) {
    generalFieldset.push(m('div.select_input', [
      formWidgets.renderSelect('level', 'ailevel', [
        '1', '2', '3', '4', '5', '6', '7', '8'
      ].map(tupleOf), settingsObj.level)
    ]));
  }
  if (settingsObj.mode) {
    var modes = (session.isConnected() && timeMode !== '0') ? [
      ['casual', '0'],
      ['rated', '1']
    ] : [
      ['casual', '0']
    ];
    generalFieldset.push(m('div.select_input', [
      formWidgets.renderSelect('mode', formName + 'mode', modes, settingsObj.mode)
    ]));
    if (settingsObj.mode() === '1') {
      generalFieldset.push(
        m('div.rating_range', [
          m('div.title', i18n('ratingRange')),
          m('div.select_input.inline', [
            formWidgets.renderSelect('Min', formName + 'rating_min',
              settings.game.human.availableRatingRanges.min, settingsObj.ratingMin, false)
          ]),
          m('div.select_input.inline', [
            formWidgets.renderSelect('Max', formName + 'rating_max',
              settings.game.human.availableRatingRanges.max, settingsObj.ratingMax, false)
          ])
        ])
      );
    }
  }

  var timeFieldset = [
    m('div.select_input', [
      formWidgets.renderSelect('clock', formName + 'timeMode', timeModes, settingsObj.timeMode)
    ])
  ];
  if (hasClock) {
    timeFieldset.push(
      m('div.select_input.inline', [
        formWidgets.renderSelect('time', formName + 'time',
          settings.game.availableTimes.map(tupleOf), settingsObj.time, false)
      ]),
      m('div.select_input.inline', [
        formWidgets.renderSelect('increment', formName + 'increment',
          settings.game.availableIncrements.map(tupleOf), settingsObj.increment, false)
      ])
    );
  }
  if (hasDays)
    timeFieldset.push(
      m('div.select_input.large_label', [
        formWidgets.renderSelect('daysPerTurn', formName + 'days',
          settings.game.availableDays.map(tupleOf), settingsObj.days, false)
      ]));

  return m('form#new_game_form.form', {
    onsubmit: function(e) {
      e.preventDefault();
      if (!settingsObj.isValid(settingsObj)) return;
      gamesMenu.close();
      swapCard();
      action();
    }
  }, [
    m('fieldset', [
      m('div.nice-radio', formWidgets.renderRadio('human', 'selected', 'human', settings.game.selected)),<commit_msg>fix horrendous bug when creating an online AI game<commit_after>var compact = require('lodash-node/modern/arrays/compact');
var utils = require('../utils');
var xhr = require('../xhr');
var settings = require('../settings');
var iScroll = require('iscroll');
var session = require('../session');
var i18n = require('../i18n');
var formWidgets = require('./widget/form');
var session = require('../session');
var moment = window.moment;
var backbutton = require('../backbutton');

// iScroll instance
var scroller = null;

var gamesMenu = {};

gamesMenu.isOpen = false;
var newGameCardSwapped = false;

var doOpen = function() {
  window.analytics.trackView('Games Menu');
  backbutton.stack.push(gamesMenu.close);
  gamesMenu.isOpen = true;
  if (utils.hasNetwork() && session.isConnected()) session.refresh();
};

gamesMenu.openNewGame = function() {
  doOpen();
  newGameCardSwapped = true;
};

gamesMenu.openNewGameCorrespondence = function() {
  settings.game.selected('human');
  settings.game.human.timeMode('2');
  gamesMenu.openNewGame();
};

gamesMenu.openCurrentGames = function() {
  doOpen();
  setTimeout(function() {
    if (scroller) scroller.goToPage(1, 0);
  }, 200);
};

gamesMenu.open = function() {
  if (session.nowPlaying().length)
    gamesMenu.openCurrentGames();
  else
    gamesMenu.openNewGame();
};

gamesMenu.close = function(fromBB) {
  if (fromBB !== 'backbutton' && gamesMenu.isOpen) backbutton.stack.pop();
  gamesMenu.isOpen = false;
};

gamesMenu.lastJoined = null;

gamesMenu.joinGame = function(g) {
  gamesMenu.lastJoined = g;
  gamesMenu.close();
  m.route('/play/' + g.fullId);
  m.redraw();
};

function startAIGame() {
  return xhr.newAiGame().then(function(data) {
    m.route('/play' + data.url.round);
  }, function(error) {
    utils.handleXhrError(error);
    throw error;
  });
}

function seekHumanGame() {
  if (settings.game.human.timeMode() === '1') m.route('/seek');
  else {
    xhr.seekGame();
    m.route('/seeks');
  }
}

function swapCard() {
  newGameCardSwapped = !newGameCardSwapped;
}

function tupleOf(x) {
  return [x, x];
}

function renderForm(formName, action, settingsObj, variants, timeModes) {
  var timeMode = settingsObj.timeMode();
  var hasClock = timeMode === '1';
  var hasDays = timeMode === '2';
  var allowWhite = !settingsObj.mode ||
    settingsObj.mode() === '0' || ['5', '6', '7'].indexOf(settingsObj.variant()) === -1 ||
    settings.game.selected() !== 'human';
  var generalFieldset = [
    m('div.select_input', [
      formWidgets.renderSelect('variant', formName + 'variant', variants, settingsObj.variant),
    ])
  ];

  if (settingsObj.color) {
    var colors = compact([
      ['randomColor', 'random'],
      allowWhite ? ['white', 'white'] : null, ['black', 'black']
    ]);
    generalFieldset.unshift(
      m('div.select_input', [
        formWidgets.renderSelect('side', formName + 'color', colors, settingsObj.color),
      ])
    );
  }
  if (settingsObj.level) {
    generalFieldset.push(m('div.select_input', [
      formWidgets.renderSelect('level', 'ailevel', [
        '1', '2', '3', '4', '5', '6', '7', '8'
      ].map(tupleOf), settingsObj.level)
    ]));
  }
  if (settingsObj.mode) {
    var modes = (session.isConnected() && timeMode !== '0') ? [
      ['casual', '0'],
      ['rated', '1']
    ] : [
      ['casual', '0']
    ];
    generalFieldset.push(m('div.select_input', [
      formWidgets.renderSelect('mode', formName + 'mode', modes, settingsObj.mode)
    ]));
    if (settingsObj.mode() === '1') {
      generalFieldset.push(
        m('div.rating_range', [
          m('div.title', i18n('ratingRange')),
          m('div.select_input.inline', [
            formWidgets.renderSelect('Min', formName + 'rating_min',
              settings.game.human.availableRatingRanges.min, settingsObj.ratingMin, false)
          ]),
          m('div.select_input.inline', [
            formWidgets.renderSelect('Max', formName + 'rating_max',
              settings.game.human.availableRatingRanges.max, settingsObj.ratingMax, false)
          ])
        ])
      );
    }
  }

  var timeFieldset = [
    m('div.select_input', [
      formWidgets.renderSelect('clock', formName + 'timeMode', timeModes, settingsObj.timeMode)
    ])
  ];
  if (hasClock) {
    timeFieldset.push(
      m('div.select_input.inline', [
        formWidgets.renderSelect('time', formName + 'time',
          settings.game.availableTimes.map(tupleOf), settingsObj.time, false)
      ]),
      m('div.select_input.inline', [
        formWidgets.renderSelect('increment', formName + 'increment',
          settings.game.availableIncrements.map(tupleOf), settingsObj.increment, false)
      ])
    );
  }
  if (hasDays)
    timeFieldset.push(
      m('div.select_input.large_label', [
        formWidgets.renderSelect('daysPerTurn', formName + 'days',
          settings.game.availableDays.map(tupleOf), settingsObj.days, false)
      ]));

  return m('form#new_game_form.form', {
    onsubmit: function(e) {
      e.preventDefault();
      if (settingsObj.isValid && !settingsObj.isValid(settingsObj)) return;
      gamesMenu.close();
      swapCard();
      action();
    }
  }, [
    m('fieldset', [
      m('div.nice-radio', formWidgets.renderRadio('human', 'selected', 'human', settings.game.selected)),
{'fraction_non_alphanumeric': 0.17633079847908745, 'fraction_numerical': 0.006273764258555133, 'mean_word_length': 3.3655601659751038, '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': '902318', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3425, 'n_words': 594}