Editing Module:Utilities

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 26: Line 26:
--round_to_significant_figures(12345.6789, 3)  -- Output: 12300
--round_to_significant_figures(12345.6789, 3)  -- Output: 12300
--round_to_significant_figures(0.0012345, 2)  -- Output: 0.0012
--round_to_significant_figures(0.0012345, 2)  -- Output: 0.0012
--round_to_significant_figures("-98765", 4)      -- Output: -98760
--round_to_significant_figures(-98765, 4)      -- Output: -98760
--round_to_significant_figures("foobar", 4)      -- Output: foobar
function p.round_to_sig_fig(num, n)
function p.round_to_sig_fig(input_value, n)
num = tonumber(input_value)
if num == nil then
return input_value
end
    if num == 0 then
    if num == 0 then
        return 0
        return 0
Line 108: Line 102:
end
end


-- Hash for color values
local slot_category_to_color = {
local slot_colors = {
["Weapon"] = "c97a03", --orange
["Weapon"] = {
["Armor"] = "659818", --green
hex = "c8761c",
["Tech"] = "8b56b4" --purple/pink
rgb = "200, 118, 28",
hsl = "31, 75%, 45%",
cmyk = "0%, 41%, 86%, 22%"
},
["Armor"] = {
hex = "6f8724",
rgb = "198, 248, 114",
hsl = "82, 91%, 71%",
cmyk = "20%, 0%, 54%, 3%"
},
["Tech"] = {
hex = "ba5ca6",
rgb = "186, 92, 166",
hsl = "313, 41%, 55%",
cmyk = "0%, 51%, 11%, 27%"
}
 
}
 
-- Hash for format configuration
local color_formats = {
hex = {
prefix = "#",
postfix = ""
},
rgb = {
prefix = "rgb(",
postfix = ")"
},
hsl = {
prefix = "hsl(",
postfix = ")"
},
cmyk = {
prefix = "cmyk(",
postfix = ")"
}
}
}
function p.get_slot_color(slot, color_format, no_wrap, debug_mode)
function p.get_slot_color(slot)
if type(slot) == 'table' and slot.args then
if type(slot) == 'table' and slot.args then
frame = slot
frame = slot
slot = frame.args[1]
slot = frame.args[1]
color_format = frame.args[2]
no_wrap = frame.args["no_wrap"]
debug_mode = frame.args["debug_mode"]
end
end
-- Validate arguments
if slot == nil then  
if slot == nil or slot == "" then return "'slot' parameter must be provided" end
return "'slot' parameter must be provided"
if color_format == nil or color_format == "" then color_format = "hex" end
end
if no_wrap == nil or no_wrap == 'false' or no_wrap == "" then no_wrap = false else no_wrap = true end
local color = slot_category_to_color[slot]
if debug_mode == nil or debug_mode == 'false' or debug_mode == "" then debug_mode = false end
local slot_data = slot_colors[slot]
if slot_data == nil then return "slot '" .. slot .. "' was not in slots_data map" end
--Retrieve the color
local color = slot_data[color_format]
if color == nil then
if color == nil then
return "color_format '" .. color_format .. "' is not in slots_data map"
return "'slot' " .. slot .. " is not in slot_category_to_color map"
end
-- Add prefix and postfix wrapping
if not no_wrap then
-- Retrieve prefix and postfix
prefix = color_formats[color_format]['prefix']
postfix = color_formats[color_format]['postfix']
-- Add to color
color = prefix .. color .. postfix
end
-- Return result
if debug_mode then
return " " .. color
end
end
return color
return color
end
function p.remove_trailing_colon(frame)
    local str = frame.args[1] or ""
    -- Remove trailing colon
    return (str:gsub(":$", ""))
end
function p.process_variables(frame)
    -- Get arguments
    local string_key = frame.args[1] or ''
    local item_name = frame.args[2] or ''
   
    -- Load required modules
    local lang_module = require('Module:Lang')
    local item_data_module = require('Module:ItemData')
   
    -- Get the original text
    local original_text = lang_module.get_string(frame, string_key) or ''
   
    -- Process the text to replace variables with their values
    local processed_text = original_text:gsub("{s:([^}]+)}", function(variable_name)
        -- Get the value from ItemData
        local value = item_data_module.get_prop({args = {item_name, variable_name}})
       
        -- If value exists, remove non-number symbols (but keep + and -)
        if value then
            -- Keep only digits (0-9), plus (+), and minus (-)
            value = value:gsub("[^0-9+.-]", "")
            -- Return empty string if nothing left, otherwise return the filtered value
            return value ~= "" and value or variable_name
        else
            return variable_name -- Fallback to variable name if value not found
        end
    end)
   
    return processed_text
end
function p.contains(tbl, str)
    for _, v in ipairs(tbl) do
        if v == str then
            return true
        end
    end
    return false
end
-- Expands multiple of the same template in a single preprocess call
-- Example:
-- expand_teampltes(frame, 'HeroIcon', {hero_atlas: {'Abrams'}, hero_bebop: {'Bebop'})
-- Returns {hero_atlas: <abrams_icon_template>, hero_bebop: <bebop_icon_template>, ...}
function p.expand_templates(frame, template_name, args_map)
    local keys = {}
    local strings = {}
    for key, args in pairs(args_map) do
        keys[#keys + 1] = key
    end
    for i, key in ipairs(keys) do
        local args = args_map[key]
        local parts = {"{{", template_name, "|"}
        for j, arg in ipairs(args) do
            if j > 1 then parts[#parts + 1] = "|" end
            parts[#parts + 1] = arg
        end
        parts[#parts + 1] = "}}"
        strings[i] = table.concat(parts)
    end
    local expanded = mw.text.split(
        frame:preprocess(table.concat(strings, "\x01")), "\x01"
    )
    local result = {}
    for i, key in ipairs(keys) do
        result[key] = expanded[i]
    end
    return result
end
-- Helper function
-- Returns true or false depending if the page exist
function p.page_exists(title_text)
    local title_obj = mw.title.new(title_text)
    return title_obj and title_obj.exists or false
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Pages included on this page: