Module:StoreLine

-- require("Module:Mw.html extension") local p = {}

local params = require('Module:Paramtest') local lang = mw.language.getContentLanguage local curr_image = require('Module:Currency Image') local yesno = require('Module:Yesno') local _altval = ' ' local ptitle = mw.title.getCurrentTitle local ns = ptitle.nsText local title = ptitle.fullText local pgTitle = ptitle.text

local _priceStrings = { buy = "Shop buys for %s %s each.", sellF = "Free sample", }

local smwData = nil function getSMWInfo(item) if smwData ~= nil then return smwData end local smw = mw.smw.ask({       ..item..,        '?Cost'    }) if smw and smw[1] then smwData = { alch = smw[1]['Cost'] }   else smwData = false end return smwData end

function commas(n) if tonumber(n) then local x_good, x = pcall(lang.formatNum, lang, tonumber(n)) if x_good then return x		end end return n end

-- function to get the currency image and name (singular vs plural) function currencyInfo(currency,price) price = tostring(price) -- body local lowcur = string.lower(currency) local clean = price:gsub('%W','') local retcur, img if tonumber(clean) and tonumber(clean) ~= 1 then retcur = currency else retcur = string.sub(currency,1,(string.len(currency)-1)) end img = curr_image(currency,price) or 'altvalue.png' img = ' ' return img, retcur end

function cost(value) if not tonumber(value) then local val = yesno(value, value) if type(val) == 'boolean' then value = val else val = string.lower(val) if val == 'not sold' or               val == 'not bought' or                val == 'none' or                val== 'n/a' then value = 'not' end end end

return value end

function p.main(frame) local args = frame:getParent.args local tempArgs = frame.args -- Params and defaults

local name, namenotes, cost, costnotes, altcur = params.defaults{ {args.name,'Item'}, {args.namenotes,''}, {args.cost,'0'}, {args.costnotes,''}, {args.altCurrency,''} }	local displayname = params.default_to(args.displayname,name) local gemwname = params.default_to(args.gemwname) gemwname = gemwname or name local currency = params.default_to(tempArgs.currency,args.currency) currency = currency or '' -- Override currency from table head with currency set on line if params.has_content(args.currency) then currency = args.currency end local nosmw if params.has_content(args.nosmw) then nosmw = true end if params.has_content(tempArgs.nosmw) then nosmw = true end -- Override for historical (removed items) local historical = false if params.has_content(args.historical) then historical = yesno(args.historical,false) end if params.has_content(tempArgs.historical) then historical = yesno(tempArgs.historical,false) end

-- Remove version number from potions, enchanted jewellery, waterskins etc for smw local cleanedName local dropVers = '' if gemwname:match(' %(%d%)$') then cleanedName, dropVers = mw.ustring.match(gemwname, '^(.-) (%(%d%))$') elseif gemwname:match('%#') then cleanedName, dropVers = mw.ustring.match(gemwname, '^(.-)%#([%w%s%(%)_]+)$') else cleanedName = mw.ustring.gsub(gemwname, ' %(%d%)$', '') end cleanedName = mw.text.trim(cleanedName) dropVers = mw.text.trim(dropVers) local smwname = cleanedName if dropVers ~= '' then -- get subobject instead smwname = cleanedName..'#'..dropVers end local gemw = true if params.has_content(tempArgs.gemw) then gemw = yesno(tempArgs.gemw or 'yes', false) end if yesno(args.gemw) ~= nil then gemw = yesno(args.gemw) end local alch = yesno(args.alch or 'yes', false) local hasAltCur = false if params.has_content(altcur) and mw.ustring.lower(altcur) ~= 'gold' then hasAltCur = true end -- Test for existance of alch value local hasmwalch, smwalchval local valueInfo = { alch = { has = false, value = 0, from = nil },       ge = { has = false, value = 0, from = nil }   }    if alch then if gemw then -- does not have no-ge override -- lookup in GEMW local hasgealch, gealchval = pcall(f_gealch,gemwname) -- alch is not disabled if hasgealch then if gealchval > -1 then -- has a alch value from GEMW valueInfo.alch = { has = true, value = tonumber(gealchval), from = 'ge' }               end end end if not valueInfo.alch.has then -- failed to find alch in GEMW or has no-ge override -- lookup in SMW --local smwret = getSMWInfo(smwname) --if smwret and smwret.alch ~= nil then --   -- alch is defined, use it            --    valueInfo.alch = { --       has = true, --       value = smwret.alch, --       from = 'smw' --   }            --elseif args.altvalue then --   --altvalue set, no alch from GE or SMW, use alt as regular alch --   valueInfo.alch = { --       has = true, --       value = tonumber(args.altvalue), --       from = 'alt' --   }            --    if hasAltCur then --       valueInfo.alch.curr = altcur --   end --else --	alch = false --end end else -- alch is disabled if args.altvalue then --Altvalue set, no alch value valueInfo.alch = { has = true, value = tonumber(args.altvalue), from = 'alt' }           if hasAltCur then valueInfo.alch.curr = altcur end end end if args.altvalue then valueInfo.ge = { has = true, value = tonumber(args.altvalue), from = 'alt' }       if hasAltCur then valueInfo.ge.curr = altcur end end -- Use 'File: .png' if no image param -- Use 'File: ' if image param; image param will include extension local image,image_n image_n = params.default_to(args.image, name .. '.png') image_n = mw.ustring.gsub(image_n, '#.+$', '.png') if image_n:lower == 'no' or params.is_empty(args.Name) then image = '' else image = mw.ustring.format('', image_n, name, image_n, title, name, cost) end

local description = args.Description -- Table row local ret = p._main{ name = name, displayname = displayname, namenotes = namenotes, cost = cost, costnotes = costnotes, currency = currency, image = image, valueInfo = valueInfo, tempArgs = tempArgs, smwname = smwname, historical = historical, nosmw = nosmw, description = description, }	return ret

end

-- main function to generate the row function p._main(args)

-- Correct currencies if not args.currency or args.currency == '' then args.currency = 'coins' end if not args.valueInfo.ge.curr or args.valueInfo.ge.curr == '' then args.valueInfo.ge.curr = 'coins' end

-- Clean cost values args.cost = cost(args.cost)

-- Cost Display local costDisp, costSort, costTtl local costNum = tonumber(args.cost) if costNum == 0 then costSort = costNum costTtl = _priceStrings.sellF costDisp = 'Free' elseif costNum then costSort = costNum local sImg, sCur = currencyInfo(args.currency, costNum) costTtl = mw.ustring.format(_priceStrings.cost, commas(costNum), sCur) costDisp = commas(costNum) .. sImg elseif args.cost == 'not' or args.cost == false then costSort = -1 costTtl = 'This item is not sold by this shop' costDisp = 'Not sold' else costSort = 10e99 costTtl = 'Cost price entered is invalid or none was given.' costDisp = ' Error ' end local ret = mw.html.create('tr') -- inventory image :td{ css = {'text-align', 'center'}, class = 'inventory-image', wikitext = args.image, }           -- item name :td{ css = {'text-align', 'left'}, class = 'item-col', wikitext = string.format('%s%s', args.name, args.displayname, #args.namenotes > 3 and args.namenotes or ''), }           -- cost :td{ css = { ['text-align'] = 'right', cursor = 'help', },               class = 'cost-col ref-left', attr = { ['data-sort-value'] = costSort, ['title'] = costTtl, },               wikitext = string.format('%s%s', #args.costnotes > 3 and args.costnotes or '', costDisp), }           -- description :IF(args.description) :td{ css = {'text-align', 'left'}, class = 'description-col', wikitext = args.description, }           :END :done --Add class if line but not table is historical :addClassIf( args.historical and not yesno(args.tempArgs.Historical, false), 'storeline-hist')

-- SMW --if ns == '' and not args.nosmw then --   local smw = {} --   local smw_sub = {} --   local source = pgTitle --   local smwNameNote = mw.text.killMarkers(args.namenotes) --   local smwCostNote = mw.text.killMarkers(args.costnotes) --   --add function to reduce image to File:Some name.png --   if args.historical then --   	smw['Sells item hist'] = args.name --   	smw_sub = { --           ['Sold by hist'] = source, --           ['Sold item image'] = mw.text.encode(args.image), --           ['Sold item hist'] = args.name, --           ['Sold item text'] = args.displayname, --           ['Sold item notes'] = smwNameNote, --           ['Store cost price'] = args.cost, --           ['Store cost notes'] = smwCostNote, --           ['Store currency'] = args.currency, --       }    --    else --   	smw['Sells item'] = args.name --   	smw_sub = { --           ['Sold by'] = source, --           ['Sold item image'] = mw.text.encode(args.image), --           ['Sold item'] = args.name, --           ['Sold item text'] = args.displayname, --           ['Sold item notes'] = smwNameNote, --           ['Store cost price'] = args.cost, --           ['Store cost notes'] = smwCostNote, --           ['Store currency'] = args.currency, --       }    --    end --   mw.smw.subobject(smw_sub, args.smwname .. ' ' .. args.sell) -- add item subobject to page --   mw.smw.set(smw) -- add data to page --end

return tostring(ret) end

return p --