glitchdetector icon

EMC List

glitchdetector | PRO | 10/14/23 03:16:58 PM UTC (Edited) | 0 ⭐ | 782 👁️ | Never ⏰ | []
Lua |

3.25 KB

|

None

|

0 👍

/

0 👎

-- Computercraft Program
-- by glitchdetector
 
function ReadableNumber(num, places, full)
    num = tonumber(num)
    if not num then return 0 end
    local ret
    local placeValue = ("%%.%df"):format(places or 0)
    local isNegative = num < 0
    num = math.abs(num)
    if num >= 1000000000000 then
        ret = placeValue:format(num / 1000000000000) .. (full and " trillion" or "T") -- trillion
    elseif num >= 1000000000 then
        ret = placeValue:format(num / 1000000000) .. (full and " billion" or "B") -- billion
    elseif num >= 1000000 then
        ret = placeValue:format(num / 1000000) .. (full and " million" or "M") -- million
    elseif num >= 1000 and not full then
        ret = placeValue:format(num / 1000) .. "k" -- thousand
    elseif num >= 1 then
        ret = num -- hundreds
    else
        ret = num
    end
    return (isNegative and "- " or "") .. ret
end
 
function GetEMC(link)
    local items = link.list()
    if not items then return 0 end
    if not items[1] then return 0 end
    return items[1].count or 0
end
 
function ClearScreen(monitor)
    monitor.clear()
    monitor.setCursorPos(1, 1)
end
 
function NextLine(monitor)
    local x, y = monitor.getCursorPos()
    monitor.setCursorPos(1, y + 1)
end
 
function WriteCenterAligned(monitor, text)
    local w, h = monitor.getSize()
    local x, y = monitor.getCursorPos()
    monitor.setCursorPos(math.floor(w / 2 - #text / 2), y)
    monitor.write(text)
end
 
function WriteLeftAligned(monitor, text)
    local x, y = monitor.getCursorPos()
    monitor.setCursorPos(1, y)
    monitor.write(text)
end
 
function WriteRightAligned(monitor, text)
    local w, h = monitor.getSize()
    local x, y = monitor.getCursorPos()
    monitor.setCursorPos(w - #text + 1, y)
    monitor.write(text)
end
 
-- Program Code
 
local Users = {
    {"top", "Glitch"},
    {"right", "Collins"},
}
 
for _, user in next, Users do
    user[3] = peripheral.wrap(user[1])
end
 
local monitor = peripheral.wrap("monitor_0")
local last = 0
local timespan = 5
local timespanFactor = 60 / timespan
 
local history = {}
function CalculateHistory(new)
    table.insert(history, new)
    while #history > timespanFactor do
        table.remove(history, 1)
    end
    local sum = 0
    for _, v in next, history do
        sum = sum + v
    end
    return sum / #history
end
 
while true do
    ClearScreen(monitor)
    WriteCenterAligned(monitor, "Energy Matter Covalence")
    NextLine(monitor)
    local sum = 0
    for n, user in next, Users do
        WriteLeftAligned(monitor, user[2])
        local EMC = GetEMC(user[3])
        sum = sum + EMC
        WriteRightAligned(monitor, ReadableNumber(EMC, 2, false) .. " EMC")
        NextLine(monitor)
    end
    WriteCenterAligned(monitor, ReadableNumber(sum, 2, true) .. " EMC")
 
    NextLine(monitor)
    local difference = (sum - last)
    local isNeg = difference < 0
    if isNeg then
        monitor.setTextColour(colors.red)
    else
        monitor.setTextColour(colors.green)
    end
    last = sum
    WriteCenterAligned(monitor, (isNeg and "" or "+ ") .. ReadableNumber(CalculateHistory(difference), 2, true) .. " / minute")
    monitor.setTextColour(colors.white)
 
    sleep(timespan)
end

Comments