Module:Test: Difference between revisions

From Roat Pkz
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(42 intermediate revisions by the same user not shown)
Line 1: Line 1:
local json = require("Module:GEPrices/data.json")
-- This is a Lua module for MediaWiki
-- It provides functionality to calculate percentages of a number


-- Function to read data from JSON file
-- Create a table to hold the functions
local function readJSON(filename)
local Test = {}
    local file = io.open(filename, "r") -- Open file in read mode
    if not file then return nil end -- If file doesn't exist, return nil


     local content = file:read("*a") -- Read the entire content
-- Function to calculate percentages
     file:close() -- Close the file
function Test.calculatePercentages(frame)
     return json.decode(content) -- Decode JSON content and return
    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(denominator * 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(denominator * 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(denominator * 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(denominator * 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
     local resultTable = "{| class=\"wikitable\"\n"
    resultTable = resultTable .. "! " .. table.concat(labels, " !! ") .. "\n|-\n"
    resultTable = resultTable .. "| " .. table.concat(percentages, " || ") .. "\n|}"
   
    return resultTable
end
end


-- Main function
-- Export the module
local function main()
return Test
    local filename = "Module:GEPrices/data.json" -- JSON file name
    local data = readJSON(filename) -- Read data from JSON file
    if not data then
        print("Error: Could not read JSON file")
        return
    end
 
    -- Take input from user
    io.write("Enter the name of the item: ")
    local itemName = io.read()
 
    -- Search for the item in the data
    local price = data[itemName]
 
    if price then
        print("Price of", itemName, ":", price)
    else
        print("Item not found")
    end
end
 
main() -- Run the main function

Latest revision as of 16:29, 1 May 2024

Documentation for this module may be created at Module:Test/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(denominator * 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(denominator * 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(denominator * 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(denominator * 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
    local resultTable = "{| class=\"wikitable\"\n"
    resultTable = resultTable .. "! " .. table.concat(labels, " !! ") .. "\n|-\n"
    resultTable = resultTable .. "| " .. table.concat(percentages, " || ") .. "\n|}"
    
    return resultTable
end

-- Export the module
return Test