Module:Sandbox/User:Kelsey/StoreLine

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:13, 17 October 2024 (Created page with "local p = {} local lang = mw.language.getContentLanguage() local var = mw.ext.VariablesLua local params = require('Module:Paramtest') local yesno = require('Module:Yesno') local currency_image = require("Module:Currency Image") local commas = require("Module:Addcommas") local exchange = require('Module:Exchange') local find_gevalue = exchange._value local find_geprice = exchange._price local geprices_data = mw.loadJsonData('Module:GEPrices/data.json') local var = mw.ex..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Template:Module sandbox/doc. [edit] [history] [purge]
Module:Sandbox/User:Kelsey/StoreLine requires Module:Addcommas.
Module:Sandbox/User:Kelsey/StoreLine requires Module:Currency Image.
Module:Sandbox/User:Kelsey/StoreLine requires Module:Exchange.
Module:Sandbox/User:Kelsey/StoreLine requires Module:Paramtest.
Module:Sandbox/User:Kelsey/StoreLine requires Module:Yesno.
Module:Sandbox/User:Kelsey/StoreLine loads data from Module:DropsLine/itemData.json.
Module:Sandbox/User:Kelsey/StoreLine loads data from Module:GEHighAlchs/data.json.
Module:Sandbox/User:Kelsey/StoreLine loads data from Module:GEPrices/data.json.

This module is a sandbox for Kelsey. It can be used to test changes to existing modules, prototype new modules, or just experimenting with lua features.

Invocations of this sandbox should be kept in userspace; if the module is intended for use in other namespaces, it should be moved out of the sandbox into a normal module and template.

This default documentation can be overridden by creating the /doc subpage of this module, as normal.

local p = {}
local lang = mw.language.getContentLanguage()
local var = mw.ext.VariablesLua
local params = require('Module:Paramtest')
local yesno = require('Module:Yesno')
local currency_image = require("Module:Currency Image")
local commas = require("Module:Addcommas")

local exchange = require('Module:Exchange')
local find_gevalue = exchange._value
local find_geprice = exchange._price

local geprices_data = mw.loadJsonData('Module:GEPrices/data.json')
local var = mw.ext.VariablesLua

local ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local title = ptitle.fullText
local pgTitle = ptitle.text

local function ticktime(ticks)
    local ret = ''
    local days = math.floor(ticks/144000)
    local hours = math.floor(ticks%144000/6000)
    local minutes = math.floor(ticks%6000/100)
    local seconds = ticks%100*0.6
    if days > 0 then
        ret = ret .. commas._add(days) .. 'd '
    end
    if hours > 0 then
        ret = ret .. hours .. 'h '
    end
    if minutes > 0 then
        ret = ret .. minutes .. 'm '
    end
    if seconds > 0 then
        ret = ret .. seconds .. 's'
    end
    return ret
end

function p.main(frame)
    local args = frame:getParent().args

    -- Params and defaults
    local name, stock, buyvalue, sellvalue, geprice, rowShopVersion = params.defaults{
        {args.name or args.Name, ''},
        {args.stock or args.Stock, ''},
        {args.buy or args.Buy, ''},
        {args.sell or args.Sell, ''},
        {args.geprice or args.geprice, ''},
        {args.shopversion, ''}
    }
    local itemvalue = ''
    local gemwname = params.default_to(args.gemwname,name)
    local smwname = params.default_to(args.smwname,name)
    local displayname = params.default_to(args.displayname or args.DisplayName,name)
    local image = 'File:' .. params.default_to(args.image or args.Image, name .. '.png')
    local gemw = yesno(args.gemw or 'yes', false)
    local restock = params.default_to(args.restock or args.Restock,-1)

    -- Check precached Module:GEPrices/data
    if gemw and geprice == '' then
        local cached_price = geprices_data[gemwname]
        if type(cached_price) == 'number' and cached_price > 0 then
            geprice = cached_price
        end
    end

    -- Lookup GE price
    if gemw and geprice == '' then
        local has_geprice, geprice_check = pcall(find_geprice,gemwname)
        if has_geprice and geprice_check > -1 then
            geprice = geprice_check
        end
    end

    -- Lookup GE value
    if gemw and itemvalue == '' then
        local has_gevalue, gevalue = pcall(find_gevalue,gemwname)
        if has_gevalue and gevalue > -1 then
            itemvalue = gevalue
        end
    end

    -- Lookup SMW value
    if itemvalue == ''  then
        itemvalue = getSMWInfo(smwname) or ''
    end

    -- Check precached Module:DropsLine/itemData - gets GE alch so inaccurate
    if itemvalue == '' then
        local droppeditem_data = mw.loadJsonData('Module:DropsLine/itemData.json')
        local cached_dropdata = droppeditem_data[name]
        if type(cached_dropdata) == 'table' and type(cached_dropdata[2]) == 'number' then
            itemvalue = cached_dropdata[2]/.6
        end
    end

    -- Check precached Module:GEHighAlch/data - gets GE alch so inaccurate
    if itemvalue == '' then
        local highalch_data = mw.loadJsonData('Module:GEHighAlchs/data.json')
        local cached_data = highalch_data[name]
        if type(cached_data) == 'number' and cached_data > -1 then
            itemvalue = cached_data/.6
        end
    end

    local buymultiplier = var.var('BuyMultiplier', 1000)
    local sellmultiplier = var.var('SellMultiplier', 1000)
    local currency = var.var('Currency', 'Coins')
    local namenotes = var.var('NameNotes', '')
    local delta = var.var('Delta', 10)
    local useSmw = yesno(var.var('smw','yes'), true)
    local hideimage = yesno(var.var('hideImage','no'), false)
    local hidege = yesno(var.var('hideGE','no'), false)
    local hidesell = yesno(var.var('hideSell','no'), false)
    local hidebuy = yesno(var.var('hideBuy','no'), false)
    local hidestock = yesno(var.var('hideStock','no'), false)
    local hiderestock = yesno(var.var('hideRestock','no'), false)
    local tableShopVersion = var.var('ShopVersion', '')

    buyvalue = commas._strip(buyvalue)
    sellvalue = commas._strip(sellvalue)
    local buy_smw = tonumber(buyvalue)
    local sell_smw = tonumber(sellvalue) or 1e10
    local ge_sort = tonumber(geprice)
    local buyCurrency = ''
    local sellCurrency = ''

    if buyvalue ~= 'N/A' then
        if buyvalue == '' and itemvalue ~= '' then
            buyvalue = math.floor(math.max(itemvalue*buymultiplier/1000,itemvalue*0.1))
            buy_smw = buyvalue
        end
        buyvalue = showCurrencyAmount(currency, buyvalue)
    end
    if sellvalue ~= 'N/A' then
        if sellvalue == '' and itemvalue ~= '' then
            sellvalue = math.floor(math.max(itemvalue*sellmultiplier/1000,1))
            sell_smw = sellvalue
        end
        sellvalue = showCurrencyAmount(currency, sellvalue)
    end

    if gemw and tonumber(geprice) ~= nil then
        geprice = showCurrencyAmount('coins', geprice)
    else
        ge_sort = 0
        geprice = 'Not sold'
    end

    if stock=='inf' then stock='∞' end --inf is easier to type
    if stock=='∞' then restock='N/A' end --self-documenting code

    local ret = mw.html.create('tr'):css('text-align','center')
    if not hideimage then
        ret:tag('td'):wikitext(mw.ustring.format('[[%s|link=%s]]', image, name))
    end
    -- no hideXXX parameter for the column that lists the actual items.
    ret:tag('td'):css('text-align','left'):wikitext(mw.ustring.format('[[%s|%s]]', name, displayname))
    if not hidestock then
        if stock=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        elseif stock=='∞' then
            ret:tag('td'):wikitext('<span style="font-size:120%;">∞</span>'):done()
        else
            ret:tag('td')
                :wikitext(stock)
            :done()
        end
    end
    if not hiderestock then
        if restock=='N/A' or stock=='N/A' or stock=='∞' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        elseif restock == -1 then
            ret:tag('td'):attr('data-sort-value', -1):wikitext('<small>unknown</small>[[Category:Needs restock time]]'):done()
        else
            ret:tag('td'):attr('data-sort-value', restock)
                :wikitext(ticktime(tonumber(restock) or 0) .. ' (' .. commas._add(restock) .. 't)')
            :done()
        end
    else
        restock='N/A' -- Set restock to N/A if hideRestock is set to true in the header
    end
    if not hidesell then
        if sellvalue == 'N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        elseif sell_smw == 0 then
            ret:tag('td')
                :attr('data-sort-value', 0)
                :wikitext('<small>Free</small>')
            :done()
        else
            ret:tag('td')
                :attr('data-sort-value', sell_smw)
                :wikitext(sellvalue)
            :done()
        end
    end
    if not hidebuy then
        if buyvalue=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        else
            ret:tag('td')
                    :attr('data-sort-value', buy_smw or 0)
                    :wikitext(buyvalue)
               :done()
        end
    end
    if not hidege then
        ret:tag('td')
                :attr('data-sort-value', ge_sort)
                :wikitext(geprice)
            :done()
    end

    local i = 1
    while args['column' .. i] do
        if args['column' .. i]=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        else
        ret:tag('td'):wikitext(args['column' .. i]):done()
        end
        i = i+1
    end

    local onMain = ns == '' or ns == 'RuneScape'
    local unrecognizedShopVersionCategory = ''
    if onMain and useSmw then

        local smw = {}
        local smw_sub = {}
        local source = title

        local subobjectCount = 1
        if var.varexists('SoldItemCount') then
            subobjectCount = var.var('SoldItemCount', 1)
            subobjectCount = subobjectCount + 1
            var.vardefine('SoldItemCount', subobjectCount)
        else
            var.vardefine('SoldItemCount', 1)
        end
        local subobjectName = 'SOLDITEM_'..subobjectCount..'_'..smwname

        -- shop versions
        local shopVersionKeys = 'DEFAULT'
        if params.has_content(tableShopVersion) then
            -- versions applied to the entire table
           shopVersionKeys = tableShopVersion
        end
        if params.has_content(rowShopVersion) then
            -- versions applied to this row
            shopVersionKeys = rowShopVersion
        end

        local shopVersions = {}
        local membersValues = {}
        local locationsValues = {}
        for shopVersion in string.gmatch(shopVersionKeys, ' *([^,]+) *') do
            table.insert(shopVersions, shopVersion)

            local membersVar = string.format("ShopInfo_members_%s", shopVersion)
            local locationsVar = string.format("ShopInfo_location_%s", shopVersion)
            if (not var.varexists(membersVar) or not var.varexists(locationsVar)) and shopVersion ~= 'DEFAULT' then
                unrecognizedShopVersionCategory = unrecognizedShopVersionCategory..'[[Category:Uses unrecognized shop version]]'
            end
            local curMembersValues = var.var(membersVar)
            local curLocationsValues = var.var(locationsVar)
            table.insert(membersValues, curMembersValues)
            table.insert(locationsValues, curLocationsValues)
        end

        if #shopVersions == 1 then
            membersValues = membersValues[1]
            locationsValues = locationsValues[1]
        end

        local tableWideMembersOverride = var.var('StoreTable_MembersOverride')
        local tableWideLocationOverride = var.var('StoreTable_LocationOverride')
        if tableWideMembersOverride ~= '' then
            membersValues = tableWideMembersOverride
        end
        if tableWideLocationOverride ~= '' then
            locationsValues = tableWideLocationOverride
        end

        local rowWideMembersOverride = args.members or ''
        local rowWideLocationsOverride = args.location or ''
        if rowWideMembersOverride ~= '' then
            membersValues = rowWideMembersOverride
        end
        if rowWideLocationsOverride ~= '' then
            locationsValues = rowWideLocationsOverride
        end

        local smw_json = {
            ['Sold by'] = source,
            ['Sold item image'] = image,
            ['Sold item'] = name,
            ['Display name'] = displayname,
            ['Store stock'] = stock,
            ['Restock time'] = restock,
            ['Store sell price'] = sell_smw,
            ['Store buy price'] = buy_smw or 'N/A',
            ['Store currency'] = currency,
            ['Store notes'] = namenotes,
            ['Store delta'] = delta,
            ['Members'] = membersValues,
            ['Location'] = locationsValues,
        }

        if shopVersionKeys ~= 'DEFAULT' then
            smw_json['Shop version'] = shopVersions
        end

        smw['Sells item'] = name
        smw_sub['Sold by'] = source
        smw_sub['Sold item image'] = image
        smw_sub['Sold item'] = name
        smw_sub['Sold item text'] = name
        smw_sub['Store stock'] = stock
        smw_sub['Restock time'] = restock
        smw_sub['Store sell price'] = sell_smw --type = number for sorting purposes
        smw_sub['Store buy price'] = buy_smw or 'N/A'
        smw_sub['Store currency'] = currency
        smw_sub['Store notes'] = namenotes
        smw_sub['Store delta'] = delta
        smw_sub['Sold item JSON'] = mw.text.jsonEncode(smw_json)
        mw.smw.subobject(smw_sub, subobjectName) -- add item subobject to page
        mw.smw.set(smw) -- add data to page
    end
    return tostring(ret)
end

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

function showCurrencyAmount(currency, amount)
    local image = currency and currency_image(currency, amount) or ''
    if image ~= '' and currency and tonumber(amount) then
        return string.format('[[File:%s|link=%s]] %s', image, currency, commas._add(amount))
    else
        return amount
    end
end

return p