Module:StoreLine: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
-- This is a Lua module for MediaWiki
local lang = mw.language.getContentLanguage()
-- It provides functionality to calculate percentages of a number
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')
-- Create a table to hold the functions
local find_gevalue = exchange._value
local Test = {}
local find_geprice = exchange._price


local geprices_data = mw.loadJsonData('Module:GEPrices/data.json')
-- Function to calculate percentages
 
function Test.calculatePercentages(frame)
local ptitle = mw.title.getCurrentTitle()
     local args = frame.args
local ns = ptitle.nsText
     local dropRate = args[1] or "0/1" -- Extract the drop rate from the args, default to "0/1"
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 .. 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 = 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, ''}
    }
     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
     -- Parsing the drop rate
     if gemw and geprice == '' then
    local numerator, denominator = dropRate:match("(%d+)/(%d+)")
        local cached_price = geprices_data[gemwname]
     denominator = tonumber(denominator) or 1 -- Convert denominator to a number or default to 1
        if type(cached_price) == 'number' and cached_price > 0 then
            geprice = cached_price
        end
    end
      
      
     -- Lookup GE price
    local percentages = {}
     if gemw and geprice == '' then
    -- Original number
        local has_geprice, geprice_check = pcall(find_geprice,gemwname)
    table.insert(percentages, dropRate)
        if has_geprice and geprice_check > -1 then
    -- Donator (85% of original drop rate)
            geprice = geprice_check
    local donatorDenominator = math.floor(denominator * 0.85)
        end
    local donator = string.format("%d/%d", numerator, donatorDenominator)
     end
    table.insert(percentages, donator)
     -- Super donator (75% of original drop rate)
     local superDonatorDenominator = math.floor(denatorDenominator * 0.75)
    local superDonator = string.format("%d/%d", numerator, superDonatorDenominator)
    table.insert(percentages, superDonator)
    -- Extreme donator (70% of original drop rate)
    local extremeDonatorDenominator = math.floor(superDonatorDenominator * 0.70)
    local extremeDonator = string.format("%d/%d", numerator, extremeDonatorDenominator)
    table.insert(percentages, extremeDonator)
    -- Legendary donator (65% of original drop rate)
    local legendaryDonatorDenominator = math.floor(extremeDonatorDenominator * 0.65)
    local legendaryDonator = string.format("%d/%d", numerator, legendaryDonatorDenominator)
    table.insert(percentages, legendaryDonator)
    -- Royal donator (60% of original drop rate)
    local royalDonatorDenominator = math.floor(legendaryDonatorDenominator * 0.60)
    local royalDonator = string.format("%d/%d", numerator, royalDonatorDenominator)
     table.insert(percentages, royalDonator)
      
      
     -- Lookup GE value
     -- Custom labels
     if gemw and itemvalue == '' then
     local labels = {"Drop Rate", "Donator", "Super donator", "Extreme donator", "Legendary donator", "Royal donator"}
        local has_gevalue, gevalue = pcall(find_gevalue,gemwname)
        if has_gevalue and gevalue > -1 then
            itemvalue = gevalue
        end
    end
      
      
 
    -- Format the result as a table with red background color for bottom cells
 
    local resultTable = '{| class="wikitable"\n'
    resultTable = resultTable .. '|-\n'
    resultTable = resultTable .. '! ' .. table.concat(labels, ' !! ') .. '\n|-\n'
    resultTable = resultTable .. '| ' .. table.concat(percentages, ' || ') .. '\n|-\n'
    resultTable = resultTable .. '| || style="background-color: #800000; color: white;" | ' .. table.concat(percentages, ' || style="background-color: #800000; color: white;" | ') .. '\n|}'
      
      
    -- Check precached Module:GEHighAlch/data - gets GE alch so inaccurate
     return resultTable
    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
   
 
   
    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 gemw and tonumber(geprice) ~= nil then
        geprice = showCurrencyAmount('Pkp', 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=]]', 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', name, displayname))
 
 
 
    if not hidege then
        ret:tag('td')
                :attr('data-sort-value', ge_sort)
                :wikitext(geprice)
            :done()
    end
       
   
    local onMain = ns == '' or ns == 'RuneScape'
    if onMain and useSmw then
        local smw = {}
        local smw_sub = {}
        local source = pgTitle
        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'] = displayname
        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
        mw.smw.subobject(smw_sub) -- add item subobject to page
        mw.smw.set(smw) -- add data to page
    end
     return tostring(ret)
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
end


return p
-- Export the module
return Test

Revision as of 23:11, 3 April 2024

Documentation for this module may be created at Module:StoreLine/doc

-- This is a Lua module for MediaWiki
-- It provides functionality to calculate percentages of a number

-- Create a table to hold the functions
local Test = {}

-- Function to calculate percentages
function Test.calculatePercentages(frame)
    local args = frame.args
    local dropRate = args[1] or "0/1" -- Extract the drop rate from the args, default to "0/1"
    
    -- Parsing the drop rate
    local numerator, denominator = dropRate:match("(%d+)/(%d+)")
    denominator = tonumber(denominator) or 1 -- Convert denominator to a number or default to 1
    
    local percentages = {}
    -- Original number
    table.insert(percentages, dropRate)
    -- Donator (85% of original drop rate)
    local donatorDenominator = math.floor(denominator * 0.85)
    local donator = string.format("%d/%d", numerator, donatorDenominator)
    table.insert(percentages, donator)
    -- Super donator (75% of original drop rate)
    local superDonatorDenominator = math.floor(denatorDenominator * 0.75)
    local superDonator = string.format("%d/%d", numerator, superDonatorDenominator)
    table.insert(percentages, superDonator)
    -- Extreme donator (70% of original drop rate)
    local extremeDonatorDenominator = math.floor(superDonatorDenominator * 0.70)
    local extremeDonator = string.format("%d/%d", numerator, extremeDonatorDenominator)
    table.insert(percentages, extremeDonator)
    -- Legendary donator (65% of original drop rate)
    local legendaryDonatorDenominator = math.floor(extremeDonatorDenominator * 0.65)
    local legendaryDonator = string.format("%d/%d", numerator, legendaryDonatorDenominator)
    table.insert(percentages, legendaryDonator)
    -- Royal donator (60% of original drop rate)
    local royalDonatorDenominator = math.floor(legendaryDonatorDenominator * 0.60)
    local royalDonator = string.format("%d/%d", numerator, royalDonatorDenominator)
    table.insert(percentages, royalDonator)
    
    -- Custom labels
    local labels = {"Drop Rate", "Donator", "Super donator", "Extreme donator", "Legendary donator", "Royal donator"}
    
    -- Format the result as a table with red background color for bottom cells
    local resultTable = '{| class="wikitable"\n'
    resultTable = resultTable .. '|-\n'
    resultTable = resultTable .. '! ' .. table.concat(labels, ' !! ') .. '\n|-\n'
    resultTable = resultTable .. '| ' .. table.concat(percentages, ' || ') .. '\n|-\n'
    resultTable = resultTable .. '| || style="background-color: #800000; color: white;" | ' .. table.concat(percentages, ' || style="background-color: #800000; color: white;" | ') .. '\n|}'
    
    return resultTable
end

-- Export the module
return Test