diff --git a/js-src/modules/controllers/page.js b/js-src/modules/controllers/page.js index a2f074b..4614f6b 100755 --- a/js-src/modules/controllers/page.js +++ b/js-src/modules/controllers/page.js @@ -1,8 +1,8 @@ 'use strict'; -var XpItem = require('../models/shipBuild/xpItem'); -var itemTypes = require('../models/shipBuild/itemTypes'); -var Build = require('../models/shipBuild'); +var XpItem = require('xwing-hotac-lib/modules/models/shipBuild/xpItem'); +var itemTypes = require('xwing-hotac-lib/modules/models/shipBuild/itemTypes'); +var Build = require('xwing-hotac-lib/modules/models/shipBuild'); var events = require('./events'); var headerView = require('../views/header'); var newView = require('../views/newView'); diff --git a/js-src/modules/controllers/urlHash.js b/js-src/modules/controllers/urlHash.js index e06a3a1..1cc5f32 100644 --- a/js-src/modules/controllers/urlHash.js +++ b/js-src/modules/controllers/urlHash.js @@ -5,7 +5,7 @@ var codec = window.JsonUrl('lzma'); var _map = require('lodash/map'); var $ = require('jquery'); -var XpItem = require('../models/shipBuild/xpItem'); +var XpItem = require('xwing-hotac-lib/modules/models/shipBuild/xpItem'); var EnemyDefeatsModel = require('../models/enemyDefeats'); module.exports = { diff --git a/js-src/modules/models/shipBuild.js b/js-src/modules/models/shipBuild.js deleted file mode 100755 index 78a3ec5..0000000 --- a/js-src/modules/models/shipBuild.js +++ /dev/null @@ -1,246 +0,0 @@ -'use strict'; - -var _each = require('lodash/each'); -var _map = require('lodash/map'); -var _clone = require('lodash/clone'); -var _uniq = require('lodash/uniq'); - -var ships = require('./ships'); -var events = require('../controllers/events'); -var XpItem = require('./shipBuild/xpItem'); -var itemTypes = require('./shipBuild/itemTypes'); -var arrayUtils = require('../utils/array-utils'); - -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.enemyDefeats = new EnemyDefeatsModel(enemyDefeats); - - this.setPilotSkill(2); - this.processHistory(xpHistory); - - var upgradeIds = this.getUpgradeIdsFromHistory(); - var pilotIds = this.getPilotIdsFromHistory(); - - this.upgradeSlots = new UpgradesSlotsModel(this); - this.upgrades = new UpgradesModel(this, upgradeIds, equippedUpgrades, pilotIds, equippedAbilities); - - this.ready = true; - events.trigger('model.build.ready', this); -}; - -ShipBuild.prototype.getPilotIdsFromHistory = function () { - var pilotIds = []; - _each(this.xpHistory, function (xpItem) { - if (xpItem.upgradeType === itemTypes.BUY_PILOT_ABILITY) { - pilotIds.push(xpItem.data.pilotId); - } - if (xpItem.upgradeType === itemTypes.LOSE_PILOT_ABILITY) { - arrayUtils.removeFirstMatchingValue(pilotIds, xpItem.data.pilotId); - } - }); - - return pilotIds; -}; - -ShipBuild.prototype.getUpgradeIdsFromHistory = function () { - var upgradeIds = []; - _each(this.xpHistory, function (xpItem) { - if (xpItem.upgradeType === itemTypes.BUY_UPGRADE) { - upgradeIds.push(xpItem.data.upgradeId); - } - if (xpItem.upgradeType === itemTypes.LOSE_UPGRADE) { - arrayUtils.removeFirstMatchingValue(upgradeIds, xpItem.data.upgradeId); - } - }); - - return upgradeIds; -}; - -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.LOSE_UPGRADE) { - thisBuild.addToHistory(itemTypes.LOSE_UPGRADE, { - upgradeId: xpItem.data.upgradeId - }); - } else if (xpItem.upgradeType === itemTypes.MISSION) { - thisBuild.completeMission(xpItem.data.missionId); - } else if (xpItem.upgradeType === itemTypes.XP) { - thisBuild.addMissionXp(xpItem.data.xp); - } else if (xpItem.upgradeType === itemTypes.BUY_PILOT_ABILITY) { - thisBuild.addToHistory(itemTypes.BUY_PILOT_ABILITY, { - pilotId: xpItem.data.pilotId - }); - } else if (xpItem.upgradeType === itemTypes.LOSE_PILOT_ABILITY) { - thisBuild.addToHistory(itemTypes.LOSE_PILOT_ABILITY, { - pilotId: xpItem.data.pilotId - }); - } - }); -}; - -ShipBuild.prototype.addXp = function (xp) { - this.currentXp += xp; - events.trigger('model.build.xp.update', this); -}; - -ShipBuild.prototype.setStartingShip = function (shipId) { - this.currentShip = ships.getById(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, - xpItem: xpItem - }); -}; - -ShipBuild.prototype.changeShip = function (shipId) { - this.addToHistory(itemTypes.SHIP_TYPE, { - shipId: shipId - }); - this.currentShip = ships.getById(shipId); - if (this.ready === true) { - this.upgradeSlots.reset(); - this.upgrades.refreshUpgradesState(); - } - events.trigger('model.build.currentShip.update', this); -}; - -ShipBuild.prototype.addMissionXp = function (xpAmount) { - this.addToHistory(itemTypes.XP, { - xp: xpAmount - }); -}; - -ShipBuild.prototype.completeMission = function (missionId) { - this.addToHistory(itemTypes.MISSION, { - missionId: missionId - }); -}; - -ShipBuild.prototype.exportEquippedUpgradesString = function () { - var upgradeIds = _map(this.upgrades.equippedUpgrades, function (item) { - return item.id; - }); - return JSON.stringify(upgradeIds); -}; - -ShipBuild.prototype.exportEquippedAbilitiesString = function () { - var pilotIds = _map(this.upgrades.equippedAbilities, function (item) { - return item.id; - }); - return JSON.stringify(pilotIds); -}; - -ShipBuild.prototype.getStats = function () { - var statValues = { - attack: this.currentShip.shipData.attack, - agility: this.currentShip.shipData.agility, - hull: this.currentShip.shipData.hull, - shields: this.currentShip.shipData.shields - }; - // Add nay stats from upgrades - _each(this.upgrades.equippedUpgrades, function (upgrade) { - if (upgrade.grants) { - _each(upgrade.grants, function (grant) { - if (grant.type === 'stats') { - statValues[grant.name] += grant.value; - } - }); - } - }); - return statValues; -}; - -ShipBuild.prototype.getActions = function () { - // Start with base ship actions - var actions = _clone(this.currentShip.shipData.actions, true); - // Add any actions from upgrades - _each(this.upgrades.equippedUpgrades, function (upgrade) { - if (upgrade.grants) { - _each(upgrade.grants, function (grant) { - if (grant.type === 'action') { - actions.push(grant.name); - } - }); - } - }); - actions = _uniq(actions); - - var slugifiedActions = []; - _each(actions, function (action) { - var slugifiedAction = action.replace(' ', '').replace('-', ''); - slugifiedAction = slugifiedAction.toLowerCase(); - slugifiedActions.push(slugifiedAction); - }); - - return slugifiedActions; -}; - -// Return Pilot as modified by any upgrades -ShipBuild.prototype.getModifiedPs = function () { - var modifiedPilotSkill = this.pilotSkill; - - // Add any actions from upgrades - _each(this.upgrades.equippedUpgrades, function (upgrade) { - if (upgrade.grants) { - _each(upgrade.grants, function (grant) { - if (grant.type === 'stats' && grant.name === 'skill') { - var psIncrease = grant.value; - modifiedPilotSkill += psIncrease; - } - }); - } - }); - - return modifiedPilotSkill; -}; - -module.exports = ShipBuild; - diff --git a/js-src/modules/models/shipBuild/itemTypes.js b/js-src/modules/models/shipBuild/itemTypes.js deleted file mode 100755 index c379681..0000000 --- a/js-src/modules/models/shipBuild/itemTypes.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -module.exports = { - SHIP_TYPE: 'ship', - PILOT_SKILL: 'pilotSkill', - BUY_UPGRADE: 'buyUpgrade', - LOSE_UPGRADE: 'loseUpgrade', - XP: 'xp', - MISSION: 'mission', - STARTING_SHIP_TYPE: 'startingShip', - BUY_PILOT_ABILITY: 'pilotAbility', - LOSE_PILOT_ABILITY: 'losePilotAbility' -}; diff --git a/js-src/modules/models/shipBuild/upgradeSlots.js b/js-src/modules/models/shipBuild/upgradeSlots.js deleted file mode 100644 index 5959e9e..0000000 --- a/js-src/modules/models/shipBuild/upgradeSlots.js +++ /dev/null @@ -1,130 +0,0 @@ -'use strict'; - -var _map = require('lodash/map'); -var _each = require('lodash/each'); -var _uniq = require('lodash/uniq'); - -var upgradeSlotsModel = function (build) { - this.build = build; - this.reset(); -}; - -upgradeSlotsModel.prototype.reset = function () { - var shipSlots = this.getShipSlots(); - this.free = shipSlots.free; - this.allBase = shipSlots.allBase; - this.enabled = shipSlots.enabled; - this.disabled = shipSlots.disabled; - this.resetAdditionalSlots(); -}; - -// Return array of all the possible slots allowed on the ship -// Each item contains slot type and required pilotSkill to unlock -// This is the total number of slots possible base don the ship chassis -upgradeSlotsModel.prototype.getShipSlots = function () { - var thisBuild = this.build; - - // elite slots are dependent on pilot level - var allBaseSlots = _map(this.build.currentShip.upgradeSlots, function (upgradeSlot) { - return { - type: upgradeSlot - }; - }); - - allBaseSlots = allBaseSlots.concat([ - { - type: 'Title' - }, - { - type: 'Modification' - }, - { - type: 'Modification', - pilotSkill: 4 - }, - { - type: 'Modification', - pilotSkill: 6 - }, - { - type: 'Modification', - pilotSkill: 8 - }, - { - type: 'Elite', - pilotSkill: 3 - }, - { - type: 'Elite', - pilotSkill: 5 - }, - { - type: 'Elite', - pilotSkill: 7 - }, - { - type: 'Elite', - pilotSkill: 9 - } - ]); - - // Now work out what slots are allowed in current build dependent on PS - var enabledSlots = []; - var disabledSlots = []; - - _each(allBaseSlots, function (slot) { - if (thisBuild.pilotSkill < slot.pilotSkill) { - // slot should be disabled - disabledSlots.push(slot); - } else { - enabledSlots.push(slot); - } - }); - - var startingSlots = _map(this.build.currentShip.startingUpgrades, function (upgrade) { - return { - type: upgrade.slot, - upgrade: upgrade - }; - }); - - return { - free: startingSlots, - allBase: allBaseSlots, - enabled: enabledSlots, - disabled: disabledSlots - }; -}; - -upgradeSlotsModel.prototype.resetAdditionalSlots = function () { - this.slotsFromUpgrades = []; -}; - -upgradeSlotsModel.prototype.addAdditionalSlot = function (upgradeType) { - var upgradeSlot = { - type: upgradeType - }; - this.slotsFromUpgrades.push(upgradeSlot); - - // we return the index of the newly added slot, so that the caller can modify if needed - return this.slotsFromUpgrades.length - 1; -}; - -// Return array of upgrade types enabled in current state of ship build (enabled + upgrades) -upgradeSlotsModel.prototype.allUsableSlotTypes = function () { - var allowedSlots = this.enabled; - var slotsFromUpgrades = this.slotsFromUpgrades; - - var allEnabledSlotTypes = allowedSlots.concat(slotsFromUpgrades); - // map output so that it is just a list of types - allEnabledSlotTypes = _map(allEnabledSlotTypes, function (item) { - return item.type; - }); - - // Remove any duplicates in array - allEnabledSlotTypes = _uniq(allEnabledSlotTypes); - - return allEnabledSlotTypes; -}; - -module.exports = upgradeSlotsModel; diff --git a/js-src/modules/models/shipBuild/upgradesModel.js b/js-src/modules/models/shipBuild/upgradesModel.js deleted file mode 100644 index 3914f43..0000000 --- a/js-src/modules/models/shipBuild/upgradesModel.js +++ /dev/null @@ -1,443 +0,0 @@ -'use strict'; - -var _map = require('lodash/map'); -var _filter = require('lodash/filter'); -var _bind = require('lodash/bind'); -var _each = require('lodash/each'); -var _difference = require('lodash/difference'); -var _findIndex = require('lodash/findIndex'); -var _isUndefined = require('lodash/isUndefined'); -var _find = require('lodash/find'); -var _first = require('lodash/first'); -var _clone = require('lodash/clone'); - -var upgradesImport = require('../upgrades'); -var keyedUpgrades = upgradesImport.keyed; -var pilots = require('../pilots'); -var uniquePilots = pilots.unique; - -var events = require('../../controllers/events'); -var arrayUtils = require('../../utils/array-utils'); - -var upgradesModel = function (build, upgradeIdList, equippedIdList, pilotIds, equippedAbilityIds) { - this.build = build; - // Upgrades in order of purchase - this.purchased = this.upgradesFromIds(upgradeIdList); - this.purchasedAbilities = this.abilitiesFromIds(pilotIds); - this.equippedUpgrades = this.upgradesFromIds(equippedIdList); - this.equippedAbilities = this.abilitiesFromIds(equippedAbilityIds); - this.refreshUpgradesState(); -}; - -upgradesModel.prototype.upgradesFromIds = function (upgradeIdList) { - return _map(upgradeIdList, upgradesImport.getById); -}; - -upgradesModel.prototype.abilitiesFromIds = function (abilityIdList) { - return _map(abilityIdList, pilots.getById); -}; - -upgradesModel.prototype.refreshUpgradesState = function () { - this.all = this.purchased.concat(this.build.currentShip.startingUpgrades); - // Validate and equip upgrades to slots - var validatedEquippedUpgrades = this.validateUpgrades(this.equippedUpgrades); - var validatedEquippedAbilities = this.validateAbilities(this.equippedAbilities); - var equipped = this.equipUpgradesToSlots(validatedEquippedUpgrades, validatedEquippedAbilities); - this.equippedUpgrades = equipped.equippedUpgrades; - this.equippedAbilities = equipped.equippedAbilities; - // Can only call getDisabled() once equipped is set, as it needs to look at slots potentially added by equipping - this.disabled = this.getDisabledUpgrades(); - this.disabledAbilities = this.getDisabledAbilities(); - this.unequipped = this.getUnequippedUpgrades(); - this.unequippedAbilities = this.getUnequippedAbilities(); -}; - -upgradesModel.prototype.validateUpgrades = function (upgradesList) { - // Make sure equipped list only contains upgrades we have purchased or started with - var filteredUpgrades = arrayUtils.intersectionSingle(upgradesList, this.all); - // Make sure equipped list only contains upgrade types allowed on ship - filteredUpgrades = _filter(filteredUpgrades, _bind(this.upgradeAllowedOnShip, this)); - return filteredUpgrades; -}; - -upgradesModel.prototype.validateAbilities = function (pilotsList) { - // Make sure equipped list only contains upgrades we have purchased - var filteredUpgrades = arrayUtils.intersectionSingle(pilotsList, this.purchasedAbilities); - // Make sure equipped list only contains abilities allowed in the build - filteredUpgrades = _filter(filteredUpgrades, _bind(this.abilityAllowedInBuild, this)); - return filteredUpgrades; -}; - -upgradesModel.prototype.getDisabledUpgrades = function () { - var thisModel = this; - var slotsAllowedInBuild = this.build.upgradeSlots.allUsableSlotTypes(); - - var disabledUpgrades = []; - - _each(this.purchased, function (upgrade) { - var allowedOnShip = thisModel.upgradeAllowedOnShip(upgrade); - var allowedInSlots = (slotsAllowedInBuild.indexOf(upgrade.slot) > -1); - - if (!allowedOnShip || !allowedInSlots) { - disabledUpgrades.push(upgrade); - } - }); - - return disabledUpgrades; -}; - -upgradesModel.prototype.getDisabledAbilities = function () { - var thisModel = this; - var slotsAllowedInBuild = this.build.upgradeSlots.allUsableSlotTypes(); - - var disabledUpgrades = []; - - // Abilities only go in Elite slots - var allowedInSlots = (slotsAllowedInBuild.indexOf('Elite') > -1); - - _each(this.purchasedAbilities, function (pilot) { - var allowedOnShip = thisModel.abilityAllowedInBuild(pilot); - - if (!allowedOnShip || !allowedInSlots) { - disabledUpgrades.push(pilot); - } - }); - - return disabledUpgrades; -}; - -upgradesModel.prototype.getUnequippedUpgrades = function () { - // Remove *All* copies of any upgrades which should be disabled - var notDisabled = _difference(this.purchased, this.disabled); - // Remove one copy of each item which is equipped - var unequipped = arrayUtils.differenceSingle(notDisabled, this.equippedUpgrades); - return unequipped; -}; - -upgradesModel.prototype.getUnequippedAbilities = function () { - // Remove *All* copies of any abiltiies which should be disabled - var notDisabled = _difference(this.purchasedAbilities, this.disabledAbilities); - // Remove one copy of each item which is equipped - var unequipped = arrayUtils.differenceSingle(notDisabled, this.equippedAbilities); - return unequipped; -}; - -upgradesModel.prototype.buyCard = function (upgradeId) { - var upgrade = upgradesImport.getById(upgradeId); - this.purchased.push(upgrade); - this.refreshUpgradesState(); - events.trigger('model.build.upgrades.add', this.build); -}; - -upgradesModel.prototype.buyPilotAbility = function (pilotId) { - var pilot = pilots.getById(pilotId); - this.purchasedAbilities.push(pilot); - this.refreshUpgradesState(); - events.trigger('model.build.pilotAbilities.add', this.build); -}; - -upgradesModel.prototype.loseCard = function (upgradeId) { - // remove the first version of this upgrade we find in the purchased list - var foundIndex = _findIndex(this.purchased, function (item) { - return item.id === upgradeId; - }); - if (!_isUndefined(foundIndex)) { - // remove found upgrade from purchased list - this.purchased.splice(foundIndex, 1); - } - this.refreshUpgradesState(); - events.trigger('model.build.upgrades.lose', this.build); -}; - -upgradesModel.prototype.loseAbility = function (pilotId) { - var foundIndex = _findIndex(this.purchasedAbilities, function (item) { - return item.id === pilotId; - }); - if (!_isUndefined(foundIndex)) { - this.purchasedAbilities.splice(foundIndex, 1); - } - this.refreshUpgradesState(); - events.trigger('model.build.pilotAbilities.lose', this.build); -}; - -upgradesModel.prototype.equip = function (upgradeId) { - var upgrade = upgradesImport.getById(upgradeId); - this.equippedUpgrades.push(upgrade); - this.refreshUpgradesState(); - events.trigger('model.build.equippedUpgrades.update', this.build); -}; - -upgradesModel.prototype.equipAbility = function (pilotId) { - var pilot = pilots.getById(pilotId); - this.equippedAbilities.push(pilot); - this.refreshUpgradesState(); - events.trigger('model.build.equippedUpgrades.update', this.build); -}; - -upgradesModel.prototype.unequipUpgrade = function (upgradeId) { - // find the first instance of this upgrade in the equipped list. - // We only look for the first time it appears, as there may be several of the same card equipped - var removeIndex = _findIndex(this.equippedUpgrades, function (upgrade) { - return upgrade.id === upgradeId; - }); - // Now remove found index from equipped list - if (removeIndex > -1) { - this.equippedUpgrades.splice(removeIndex, 1); - this.refreshUpgradesState(); - events.trigger('model.build.equippedUpgrades.update', this.build); - } -}; - -upgradesModel.prototype.unequipAbility = function (pilotId) { - // find the first instance of this upgrade in the equipped list. - // We only look for the first time it appears, as there may be several of the same card equipped - var removeIndex = _findIndex(this.equippedAbilities, function (pilot) { - return pilot.id === pilotId; - }); - // Now remove found index from equipped list - if (removeIndex > -1) { - this.equippedAbilities.splice(removeIndex, 1); - this.refreshUpgradesState(); - events.trigger('model.build.equippedUpgrades.update', this.build); - } -}; - -// Return array of upgrades of specific type which are legal to purchased for current build -// (e.g. restricted by chassis, size, already a starting upgrade, already purchased etc.) -upgradesModel.prototype.getAvailableToBuy = function (upgradeType) { - var upgradesOfType = keyedUpgrades[upgradeType]; - var allowedUpgrades = _filter(upgradesOfType, _bind(this.upgradeAllowedOnShip, this)); - allowedUpgrades = _filter(allowedUpgrades, _bind(this.upgradeAllowedInBuild, this)); - return allowedUpgrades; -}; - -upgradesModel.prototype.getAbilitiesAvailableToBuy = function () { - var allAbilities = uniquePilots; - var allowedPilots = _difference(allAbilities, this.purchasedAbilities); - var sortedPilots = pilots.sortList(allowedPilots); - return sortedPilots; -}; - -upgradesModel.prototype.upgradeAllowedOnShip = function (upgrade) { - // Remove any upgrades for different ships - if (upgrade.ship && upgrade.ship.indexOf(this.build.currentShip.shipData.name) < 0) { - return false; - } - - // Remove any upgrades for different ship sizes - if (upgrade.size && upgrade.size.indexOf(this.build.currentShip.shipData.size) < 0) { - return false; - } - - return true; -}; - -upgradesModel.prototype.abilityAllowedInBuild = function (pilot) { - // Remove pilots whose PS is higher than build - if (pilot.skill > this.build.pilotSkill) { - return false; - } - - return true; -}; - -upgradesModel.prototype.upgradeAllowedInBuild = function (upgrade) { - // Don't show anything which is a starting upgrade for the ship - if (this.build.currentShip.startingUpgrades) { - var found = _find(this.build.currentShip.startingUpgrades, function (startingUpgrade) { - return startingUpgrade.xws === upgrade.xws; - }); - if (found) { - return false; - } - } - - // Remove any upgrades the build already has - var upgradeExists = _find(this.all, function (existingUpgrade) { - // Check xws instead of ID so we remove both sides of dual cards - return existingUpgrade.xws === upgrade.xws; - }); - - if (upgradeExists) { - var upgradeIsAllowed = false; - // filter out any upgrades the player already has - // except - // * secondary weapons & bombs - if (upgrade.slot === 'Bomb' || upgrade.slot === 'Torpedo' || upgrade.slot === 'Cannon' || upgrade.slot === 'Turret' || upgrade.slot === 'Missile') { - upgradeIsAllowed = true; - // * hull upgrade and shield upgrade - } else if (upgrade.xws === 'hullupgrade' || upgrade.xws === 'shieldupgrade') { - upgradeIsAllowed = true; - } - if (!upgradeIsAllowed) { - return false; - } - } - - return true; -}; - -upgradesModel.prototype.abilityAlreadyInBuild = function (abilityPilot) { - // Remove any abilities the build already has - var abilityExists = _find(this.purchasedAbilities, function (existingAbility) { - return existingAbility.id === abilityPilot.id; - }); - - if (abilityExists) { - return true; - } - - return false; -}; - -upgradesModel.prototype.canEquipUpgrade = function (upgradeId) { - var upgradeSlots = this.build.upgradeSlots; - var upgrade = upgradesImport.getById(upgradeId); - - var canEquip = false; - - _each(upgradeSlots.enabled, function (upgradeSlot) { - if (upgradeSlot.type === upgrade.slot) { - // this slot is the right type for the upgrade - if (!upgradeSlot.equipped) { - // This slot is free - canEquip = true; - } - } - }); - - return canEquip; -}; - -upgradesModel.prototype.canEquipAbilties = function () { - var upgradeSlots = this.build.upgradeSlots; - - var canEquip = false; - - _each(upgradeSlots.enabled, function (upgradeSlot) { - if (upgradeSlot.type === 'Elite') { - // this slot is the right type for the upgrade - if (!upgradeSlot.equipped) { - // This slot is free - canEquip = true; - } - } - }); - - return canEquip; -}; - -upgradesModel.prototype.equipUpgradesToSlots = function (upgradesToEquip, abilitiesToEquip) { - var thisModel = this; - - var remainingUpgradesToEquip = _clone(upgradesToEquip); - var remainingAbilitiesToEquip = _clone(abilitiesToEquip); - var equippedUpgrades = []; - var equippedAbilities = []; - - var upgradeSlots = this.build.upgradeSlots; - // Reset additonal slots as we are about to repopulate through equipping - upgradeSlots.resetAdditionalSlots(); - - var newSlotIndices = []; - - _each(upgradeSlots.free, function (upgradeSlot) { - var matchingUpgrade = thisModel.matchFreeSlot(upgradeSlot, remainingUpgradesToEquip); - var slotsAddedIndices = thisModel.equipSlot(upgradeSlot, matchingUpgrade, equippedUpgrades, equippedAbilities, remainingUpgradesToEquip, remainingAbilitiesToEquip); - // If we added any new slots as part of equipping this upgrade, add them to the list - newSlotIndices = newSlotIndices.concat(slotsAddedIndices); - }); - - _each(upgradeSlots.enabled, function (upgradeSlot) { - var matchingUpgrade = thisModel.matchSlot(upgradeSlot, remainingUpgradesToEquip, remainingAbilitiesToEquip); - var slotsAddedIndices = thisModel.equipSlot(upgradeSlot, matchingUpgrade, equippedUpgrades, equippedAbilities, remainingUpgradesToEquip, remainingAbilitiesToEquip); - // If we added any new slots as part of equipping this upgrade, add them to the list - newSlotIndices = newSlotIndices.concat(slotsAddedIndices); - }); - - // If we added any slots via upgrades, equip to them now - while (newSlotIndices.length > 0) { - // get the first item index from the array - var itemIndex = newSlotIndices.shift(); - // try to equip to the additional slot at that index - var matchingUpgrade = this.matchSlot(this.build.upgradeSlots.slotsFromUpgrades[itemIndex], remainingUpgradesToEquip, remainingAbilitiesToEquip); - var slotsAddedIndices = this.equipSlot(this.build.upgradeSlots.slotsFromUpgrades[itemIndex], matchingUpgrade, equippedUpgrades, equippedAbilities, remainingUpgradesToEquip, remainingAbilitiesToEquip); - // If we added yet more slots as part of equipping this upgrade, add them to the list - newSlotIndices = newSlotIndices.concat(slotsAddedIndices); - } - - return { - equippedUpgrades: equippedUpgrades, - equippedAbilities: equippedAbilities - }; -}; - -upgradesModel.prototype.matchFreeSlot = function (upgradeSlot, remainingUpgradesToEquip) { - // Is there an equipped upgrade for this slot? - var matchingUpgrade = _find(remainingUpgradesToEquip, function (upgrade) { - return upgrade.id === upgradeSlot.upgrade.id; - }); - return matchingUpgrade; -}; - -upgradesModel.prototype.matchSlot = function (upgradeSlot, remainingUpgradesToEquip, remainingAbilitiesToEquip) { - // Is there an equipped upgrade for this slot? - var matchingUpgrade = _find(remainingUpgradesToEquip, function (upgrade) { - return upgrade.slot === upgradeSlot.type; - }); - - if (!matchingUpgrade && upgradeSlot.type === 'Elite') { - // We didn't find a match, and this is elite, so also check for matching abilities - matchingUpgrade = _first(remainingAbilitiesToEquip); - } - return matchingUpgrade; -}; - -upgradesModel.prototype.equipSlot = function (upgradeSlot, upgradeToEquip, equippedUpgrades, equippedAbilities, remainingUpgradesToEquip, remainingAbilitiesToEquip) { - var addedSlotsIndices = []; - - // clear existing upgrade from slot - delete upgradeSlot.equipped; - - if (upgradeToEquip) { - if (this.upgradeisAbility(upgradeToEquip)) { - // remove this upgrade from the list available to match slots - arrayUtils.removeFirstMatchingValue(remainingAbilitiesToEquip, upgradeToEquip); - equippedAbilities.push(upgradeToEquip); - } else { - // remove this upgrade from the list available to match slots - arrayUtils.removeFirstMatchingValue(remainingUpgradesToEquip, upgradeToEquip); - equippedUpgrades.push(upgradeToEquip); - // Add any extra slots granted by the upgrade - addedSlotsIndices = this.addUpgradeGrantsSlot(upgradeToEquip); - } - upgradeSlot.equipped = upgradeToEquip; - } - - // If we added additional slots via a grant on this upgrade, let the caller know - return addedSlotsIndices; -}; - -upgradesModel.prototype.addUpgradeGrantsSlot = function (upgrade) { - var thisModel = this; - var addedSlotIndices = []; - _each(upgrade.grants, function (grant) { - if (grant.type === 'slot') { - var addedSlotIndex = thisModel.build.upgradeSlots.addAdditionalSlot(grant.name); - addedSlotIndices.push(addedSlotIndex); - } - }); - return addedSlotIndices; -}; - -// Return boolean whether upgrade is an ability. Can be used to differentiate between equipped upgrades -// which are card and which are pilot abilities -upgradesModel.prototype.upgradeisAbility = function (upgrade) { - if (upgrade.skill) { - // only pilot cards have skill property, not upgrade cards - return true; - } - return false; -}; - -module.exports = upgradesModel; diff --git a/js-src/modules/models/shipBuild/xpItem.js b/js-src/modules/models/shipBuild/xpItem.js deleted file mode 100755 index fc4e4ae..0000000 --- a/js-src/modules/models/shipBuild/xpItem.js +++ /dev/null @@ -1,153 +0,0 @@ -'use strict'; - -var itemTypes = require('./itemTypes'); -var ships = require('../ships'); -var upgrades = require('../upgrades'); -var pilots = require('../pilots'); -var missions = require('../missions'); - -var XpItem = function (upgradeType, data) { - this.upgradeType = upgradeType; - this.data = data; -}; - -XpItem.prototype.cost = function () { - if (this.upgradeType === itemTypes.SHIP_TYPE) { - return -5; - } else if (this.upgradeType === itemTypes.STARTING_SHIP_TYPE) { - var startingShip = ships.getById(this.data.shipId); - return startingShip.startingXp; - } else if (this.upgradeType === itemTypes.PILOT_SKILL) { - // pilot skill costs double skill level upgrading to - return (this.data.pilotSkill * 2) * -1; - } else if (this.upgradeType === itemTypes.XP) { - return this.data.xp; - } else if (this.upgradeType === itemTypes.BUY_UPGRADE) { - var upgrade = upgrades.getById(this.data.upgradeId); - if (upgrade.points === 0) { - return 0; - } - if (upgrade.slot === 'Elite') { - // Elites are double printed points cost - return upgrade.points * -2; - } - return upgrade.points * -1; - } else if (this.upgradeType === itemTypes.BUY_PILOT_ABILITY) { - var pilot = pilots.getById(this.data.pilotId); - return pilot.skill * -1; - } - return 0; -}; - -XpItem.prototype.label = function () { - if (this.upgradeType === itemTypes.SHIP_TYPE) { - var ship = ships.getById(this.data.shipId); - return 'Change ship: ' + ship.shipData.name; - } else if (this.upgradeType === itemTypes.STARTING_SHIP_TYPE) { - var startingShip = ships.getById(this.data.shipId); - return 'Starting ship: ' + startingShip.shipData.name; - } else if (this.upgradeType === itemTypes.PILOT_SKILL) { - return 'Upgrade pilot skill: PS ' + this.data.pilotSkill; - } else if (this.upgradeType === itemTypes.MISSION) { - var mission = missions.getById(this.data.missionId); - return 'Completed mission: ' + mission.name; - } else if (this.upgradeType === itemTypes.XP) { - return 'Gain XP'; - } else if (this.upgradeType === itemTypes.BUY_UPGRADE) { - var upgrade = upgrades.getById(this.data.upgradeId); - return upgrade.slot + ': ' + (upgrade.dualCardName || upgrade.name); - } else if (this.upgradeType === itemTypes.BUY_PILOT_ABILITY) { - var pilot = pilots.getById(this.data.pilotId); - return 'Pilot Ability: ' + pilot.name; - } else if (this.upgradeType === itemTypes.LOSE_UPGRADE) { - var lostUpgrade = upgrades.getById(this.data.upgradeId); - return 'Lose upgrade: ' + (lostUpgrade.dualCardName || lostUpgrade.name); - } else if (this.upgradeType === itemTypes.LOSE_PILOT_ABILITY) { - var lostPilot = pilots.getById(this.data.pilotId); - return 'Lose pilot ability: ' + lostPilot.name; - } - return ''; -}; - -XpItem.prototype.exportString = function () { - var idString = ''; - var dataString = ''; - if (this.upgradeType === itemTypes.SHIP_TYPE) { - dataString = this.data.shipId; - idString = 'ST'; - } else if (this.upgradeType === itemTypes.STARTING_SHIP_TYPE) { - dataString = this.data.shipId; - idString = 'SST'; - } else if (this.upgradeType === itemTypes.PILOT_SKILL) { - dataString = this.data.pilotSkill; - idString = 'PS'; - } else if (this.upgradeType === itemTypes.MISSION) { - dataString = this.data.missionId; - idString = 'MIS'; - } else if (this.upgradeType === itemTypes.XP) { - dataString = this.data.xp; - idString = 'XP'; - } else if (this.upgradeType === itemTypes.BUY_UPGRADE) { - dataString = this.data.upgradeId; - idString = 'UP'; - } else if (this.upgradeType === itemTypes.BUY_PILOT_ABILITY) { - dataString = this.data.pilotId; - idString = 'PA'; - } else if (this.upgradeType === itemTypes.LOSE_UPGRADE) { - dataString = this.data.upgradeId; - idString = 'LUP'; - } else if (this.upgradeType === itemTypes.LOSE_PILOT_ABILITY) { - dataString = this.data.pilotId; - idString = 'LPA'; - } - return { - key: idString, - value: dataString - }; -}; - -XpItem.prototype.parseExportString = function (exportObject) { - var idString = exportObject.key; - var dataString = exportObject.value; - this.data = {}; - - if (idString === 'SST') { - this.data.shipId = dataString; - this.upgradeType = itemTypes.STARTING_SHIP_TYPE; - } else if (idString === 'ST') { - this.data.shipId = dataString; - this.upgradeType = itemTypes.SHIP_TYPE; - } else if (idString === 'PS') { - this.data.pilotSkill = parseInt(dataString, 10); - this.upgradeType = itemTypes.PILOT_SKILL; - } else if (idString === 'MIS') { - this.data.missionId = parseInt(dataString, 10); - this.upgradeType = itemTypes.MISSION; - } else if (idString === 'XP') { - this.data.xp = parseInt(dataString, 10); - this.upgradeType = itemTypes.XP; - } else if (idString === 'UP') { - this.data.upgradeId = parseInt(dataString, 10); - this.upgradeType = itemTypes.BUY_UPGRADE; - } else if (idString === 'PA') { - this.data.pilotId = parseInt(dataString, 10); - this.upgradeType = itemTypes.BUY_PILOT_ABILITY; - } else if (idString === 'LUP') { - this.data.upgradeId = parseInt(dataString, 10); - this.upgradeType = itemTypes.LOSE_UPGRADE; - } else if (idString === 'LPA') { - this.data.pilotId = parseInt(dataString, 10); - this.upgradeType = itemTypes.LOSE_PILOT_ABILITY; - } -}; - -XpItem.prototype.parseExportStringLessV3 = function (exportString) { - var splitItems = exportString.split('='); - var splitObject = { - key: splitItems[0], - value: splitItems[1] - }; - this.parseExportString(splitObject); -}; - -module.exports = XpItem; diff --git a/js-src/modules/views/xpHistory.js b/js-src/modules/views/xpHistory.js index 3724e21..422dfed 100644 --- a/js-src/modules/views/xpHistory.js +++ b/js-src/modules/views/xpHistory.js @@ -5,7 +5,7 @@ var _each = require('lodash/each'); var events = require('../controllers/events'); var modalController = require('../controllers/modals'); var templateUtils = require('../utils/templates'); -var itemTypes = require('../models/shipBuild/itemTypes'); +var itemTypes = require('xwing-hotac-lib/modules/models/shipBuild/itemTypes'); module.exports = { renderTable: function (build) { diff --git a/package-lock.json b/package-lock.json index 0573037..0d26398 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7263,6 +7263,10 @@ "integrity": "sha1-5akI+lvPCET+pbNyvkp7pZxqmGw=", "dev": true }, + "xwing-hotac-lib": { + "version": "github:grahamgilchrist/xwing-hotac-lib#eacf337d7940726c79092aee171f19982776900c", + "dev": true + }, "y18n": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.1.tgz", diff --git a/package.json b/package.json index 8c0e1ce..d2c9bbc 100755 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "type": "git", "url": "git+https://github.com/grahamgilchrist/hotac-squad-builder.git" }, - "author": "", + "author": "Graham Gilchrist", + "license": "MIT", "bugs": { "url": "https://github.com/grahamgilchrist/hotac-squad-builder/issues" }, @@ -32,7 +33,8 @@ "pump": "^1.0.3", "run-sequence": "^2.2.0", "vinyl-source-stream": "1.x", - "xwing-data": "0.62.x" + "xwing-data": "0.62.x", + "xwing-hotac-lib": "grahamgilchrist/xwing-hotac-lib#0.1.1" }, "dependencies": {} }