Module:Gemshop: Difference between revisions

From IdleOn MMO Wiki
(To improve maintainability, now raw data can be processed without conversion.)
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 72: Line 72:
     local data = mtxinfo_to_2dtable(mtxinfo)
     local data = mtxinfo_to_2dtable(mtxinfo)
    -- TODO
for _, item in pairs(data) do
for _, item in pairs(data) do
if item.tabno == tab_number and item.sectionno == section_number and item.display ~= "Blank" then
if item.tabno == tab_number and item.sectionno == section_number and item.display ~= "Blank" then
Line 81: Line 80:
             ret = ret .. string.format(
             ret = ret .. string.format(
                 ROW_TEMPLATE,
                 ROW_TEMPLATE,
                 item.name,
                 item.display,
                 item.name,
                 item.name,
                 item.description,
                 item.description,

Latest revision as of 06:16, 29 July 2024

Data Here

The data is raw data without any conversion

Maintenance Procedure: simply update the return value of ha.MTXinfo() in N.js


local mtxinfo = mw.loadJsonData('Module:Gemshop/MTXinfo.json')
local p = {}

-- Gemshop/data.json is return value of ha.MTXinfo() in N.js without any conversion

-- itemDisplayName, mtxName, desc, maxPurchases, cost, extra stuff, total stuff
local ROW_TEMPLATE = [=[<tr>
<td>[[File:%s.png|50px]]</td>
<td>%s</td>
<td>%s</td>
<td style="text-align:center;">%s</td>
<td>'''Base:''' %d%s%s</td>
</tr>]=]

-- Trim whitespace from args, and treat blank args as nil
local function preprocessArg(s)
    if not s then
        return nil
    end
    s = s:match('^%s*(.-)%s*$') -- trim whitespace
    if s == '' then
        return nil
    else
        return s
    end
end

-- Convert underscores to spaces
local function underscore_to_space(s)
    return s:gsub('_+', ' ')
end

-- Convert to title capitalization
local function toTitleCase(str)
    return (str:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end))
end

-- Convert raw 4d MTXinfo to 2d table
local function mtxinfo_to_2dtable(mtxinfo)
    local data = {}
    local index = 1

    for tabno, tab in ipairs(mtxinfo) do
        for sectionno, section in ipairs(tab) do
            for itemno, item in ipairs(section) do
                data[index] = {
                    tabno = tabno,
                    sectionno = sectionno,
                    itemno = itemno,
                    display = item[1],
                    name = toTitleCase(underscore_to_space(item[2])),
                    description = underscore_to_space(item[3]),
                    cost = tonumber(item[4]),
                    no = tonumber(item[5]),
                    maxPurchases = tonumber(item[6]),
                    qty = tonumber(item[7]),
                    costIncrement = tonumber(item[8])
                }
                index = index + 1
            end
        end
    end

    return data
end

function p.bySections(frame)
	local args = frame.args
	local tab_number = tonumber(preprocessArg(args.tab_number)) + 1
	local section_number = tonumber(preprocessArg(args.section_number))
	local ret = ""
    local data = mtxinfo_to_2dtable(mtxinfo)
	
	for _, item in pairs(data) do
		if item.tabno == tab_number and item.sectionno == section_number and item.display ~= "Blank" then
            local maxPurchases = item.maxPurchases == 100000 and "∞" or item.maxPurchases
            local extra = item.costIncrement > 0 and string.format([[<br/>'''Increment:''' %d<br/>'''Final:''' %d]], item.costIncrement, item.cost + item.costIncrement * (item.maxPurchases - 1)) or ""
            local total = (1 < item.maxPurchases and item.maxPurchases < 100000) and string.format([[<br/>'''Total:''' %d]], (item.maxPurchases / 2) * (item.cost * 2 + item.costIncrement * (item.maxPurchases - 1))) or ""
            
            ret = ret .. string.format(
                ROW_TEMPLATE,
                item.display,
                item.name,
                item.description,
                maxPurchases,
                item.cost,
                extra,
                total
            )
        end
	end

	return ret
	
end

return p