Line data Source code
1 : /** 2 : * This class provides a cache to all resource names and properties defined by the JSON files. 3 : */ 4 : function Resources() 5 : { 6 1 : this.resourceData = []; 7 1 : this.resourceDataObj = {}; 8 1 : this.resourceCodes = []; 9 1 : this.resourceNames = {}; 10 1 : this.resourceCodesByProperty = {}; 11 : 12 1 : for (let filename of Engine.ListDirectoryFiles("simulation/data/resources/", "*.json", false)) 13 : { 14 2 : let data = Engine.ReadJSONFile(filename); 15 2 : if (!data) 16 0 : continue; 17 : 18 2 : if (data.code != data.code.toLowerCase()) 19 0 : warn("Resource codes should use lower case: " + data.code); 20 : 21 2 : this.resourceData.push(data); 22 2 : this.resourceDataObj[data.code] = data; 23 2 : this.resourceCodes.push(data.code); 24 2 : this.resourceNames[data.code] = data.name; 25 2 : for (let subres in data.subtypes) 26 4 : this.resourceNames[subres] = data.subtypes[subres]; 27 : 28 2 : for (let property in data.properties) 29 : { 30 3 : if (!this.resourceCodesByProperty[data.properties[property]]) 31 2 : this.resourceCodesByProperty[data.properties[property]] = []; 32 3 : this.resourceCodesByProperty[data.properties[property]].push(data.code); 33 : } 34 : } 35 : 36 : // Sort arrays by specified order 37 3 : let resDataSort = (a, b) => a.order < b.order ? -1 : a.order > b.order ? +1 : 0; 38 2 : let resSort = (a, b) => resDataSort( 39 2 : this.resourceData.find(resource => resource.code == a), 40 4 : this.resourceData.find(resource => resource.code == b) 41 : ); 42 : 43 1 : this.resourceData.sort(resDataSort); 44 1 : this.resourceCodes.sort(resSort); 45 1 : for (let property in this.resourceCodesByProperty) 46 2 : this.resourceCodesByProperty[property].sort(resSort); 47 : 48 1 : deepfreeze(this.resourceData); 49 1 : deepfreeze(this.resourceDataObj); 50 1 : deepfreeze(this.resourceCodes); 51 1 : deepfreeze(this.resourceNames); 52 1 : deepfreeze(this.resourceCodesByProperty); 53 : } 54 : 55 : /** 56 : * Returns the objects defined in the JSON files for all available resources, 57 : * ordered as defined in these files. 58 : */ 59 72 : Resources.prototype.GetResources = function() 60 : { 61 2 : return this.resourceData; 62 : }; 63 : 64 : /** 65 : * Returns the object defined in the JSON file for the given resource. 66 : */ 67 72 : Resources.prototype.GetResource = function(type) 68 : { 69 1 : return this.resourceDataObj[type]; 70 : }; 71 : 72 : /** 73 : * Returns an array containing all resource codes ordered as defined in the resource files. 74 : * @return {string[]} - Data of the form [ "food", "wood", ... ]. 75 : */ 76 72 : Resources.prototype.GetCodes = function() 77 : { 78 1 : return this.resourceCodes; 79 : }; 80 : 81 : /** 82 : * Returns an array containing all barterable resource codes ordered as defined in the resource files. 83 : * @return {string[]} - Data of the form [ "food", "wood", ... ]. 84 : */ 85 72 : Resources.prototype.GetBarterableCodes = function() 86 : { 87 1 : return this.resourceCodesByProperty.barterable || []; 88 : }; 89 : 90 : /** 91 : * Returns an array containing all tradable resource codes ordered as defined in the resource files. 92 : * @return {string[]} - Data of the form [ "food", "wood", ... ]. 93 : */ 94 72 : Resources.prototype.GetTradableCodes = function() 95 : { 96 1 : return this.resourceCodesByProperty.tradable || []; 97 : }; 98 : 99 : /** 100 : * Returns an array containing all tributable resource codes ordered as defined in the resource files. 101 : * @return {string[]} - Data of the form [ "food", "wood", ... ]. 102 : */ 103 72 : Resources.prototype.GetTributableCodes = function() 104 : { 105 1 : return this.resourceCodesByProperty.tributable || []; 106 : }; 107 : 108 : /** 109 : * Returns an object mapping resource codes to translatable resource names. Includes subtypes. 110 : * For example { "food": "Food", "fish": "Fish", "fruit": "Fruit", "metal": "Metal", ... } 111 : */ 112 72 : Resources.prototype.GetNames = function() 113 : { 114 1 : return this.resourceNames; 115 : };