<commit_before>var Game = function(canvas, imgs_bucket) {
  this.score = 0;
  this.is_active = true;
  this.imgs_bucket = imgs_bucket;

  // private attributes
  var drawAdapter     = new DrawAdapter(canvas);
  var field           = new Field(drawAdapter);
  var figureBuilder   = new FigureBuilder(drawAdapter);
  var next_figure     = figureBuilder.build_random_figure();
  var figure          = figureBuilder.build_random_figure();
  var self            = this;

  this.redraw = function() {
    drawAdapter.clear_canvas();

    field.drawField();
    next_figure.drawFigure(270, 100);
    figure.drawFigure(10, 10);

    drawAdapter.addScore(this.score);
  };

  this.animate = function() {
    if (this.is_active == false) { return false; };
    if (figure.pullDown(field) == false) { handleLanding(); };

    this.redraw();

    var new_speed = 1000 - this.score * 2;
    setTimeout(function() { self.animate(); }, new_speed);
    return true;
  };

  this.keydownListener = function(key) {
    if (this.is_active == false) { return false; };

    switch(key) {
      case 37: // left
        figure.pullLeft(0, field);
        break;
      case 38: // up
        figure.turn(0, field.x_cells, field);
        break;
      case 39: // right
        figure.pullRight(field.x_cells, field);
        break;
      case 40: // down
        if (figure.pullDown(field) == false) { handleLanding(); };
        break;
      default: return; // exit this handler for other keys
    };

    this.redraw();
  };

  // private methods
    var gameOver = function() {
      self.is_active = false;
      drawAdapter.draw_image(self.imgs_bucket['game_over']);
    };

    var addScorePerLine = function(lines_count) {
      self.score += lines_count * 10;
    };

    var addScorePerFigure = function() {
      self.score += 1;
    };

    var renew_figure = function() {
      figure = next_figure;
      next_figure = figureBuilder.build_random_figure();
    };

    var giveEncourage = function(removed_lines) {
      if (removed_lines == 2) { simpleEncourage(); };
      if (removed_lines >= 3) { greateEncourage(); };
    };

    // TODO: implement
    var simpleEncourage = function() { console.log('Good!'); };
    var greateEncourage = function() { console.log('Cool!'); };

    var handleRemovedLinesScore = function(removed_lines) {
      addScorePerLine(removed_lines);
      giveEncourage(removed_lines);
    };

    var handleLanding = function() {
      field.addToCover(figure.countShape());
      addScorePerFigure();

      var removed_lines = field.handleFullCoveredLines();
      handleRemovedLinesScore(removed_lines);
      renew_figure();

      if (field.isOverfilled()) { gameOver(); };
    };

  return this;
};<commit_msg>refactoring game class a bit more<commit_after>var Game = function(canvas, imgs_bucket) {
  this.score = 0;
  this.is_active = true;
  this.imgs_bucket = imgs_bucket;

  // private attributes
  var draw_adapter    = new DrawAdapter(canvas);
  var field           = new Field(draw_adapter);
  var figure_builder  = new FigureBuilder(draw_adapter);
  var next_figure     = figure_builder.build_random_figure();
  var figure          = figure_builder.build_random_figure();
  var self            = this;

  this.redraw = function() {
    draw_adapter.clear_canvas();

    field.drawField();
    next_figure.drawFigure(270, 100);
    figure.drawFigure(10, 10);

    draw_adapter.addScore(this.score);
  };

  this.animate = function() {
    if (this.is_active == false) { return false; };
    if (figure.pullDown(field) == false) { handleLanding(); };

    this.redraw();

    var new_speed = 1000 - this.score * 2;
    setTimeout(function() { self.animate(); }, new_speed);
    return true;
  };

  this.keydownListener = function(key) {
    if (this.is_active == false) { return false; };

    switch(key) {
      case 37: // left
        figure.pullLeft(0, field);
        break;
      case 38: // up
        figure.turn(0, field.x_cells, field);
        break;
      case 39: // right
        figure.pullRight(field.x_cells, field);
        break;
      case 40: // down
        if (figure.pullDown(field) == false) { handleLanding(); };
        break;
      default: return; // exit this handler for other keys
    };

    this.redraw();
  };

  // private methods
    var gameOver = function() {
      self.is_active = false;
      draw_adapter.draw_image(self.imgs_bucket['game_over']);
    };

    var addScorePerLine = function(lines_count) {
      self.score += lines_count * 10;
    };

    var addScorePerFigure = function() {
      self.score += 1;
    };

    var renewFigure = function() {
      figure = next_figure;
      next_figure = figure_builder.build_random_figure();
    };

    var giveEncourage = function(removed_lines) {
      if (removed_lines == 2) { simpleEncourage(); };
      if (removed_lines >= 3) { greateEncourage(); };
    };

    // TODO: implement
    var simpleEncourage = function() { console.log('Good!'); };
    // TODO: implement
    var greateEncourage = function() { console.log('Cool!'); };

    var handleRemovingLines = function() {
      var removed_lines = field.handleFullCoveredLines();

      addScorePerLine(removed_lines);
      giveEncourage(removed_lines);
    };

    var handleLanding = function() {
      field.addToCover(figure.countShape());
      addScorePerFigure();

      handleRemovingLines();
      renewFigure();

      if (field.isOverfilled()) { gameOver(); };
    };

  return this;
};
{'fraction_non_alphanumeric': 0.13232617400800872, 'fraction_numerical': 0.011285038223516564, 'mean_word_length': 2.702830188679245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7075006', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1663, 'n_words': 357}