From fc9edd2b48d4cfc6971e381e374f4f012fc1e5c8 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Sun, 8 Jan 2023 17:22:47 -0600 Subject: [PATCH] On demand data loading --- src/app.rb | 8 -- src/data/gold/stocks/elf.json | 2 +- src/data/gold/stocks/man.json | 2 +- src/data/gold/stocks/orc.json | 2 +- src/data/gold/stocks/roden.json | 2 +- src/data/gold/stocks/wolf.json | 2 +- src/public/js/burning-service.js | 146 +++++++++++++++++++------------ src/public/js/burning.js | 48 ++++++---- src/views/partials/main.erb | 9 +- 9 files changed, 128 insertions(+), 93 deletions(-) diff --git a/src/app.rb b/src/app.rb index c4cc8ff..7748071 100644 --- a/src/app.rb +++ b/src/app.rb @@ -61,14 +61,6 @@ get '/lifepaths/:stock' do end end -get '/starting_stat_pts/:stock' do - if DATA[:stocks].keys.include? params['stock'] - json DATA[:stat_pts][params['stock']] - else - 404 - end -end - get '/resources/:stock' do if DATA[:stocks].keys.include? params['stock'] json DATA[:resources][params['stock']] diff --git a/src/data/gold/stocks/elf.json b/src/data/gold/stocks/elf.json index 728a573..48a6c7d 100644 --- a/src/data/gold/stocks/elf.json +++ b/src/data/gold/stocks/elf.json @@ -1,6 +1,6 @@ { "key": "elf", - "name": "elf", + "name": "Elf", "stride": 8, "common_traits": [ "Born Under The Silver Stars", diff --git a/src/data/gold/stocks/man.json b/src/data/gold/stocks/man.json index 9bfcf12..7e452c7 100644 --- a/src/data/gold/stocks/man.json +++ b/src/data/gold/stocks/man.json @@ -1,6 +1,6 @@ { "key": "man", - "name": "man", + "name": "Man", "stride": 7, "common_traits": [ ], diff --git a/src/data/gold/stocks/orc.json b/src/data/gold/stocks/orc.json index 3f8a9b7..36daaca 100644 --- a/src/data/gold/stocks/orc.json +++ b/src/data/gold/stocks/orc.json @@ -1,6 +1,6 @@ { "key": "orc", - "name": "orc", + "name": "Orc", "stride": 7, "common_traits": [ "Cannibal", diff --git a/src/data/gold/stocks/roden.json b/src/data/gold/stocks/roden.json index a5939f1..1914eda 100644 --- a/src/data/gold/stocks/roden.json +++ b/src/data/gold/stocks/roden.json @@ -1,6 +1,6 @@ { "key": "roden", - "name": "roden", + "name": "Roden", "stride": 8, "common_traits": [ "Aecer's Likeness", diff --git a/src/data/gold/stocks/wolf.json b/src/data/gold/stocks/wolf.json index 029ae7a..c717798 100644 --- a/src/data/gold/stocks/wolf.json +++ b/src/data/gold/stocks/wolf.json @@ -1,6 +1,6 @@ { "key": "wolf", - "name": "wolf", + "name": "Wolf", "stride": 11, "common_traits": [ "Crushing Jaws", diff --git a/src/public/js/burning-service.js b/src/public/js/burning-service.js index 7227394..066f0ed 100644 --- a/src/public/js/burning-service.js +++ b/src/public/js/burning-service.js @@ -9,7 +9,7 @@ function Settings() { /**** Class AppropriateWeaponsService (Angular Service) ****/ function AppropriateWeaponsService($modal, $http) { - // This class will store a hash which maps lifepath names to a list of + // This class will store a hash which maps lifepath names to a list of // weapons that are appropriate for that lifepath. this.appropriateWeapons = {}; @@ -167,7 +167,7 @@ function WeaponOfChoiceService($modal, $http) { } return has; } - + this.selectWeaponOfChoice = function (displayLp, onSelect){ if( this.hasWeaponOfChoice(displayLp) ){ this.selectWeaponOfChoiceByModal(displayLp.name, function(selected){ @@ -250,67 +250,107 @@ function BurningDataService($http) { roots: [root1, root2, ...] skill_name: roots: [root1, root2, ...] - + */ this.skills = {}; /* JSON data structure representing all available traits */ this.traits = {}; - + /* JSON data structure representing all available resources (gear/property) */ this.resources = {}; // A hash of StartingStatPoints objects keyed by stock. this.startingStatPts = {}; - // this.dataSetsLoaded = 0; - // // Total data sets: - // // lifepaths: 7 (man, dwarf, elf, orc, roden, wolf, troll) - // // stat points: 7 (man, dwarf, elf, orc, roden, wolf, troll) - // // skills - // // traits - // // resources: 7 (man, dwarf, elf, orc, roden, wolf. troll) - // // TOTAL: 23 - // this.totalDataSets = 23; - // this.onAllDatasetsLoaded = null; - this.registerOnAllDatasetsLoaded = function(callback){ - if ( this.dataSetsLoaded >= this.totalDataSets ){ + this.events = { + stocksLoaded: { triggered : false, callbacks : [] }, + traitsLoaded: { triggered : false, callbacks : [] }, + skillsLoaded: { triggered : false, callbacks : [] }, + } + + this.registerEvent = function(eventName, callback){ + if(this.events[eventName].triggered) + callback(); + this.events[eventName].callbacks.push(callback); + } + this.triggerEvent = function(eventName) { + myself.events[eventName].triggered = true; + for(var callback of myself.events[eventName].callbacks) { callback(); } - this.onAllDatasetsLoaded = callback; } - // this.datasetLoaded = function(){ - // this.dataSetsLoaded += 1; - // if ( this.onAllDatasetsLoaded && (this.dataSetsLoaded >= this.totalDataSets) ){ - // this.onAllDatasetsLoaded(); - // } - // if ( this.dataSetsLoaded > this.totalDataSets){ - // console.log("Error: the totalDataSets setting in BurningDataService is too low! This will cause wierd errors. Please adjust it"); - // } - // } + this.stockEvents = {}; + this.defineStockEvent = function(stockName, eventName) { + if(!(stockName in myself.stockEvents)) myself.stockEvents[stockName] = {}; + myself.stockEvents[stockName][eventName] = { triggered : false, callbacks : [] } ; + } + this.registerStockEvent = function(stockName, eventName, callback){ + if(myself.stockEvents[stockName][eventName].triggered) + callback(); + myself.stockEvents[stockName][eventName].callbacks.push(callback); + } + this.triggerStockEvent = function(stockName, eventName) { + myself.stockEvents[stockName][eventName].triggered = true; + for(var callback of myself.stockEvents[stockName][eventName].callbacks) { + callback(); + } + } + + var myself = this; var stocks; + + // Log events + this.registerEvent("stocksLoaded", function() { + console.log("Stocks fetched from server: ", Object.keys(myself.stocks)) + // DEBUG: + // console.log("Stocks fetched from server: ", myself.stocks) + }); + this.registerEvent("skillsLoaded", function() { + console.log("Loaded " + Object.keys(myself.skills).length + " skills."); + // DEBUG: + // console.log("Loaded skills: ", myself.skills); + }); + this.registerEvent("traitsLoaded", function() { + console.log("Loaded " + Object.keys(myself.traits).length + " traits."); + // DEBUG: + // console.log("Loaded traits: ", myself.traits); + }); + $http.get("/stocks", {'timeout': 3000}). success(function(data,status,headers,config){ - console.log(data); + // DEBUG: + // console.log(data); myself.stocks = data; stocks = Object.keys(data); - for (var i = 0; i < stocks.length; i++) { - loadLifepathsForStock(stocks[i]); - loadStartingStatPtsForStock(stocks[i]); - loadResourcesForStock(stocks[i]); + for (var stock of stocks) { + myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); + myself.defineStockEvent(stock, "lifepathsLoaded"); + myself.defineStockEvent(stock, "resourcesLoaded"); + + // Log events + myself.registerStockEvent(stock, "lifepathsLoaded", function () { + // console.log("Loaded " + Object.keys(myself.lifepaths[stock]).length + " lifepaths for " + stock + "."); + // DEBUG: + console.log("Loaded " + stock, myself.lifepaths[stock]); + }); + myself.registerStockEvent(stock, "resourcesLoaded", function () { + // console.log("Loaded " + Object.keys(myself.resources[stock]).length + " resources for " + stock + "."); + // DEBUG: + console.log("Loaded " + stock, myself.resources[stock]); + }); } - console.log("Stocks fetched from server: " + stocks) - myself.onAllDatasetsLoaded() + myself.triggerEvent("stocksLoaded"); }). error(function(data,status,headers,config){ console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data); }); /* Load lifepaths from server */ - var loadLifepathsForStock = function(stock){ + this.loadLifepathsForStock = function(stock){ if( ! isValidStock(stock) ){ console.log("Loading lifepaths failed: asked to load lifepaths for invalid stock " + stock); return @@ -319,26 +359,16 @@ function BurningDataService($http) { $http.get("/lifepaths/" + stock, {'timeout': 3000} ). success(function(data,status,headers,config){ myself.lifepaths[stock] = data; - // myself.datasetLoaded(); - console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths).length + " settings"); + myself.triggerStockEvent(stock, "lifepathsLoaded"); + console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings"); }). error(function(data,status,headers,config){ - // myself.datasetLoaded(); console.log("Error: Getting "+stock+" lifepaths from server failed: HTTP code " + status + ": " + data); }); } - /* Load starting stat points table from server */ - var loadStartingStatPtsForStock = function(stock){ - if( ! isValidStock(stock) ){ - console.log("Loading starting stat points failed: asked to load pts for invalid stock " + stock); - return - } - myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); - } - /* Load resources from server */ - var loadResourcesForStock = function(stock){ + this.loadResourcesForStock = function(stock){ if( ! isValidStock(stock) ){ console.log("Loading resources failed: asked to load for invalid stock " + stock); return @@ -347,11 +377,10 @@ function BurningDataService($http) { $http.get("/resources/" + stock, {'timeout': 3000} ). success(function(data,status,headers,config){ myself.resources[stock] = data; - // myself.datasetLoaded(); + myself.triggerStockEvent(stock, "resourcesLoaded"); console.log("Loaded "+stock+" resources. "); }). error(function(data,status,headers,config){ - // myself.datasetLoaded(); console.log("Error: Getting "+stock+" stat points from server failed: HTTP code " + status + ": " + data); }); } @@ -361,11 +390,9 @@ function BurningDataService($http) { $http.get("/skills", {'timeout': 3000} ). success(function(data,status,headers,config){ myself.skills = data; - // myself.datasetLoaded(); - console.log("Loaded skills. "); + myself.triggerEvent("skillsLoaded"); }). error(function(data,status,headers,config){ - // myself.datasetLoaded(); console.log("Error: Getting skills from server failed: HTTP code " + status + ": " + data); }); @@ -373,13 +400,22 @@ function BurningDataService($http) { $http.get("/traits", {'timeout': 3000} ). success(function(data,status,headers,config){ myself.traits = data; - // myself.datasetLoaded(); - console.log("Loaded traits. "); + myself.triggerEvent("traitsLoaded"); }). error(function(data,status,headers,config){ - // myself.datasetLoaded(); console.log("Error: Getting traits from server failed: HTTP code " + status + ": " + data); }); + // DEBUG: + // setTimeout(() => testStockLoading(myself), (2 * 1000)); +} + +function testStockLoading(dataService) { + console.log(dataService); + for (var stock in dataService.stocks) { + console.log(stock); + dataService.loadLifepathsForStock(stock); + dataService.loadResourcesForStock(stock); + } } /**** End BurningDataService ****/ diff --git a/src/public/js/burning.js b/src/public/js/burning.js index d59402c..f4d8ed2 100644 --- a/src/public/js/burning.js +++ b/src/public/js/burning.js @@ -160,7 +160,7 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo } }; // Setting names for use in the Add Lifepath section - $scope.settingNames = ["Loading..."] + $scope.settingNames = []; $scope.currentSettingLifepathNames = []; // The currently selected lifepath $scope.currentSettingLifepath = "Loading..."; @@ -207,10 +207,10 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo $scope.name = ""; // Character stock. One of man, dwarf, orc, elf - if ( ! isValidStock(stock) ){ - console.log("Invalid stock '"+stock+"' passed to BurningCtrl.initialize. Defaulting to man"); - stock = "man"; - } + // if ( ! isValidStock(stock) ){ + // console.log("Invalid stock '"+stock+"' passed to BurningCtrl.initialize. Defaulting to man"); + // stock = "man"; + // } $scope.stock = stock; @@ -302,7 +302,7 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo } - $scope.initialize("man"); + $scope.initialize(); if ( characterStorage.currentCharacter ){ //console.log("Loading current character"); @@ -364,20 +364,29 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo } $scope.onStockChange = function(){ + if(!$scope.stock) return; var oldName = $scope.name; + burningData.registerStockEvent($scope.stock, "lifepathsLoaded", function () { + // Make a blank character sheet + $scope.initialize($scope.stock); - // Make a blank character sheet - $scope.initialize($scope.stock); + if ( oldName.length == 0 ){ + $scope.generateName(); + } else { + $scope.name = oldName; + } + calculateSettingNames($scope, burningData); + calculateCurrentSettingLifepathNames($scope, burningData); + calculateSpecialTraitsForDisplay($scope, burningData); + }); + if(!burningData.lifepaths[$scope.stock]) { + burningData.loadLifepathsForStock($scope.stock); + } - if ( oldName.length == 0 ){ - $scope.generateName(); - } else { - $scope.name = oldName; + if(!burningData.resources[$scope.stock]) { + burningData.loadResourcesForStock($scope.stock); } - calculateSettingNames($scope, burningData); - calculateCurrentSettingLifepathNames($scope, burningData); - calculateSpecialTraitsForDisplay($scope, burningData); } $scope.onSettingChange = function(){ @@ -403,8 +412,13 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo calculateUnspentSkillPoints($scope); } - burningData.registerOnAllDatasetsLoaded(function(){ - onLifepathsLoad($scope, burningData); + // burningData.registerOnAllDatasetsLoaded(function(){ + // onLifepathsLoad($scope, burningData); + // }); + + burningData.registerEvent("stocksLoaded", function() { + $scope.stocks = [{ name: "Select a stock" }] + $scope.stocks = $scope.stocks.concat(Object.values(burningData.stocks)); }); $scope.$on('$locationChangeStart', function(event, nextUrl, currentUrl) { diff --git a/src/views/partials/main.erb b/src/views/partials/main.erb index 6da38d0..11fa49d 100644 --- a/src/views/partials/main.erb +++ b/src/views/partials/main.erb @@ -94,14 +94,7 @@
-