Editing Module:Utils
From Eco - English Wiki
Your changes will be displayed to readers once an authorized user accepts them. (help)
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 28: | Line 28: | ||
return icon | return icon | ||
else | else | ||
return ' | return 'NoImage.png' | ||
end | end | ||
end | end | ||
Line 48: | Line 44: | ||
end | end | ||
return set[item] ~= nil | return set[item] ~= nil | ||
end | |||
--- Build HTML code for an icon image. | |||
-- @param name string | |||
-- @param size string|nil One of: <code>"iconNormal"</code> (64px) or <code>"iconRecipe"</code> (44px). Default: <code>"iconNormal"</code> | |||
-- @param bg string|nil | |||
-- @param border string|nil | |||
-- @param too_expensive boolean|nil | |||
-- @author User:Avaren | |||
function p.build_icon(name, link, size, bg, border, too_expensive) | |||
local L = require('Module:Localization') -- local import | |||
if not size then | |||
size = 'iconNormal' | |||
end | |||
local icon_bg | |||
if bg then | |||
icon_bg = bg | |||
end | |||
local icon_border | |||
if border then | |||
icon_border = border | |||
end | |||
local item_data = mw.loadData('Module:ItemData') | |||
local item = item_data.items[name] | |||
local image | |||
if item then | |||
if item['group'] == L.t('Skill Books') then | |||
image = 'SkillBook.png' | |||
icon_bg = 'iconGold' | |||
elseif item['group'] == L.t('Skill Scrolls') then | |||
image = 'Skill Scroll' | |||
icon_bg = 'iconGold' | |||
-- Attempt to generate skill page | |||
elseif in_array(L.t('Basic Research'), item['tagGroups']) then | |||
image = string.sub(item['untranslated'], 1, -7):gsub('%s+', '') .. '_Icon.png' | |||
icon_bg = 'paperBasic' | |||
elseif in_array(L.t('Advanced Research'), item['tagGroups']) then | |||
image = string.sub(item['untranslated'], 1, -10):gsub('%s+', '') .. '_Icon.png' | |||
icon_bg = 'paperAdvanced' | |||
elseif in_array(L.t('Modern Research'), item['tagGroups']) then | |||
image = string.sub(item['untranslated'], 1, -8):gsub('%s+', '') .. '_Icon.png' | |||
icon_bg = 'paperModern' | |||
else | |||
image = p.checkImage(item['untranslated'], too_expensive) | |||
end | |||
if not icon_bg then | |||
if item['group'] == L.t('Food') then | |||
icon_bg = 'iconGreen' | |||
elseif item['carried'] == L.t('Hands') then | |||
icon_bg = 'iconBrown' | |||
end | |||
end | |||
else | |||
image = p.checkImage(name, too_expensive) | |||
end | |||
if not icon_bg then | |||
icon_bg = 'iconBlue' | |||
end | |||
if border then | |||
icon_border = border | |||
else | |||
icon_border = 'borderBlue' | |||
end | |||
if size == 'iconNormal' then | |||
icon_container = 'iconContainer' | |||
else | |||
icon_container = 'iconContainerSmall' | |||
end | |||
if not link then | |||
link = '' | |||
else | |||
link = '|link='..link | |||
end | |||
local file = '[[File:' .. image .. '|frameless|class=' .. size .. ' ' .. icon_bg .. link ..']]' | |||
return '<div class="' .. icon_container .. '"><div class="iconStack">' .. file .. '</div><div class="iconBorder ' .. icon_border .. '" style="position:absolute;"></div></div>' | |||
end | |||
--- Get HTML code for an icon image. | |||
-- @author User:Avaren | |||
function p.Icon(frame) | |||
args = p.normaliseArgs(frame) | |||
return p.build_icon(args.name, args.link, args.size, args.bg, args.border, args.too_expensive) | |||
end | end | ||
Line 152: | Line 237: | ||
--- Get all keys from <code>tbl</code> and sort them in alphabetical order. | --- Get all keys from <code>tbl</code> and sort them in alphabetical order. | ||
-- @param #table tbl Table to get keys from | -- @param #table tbl Table to get keys from | ||
-- @return #table Input table keys in alphabetical order. | -- @return #table Input table keys in alphabetical order, or <code>tbl</code> exactly if it has 0 or 1 items. | ||
-- @author User:Demian | -- @author User:Demian | ||
-- @see getSortedValues | -- @see getSortedValues | ||
function p.getSortedKeys(tbl) | function p.getSortedKeys(tbl) | ||
local sorted = {} | -- Check if there is anything to even sort. | ||
if p.tableHasAtMostOneItem(tbl) then | |||
return tbl | |||
else | |||
local sorted = {} | |||
for key, _ in pairs(tbl) do | |||
table.insert(sorted, key) | |||
end | |||
table.sort(sorted) | |||
return sorted | |||
end | |||
end | end | ||
--- Get all values from <code>tbl</code> and sort them in alphabetical order. | --- Get all values from <code>tbl</code> and sort them in alphabetical order. | ||
-- @param #table tbl Table to get values from | -- @param #table tbl Table to get values from | ||
-- @return #table Input table values in alphabetical order. | -- @return #table Input table values in alphabetical order, or <code>tbl</code> exactly if it has 0 or 1 items. | ||
-- @author User:Demian | -- @author User:Demian | ||
-- @see getSortedKeys | -- @see getSortedKeys | ||
function p.getSortedValues(tbl) | function p.getSortedValues(tbl) | ||
local sorted = {} | -- Check if there is anything to even sort. | ||
if p.tableHasAtMostOneItem(tbl) then | |||
return tbl | |||
else | |||
local sorted = {} | |||
for _, value in ipairs(tbl) do | |||
table.insert(sorted, value) | |||
end | |||
table.sort(sorted) | |||
return sorted | |||
end | |||
end | end | ||
Line 323: | Line 418: | ||
end | end | ||
function p. | --- Try to iterate a table to see if it has more than 0 items. | ||
-- | |||
-- Will at most iterate over the first item. | |||
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly. | |||
-- @param #table tbl Table check | |||
-- @return #bool <code>true</code> if table has at least 1 item. | |||
-- @author User:Demian | |||
function p.tableHasData(tbl) | |||
for _, _ in ipairs(tbl) do | |||
return true | |||
end | |||
return false | |||
end | |||
--- Try to iterate a table to see if it has less than 2 items. | |||
-- | |||
-- Will at most iterate over the first two items. | |||
-- <code>mw.LoadData</code> prevents <code>#tbl</code> from working correctly. | |||
-- @param #table tbl Table check | |||
-- @return #bool <code>true</code> if table has 0 or 1 items, otherwise <code>false</code>. | |||
-- @author User:Demian | |||
function p.tableHasAtMostOneItem(tbl) | |||
local count = 0 | |||
for _, value in ipairs(tbl) do | |||
count = count + 1 | |||
if 1 < count then | |||
return false | |||
end | |||
end | |||
return 2 > count | |||
end | end | ||
return p | return p |