On demand data loading

pull/8/head
Daniel Asher Resnick 2 years ago
parent 302f758bf0
commit fc9edd2b48
  1. 8
      src/app.rb
  2. 2
      src/data/gold/stocks/elf.json
  3. 2
      src/data/gold/stocks/man.json
  4. 2
      src/data/gold/stocks/orc.json
  5. 2
      src/data/gold/stocks/roden.json
  6. 2
      src/data/gold/stocks/wolf.json
  7. 146
      src/public/js/burning-service.js
  8. 48
      src/public/js/burning.js
  9. 9
      src/views/partials/main.erb

@ -61,14 +61,6 @@ get '/lifepaths/:stock' do
end end
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 get '/resources/:stock' do
if DATA[:stocks].keys.include? params['stock'] if DATA[:stocks].keys.include? params['stock']
json DATA[:resources][params['stock']] json DATA[:resources][params['stock']]

@ -1,6 +1,6 @@
{ {
"key": "elf", "key": "elf",
"name": "elf", "name": "Elf",
"stride": 8, "stride": 8,
"common_traits": [ "common_traits": [
"Born Under The Silver Stars", "Born Under The Silver Stars",

@ -1,6 +1,6 @@
{ {
"key": "man", "key": "man",
"name": "man", "name": "Man",
"stride": 7, "stride": 7,
"common_traits": [ "common_traits": [
], ],

@ -1,6 +1,6 @@
{ {
"key": "orc", "key": "orc",
"name": "orc", "name": "Orc",
"stride": 7, "stride": 7,
"common_traits": [ "common_traits": [
"Cannibal", "Cannibal",

@ -1,6 +1,6 @@
{ {
"key": "roden", "key": "roden",
"name": "roden", "name": "Roden",
"stride": 8, "stride": 8,
"common_traits": [ "common_traits": [
"Aecer's Likeness", "Aecer's Likeness",

@ -1,6 +1,6 @@
{ {
"key": "wolf", "key": "wolf",
"name": "wolf", "name": "Wolf",
"stride": 11, "stride": 11,
"common_traits": [ "common_traits": [
"Crushing Jaws", "Crushing Jaws",

@ -9,7 +9,7 @@ function Settings() {
/**** Class AppropriateWeaponsService (Angular Service) ****/ /**** Class AppropriateWeaponsService (Angular Service) ****/
function AppropriateWeaponsService($modal, $http) { 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. // weapons that are appropriate for that lifepath.
this.appropriateWeapons = {}; this.appropriateWeapons = {};
@ -167,7 +167,7 @@ function WeaponOfChoiceService($modal, $http) {
} }
return has; return has;
} }
this.selectWeaponOfChoice = function (displayLp, onSelect){ this.selectWeaponOfChoice = function (displayLp, onSelect){
if( this.hasWeaponOfChoice(displayLp) ){ if( this.hasWeaponOfChoice(displayLp) ){
this.selectWeaponOfChoiceByModal(displayLp.name, function(selected){ this.selectWeaponOfChoiceByModal(displayLp.name, function(selected){
@ -250,67 +250,107 @@ function BurningDataService($http) {
roots: [root1, root2, ...] roots: [root1, root2, ...]
skill_name: skill_name:
roots: [root1, root2, ...] roots: [root1, root2, ...]
*/ */
this.skills = {}; this.skills = {};
/* JSON data structure representing all available traits */ /* JSON data structure representing all available traits */
this.traits = {}; this.traits = {};
/* JSON data structure representing all available resources (gear/property) */ /* JSON data structure representing all available resources (gear/property) */
this.resources = {}; this.resources = {};
// A hash of StartingStatPoints objects keyed by stock. // A hash of StartingStatPoints objects keyed by stock.
this.startingStatPts = {}; this.startingStatPts = {};
// this.dataSetsLoaded = 0; this.events = {
// // Total data sets: stocksLoaded: { triggered : false, callbacks : [] },
// // lifepaths: 7 (man, dwarf, elf, orc, roden, wolf, troll) traitsLoaded: { triggered : false, callbacks : [] },
// // stat points: 7 (man, dwarf, elf, orc, roden, wolf, troll) skillsLoaded: { triggered : false, callbacks : [] },
// // skills }
// // traits
// // resources: 7 (man, dwarf, elf, orc, roden, wolf. troll) this.registerEvent = function(eventName, callback){
// // TOTAL: 23 if(this.events[eventName].triggered)
// this.totalDataSets = 23; callback();
// this.onAllDatasetsLoaded = null; this.events[eventName].callbacks.push(callback);
this.registerOnAllDatasetsLoaded = function(callback){ }
if ( this.dataSetsLoaded >= this.totalDataSets ){ this.triggerEvent = function(eventName) {
myself.events[eventName].triggered = true;
for(var callback of myself.events[eventName].callbacks) {
callback(); callback();
} }
this.onAllDatasetsLoaded = callback;
} }
// this.datasetLoaded = function(){ this.stockEvents = {};
// this.dataSetsLoaded += 1; this.defineStockEvent = function(stockName, eventName) {
// if ( this.onAllDatasetsLoaded && (this.dataSetsLoaded >= this.totalDataSets) ){ if(!(stockName in myself.stockEvents)) myself.stockEvents[stockName] = {};
// this.onAllDatasetsLoaded(); myself.stockEvents[stockName][eventName] = { triggered : false, callbacks : [] } ;
// } }
// if ( this.dataSetsLoaded > this.totalDataSets){ this.registerStockEvent = function(stockName, eventName, callback){
// console.log("Error: the totalDataSets setting in BurningDataService is too low! This will cause wierd errors. Please adjust it"); 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 myself = this;
var stocks; 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}). $http.get("/stocks", {'timeout': 3000}).
success(function(data,status,headers,config){ success(function(data,status,headers,config){
console.log(data); // DEBUG:
// console.log(data);
myself.stocks = data; myself.stocks = data;
stocks = Object.keys(data); stocks = Object.keys(data);
for (var i = 0; i < stocks.length; i++) { for (var stock of stocks) {
loadLifepathsForStock(stocks[i]); myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats);
loadStartingStatPtsForStock(stocks[i]); myself.defineStockEvent(stock, "lifepathsLoaded");
loadResourcesForStock(stocks[i]); 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.triggerEvent("stocksLoaded");
myself.onAllDatasetsLoaded()
}). }).
error(function(data,status,headers,config){ error(function(data,status,headers,config){
console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data); console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data);
}); });
/* Load lifepaths from server */ /* Load lifepaths from server */
var loadLifepathsForStock = function(stock){ this.loadLifepathsForStock = function(stock){
if( ! isValidStock(stock) ){ if( ! isValidStock(stock) ){
console.log("Loading lifepaths failed: asked to load lifepaths for invalid stock " + stock); console.log("Loading lifepaths failed: asked to load lifepaths for invalid stock " + stock);
return return
@ -319,26 +359,16 @@ function BurningDataService($http) {
$http.get("/lifepaths/" + stock, {'timeout': 3000} ). $http.get("/lifepaths/" + stock, {'timeout': 3000} ).
success(function(data,status,headers,config){ success(function(data,status,headers,config){
myself.lifepaths[stock] = data; myself.lifepaths[stock] = data;
// myself.datasetLoaded(); myself.triggerStockEvent(stock, "lifepathsLoaded");
console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths).length + " settings"); console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings");
}). }).
error(function(data,status,headers,config){ error(function(data,status,headers,config){
// myself.datasetLoaded();
console.log("Error: Getting "+stock+" lifepaths from server failed: HTTP code " + status + ": " + data); 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 */ /* Load resources from server */
var loadResourcesForStock = function(stock){ this.loadResourcesForStock = function(stock){
if( ! isValidStock(stock) ){ if( ! isValidStock(stock) ){
console.log("Loading resources failed: asked to load for invalid stock " + stock); console.log("Loading resources failed: asked to load for invalid stock " + stock);
return return
@ -347,11 +377,10 @@ function BurningDataService($http) {
$http.get("/resources/" + stock, {'timeout': 3000} ). $http.get("/resources/" + stock, {'timeout': 3000} ).
success(function(data,status,headers,config){ success(function(data,status,headers,config){
myself.resources[stock] = data; myself.resources[stock] = data;
// myself.datasetLoaded(); myself.triggerStockEvent(stock, "resourcesLoaded");
console.log("Loaded "+stock+" resources. "); console.log("Loaded "+stock+" resources. ");
}). }).
error(function(data,status,headers,config){ error(function(data,status,headers,config){
// myself.datasetLoaded();
console.log("Error: Getting "+stock+" stat points from server failed: HTTP code " + status + ": " + data); 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} ). $http.get("/skills", {'timeout': 3000} ).
success(function(data,status,headers,config){ success(function(data,status,headers,config){
myself.skills = data; myself.skills = data;
// myself.datasetLoaded(); myself.triggerEvent("skillsLoaded");
console.log("Loaded skills. ");
}). }).
error(function(data,status,headers,config){ error(function(data,status,headers,config){
// myself.datasetLoaded();
console.log("Error: Getting skills from server failed: HTTP code " + status + ": " + data); console.log("Error: Getting skills from server failed: HTTP code " + status + ": " + data);
}); });
@ -373,13 +400,22 @@ function BurningDataService($http) {
$http.get("/traits", {'timeout': 3000} ). $http.get("/traits", {'timeout': 3000} ).
success(function(data,status,headers,config){ success(function(data,status,headers,config){
myself.traits = data; myself.traits = data;
// myself.datasetLoaded(); myself.triggerEvent("traitsLoaded");
console.log("Loaded traits. ");
}). }).
error(function(data,status,headers,config){ error(function(data,status,headers,config){
// myself.datasetLoaded();
console.log("Error: Getting traits from server failed: HTTP code " + status + ": " + data); 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 ****/ /**** End BurningDataService ****/

@ -160,7 +160,7 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
} }
}; };
// Setting names for use in the Add Lifepath section // Setting names for use in the Add Lifepath section
$scope.settingNames = ["Loading..."] $scope.settingNames = [];
$scope.currentSettingLifepathNames = []; $scope.currentSettingLifepathNames = [];
// The currently selected lifepath // The currently selected lifepath
$scope.currentSettingLifepath = "Loading..."; $scope.currentSettingLifepath = "Loading...";
@ -207,10 +207,10 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
$scope.name = ""; $scope.name = "";
// Character stock. One of man, dwarf, orc, elf // Character stock. One of man, dwarf, orc, elf
if ( ! isValidStock(stock) ){ // if ( ! isValidStock(stock) ){
console.log("Invalid stock '"+stock+"' passed to BurningCtrl.initialize. Defaulting to man"); // console.log("Invalid stock '"+stock+"' passed to BurningCtrl.initialize. Defaulting to man");
stock = "man"; // stock = "man";
} // }
$scope.stock = stock; $scope.stock = stock;
@ -302,7 +302,7 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
} }
$scope.initialize("man"); $scope.initialize();
if ( characterStorage.currentCharacter ){ if ( characterStorage.currentCharacter ){
//console.log("Loading current character"); //console.log("Loading current character");
@ -364,20 +364,29 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
} }
$scope.onStockChange = function(){ $scope.onStockChange = function(){
if(!$scope.stock) return;
var oldName = $scope.name; var oldName = $scope.name;
burningData.registerStockEvent($scope.stock, "lifepathsLoaded", function () {
// Make a blank character sheet
$scope.initialize($scope.stock);
// Make a blank character sheet if ( oldName.length == 0 ){
$scope.initialize($scope.stock); $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 ){ if(!burningData.resources[$scope.stock]) {
$scope.generateName(); burningData.loadResourcesForStock($scope.stock);
} else {
$scope.name = oldName;
} }
calculateSettingNames($scope, burningData);
calculateCurrentSettingLifepathNames($scope, burningData);
calculateSpecialTraitsForDisplay($scope, burningData);
} }
$scope.onSettingChange = function(){ $scope.onSettingChange = function(){
@ -403,8 +412,13 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
calculateUnspentSkillPoints($scope); calculateUnspentSkillPoints($scope);
} }
burningData.registerOnAllDatasetsLoaded(function(){ // burningData.registerOnAllDatasetsLoaded(function(){
onLifepathsLoad($scope, burningData); // 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) { $scope.$on('$locationChangeStart', function(event, nextUrl, currentUrl) {

@ -94,14 +94,7 @@
</strong> </strong>
</div> </div>
<div class='col-md-2'> <div class='col-md-2'>
<select class='form-control' ng-change='onStockChange()' ng-model='stock'> <select class='form-control' ng-change='onStockChange()' ng-model='stock' ng-options='s.key as s.name for s in stocks'>
<option value='man'>Man</option>
<option value='dwarf'>Dwarf</option>
<option value='elf'>Elf</option>
<option value='orc'>Orc</option>
<option value='roden'>Roden</option>
<option value='wolf'>Great Wolf</option>
<option value='troll'>Troll</option>
</select> </select>
</div> </div>
<div class='col-md-1'> <div class='col-md-1'>

Loading…
Cancel
Save