DataFetch: Difference between revisions
From Project Rebearth
FIX okay??!??! :/ |
No edit summary |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
-- Written with ❤️ by fshark | -- Written with ❤️ mostly by fshark | ||
local NOT_APPLICABLE = "N/A" | local NOT_APPLICABLE = "N/A" | ||
| Line 9: | Line 9: | ||
local abbreviations = { | local abbreviations = { | ||
wood = "w", | wood = "w", | ||
stone = "s", | stone = "s", | ||
earth = "e", | earth = "e", | ||
arctic = "a", | arctic = "a", | ||
empty = "e", | empty = "e", | ||
potato = "p", | potato = "p", | ||
| Line 24: | Line 24: | ||
} | } | ||
local function getAbbreviation(biomeName) | local function getAbbreviation(biomeName) | ||
return abbreviations[string.lower(biomeName)] or biomeName | return abbreviations[string.lower(biomeName)] or biomeName | ||
end | end | ||
local function getValidArg(...) | local function getValidArg(...) | ||
for i = 1, select("#", ...) do | for i = 1, select("#", ...) do | ||
| Line 37: | Line 35: | ||
end | end | ||
end | end | ||
return "" | return "" | ||
end | end | ||
| Line 53: | Line 51: | ||
end | end | ||
return NOT_APPLICABLE | return NOT_APPLICABLE | ||
end | |||
local function colorize(value, mode) | |||
local num = tonumber(value) | |||
if not num then | |||
return value | |||
end | |||
local isBuff, isNerf | |||
if mode == "mult" then | |||
isBuff = num > 1 | |||
isNerf = num < 1 | |||
elseif mode == "imult" then | |||
isBuff = num < 1 | |||
isNerf = num > 1 | |||
else | |||
isBuff = num > 0 | |||
isNerf = num < 0 | |||
end | |||
if isBuff then | |||
local display = (mode == nil or mode == "" or mode == "rate") and ("+" .. value) or value | |||
return '<span class="rb-buff">' .. display .. '</span>' | |||
elseif isNerf then | |||
return '<span class="rb-nerf">' .. value .. '</span>' | |||
else | |||
return value | |||
end | |||
end | end | ||
return { | return { | ||
GetBuildingData = function(f) | GetBuildingData = function(f) | ||
local args = f.args | local args = f.args | ||
local section = getValidArg(args.section) | |||
local name = getValidArg(args.name) | local name = getValidArg(args.name) | ||
local buildingData = | |||
local source | |||
if section ~= "" then | |||
source = data[section] | |||
else | |||
source = data["buildings"] or data | |||
end | |||
if not source then | |||
return NOT_APPLICABLE | |||
end | |||
local buildingData = source[name] | |||
if not buildingData then | if not buildingData then | ||
| Line 71: | Line 112: | ||
local crop = getAbbreviation(getValidArg(args.crop)) | local crop = getAbbreviation(getValidArg(args.crop)) | ||
local result = valueFind(buildingData, targetData, resource, biome, crop) | |||
local color = getValidArg(args.color) | |||
if color == "yes" then | |||
local mode = getValidArg(args.mode) | |||
if mode == "" then mode = "rate" end | |||
result = colorize(result, mode) | |||
end | |||
return result | |||
end | end | ||
} | } | ||
Latest revision as of 16:00, 10 March 2026
Documentation for this module may be created at Module:DataFetch/doc
-- Written with ❤️ mostly by fshark
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 abbreviations = {
wood = "w",
stone = "s",
earth = "e",
arctic = "a",
empty = "e",
potato = "p",
grain = "g",
maize = "m",
sheep = "s",
goat = "b",
cow = "c",
}
local function getAbbreviation(biomeName)
return abbreviations[string.lower(biomeName)] or biomeName
end
local function getValidArg(...)
for i = 1, select("#", ...) do
local arg = select(i, ...)
if arg and arg ~= "" then
return string.lower(arg)
end
end
return ""
end
local function valueFind(t, ...)
for i = 1, select("#", ...) do
local k = select(i, ...)
local v = t[k]
if type(v) == "table" then
return valueFind(v, ...)
end
if v ~= nil then
return v
end
end
return NOT_APPLICABLE
end
local function colorize(value, mode)
local num = tonumber(value)
if not num then
return value
end
local isBuff, isNerf
if mode == "mult" then
isBuff = num > 1
isNerf = num < 1
elseif mode == "imult" then
isBuff = num < 1
isNerf = num > 1
else
isBuff = num > 0
isNerf = num < 0
end
if isBuff then
local display = (mode == nil or mode == "" or mode == "rate") and ("+" .. value) or value
return '<span class="rb-buff">' .. display .. '</span>'
elseif isNerf then
return '<span class="rb-nerf">' .. value .. '</span>'
else
return value
end
end
return {
GetBuildingData = function(f)
local args = f.args
local section = getValidArg(args.section)
local name = getValidArg(args.name)
local source
if section ~= "" then
source = data[section]
else
source = data["buildings"] or data
end
if not source then
return NOT_APPLICABLE
end
local buildingData = source[name]
if not buildingData then
return NOT_APPLICABLE
end
local targetData = getValidArg(args.data)
local biome = getAbbreviation(getValidArg(args.biome))
local resource = getValidArg(args.resource, args.res)
local crop = getAbbreviation(getValidArg(args.crop))
local result = valueFind(buildingData, targetData, resource, biome, crop)
local color = getValidArg(args.color)
if color == "yes" then
local mode = getValidArg(args.mode)
if mode == "" then mode = "rate" end
result = colorize(result, mode)
end
return result
end
}