<commit_before>var Q = require('q');
var _ = require('underscore');
var gameIdHelper = require('./gameIdHelper.js');
var blobServiceHelper = require('./blobServiceHelper.js');

module.exports = function(blobService) {
  return {
    self: this,
    blobService: blobService,
    getGame: function(gameId) {
      var d = Q.defer();

      try {
        blobService.getBlobToText('scores', gameId, function(err, results) {
          if (err) {
            throw err;
          }
          d.resolve(JSON.parse(results));
        });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    },
    updateGame: function(game) {
      var d = Q.defer();

      try {
        blobService.createBlockBlobFromText('scores', game.id, JSON.stringify(game), function(err, result, response) {
          if (err) {
            d.reject(err);
          } else {
            d.resolve(game);
          }
        });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    },
    startGame: function(game) {
      var d = Q.defer();

      try {
        var timeInMs = Date.now();

        game.id = gameIdHelper.createGameId(game.course, game.players, timeInMs);
        game.date = new Date(timeInMs).toDateString();
        game.scores = {};
        game.players.forEach(function(p) {
          game.scores[p.id] = [];
          game.scores[p.id].length = game.course.numberOfHoles;
        });

        blobServiceHelper.acquireLease(blobService, 'scores', 'gameIndex')
          .then(function(leaseId) {
            blobService.getBlobToText('scores', 'gameIndex', function(err, results) {
              try {
                var games = JSON.parse(results);
                var contains = _.contains(games, function(gId) {
                  return gId === game.id;
                });

                if (!contains) {
                  games.push(game.id);
                  blobService.createBlockBlobFromText('scores', 'gameIndex', JSON.stringify(games), function(err, result, response) {
                    if (err) {
                      throw err;
                    }
                  });
                }
              } catch(e) {
                throw e;
              } finally {
                blobService.releaseLease('scores', 'gameIndex', leaseId, function () {});
              }
            });

            blobService.createBlockBlobFromText('scores', game.id, JSON.stringify(game), function(err, result, response) {
              if (err) {
                d.reject(err);
              } else {
                d.resolve(game);
              }
            });
          });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    }
  };
};<commit_msg>When a new game is started, adding that game id to the list of games for the player. players table holds the list of players in 'index' and each 'id' entry holds an array of game ids<commit_after>var Q = require('q');
var _ = require('underscore');
var gameIdHelper = require('./gameIdHelper.js');
var blobServiceHelper = require('./blobServiceHelper.js');

module.exports = function(blobService) {
  var self = {
    self: this,
    blobService: blobService,
    getGame: function(gameId) {
      var d = Q.defer();

      try {
        blobService.getBlobToText('scores', gameId, function(err, results) {
          if (err) {
            throw err;
          }
          d.resolve(JSON.parse(results));
        });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    },
    updateGame: function(game) {
      var d = Q.defer();

      try {
        blobService.createBlockBlobFromText('scores', game.id, JSON.stringify(game), function(err, result, response) {
          if (err) {
            d.reject(err);
          } else {
            d.resolve(game);
          }
        });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    },

    addGameToPlayer: function(gameId, playerId) {
      try {
        blobServiceHelper.acquireLease(blobService, 'players', playerId)
          .then(function(leaseId) {
            blobService.getBlobToText('players', playerId, function(err, results) {
              try {
                var games = JSON.parse(results);
                var contains = _.contains(games, function(gId) {
                  return gId == gameId;
                });

                if (!contains) {
                  games.push(gameId);
                  blobService.createBlockBlobFromText('players', playerId, JSON.stringify(games), function(err, result, resp) {
                    if (err) {
                      throw err;
                    }
                  });
                }
              } catch(e) {
                throw e;
              } finally {
                blobService.releaseLease('players', playerId, leaseId, function () {});
              }
            });
          });
      } catch(e) {
        // Do nothing. Player just won't be able to see this game in their history
        console.log(e);
      }
    },

    startGame: function(game) {
      var d = Q.defer();

      try {
        var timeInMs = Date.now();

        game.id = gameIdHelper.createGameId(game.course, game.players, timeInMs);
        game.date = new Date(timeInMs).toDateString();
        game.scores = {};
        game.players.forEach(function(p) {
          game.scores[p.id] = [];
          game.scores[p.id].length = game.course.numberOfHoles;
        });

        blobServiceHelper.acquireLease(blobService, 'scores', 'gameIndex')
          .then(function(leaseId) {
            blobService.getBlobToText('scores', 'gameIndex', function(err, results) {
              try {
                var games = JSON.parse(results);
                var contains = _.contains(games, function(gId) {
                  return gId === game.id;
                });

                if (!contains) {
                  games.push(game.id);
                  blobService.createBlockBlobFromText('scores', 'gameIndex', JSON.stringify(games), function(err, result, response) {
                    if (err) {
                      throw err;
                    }
                  });
                }
              } catch(e) {
                throw e;
              } finally {
                blobService.releaseLease('scores', 'gameIndex', leaseId, function () {});
              }
            });

            game.players.forEach(function(p) {
              self.addGameToPlayer(game.id, p.id);
            });

            blobService.createBlockBlobFromText('scores', game.id, JSON.stringify(game), function(err, result, response) {
              if (err) {
                d.reject(err);
              } else {
                d.resolve(game);
              }
            });
          });
      } catch(e) {
        d.reject(e);
      }

      return d.promise;
    }
  };

  return self;
};
{'fraction_non_alphanumeric': 0.12862529002320186, 'fraction_numerical': 0.0, 'mean_word_length': 1.5043572984749456, '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': '7615233', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1882, 'n_words': 370}