<commit_before>var _   = require('lodash');
var fs  = require('fs');
var jsf = require('jsonfile');

var Pokemon = require('./pokemon');

/**
 * Object Definition
 */

var Inventory = function (username) {
  this.username = username;
  this.data = {
    candy:    [],
    pokemon:  []
  };
};

/**
 * Prototypes
 */

Inventory.prototype.load = function (candy, pokemon) {
  if (fs.existsSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json')) {
    this.data.candy   = jsf.readFileSync(__dirname + '/../data/save/' + this.username + '/inventory-candy.json');
    this.data.pokemon = _.map(jsf.readFileSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json'), function (p) {
      return new Pokemon(p);
    });
    this.initialize(candy, pokemon);
  } else {
    this.data.candy   = candy;
    this.data.pokemon = _.map(pokemon, function (p) {
      return new Pokemon(p);
    });
  }
};

Inventory.prototype.save = function () {
  var formatted = _.map(this.data.pokemon, function (p) {
    return p.data;
  });
  if (!fs.existsSync(__dirname + '/../data/save/' + this.username)) fs.mkdirSync(__dirname + '/../data/save/' + this.username);
  jsf.writeFileSync(__dirname + '/../data/save/' + this.username + '/inventory-candy.json', this.data.candy, {spaces: 2});
  jsf.writeFileSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json', formatted, {spaces: 2});
};

Inventory.prototype.initialize = function (candy, pokemon) {
  var self = this;

  this.data.candy = candy;

  var idMap = _.reduce(this.data.pokemon, function (result, p) {
    result[p.id] = false;
    return result;
  }, {});

  _.forEach(pokemon, function (data) {
    var p = _.find(self.data.pokemon, {data: {id: data.id}});
    if (p) p.update(data);
    else self.data.pokemon.push(new Pokemon(data));
    idMap[data.id] = true;
  });

  _.forEach(idMap, function (exists, id) {
    if (!exists) _.remove(self.data.pokemon, {data: {id: data.id}});
  });

};

Inventory.prototype.update = function (candy, pokemon) {
  var self = this;

  _.forEach(candy, function (c) {
    var data = _.find(self.data.candy, {family_id: c.family_id});
    if (data !== undefined) data.candy = c.candy;
    else self.data.candy.push(c);
  });

  _.forEach(pokemon, function (data) {<commit_msg>Fixed inventory initialization error<commit_after>var _   = require('lodash');
var fs  = require('fs');
var jsf = require('jsonfile');

var Pokemon = require('./pokemon');

/**
 * Object Definition
 */

var Inventory = function (username) {
  this.username = username;
  this.data = {
    candy:    [],
    pokemon:  []
  };
};

/**
 * Prototypes
 */

Inventory.prototype.load = function (candy, pokemon) {
  if (fs.existsSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json')) {
    this.data.candy   = jsf.readFileSync(__dirname + '/../data/save/' + this.username + '/inventory-candy.json');
    this.data.pokemon = _.map(jsf.readFileSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json'), function (p) {
      return new Pokemon(p);
    });
    this.initialize(candy, pokemon);
  } else {
    this.data.candy   = candy;
    this.data.pokemon = _.map(pokemon, function (p) {
      return new Pokemon(p);
    });
  }
};

Inventory.prototype.save = function () {
  var formatted = _.map(this.data.pokemon, function (p) {
    return p.data;
  });
  if (!fs.existsSync(__dirname + '/../data/save/' + this.username)) fs.mkdirSync(__dirname + '/../data/save/' + this.username);
  jsf.writeFileSync(__dirname + '/../data/save/' + this.username + '/inventory-candy.json', this.data.candy, {spaces: 2});
  jsf.writeFileSync(__dirname + '/../data/save/' + this.username + '/inventory-pokemon.json', formatted, {spaces: 2});
};

Inventory.prototype.initialize = function (candy, pokemon) {
  var self = this;

  this.data.candy = candy;

  var idMap = _.reduce(this.data.pokemon, function (result, p) {
    result[p.id] = false;
    return result;
  }, {});

  _.forEach(pokemon, function (data) {
    var p = _.find(self.data.pokemon, {data: {id: data.id}});
    if (p) p.update(data);
    else self.data.pokemon.push(new Pokemon(data));
    idMap[data.id] = true;
  });

  _.forEach(idMap, function (exists, id) {
    if (!exists) _.remove(self.data.pokemon, {data: {id: id}});
  });

};

Inventory.prototype.update = function (candy, pokemon) {
  var self = this;

  _.forEach(candy, function (c) {
    var data = _.find(self.data.candy, {family_id: c.family_id});
    if (data !== undefined) data.candy = c.candy;
    else self.data.candy.push(c);
  });

  _.forEach(pokemon, function (data) {
{'fraction_non_alphanumeric': 0.1914482165878814, 'fraction_numerical': 0.0008594757198109154, 'mean_word_length': 3.2279745685740235, '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': '6369467', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1647, 'n_words': 323}