Use Fetch API and thenify BurningData

pull/8/head
Daniel Asher Resnick 2 years ago
parent 74ed09d830
commit a98115c647
  1. 116
      src/public/js/burning-service.js

@ -1,4 +1,4 @@
var DEBUG = true;
/**** Class Settings (Angular Service) ****/ /**** Class Settings (Angular Service) ****/
function Settings() { function Settings() {
this.enforceLifepathReqts = true; this.enforceLifepathReqts = true;
@ -218,13 +218,14 @@ function CharacterStorageService($http) {
/* Load character names from server */ /* Load character names from server */
this.loadCharacterNames = function(){ this.loadCharacterNames = function(){
$http.get("/list_chars/user1", {'timeout': 3000} ). fetch("/list_chars/user1")
success(function(data,status,headers,config){ .then((response) => response.json())
.then((data) => {
myself.characterIdAndNames = data; myself.characterIdAndNames = data;
console.log("Loaded saved character names"); console.log("Loaded saved character names");
}). })
error(function(data,status,headers,config){ .catch((error) => {
console.log("Error: Loading saved character names from server failed: HTTP code " + status + ": " + data); console.log("Error: Loading saved character names from server failed: "+error);
}); });
} }
@ -305,24 +306,25 @@ function BurningDataService($http) {
// Log events // Log events
this.registerEvent("stocksLoaded", function() { this.registerEvent("stocksLoaded", function() {
console.log("Stocks fetched from server: ", Object.keys(myself.stocks)) console.log("Stocks fetched from server: ", Object.keys(myself.stocks))
// DEBUG: if(DEBUG)
// console.log("Stocks fetched from server: ", myself.stocks) console.log("Stocks fetched from server: ", myself.stocks)
}); });
this.registerEvent("skillsLoaded", function() { this.registerEvent("skillsLoaded", function() {
console.log("Loaded " + Object.keys(myself.skills).length + " skills."); console.log("Loaded " + Object.keys(myself.skills).length + " skills.");
// DEBUG: if(DEBUG)
// console.log("Loaded skills: ", myself.skills); console.log("Loaded skills: ", myself.skills);
}); });
this.registerEvent("traitsLoaded", function() { this.registerEvent("traitsLoaded", function() {
console.log("Loaded " + Object.keys(myself.traits).length + " traits."); console.log("Loaded " + Object.keys(myself.traits).length + " traits.");
// DEBUG: if(DEBUG)
// console.log("Loaded traits: ", myself.traits); console.log("Loaded traits: ", myself.traits);
}); });
$http.get("/stocks", {'timeout': 3000}). fetch("/stocks")
success(function(data,status,headers,config){ .then((response) => response.json())
// DEBUG: .then((data) => {
// console.log(data); if(DEBUG)
console.log("Loaded stock data: "+data);
myself.stocks = data; myself.stocks = data;
for (var stock of Object.keys(data)) { for (var stock of Object.keys(data)) {
myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats); myself.startingStatPts[stock] = new StartingStatPoints(myself.stocks[stock].starting_stats);
@ -330,67 +332,59 @@ function BurningDataService($http) {
myself.defineStockEvent(stock, "resourcesLoaded"); myself.defineStockEvent(stock, "resourcesLoaded");
} }
myself.triggerEvent("stocksLoaded"); myself.triggerEvent("stocksLoaded");
}). })
error(function(data,status,headers,config){ .catch((error) => {
console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data); console.log("Error: Getting stocks from server failed: "+error);
});
/* Load skills from server */
fetch("/skills")
.then((response) => response.json())
.then((data) => {
myself.skills = data;
myself.triggerEvent("skillsLoaded");
})
.catch((error) => {
console.log("Error: Getting skills from server failed: "+error);
});
/* Load traits from server */
fetch("/traits")
.then((response) => response.json())
.then((data) => {
myself.traits = data;
myself.triggerEvent("traitsLoaded");
})
.catch((error) => {
console.log("Error: Getting traits from server failed: "+error);
}); });
/* Load lifepaths from server */ /* Load lifepaths from server */
this.loadLifepathsForStock = function(stock){ this.loadLifepathsForStock = function(stock){
$http.get("/lifepaths/" + stock, {'timeout': 3000} ). fetch("/lifepaths/" + stock)
success(function(data,status,headers,config){ .then((response) => response.json())
.then((data) => {
myself.lifepaths[stock] = data; myself.lifepaths[stock] = data;
myself.triggerStockEvent(stock, "lifepathsLoaded"); myself.triggerStockEvent(stock, "lifepathsLoaded");
console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings"); console.log("Loaded "+stock+" lifepaths. " + Object.keys(myself.lifepaths[stock]).length + " settings");
}). })
error(function(data,status,headers,config){ .catch((error) => {
console.log("Error: Getting "+stock+" lifepaths from server failed: HTTP code " + status + ": " + data); console.log("Error: Getting "+stock+" lifepaths from server failed: "+error);
}); });
} }
/* Load resources from server */ /* Load resources from server */
this.loadResourcesForStock = function(stock){ this.loadResourcesForStock = function(stock){
$http.get("/resources/" + stock, {'timeout': 3000} ). fetch("/resources/" + stock)
success(function(data,status,headers,config){ .then((response) => response.json())
.then((data) => {
myself.resources[stock] = data; myself.resources[stock] = data;
myself.triggerStockEvent(stock, "resourcesLoaded"); myself.triggerStockEvent(stock, "resourcesLoaded");
console.log("Loaded "+stock+" resources. "); console.log("Loaded "+stock+" resources. ");
}). })
error(function(data,status,headers,config){ .catch((error) => {
console.log("Error: Getting "+stock+" stat points from server failed: HTTP code " + status + ": " + data); console.log("Error: Getting "+stock+" stat points from server failed: "+error);
});
}
/* Load skills from server */
$http.get("/skills", {'timeout': 3000} ).
success(function(data,status,headers,config){
myself.skills = data;
myself.triggerEvent("skillsLoaded");
}).
error(function(data,status,headers,config){
console.log("Error: Getting skills from server failed: HTTP code " + status + ": " + data);
});
/* Load traits from server */
$http.get("/traits", {'timeout': 3000} ).
success(function(data,status,headers,config){
myself.traits = data;
myself.triggerEvent("traitsLoaded");
}).
error(function(data,status,headers,config){
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 ****/

Loading…
Cancel
Save