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. 138
      src/public/js/burning-service.js
  8. 34
      src/public/js/burning.js
  9. 9
      src/views/partials/main.erb

@ -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']]

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

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

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

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

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

@ -263,54 +263,94 @@ function BurningDataService($http) {
// 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 ****/

@ -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,8 +364,9 @@ 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);
@ -374,10 +375,18 @@ function BurningCtrl($scope, $http, $modal, $timeout, settings, appropriateWeapo
} else {
$scope.name = oldName;
}
calculateSettingNames($scope, burningData);
calculateCurrentSettingLifepathNames($scope, burningData);
calculateSpecialTraitsForDisplay($scope, burningData);
});
if(!burningData.lifepaths[$scope.stock]) {
burningData.loadLifepathsForStock($scope.stock);
}
if(!burningData.resources[$scope.stock]) {
burningData.loadResourcesForStock($scope.stock);
}
}
$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) {

@ -94,14 +94,7 @@
</strong>
</div>
<div class='col-md-2'>
<select class='form-control' ng-change='onStockChange()' ng-model='stock'>
<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 class='form-control' ng-change='onStockChange()' ng-model='stock' ng-options='s.key as s.name for s in stocks'>
</select>
</div>
<div class='col-md-1'>

Loading…
Cancel
Save