Project Rebearth
Menu
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Search
What links here
Related changes
Page information
Recent changes
Help
Not logged in
Talk
Contributions
Create account
Log in
Actions
Module
Discussion
Edit
View history
Module
Editing
DataFetch Test
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
local NOT_APPLICABLE = "N/A" local title = mw.title.new("Module:DataFetch/data.json") local data = title and title.exists and mw.text.jsonDecode(title:getContent()) assert(data, "Data failed to load. Please check \"Module:DataFetch/data.json\" exists.") local biomeKeys = { wood = "w", stone = "s", earth = "e", arctic = "a" } -- if the biome argument is the name of the biome rather than its letter, return the corresponding letter local function getBiomeKey(biomeName) return biomeKeys[string.lower(biomeName)] or biomeName end -- returns first argument that is not an empty string local function getValidArg(...) for i = 1, select("#", ...) do local arg = select(i, ...) if arg and arg ~= "" then return arg end end end local function valueFindRecursive(t, ...) for i = 1, select("#", ...) do local k = select(i, ...) local v = t[k] if type(v) == "table" then return valueFindRecursive(v, ...) end if v ~= nil then return v end end return NOT_APPLICABLE end return { -- Universal getter for any property, for use with Template:BuildingData GetBuildingData = function(f) local args = f.args local targetData = getValidArg(args.data) local biome = getBiomeKey(getValidArg(args.biome)) local resource = getValidArg(args.resource, args.res) local name = getValidArg(args.name) local buildingData = data[name] if not buildingData then return NOT_APPLICABLE end -- if targetData == "cost" then -- local costData = buildingData.cost -- if not costData then -- return NOT_APPLICABLE -- end -- local biomeValues = costData[getBiomeKey(biome)] -- return biomeValues[resource] or NOT_APPLICABLE -- elseif targetData == "rate" then -- local rateData = buildingData.rate -- if not rateData then -- return NOT_APPLICABLE -- end -- return rateData[resource] or NOT_APPLICABLE -- elseif targetData == "balance" then -- local rateData = buildingData.balance -- if not rateData then -- return NOT_APPLICABLE -- end -- return rateData[resource] or NOT_APPLICABLE -- end -- attempt to retrieve other data if none has been returned yet return valueFindRecursive(buildingData, targetData, resource, biome) end }
Summary:
Please note that all contributions to Project Rebearth may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Project Rebearth:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Page included on this page:
Module:DataFetch Test/doc
(
edit
)