bugfix250107.1
全站通知:

模块:礼包数据转json

来自恋与深空WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:礼包数据转json/doc创建

local p = {}

function p.toJSON(frame)
    local result = {}
	
	for key, value in pairs(frame.args) do
		
        local key = key:match("^道具%-(.+)$")
        if key and value then
            result[key] = tonumber(value)
        end
	end

    return mw.text.jsonEncode(result)
end

function p.randomItemToJSON(frame)
	local random_items = {}	

    for key, value in pairs(frame.args) do
        local index, name = key:match("^随机道具%-(%d+)%*(.+)$")
        if name and value then
            local quantity, chance = value:match("([%d%.]+)%*([%d%.]+)")
            if quantity and chance then
                table.insert(random_items, {
                    ["名称"] = name,
                    ["数量"] = tonumber(quantity),
                    ["概率"] = tonumber(chance)
                })
            end
    	end
    end
    local json = mw.text.jsonEncode(random_items)
    return json
end 

function p.debugArgs(frame)
    local parent = frame:getParent()
    
    local args = parent and parent.args or frame.args

    local output = {}
    for k, v in pairs(args) do
        table.insert(output, tostring(k) .. " = " .. tostring(v))
    end

    return table.concat(output, "<br>")
end

return p