<commit_before><link rel="import" href="lodash/sum.html">
<link rel="import" href="bridge-player-name.html">

<script>
((scope) => {

  function calcStats(score) {
    let combo = 0;
    let miss = 0;
    let maxCombo = 0;

    for (let s of score) {
      if (s > 0) {
        combo++;
      } else {
        if (maxCombo < combo) {
          maxCombo = combo;
        }
        combo = 0;
        miss++;
      };
    }

    return [combo, miss, maxCombo];
  }


  scope.Player = class PlayerModel {

    constructor(base) {

      // The following two properties are controlled by game service.
      this.bid = null;  // Bid for current round
      this.win = null;  // Win Trick for current round

      this._buffer = {
        lastRound: null,
        combo: null,
        miss: null,
        maxCombo: null
      }

      if (typeof base === 'undefined') {
        // Whole new player.
        this.score = [];
        this.name = Bridge.randomName();
      } else if (typeof base === 'string') {
        // Given a name. Construct a new Player with given name.
        this.score = [];
        this.name = base;
      } else {
        // Load from parsed JSON object
        this.score = base.score;
        this.name = base.name;
        this.bid = base.bid;
        this.win = base.win;
      }
    }

    get totalScore() {
      return lodash.sum(this.score);
    }

    get combo() {
      this.calcStats();
      return this._buffer.combo;
    }

    get miss() {
      this.calcStats();
      return this._buffer.miss;
    }

    get maxCombo() {
      this.calcStats();
      return this._buffer.maxCombo;
    }

    get comment() {
      this.calcStats();

      if (this.score.length < 3) {
        return '';
      }

      if (this.miss === 0) {
        return '(ﾉ◕ヮ◕)ﾉ*:･ﾟ✧ PERFECT ✧ﾟ･: *ヽ(◕ヮ◕ヽ)';
      }

      if (this.miss === 1) {
        return '(☞ﾟヮﾟ)☞ UCCU ☜(ﾟヮﾟ☜)';
      }

      if (this.miss === this.score.length) {
        return "Add oil (ง'̀-'́)ง";
      }

      if (this.totalScore <= 0) {
        return 'Negative score. ಠ_ಠ';
      }

      return '';
    }

    calcStats() {
      if (this._buffer.lastRound === this.score.length) {
        return;
      }
      this._buffer.lastRound = this.score.length;
      [this._buffer.combo, this._buffer.miss, this._buffer.maxCombo] = calcStats(this.score);
    }
  }
})(Bridge);
</script><commit_msg>FEAT: total score of player is now cached<commit_after><link rel="import" href="lodash/sum.html">
<link rel="import" href="bridge-player-name.html">

<script>
((scope) => {

  function calcStats(score) {
    let combo = 0;
    let miss = 0;
    let maxCombo = 0;

    for (let s of score) {
      if (s > 0) {
        combo++;
      } else {
        if (maxCombo < combo) {
          maxCombo = combo;
        }
        combo = 0;
        miss++;
      };
    }

    return [combo, miss, maxCombo, lodash.sum(score)];
  }


  scope.Player = class PlayerModel {

    constructor(base) {

      // The following two properties are controlled by game service.
      this.bid = null;  // Bid for current round
      this.win = null;  // Win Trick for current round

      this._buffer = {
        lastRound: null,
        combo: null,
        miss: null,
        maxCombo: null,
        sum: null
      }

      if (typeof base === 'undefined') {
        // Whole new player.
        this.score = [];
        this.name = Bridge.randomName();
      } else if (typeof base === 'string') {
        // Given a name. Construct a new Player with given name.
        this.score = [];
        this.name = base;
      } else {
        // Load from parsed JSON object
        this.score = base.score;
        this.name = base.name;
        this.bid = base.bid;
        this.win = base.win;
      }
    }

    get totalScore() {
      this.calcStats()
      return this._buffer.sum;
    }

    get combo() {
      this.calcStats();
      return this._buffer.combo;
    }

    get miss() {
      this.calcStats();
      return this._buffer.miss;
    }

    get maxCombo() {
      this.calcStats();
      return this._buffer.maxCombo;
    }

    get comment() {
      this.calcStats();

      if (this.score.length < 3) {
        return '';
      }

      if (this.miss === 0) {
        return '(ﾉ◕ヮ◕)ﾉ*:･ﾟ✧ PERFECT ✧ﾟ･: *ヽ(◕ヮ◕ヽ)';
      }

      if (this.miss === 1) {
        return '(☞ﾟヮﾟ)☞ UCCU ☜(ﾟヮﾟ☜)';
      }

      if (this.miss === this.score.length) {
        return "Add oil (ง'̀-'́)ง";
      }

      if (this.totalScore <= 0) {
        return 'Negative score. ಠ_ಠ';
      }

      return '';
    }

    calcStats() {
      if (this._buffer.lastRound === this.score.length) {
        return;
      }
      this._buffer.lastRound = this.score.length;
      [this._buffer.combo, this._buffer.miss, this._buffer.maxCombo, this._buffer.sum] = calcStats(this.score);
    }
  }
})(Bridge);
</script>
{'fraction_non_alphanumeric': 0.14783678490875538, 'fraction_numerical': 0.0036907935206069303, 'mean_word_length': 1.6965174129353233, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '7031382', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1665, 'n_words': 385}