From 74ed09d830bc247688cb62ea251ca5726762de6a Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Sat, 14 Jan 2023 14:57:10 -0600 Subject: [PATCH] Lookup stocks by adjective field Don't use a hardcoded list of values in the source... --- src/public/js/burning-service.js | 14 +------ src/public/js/burning.js | 69 +++----------------------------- 2 files changed, 7 insertions(+), 76 deletions(-) diff --git a/src/public/js/burning-service.js b/src/public/js/burning-service.js index 15a2010..242aaab 100644 --- a/src/public/js/burning-service.js +++ b/src/public/js/burning-service.js @@ -301,7 +301,6 @@ function BurningDataService($http) { var myself = this; - var stocks; // Log events this.registerEvent("stocksLoaded", function() { @@ -325,8 +324,7 @@ function BurningDataService($http) { // DEBUG: // console.log(data); myself.stocks = data; - stocks = Object.keys(data); - for (var stock of stocks) { + for (var stock of Object.keys(data)) { myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); myself.defineStockEvent(stock, "lifepathsLoaded"); myself.defineStockEvent(stock, "resourcesLoaded"); @@ -339,11 +337,6 @@ function BurningDataService($http) { /* Load lifepaths from server */ this.loadLifepathsForStock = function(stock){ - if( ! isValidStock(stock) ){ - console.log("Loading lifepaths failed: asked to load lifepaths for invalid stock " + stock); - return - } - $http.get("/lifepaths/" + stock, {'timeout': 3000} ). success(function(data,status,headers,config){ myself.lifepaths[stock] = data; @@ -357,11 +350,6 @@ function BurningDataService($http) { /* Load resources from server */ this.loadResourcesForStock = function(stock){ - if( ! isValidStock(stock) ){ - console.log("Loading resources failed: asked to load for invalid stock " + stock); - return - } - $http.get("/resources/" + stock, {'timeout': 3000} ). success(function(data,status,headers,config){ myself.resources[stock] = data; diff --git a/src/public/js/burning.js b/src/public/js/burning.js index 9ca9408..d2515e4 100644 --- a/src/public/js/burning.js +++ b/src/public/js/burning.js @@ -409,7 +409,6 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo calculateUnspentSkillPoints($scope); } - burningData.registerEvent("stocksLoaded", function() { $scope.stocks = [{ name: "Select a stock" }] $scope.stocks = $scope.stocks.concat(Object.values(burningData.stocks)); @@ -938,7 +937,7 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo for(var key in burningData.skills){ if ( !(key in $scope.lifepathSkills) && !(key in $scope.generalSkills) ){ var displaySkill = burningData.skills[key]; - if ( !displaySkill.stock || restrictionStockToValidStock(displaySkill.stock) == $scope.stock ) { + if ( !displaySkill.stock || restrictionStockToValidStock(burningData.stocks, displaySkill.stock) == $scope.stock ) { result.push(key); } } @@ -1187,30 +1186,6 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo return list; } -/* - - $scope.specialTraitsForDisplay = function(){ - var list = []; - - for(var traitName in burningData.traits) { - var trait = burningData.traits[traitName]; - - if ('restrict' in trait){ - if ( trait.restrict.indexOf(validStockToRestrictionStock($scope.stock)) >= 0 && - (trait.restrict.indexOf("special") >= 0 || trait.restrict.indexOf("character") >= 0) ){ - list.push(new DisplayTrait(traitName, burningData.traits)); - } - } else { - // No restriction! As long as cost > 0 (cost 0 is for traits with no cost; not purchaseable) - if ( trait.cost > 0 ) { - list.push(new DisplayTrait(traitName, burningData.traits)); - } - } - } - - return list; - } -*/ $scope.addLifepathTrait = function(traitName){ if ( $scope.unspentTraitPoints < 1 && $scope.enforcePointLimits ) @@ -2758,7 +2733,7 @@ function calculateSpecialTraitsForDisplay($scope, burningData){ var trait = burningData.traits[traitName]; if ('restrict' in trait){ - if ( trait.restrict.indexOf(validStockToRestrictionStock($scope.stock)) >= 0 && + if ( trait.restrict.indexOf(validStockToRestrictionStock(burningData.stocks, $scope.stock)) >= 0 && (trait.restrict.indexOf("special") >= 0 || trait.restrict.indexOf("character") >= 0) ){ list.push(new DisplayTrait(traitName, burningData.traits)); } @@ -2806,44 +2781,12 @@ function calculateUnspentResourcePoints($scope){ $scope.unspentResourcePoints = unspentResourcePoints; } -function isValidStock(stock){ - return stock == "man" || stock == "elf" || stock == "orc" || stock == "dwarf" || stock == "roden" || stock == "wolf" || stock =="troll"; -} - -function restrictionStockToValidStock(stock){ - - if ( stock == "mannish" ) - return "man"; - else if ( stock == "elven" ) - return "elf"; - else if ( stock == "orcish" ) - return "orc"; - else if ( stock == "dwarven" ) - return "dwarf"; - else if ( stock == "wolfish" ) - return "wolf"; - else if ( stock == "roden" ) - return "roden"; - else if ( stock == "trollish" ) - return "troll"; +function restrictionStockToValidStock(stocks, stockAdjective){ + return Object.values(stocks).findLast(s => s.adjective == stockAdjective).key; } -function validStockToRestrictionStock(stock){ - - if ( stock == "man" ) - return "mannish"; - else if ( stock == "elf" ) - return "elven"; - else if ( stock == "orc" ) - return "orcish"; - else if ( stock == "dwarf" ) - return "dwarven"; - else if ( stock == "roden" ) - return "roden"; - else if ( stock == "wolf" ) - return "wolfish"; - else if ( stock == "troll" ) - return "trollish"; +function validStockToRestrictionStock(stocks, stockName){ + return stocks[stockName].adjective; } function attributeModifyingQuestions($scope, attribute)