<commit_before>'use strict';

var _ = require('lodash');
var ships = require('./ships');
var events = require('../controllers/events');
var XpItem = require('./shipBuild/xpItem');
var itemTypes = require('./shipBuild/itemTypes');
var upgrades = require('../models/upgrades').all;
var pilots = require('../models/pilots').allRebels;
var EnemyDefeatsModel = require('../models/enemyDefeats');
var UpgradesModel = require('./shipBuild/upgradesModel');
var UpgradesSlotsModel = require('./shipBuild/upgradeSlots');

// Ship build
var ShipBuild = function (xpHistory, callsign, playerName, enemyDefeats, equippedUpgrades, equippedAbilities) {
    this.callsign = callsign;
    this.playerName = playerName;
    this.currentShip = null;
    this.xpHistory = [];
    this.currentXp = 0;
    this.pilotAbilities = [];

    this.enemyDefeats = new EnemyDefeatsModel(enemyDefeats);

    this.setPilotSkill(2);
    this.processHistory(xpHistory);

    var upgradeIds = _(xpHistory).filter(function (xpItem) {
        return xpItem.upgradeType === itemTypes.BUY_UPGRADE;
    }).map(function (xpItem) {
        return xpItem.data.upgradeId;
    }).value();

    this.upgradeSlots = new UpgradesSlotsModel(this);
    this.upgrades = new UpgradesModel(this, upgradeIds, equippedUpgrades, equippedAbilities);

    this.ready = true;
    events.trigger('model.build.ready', this);
};

ShipBuild.prototype.processHistory = function (xpHistory) {
    var thisBuild = this;

    _.each(xpHistory, function (xpItem) {
        if (xpItem.upgradeType === itemTypes.STARTING_SHIP_TYPE) {
            thisBuild.setStartingShip(xpItem.data.shipId);
        } else if (xpItem.upgradeType === itemTypes.SHIP_TYPE) {
            thisBuild.changeShip(xpItem.data.shipId);
        } else if (xpItem.upgradeType === itemTypes.PILOT_SKILL) {
            thisBuild.increasePilotSkill();
        } else if (xpItem.upgradeType === itemTypes.BUY_UPGRADE) {
            thisBuild.addToHistory(itemTypes.BUY_UPGRADE, {
                upgradeId: xpItem.data.upgradeId
            });
        } else if (xpItem.upgradeType === itemTypes.MISSION) {
            thisBuild.addMissionXp(xpItem.data.missionXp);
        } else if (xpItem.upgradeType === itemTypes.BUY_PILOT_ABILITY) {
            thisBuild.buyPilotAbility(xpItem.data.pilotId);
        }
    });
};

ShipBuild.prototype.getShipById = function (shipId) {
    var hotacShipModel = _.find(ships, function (ship) {
        return ship.id === shipId;
    });
    var newModel = _.clone(hotacShipModel, true);
    return newModel;
};

ShipBuild.prototype.getUpgradeById = function (upgradeId) {
    return _.find(upgrades, function (upgradeItem) {
        return upgradeItem.id === upgradeId;
    });
};

ShipBuild.prototype.getPilotById = function (pilotId) {
    return _.find(pilots, function (pilotCard) {
        return pilotCard.id === pilotId;
    });
};

ShipBuild.prototype.addXp = function (xp) {
    this.currentXp += xp;
    events.trigger('model.build.xp.update', this);
};

ShipBuild.prototype.setStartingShip = function (shipId) {
    this.currentShip = this.getShipById(shipId);
    this.addToHistory(itemTypes.STARTING_SHIP_TYPE, {
        shipId: shipId
    });
    events.trigger('model.build.currentShip.update', this);
};

ShipBuild.prototype.setPilotSkill = function (ps) {
    this.pilotSkill = ps;
    events.trigger('model.build.pilotSkill.update', this);
};

ShipBuild.prototype.increasePilotSkill = function () {
    this.setPilotSkill(this.pilotSkill + 1);
    this.addToHistory(itemTypes.PILOT_SKILL, {
        pilotSkill: this.pilotSkill
    });
    this.upgradeSlots.reset();
};

ShipBuild.prototype.addToHistory = function (type, data) {
    var xpItem = new XpItem(type, data);
    this.addXp(xpItem.cost());
    this.xpHistory.push(xpItem);
    events.trigger('model.build.xpHistory.add', {
        build: this,<commit_msg>Fix upgrade reset when pilot skill<commit_after>'use strict';

var _ = require('lodash');
var ships = require('./ships');
var events = require('../controllers/events');
var XpItem = require('./shipBuild/xpItem');
var itemTypes = require('./shipBuild/itemTypes');
var upgrades = require('../models/upgrades').all;
var pilots = require('../models/pilots').allRebels;
var EnemyDefeatsModel = require('../models/enemyDefeats');
var UpgradesModel = require('./shipBuild/upgradesModel');
var UpgradesSlotsModel = require('./shipBuild/upgradeSlots');

// Ship build
var ShipBuild = function (xpHistory, callsign, playerName, enemyDefeats, equippedUpgrades, equippedAbilities) {
    this.callsign = callsign;
    this.playerName = playerName;
    this.currentShip = null;
    this.xpHistory = [];
    this.currentXp = 0;
    this.pilotAbilities = [];

    this.enemyDefeats = new EnemyDefeatsModel(enemyDefeats);

    this.setPilotSkill(2);
    this.processHistory(xpHistory);

    var upgradeIds = _(xpHistory).filter(function (xpItem) {
        return xpItem.upgradeType === itemTypes.BUY_UPGRADE;
    }).map(function (xpItem) {
        return xpItem.data.upgradeId;
    }).value();

    this.upgradeSlots = new UpgradesSlotsModel(this);
    this.upgrades = new UpgradesModel(this, upgradeIds, equippedUpgrades, equippedAbilities);

    this.ready = true;
    events.trigger('model.build.ready', this);
};

ShipBuild.prototype.processHistory = function (xpHistory) {
    var thisBuild = this;

    _.each(xpHistory, function (xpItem) {
        if (xpItem.upgradeType === itemTypes.STARTING_SHIP_TYPE) {
            thisBuild.setStartingShip(xpItem.data.shipId);
        } else if (xpItem.upgradeType === itemTypes.SHIP_TYPE) {
            thisBuild.changeShip(xpItem.data.shipId);
        } else if (xpItem.upgradeType === itemTypes.PILOT_SKILL) {
            thisBuild.increasePilotSkill();
        } else if (xpItem.upgradeType === itemTypes.BUY_UPGRADE) {
            thisBuild.addToHistory(itemTypes.BUY_UPGRADE, {
                upgradeId: xpItem.data.upgradeId
            });
        } else if (xpItem.upgradeType === itemTypes.MISSION) {
            thisBuild.addMissionXp(xpItem.data.missionXp);
        } else if (xpItem.upgradeType === itemTypes.BUY_PILOT_ABILITY) {
            thisBuild.buyPilotAbility(xpItem.data.pilotId);
        }
    });
};

ShipBuild.prototype.getShipById = function (shipId) {
    var hotacShipModel = _.find(ships, function (ship) {
        return ship.id === shipId;
    });
    var newModel = _.clone(hotacShipModel, true);
    return newModel;
};

ShipBuild.prototype.getUpgradeById = function (upgradeId) {
    return _.find(upgrades, function (upgradeItem) {
        return upgradeItem.id === upgradeId;
    });
};

ShipBuild.prototype.getPilotById = function (pilotId) {
    return _.find(pilots, function (pilotCard) {
        return pilotCard.id === pilotId;
    });
};

ShipBuild.prototype.addXp = function (xp) {
    this.currentXp += xp;
    events.trigger('model.build.xp.update', this);
};

ShipBuild.prototype.setStartingShip = function (shipId) {
    this.currentShip = this.getShipById(shipId);
    this.addToHistory(itemTypes.STARTING_SHIP_TYPE, {
        shipId: shipId
    });
    events.trigger('model.build.currentShip.update', this);
};

ShipBuild.prototype.setPilotSkill = function (ps) {
    this.pilotSkill = ps;
};

ShipBuild.prototype.increasePilotSkill = function () {
    var newPilotSkill = this.pilotSkill + 1;
    this.addToHistory(itemTypes.PILOT_SKILL, {
        pilotSkill: newPilotSkill
    });
    this.setPilotSkill(newPilotSkill);
    if (this.ready === true) {
        this.upgradeSlots.reset();
        this.upgrades.refreshUpgradesState();
    }
    events.trigger('model.build.pilotSkill.update', this);
};

ShipBuild.prototype.addToHistory = function (type, data) {
    var xpItem = new XpItem(type, data);
    this.addXp(xpItem.cost());
    this.xpHistory.push(xpItem);
    events.trigger('model.build.xpHistory.add', {
        build: this,
{'fraction_non_alphanumeric': 0.1353440628564187, 'fraction_numerical': 0.0007603599036877456, 'mean_word_length': 3.4815445769449176, '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': '2628476', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2553, 'n_words': 421}