<commit_before>(function() {

var checkVersion = Dagaz.Model.checkVersion;

Dagaz.Model.checkVersion = function(design, name, value) {
  if (name != "hiashatar-invariant") {
      checkVersion(design, name, value);
  }
}

Dagaz.Model.findPiece = function(design, board, player, type) {
  var positions = design.allPositions();
  for (var i = 0; i < positions.length; i++) {
       var piece = board.getPiece(positions[i]);
       if ((piece !== null) && (piece.type == type) && (piece.player == player)) {
           return positions[i];
       }
  }
  return null;
}

var checkStop = function(design, board, player, pos, hia) {
  for (var dir = 0; dir < design.dirs.length; dir++) {
       var p = design.navigate(player, pos, dir);
       if (p !== null) {
           var piece = board.getPiece(p);
           if ((piece !== null) && (piece.player == player) && (piece.type == hia)) return true;
       }
  }
  return false;
}

var checkDirection = function(design, board, player, pos, dir, leapers, riders, hia) {
  var p = design.navigate(player, pos, dir);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece !== null) {
      if (piece.player == player) return false;
      return (_.indexOf(leapers, piece.type) >= 0) || (_.indexOf(riders, piece.type) >= 0);
  }
  while (piece === null) {
      if (checkStop(design, board, player, p, hia)) return false;
      p = design.navigate(player, p, dir);
      if (p === null) return false;
      piece = board.getPiece(p);
  }
  if (piece.player == player) return false;
  return _.indexOf(riders, piece.type) >= 0;
}

var checkLeap = function(design, board, player, pos, o, d, knight) {
  var p = design.navigate(player, pos, o);
  if (p === null) return false;
  p = design.navigate(player, p, d);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece === null) return false;
  return (piece.player != player) && (piece.type == knight);
}

var checkStep = function(design, board, player, pos, d, hia) {
  var p = design.navigate(player, pos, d);
  if (p === null) return false;
  if (board.getPiece(p) !== null) return false;
  if (checkStop(design, board, player, p, hia)) return false;
  p = design.navigate(player, p, d);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece === null) return false;
  return (piece.player != player) && (piece.type == hia);
}

Dagaz.Model.checkPositions = function(design, board, player, positions) {
  var king   = design.getPieceType("King");
  var pawn   = design.getPieceType("Pawn");
  var rook   = design.getPieceType("Rook");
  var knight = design.getPieceType("Knight");
  var bishop = design.getPieceType("Bishop");
  var queen  = design.getPieceType("Queen");
  var hia    = design.getPieceType("Hia");
  var n  = design.getDirection("n");  var w  = design.getDirection("w");
  var s  = design.getDirection("s");  var e  = design.getDirection("e");
  var nw = design.getDirection("nw"); var sw = design.getDirection("sw");
  var ne = design.getDirection("ne"); var se = design.getDirection("se");
  for (var i = 0; i < positions.length; i++) {
       var pos = positions[i];
       if (checkDirection(design, board, player, pos, n,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, s,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, w,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, e,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, nw, [king, pawn, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, ne, [king, pawn, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, sw, [king, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, se, [king, hia], [bishop, queen], hia)) return true;
       if (checkLeap(design, board, player, pos, n, nw, knight)) return true;
       if (checkLeap(design, board, player, pos, n, ne, knight)) return true;
       if (checkLeap(design, board, player, pos, s, sw, knight)) return true;
       if (checkLeap(design, board, player, pos, s, se, knight)) return true;
       if (checkLeap(design, board, player, pos, w, nw, knight)) return true;
       if (checkLeap(design, board, player, pos, w, sw, knight)) return true;
       if (checkLeap(design, board, player, pos, e, ne, knight)) return true;
       if (checkLeap(design, board, player, pos, e, se, knight)) return true;
       if (checkStep(design, board, player, pos, w, hia)) return true;
       if (checkStep(design, board, player, pos, e, hia)) return true;
       if (checkStep(design, board, player, pos, s, hia)) return true;
       if (checkStep(design, board, player, pos, n, hia)) return true;
       if (checkStep(design, board, player, pos, nw, hia)) return true;
       if (checkStep(design, board, player, pos, se, hia)) return true;
       if (checkStep(design, board, player, pos, sw, hia)) return true;
       if (checkStep(design, board, player, pos, ne, hia)) return true;
  }
  return false;
}

var CheckInvariants = Dagaz.Model.CheckInvariants;

Dagaz.Model.CheckInvariants = function(board) {
  var design = Dagaz.Model.design;
  var king   = design.getPieceType("King");
  _.each(board.moves, function(move) {
      var b = board.apply(move);
      var pos  = Dagaz.Model.findPiece(design, b, board.player, king);
      if (Dagaz.Model.checkPositions(design, b, board.player, [pos])) {
          move.failed = true;
          return;
      }
  });
  CheckInvariants(board);
}

})();<commit_msg>Hiashatar (fix)<commit_after>(function() {

var checkVersion = Dagaz.Model.checkVersion;

Dagaz.Model.checkVersion = function(design, name, value) {
  if (name != "hiashatar-invariant") {
      checkVersion(design, name, value);
  }
}

Dagaz.Model.findPiece = function(design, board, player, type) {
  var positions = design.allPositions();
  for (var i = 0; i < positions.length; i++) {
       var piece = board.getPiece(positions[i]);
       if ((piece !== null) && (piece.type == type) && (piece.player == player)) {
           return positions[i];
       }
  }
  return null;
}

var checkStop = function(design, board, player, pos, hia) {
  for (var dir = 0; dir < design.dirs.length; dir++) {
       var p = design.navigate(player, pos, dir);
       if (p !== null) {
           var piece = board.getPiece(p);
           if ((piece !== null) && (piece.player == player) && (piece.type == hia)) return true;
       }
  }
  return false;
}

var checkDirection = function(design, board, player, pos, dir, leapers, riders, hia) {
  var p = design.navigate(player, pos, dir);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece !== null) {
      if (piece.player == player) return false;
      return (_.indexOf(leapers, piece.type) >= 0) || (_.indexOf(riders, piece.type) >= 0);
  }
  while (piece === null) {
      if (checkStop(design, board, player, p, hia)) return false;
      p = design.navigate(player, p, dir);
      if (p === null) return false;
      piece = board.getPiece(p);
  }
  if (piece.player == player) return false;
  return _.indexOf(riders, piece.type) >= 0;
}

var checkLeap = function(design, board, player, pos, o, d, knight) {
  var p = design.navigate(player, pos, o);
  if (p === null) return false;
  p = design.navigate(player, p, d);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece === null) return false;
  return (piece.player != player) && (piece.type == knight);
}

var checkStep = function(design, board, player, pos, d, hia) {
  var p = design.navigate(player, pos, d);
  if (p === null) return false;
  if (board.getPiece(p) !== null) return false;
  if (checkStop(design, board, player, p, hia)) return false;
  p = design.navigate(player, p, d);
  if (p === null) return false;
  var piece = board.getPiece(p);
  if (piece === null) return false;
  return (piece.player != player) && (piece.type == hia);
}

Dagaz.Model.checkPositions = function(design, board, player, positions) {
  var king   = design.getPieceType("King");
  var pawn   = design.getPieceType("Pawn");
  var rook   = design.getPieceType("Rook");
  var knight = design.getPieceType("Knight");
  var bishop = design.getPieceType("Bishop");
  var queen  = design.getPieceType("Queen");
  var hia    = design.getPieceType("Hia");
  var n  = design.getDirection("n");  var w  = design.getDirection("w");
  var s  = design.getDirection("s");  var e  = design.getDirection("e");
  var nw = design.getDirection("nw"); var sw = design.getDirection("sw");
  var ne = design.getDirection("ne"); var se = design.getDirection("se");
  for (var i = 0; i < positions.length; i++) {
       var pos = positions[i];
       if (checkDirection(design, board, player, pos, n,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, s,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, w,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, e,  [king, hia], [rook, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, nw, [king, pawn, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, ne, [king, pawn, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, sw, [king, hia], [bishop, queen], hia)) return true;
       if (checkDirection(design, board, player, pos, se, [king, hia], [bishop, queen], hia)) return true;
       if (checkLeap(design, board, player, pos, n, nw, knight)) return true;
       if (checkLeap(design, board, player, pos, n, ne, knight)) return true;
       if (checkLeap(design, board, player, pos, s, sw, knight)) return true;
       if (checkLeap(design, board, player, pos, s, se, knight)) return true;
       if (checkLeap(design, board, player, pos, w, nw, knight)) return true;
       if (checkLeap(design, board, player, pos, w, sw, knight)) return true;
       if (checkLeap(design, board, player, pos, e, ne, knight)) return true;
       if (checkLeap(design, board, player, pos, e, se, knight)) return true;
       if (checkStep(design, board, player, pos, w, hia)) return true;
       if (checkStep(design, board, player, pos, e, hia)) return true;
       if (checkStep(design, board, player, pos, s, hia)) return true;
       if (checkStep(design, board, player, pos, n, hia)) return true;
       if (checkStep(design, board, player, pos, nw, hia)) return true;
       if (checkStep(design, board, player, pos, se, hia)) return true;
       if (checkStep(design, board, player, pos, sw, hia)) return true;
       if (checkStep(design, board, player, pos, ne, hia)) return true;
  }
  return false;
}

var CheckInvariants = Dagaz.Model.CheckInvariants;

Dagaz.Model.CheckInvariants = function(board) {
  var design = Dagaz.Model.design;
  var king   = design.getPieceType("King");
  _.each(board.moves, function(move) {
      var b = board.apply(move);
      var pos  = Dagaz.Model.findPiece(design, b, board.player, king);
      if ((pos !== null) && (Dagaz.Model.checkPositions(design, b, board.player, [pos]))) {
          move.failed = true;
          return;
      }
  });
  CheckInvariants(board);
}

})();
{'fraction_non_alphanumeric': 0.16231050705697858, 'fraction_numerical': 0.0010454783063251437, 'mean_word_length': 3.51573564122738, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4730178', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3858, 'n_words': 1189}