From ce4c3722906927417d43ba0d4b46e95739a55936 Mon Sep 17 00:00:00 2001 From: Daniel Asher Resnick Date: Tue, 20 Dec 2022 01:22:07 -0600 Subject: [PATCH] Begin moving stock data to objects --- src/app.rb | 4 ++++ src/lib/data.rb | 4 +++- src/lib/stock.rb | 22 ++++++++++++++++++++++ src/public/js/burning-service.js | 20 ++++++++++++++------ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/lib/stock.rb diff --git a/src/app.rb b/src/app.rb index f540752..69be77b 100644 --- a/src/app.rb +++ b/src/app.rb @@ -49,6 +49,10 @@ get '/traits' do json DATA[:traits] end +get '/stock_objs' do + json DATA[:stock_objs] +end + get '/lifepaths/:stock' do if DATA[:stocks].include? params['stock'] json DATA[:lifepaths][params['stock']] diff --git a/src/lib/data.rb b/src/lib/data.rb index fc5abd3..713f783 100644 --- a/src/lib/data.rb +++ b/src/lib/data.rb @@ -2,6 +2,7 @@ require_relative 'data/gold' require_relative 'data/wizard' require_relative 'data/dark_elf' require_relative 'data/troll' +require_relative 'stock' module Charred class Data @@ -29,6 +30,7 @@ module Charred @data[:traits] = @data[:traits].sort.to_h @data[:skills] = @data[:skills].sort.to_h + @data[:stock_objs] = @data[:stocks].map { |s| Stock.new s, s } end def verbose_merge(to, from) @@ -54,4 +56,4 @@ module Charred end end end -end \ No newline at end of file +end diff --git a/src/lib/stock.rb b/src/lib/stock.rb new file mode 100644 index 0000000..77d623c --- /dev/null +++ b/src/lib/stock.rb @@ -0,0 +1,22 @@ +module Charred + # module Stock + class Stock + attr :key + attr :name + + def initialize(key, name) + @key = key + @name = name + end + def as_json(options = {}) + { + "key" => key, + "name" => name + } + end + def to_json(*a) + as_json.to_json(*a) + end + # end + end +end diff --git a/src/public/js/burning-service.js b/src/public/js/burning-service.js index 644ecc9..0bff544 100644 --- a/src/public/js/burning-service.js +++ b/src/public/js/burning-service.js @@ -290,7 +290,20 @@ function BurningDataService($http) { } } - var stocks = ["man", "dwarf", "elf", "orc", "roden", "wolf", "troll"]; + var stocks; + $http.get("/stock_objs", {'timeout': 3000}). + success(function(data,status,headers,config){ + stocks = data.map(s => s.key); + for (var i = 0; i < stocks.length; i++) { + loadLifepathsForStock(stocks[i]); + loadStartingStatPtsForStock(stocks[i]); + loadResourcesForStock(stocks[i]); + } + console.log("Stocks fetched from server: " + stocks) + }). + error(function(data,status,headers,config){ + console.log("Error: Getting stocks from server failed: HTTP code " + status + ": " + data); + }); var myself = this; /* Load lifepaths from server */ @@ -350,11 +363,6 @@ function BurningDataService($http) { }); } - for (var i = 0; i < stocks.length; i++) { - loadLifepathsForStock(stocks[i]); - loadStartingStatPtsForStock(stocks[i]); - loadResourcesForStock(stocks[i]); - } /* Load skills from server */ $http.get("/skills", {'timeout': 3000} ).