Module:Iteminfo

local data = mw.loadData( 'Module:iteminfo/data' ) -- loading data table.

local trim = mw.text.trim

-- cache local currentFrame

-- helper function local getArg = function(key) local value = currentFrame.args[key] if not value then return nil end value = trim(value) if value == '' then return nil else return value end end

-- credit: http://richard.warburton.it -- this version is with trim. local function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function return string.find(str,div,pos,true) end do		arr[#arr + 1] = trim(string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end arr[#arr + 1] = trim(string.sub(str,pos)) -- Attach chars right of last divider return arr end

local getStat = function(itemid, stat) return data[itemid][stat] or data[0][stat] or nil -- "or nil" will convert "false" to "nil" end

local statname={ ["acc"] = "accessory", ["autoreuse"] = "autoReuse", ["autoswing"] = "autoReuse", ["auto"] = "autoReuse", ["axe power"] = "axe", ["axepower"] = "axe", ["bait power"] = "bait", ["baitpower"] = "bait", ["body slot"] = "bodySlot", ["bodyslot"] = "bodySlot", ["buff duration"] = "buffTime", ["buff id"] = "buffType", ["buffduration"] = "buffTime", ["buffid"] = "buffType", ["bufftime"] = "buffTime", ["bufftype"] = "buffType", ["carttrack"] = "cartTrack", ["createtile"] = "createTile", ["createwall"] = "createWall", ["critical"] = "crit", ["dd2summon"] = "DD2Summon", ["dmg"] = "damage", ["fishing power"] = "fishingPole", ["fishingpole"] = "fishingPole", ["fishingpower"] = "fishingPole", ["fishing"] = "fishingPole", ["hair dye"] = "hairDye", ["hairdye"] = "hairDye", ["hammer power"] = "hammer", ["hammerpower"] = "hammer", ["head slot"] = "headSlot", ["headslot"] = "headSlot", ["heallife"] = "healLife", ["healmana"] = "healMana", ["holdstyle"] = "holdStyle", ["id"] = "type", ["internal name"] = "internalName", ["internalname"] = "internalName", ["item id"] = "type", ["itemid"] = "type", ["kb"] = "knockBack", ["knockback"] = "knockBack", ["leg slot"] = "legSlot", ["legslot"] = "legSlot", ["liferegen"] = "lifeRegen", ["makenpc"] = "makeNPC", ["maxstack"] = "maxStack", ["mount id"] = "mountType", ["mountid"] = "mountType", ["mounttype"] = "mountType", ["mount"] = "mountType", ["nomelee"] = "noMelee", ["notammo"] = "notAmmo", ["nousegraphic"] = "noUseGraphic", ["nowet"] = "noWet", ["pick power"] = "pick", ["pickpower"] = "pick", ["pickaxe power"] = "pick", ["pickaxepower"] = "pick", ["pickaxe"] = "pick", ["placestyle"] = "placeStyle", ["projectile id"] = "shoot", ["questitem"] = "questItem", ["range bonus"] = "tileBoost", ["rangebonus"] = "tileBoost", ["rarity"] = "rare", ["reusedelay"] = "reuseDelay", ["shootspeed"] = "shootSpeed", ["sound"] = "UseSound", ["sounduse"] = "UseSound", ["stack"] = "maxStack", ["stringcolor"] = "stringColor", ["tile id"] = "createTile", ["tile sub id"] = "placeStyle", ["tile subid"] = "placeStyle", ["tilesubid"] = "placeStyle", ["tile wand"] = "tileWand", ["tileboost"] = "tileBoost", ["tileid"] = "createTile", ["tilewand"] = "tileWand", ["tile"] = "createTile", ["tool speed"] = "useTime", ["toolspeed"] = "useTime", ["tooltip"] = "ToolTip", ["uniquestack"] = "uniqueStack", ["use delay"] = "reuseDelay", ["use sound"] = "UseSound", ["use style"] = "useStyle", ["use time"] = "useAnimation", ["useammo"] = "useAmmo", ["useanimation"] = "useAnimation", ["usedelay"] = "reuseDelay", ["usesound"] = "UseSound", ["usestyle"] = "useStyle", ["usetime"] = "useAnimation", ["useturn"] = "useTurn", ["use"] = "useAnimation", ["velocity"] = "shootSpeed", ["wall id"] = "createWall", ["wallid"] = "createWall", ["wall"] = "createWall", ["sell"] = "value", --non-native stats: ["buy"] = "buyValue", ["price"] = "buyValue", ["buffname"] = "buffName", ["buff name"] = "buffName", ["buff"] = "buffName", }

local proc = {} proc.axe = function(value) return tostring((value or 0) * 5) .. '%' end proc.bodySlot = function(value) return (value == -1) and "" or value end proc.createTile = proc.bodySlot proc.createWall = proc.bodySlot proc.damage = proc.bodySlot proc.hairDye = proc.bodySlot proc.headSlot = proc.bodySlot proc.legSlot = proc.bodySlot proc.mountType = proc.bodySlot proc.tileWand = proc.bodySlot proc.bait = function(value) return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'percentage', ['$percentage$'] = tostring(value) } } end proc.fishingPole = proc.bait proc.hammer = proc.bait proc.pick = proc.bait proc.buffTime = function(value) return currentFrame:expandTemplate{ title = 'duration', args = { (value or 0)/60 } } end proc.buffName = function(value, id) return currentFrame:expandTemplate{ title = 'GetBuffInfo', args = { getStat(id, 'buffType'), 'ename' } } end proc.crit = function(value, id) if getStat(id, 'damage') == -1 then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'percentage', ['$percentage$'] = tostring(value) } } -- no damage value else return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'percentage', ['$percentage$'] = tostring(value+4) } } -- has damage value end end proc.damagetype = function(value, id) if getStat(id, 'melee') then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_melee' } } end if getStat(id, 'ranged') then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_ranged' } } end if getStat(id, 'magic') then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_magic' } } end if getStat(id, 'summon') then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_summon' } } end if getStat(id, 'thrown') then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'damagetype_thrown' } } end end proc.rare = function(value) return currentFrame:expandTemplate{ title = 'rare', args = { (value or 0) } } end proc.tileBoost= function(value) if value ~= 0 then if value > 0 then value = '+' .. tostring(value) else value = tostring(value) end return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'tileBoost', ['$tiles$'] = value } } end end proc.useAmmo = function(value) if value == 40 or value == 71 or value == 97 or value == 283 or value == 771 or value == 931 then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'ammo'..tostring(value) } } end return currentFrame:expandTemplate{ title = 'itemNameFromId', args = { value } } end proc.UseSound = function(value) local str = '' for _,v in pairs(explode(',', value)) do str = str .. currentFrame:expandTemplate{ title = 'sound', args = { '', v..'.wav' } } end return str end proc.useStyle = function(value) if value == 0 then return end if value >= 1 and value <= 5 then return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'usestyle'..tostring(value) } } end return value end proc.buyValue = function(value, id) if getStat(id, 'DD2Summon') then return currentFrame:expandTemplate{ title = 'dm', args = { tonumber(getStat(id, 'value'))/10000 } } else return currentFrame:expandTemplate{ title = 'coin', args = { tostring(getStat(id, 'value'))..'cc' } } end end proc.value = function(value) value = tonumber(value or 0) if value > 0 then if value < 5 then -- items with value<5 have a sell value of 1, not 0 (contrary to regular rounding) return currentFrame:expandTemplate{ title = 'coin', args = { '1cc' } } else return currentFrame:expandTemplate{ title = 'coin', args = { tostring(math.floor(value/5))..'cc' } } end else return currentFrame:expandTemplate{ title = 'l10n', args = { 'GetItemInfo', 'noValue' } } end end

---

local p = { -- for other module, get all stats, return as a table. getItemInfo = function(itemid) if itemid >= 3930 or itemid <= 0 then itemid = 0 end local d = data[itemid] local result = {['type'] = itemid} for k,v in pairs(data[0]) do			if d[k] ~= nil and d[k] ~= v then result[k] = d[k] else result[k] = v			end end return result end,

-- for template, get all stats, setting in dplvars. getInfo = function(frame) currentFrame = frame -- cache

local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0 local prefix = getArg('prefix') or getArg(2) or '_iteminfo_'

if itemid >= 3930 or itemid <= 0 then itemid = 0 end local result = data[itemid] local args = { prefix .. 'type', itemid } for k,v in pairs(data[0]) do args[#args + 1] = prefix .. k			if result[k] ~= nil and result[k] ~= v then args[#args + 1] = result[k] else args[#args + 1] = v			end end

frame:callParserFunction{ name = '#dplvar:set', args = args } end,

-- for other module, get single stat, return this stat directly getItemStat = function(itemid, stat) if itemid >= 3930 or itemid <= 0 then itemid = 0 end if stat == 'type' then return itemid end return data[itemid][stat] or data[0][stat] end,

-- for template, get single stat, return directly getStat = function(frame) currentFrame = frame -- cache

local itemid = tonumber(getArg('id')) or tonumber(getArg(1)) or 0 local stat = getArg('stat') or getArg(2) stat = statname[stat] or stat

if not stat then return end

if itemid >= 3930 or itemid <= 0 then itemid = 0 end

local result if stat == 'type' then return itemid end if stat == 'count' then return 2929 end result = getStat(itemid, stat) if not getArg('raw') and proc[stat] then result = proc[stat](result, itemid) end

if getArg('format') then result = frame:callParserFunction('formatnum', result) end

return result end, }

--alias p.getinfo = p.getInfo p.GetInfo = p.getInfo p.getstat = p.getStat p.GetStat = p.getStat

return p