carmopereira / DU-Container-Monitor

This code is destined to Dual Universe game. This is a Container monitor for pure and ore materials. It can be used to monitor container percentages and it readouts. It was design to have a Container Hub element side each item name.
GNU General Public License v3.0
12 stars 12 forks source link

Feature request: Add parsing of target amount from container name #5

Open mkmarq opened 3 years ago

mkmarq commented 3 years ago

I'd like to be able to specify a target amount in the container hub name.

To be used in 2 ways:

  1. Color based on percentage of target amount
  2. Difference between current amount and target amount displayed, along with current amount and capacity For example, 200 units in container that holds 1000 with target of 500 might display 200/300/1000 Another example, 600 units in container that holds 1000 with target of 500 might display 600/-100/1000
mkmarq commented 3 years ago

Just got this working on my local board.

I'm using the description, if it's present and a valid number, as my target.

Code snippets: changed material_readout_cell

function material_readout_cell(name)
    local x = {}
    local html = ""
    --system.print("NAME: "..hasValue(name))
    if name == nil then
        html = [[<div class="row"><div class="name"></div><div class="percentage"></div><div class="progressbar" style="width:0%; background-color:pink">&nbsp;</div></div>]]
    else
        --system.print(name)
        x = getMaterial(name)
        --system.print("ENTRA 1")
        if next(x) == nil then
            --system.print("array vazio")
            html = [[<div class="rowinfo">PLEASE CONNECT<br>]] .. name .. [[</div>]]
        else
            local percentage = getFromArray(x, 9)
            local targetpercentage = nil
            local rawdescription = getFromArray(x, 6)
            local description = ""
            local quantity = getFromArray(x, 8)
            local totalcontainerqtdinlitre = getFromArray(x, 10)
            if rawdescription == "empty" then 
                description = "" 
            elseif tonumber(rawdescription) ~= nil then 
                description = getValueWithUnits(tonumber(rawdescription))
                if tonumber(rawdescription) > 0 then
                    targetpercentage = quantity * 100 / tonumber(rawdescription)
                end
            else 
                description = rawdescription
            end
            local color = nil
            if targetpercentage ~= nil then
                color = progresscolor(targetpercentage)
            else
                color = progresscolor(percentage)
            end
            --print_r(x)
            --system.print("MATERIAL: "..name.." | %: "..percentage)
            html = [[<div class="row"><div class="name">]] .. _G[name][1] .. [[</div><div class="percentage">]] .. percentage .. [[%</div><div class="progressbar" style="width:]] .. percentage .. [[%; background-color:]] .. color .. [[">&nbsp;</div>
            <div class="desc">]]
                .. description .. [[</div><div class="info">]] .. litreParse(quantity, totalcontainerqtdinlitre, tonumber(rawdescription)) .. [[</div></div>]]
        --system.print(progresscolor(percentage))
        end
    end
    return html
end

changed litreParse (uses code snippet included in #6 comments as base)

function litreParse(litre, total, target)
    local parsed = getValueWithUnits(litre) .. "/"
    if target ~= nil and tonumber(target) ~= nil then
        parsed = parsed .. getValueWithUnits(target-litre) .. "/"
    end
    return parsed .. getValueWithUnits(total)
end

function getValueWithUnits(value)
    local size = ""
    if value < 1000 and value > -1000 then
        size = "L"
    elseif value < 1000000 and value > -1000000 then
        value = value / 1000
        size = "kL"
    else
        value = value / 1000000
        size = "kt"
    end

    return string.format("%.1f",value)..size
end

Feel free to incorporate into the project.

If you do, then I'll be able to keep using updates as they come ^_^