<commit_before>$(function() {

    var App = function($el) {
        this.gameID;
        this.cacheDOM();
        this.init();
        this.load();
    };

    App.fn = App.prototype;

    App.fn.cacheDOM = function() {
      this.$gameStatus = $(".game-status");
      this.$gameTurn = $(".game-turn");
      this.$piece = $("#board-status .piece");
    };

    App.fn.init = function() {
        var self = this;
        var urlFields;

        console.log("Init");

        this.regex = {
            validPlay: /play\/([a-zA-Z0-9\-_]+)\//g,
            playerColor: /play\/[a-zA-Z0-9\-_]+\/(white|black)\/?/g
        };

        this.$app = $("#chessboard-app");
        this.$status = this.$app.find("#board-status");

        urlFields = this.parseUrl();
        this.gameID = urlFields.gameID;
        this.playerColor = urlFields.playerColor || 'white';

        console.log('connecting...');
        this.io = io.connect();

        this.Events().onNewGame();

        // TODO: move to load()
        this.io.on('newPosition', function (data) {
          console.log('socket: new position', data.fen, data.gameID);

          self.board.position(data.fen);
          self.game.load(data.fen);
          self.renderToGameLog(data);
          self.renderGameStatus();
        });
    };

    App.fn.load = function() {
        var self = this;

        if (this.gameID) {
            this.getGameData(this.gameID, (function(data) {
              var gameHistory = data;
              var keys = Object.keys(gameHistory);
              keys.sort();
              var lastKey = keys.slice(-1)[0];
              var position = gameHistory[lastKey].position;

              console.log('loading data...', data);
               console.log(this.gameID + " position is " + position);
                this.gameConfig = {
                    pieceTheme: '/libs/chessboardjs/www/img/chesspieces/wikipedia-classic/{piece}.png',
                    draggable: true,
                    orientation: this.playerColor,
                    onDragStart: this.Events().onDragStart,
                    onDrop: this.Events().onDrop,
                    onSnapEnd: this.Events().onSnapEnd,
                    position: position
                };

                this.game = new Chess();
                this.game.load(position);
                this.board = new ChessBoard('board', this.gameConfig);
                this.renderGameLog(gameHistory);
                this.renderGameStatus();

                window.board = this.board;
            }).bind(this));
        }

        this.renderShareGameUrl();
        this.handleJoinGame();
        this.handleShareGame();
        this.isPlayerConnected();

        this.io.emit('gameStart', {
          gameID: this.gameID,
          playerColor: this.playerColor
        });
    };

    App.fn.newGame = function() {
      var self = this;
      $.getJSON('/play/new')
       .done(function(data) {
          this.gameID = data.gameID;
       })
    };

    App.fn.getGameData = function(gameId, callback) {
        $.ajax({
            url: "/game/" + gameId,
            method: "get",
            dataType: "json"
        })
        .done(function(data) {
            callback(data);
        })
        .error(function() {
            console.log("Error loading game data.");
        });
    };

    App.fn.renderToGameLog = function(log) {
      var from = log.source;
      var to = log.target;
      var who = (log.piece[0] === this.playerColor[0]) ? 'You' : 'Opponent';
      var $li = $("<li class='log'><span class='left'>" + who + "</span> <span class='right'>" + from + " → " + to + "</span></li>");

      $('.game-log ul').prepend($li);
    };

    App.fn.renderGameStatus = function() {
      var who = 'You';
      var status = {};

      // who's turn?
      if (this.game.turn() === 'b') {
        if (this.playerColor === 'white') {
          who = 'Opponent';
        }
      }
      else if (this.game.turn() === 'w') {
        if (this.playerColor === 'black') {
          who = 'Opponent';
        }
      }

      // change piece color in game status
      if (this.game.turn() === 'w') {
        this.$piece.filter('.white-piece').removeClass('hide');
        this.$piece.filter('.black-piece').addClass('hide');
      }
      else if (this.game.turn() === 'b') {
        this.$piece.filter('.white-piece').addClass('hide');
        this.$piece.filter('.black-piece').removeClass('hide');
      }

      // in-check / in-checkmate / in-stalemate / in-draw ?
      if (this.game.in_check()) {
        if (this.game.in_checkmate()) {
          if (who === 'Opponent') {
            this.$gameTurn.text('You won! (Checkmate)');
          }
          else {
            this.$gameTurn.text('Opponent won! (Checkmate)');
          }

          status.in_checkmate = true;
        }
        else {
          if (who === 'Opponent') {
            this.$gameTurn.text("Opponent is in Check. Opponent's turn.");
          }
          else {
            this.$gameTurn.text("You are in Check. Your turn.");
          }
        }
      }
      else if (this.game.in_draw()) {
        if (this.game.in_stalemate()) {
          this.$gameTurn.text('Stalemate. Draw.');
          status.in_stalemate = true;
          status.in_draw = true;
        }
        else {
          this.$gameTurn.text('Draw.');
          status.in_draw = true;
        }
      }
      else {
        if (who === 'You') {<commit_msg>removed piece in game status on checkmate and draw<commit_after>$(function() {

    var App = function($el) {
        this.gameID;
        this.cacheDOM();
        this.init();
        this.load();
    };

    App.fn = App.prototype;

    App.fn.cacheDOM = function() {
      this.$gameStatus = $(".game-status");
      this.$gameTurn = $(".game-turn");
      this.$piece = $("#board-status .piece");
    };

    App.fn.init = function() {
        var self = this;
        var urlFields;

        console.log("Init");

        this.regex = {
            validPlay: /play\/([a-zA-Z0-9\-_]+)\//g,
            playerColor: /play\/[a-zA-Z0-9\-_]+\/(white|black)\/?/g
        };

        this.$app = $("#chessboard-app");
        this.$status = this.$app.find("#board-status");

        urlFields = this.parseUrl();
        this.gameID = urlFields.gameID;
        this.playerColor = urlFields.playerColor || 'white';

        console.log('connecting...');
        this.io = io.connect();

        this.Events().onNewGame();

        // TODO: move to load()
        this.io.on('newPosition', function (data) {
          console.log('socket: new position', data.fen, data.gameID);

          self.board.position(data.fen);
          self.game.load(data.fen);
          self.renderToGameLog(data);
          self.renderGameStatus();
        });
    };

    App.fn.load = function() {
        var self = this;

        if (this.gameID) {
            this.getGameData(this.gameID, (function(data) {
              var gameHistory = data;
              var keys = Object.keys(gameHistory);
              keys.sort();
              var lastKey = keys.slice(-1)[0];
              var position = gameHistory[lastKey].position;

              console.log('loading data...', data);
               console.log(this.gameID + " position is " + position);
                this.gameConfig = {
                    pieceTheme: '/libs/chessboardjs/www/img/chesspieces/wikipedia-classic/{piece}.png',
                    draggable: true,
                    orientation: this.playerColor,
                    onDragStart: this.Events().onDragStart,
                    onDrop: this.Events().onDrop,
                    onSnapEnd: this.Events().onSnapEnd,
                    position: position
                };

                this.game = new Chess();
                this.game.load(position);
                this.board = new ChessBoard('board', this.gameConfig);
                this.renderGameLog(gameHistory);
                this.renderGameStatus();

                window.board = this.board;
            }).bind(this));
        }

        this.renderShareGameUrl();
        this.handleJoinGame();
        this.handleShareGame();
        this.isPlayerConnected();

        this.io.emit('gameStart', {
          gameID: this.gameID,
          playerColor: this.playerColor
        });
    };

    App.fn.newGame = function() {
      var self = this;
      $.getJSON('/play/new')
       .done(function(data) {
          this.gameID = data.gameID;
       })
    };

    App.fn.getGameData = function(gameId, callback) {
        $.ajax({
            url: "/game/" + gameId,
            method: "get",
            dataType: "json"
        })
        .done(function(data) {
            callback(data);
        })
        .error(function() {
            console.log("Error loading game data.");
        });
    };

    App.fn.renderToGameLog = function(log) {
      var from = log.source;
      var to = log.target;
      var who = (log.piece[0] === this.playerColor[0]) ? 'You' : 'Opponent';
      var $li = $("<li class='log'><span class='left'>" + who + "</span> <span class='right'>" + from + " → " + to + "</span></li>");

      $('.game-log ul').prepend($li);
    };

    App.fn.renderGameStatus = function() {
      var who = 'You';
      var status = {};

      // who's turn?
      if (this.game.turn() === 'b') {
        if (this.playerColor === 'white') {
          who = 'Opponent';
        }
      }
      else if (this.game.turn() === 'w') {
        if (this.playerColor === 'black') {
          who = 'Opponent';
        }
      }

      // change piece color in game status
      if (this.game.turn() === 'w') {
        this.$piece.filter('.white-piece').removeClass('hide');
        this.$piece.filter('.black-piece').addClass('hide');
      }
      else if (this.game.turn() === 'b') {
        this.$piece.filter('.white-piece').addClass('hide');
        this.$piece.filter('.black-piece').removeClass('hide');
      }

      // in-check / in-checkmate / in-stalemate / in-draw ?
      if (this.game.in_check()) {
        if (this.game.in_checkmate()) {
          if (who === 'Opponent') {
            this.$gameTurn.text('You won! (Checkmate)');
          }
          else {
            this.$gameTurn.text('Opponent won! (Checkmate)');
          }
          this.$piece.filter('.white-piece').addClass('hide');
          this.$piece.filter('.black-piece').addClass('hide');

          status.in_checkmate = true;
        }
        else {
          if (who === 'Opponent') {
            this.$gameTurn.text("Opponent is in Check. Opponent's turn.");
          }
          else {
            this.$gameTurn.text("You are in Check. Your turn.");
          }
        }
      }
      else if (this.game.in_draw()) {
        if (this.game.in_stalemate()) {
          this.$gameTurn.text('Stalemate. Draw.');
          status.in_stalemate = true;
          status.in_draw = true;
        }
        else {
          this.$gameTurn.text('Draw.');
          status.in_draw = true;
        }
        this.$piece.filter('.white-piece').addClass('hide');
        this.$piece.filter('.black-piece').addClass('hide');
      }
      else {
        if (who === 'You') {
{'fraction_non_alphanumeric': 0.1676924449201677, 'fraction_numerical': 0.0014271697440014271, 'mean_word_length': 1.605623983267488, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1409839', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3284, 'n_words': 580}