Last active
January 1, 2021 20:49
-
-
Save InternetUnexplorer/b19c99aafb4cbbf24ac13afe3c9ebe82 to your computer and use it in GitHub Desktop.
OpenComputers script to show various statistics about the number of items in an ME network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Total number of bits that can be stored in the system | |
local TOTAL_BITS = 1174405120 | |
-- Approximate number of seconds between average rate resets | |
local AVG_INTERVAL = 3600 -- 1 hour | |
local component = require "component" | |
local event = require "event" | |
local term = require "term" | |
local interface = component.me_interface | |
-- Calculate the current number of bits used in the system | |
local function getUsedBits() | |
local items = interface.getItemsInNetwork() | |
local total = 0 | |
for _, item in ipairs(items) do | |
total = total + 8 + item.size | |
end | |
return total | |
end | |
-- Write a vertically centered message on the screen | |
local function writeCentered(message, row, color) | |
local width = term.getViewport() | |
term.setCursor(math.max(0, (width - message:len()) / 2 + 1), row) | |
term.gpu().setForeground(color) | |
term.write(message, false) | |
end | |
-- Previous count value, used for rate calculation | |
local lastCount = nil | |
-- Numerator and denominator, used for calculating average rate | |
local avgNum = 0 | |
local avgDen = 0 | |
while true do | |
-- Get the current number of bits used in the system | |
local count = getUsedBits() | |
-- Format the first line without rate | |
local line1 = string.format("%d bits used", count) | |
-- If the previous count is available, calculate and append rate | |
if lastCount then | |
local diff = count - lastCount | |
line1 = line1..string.format(" (%+d bits/s)", diff) | |
end | |
-- Format the second line without time estimate | |
local line2 = string.format("~%d%% used", | |
math.floor(count / TOTAL_BITS * 100)) | |
-- If the rate is available, update the average and append time estimate | |
if lastCount then | |
avgNum = avgNum + (count - lastCount) | |
avgDen = avgDen + 1 | |
-- Calculate estimate and average | |
if avgNum > 0 and count < TOTAL_BITS then | |
local secsUntilFull = (TOTAL_BITS - count) / (avgNum / avgDen) | |
line2 = line2..string.format(" (~%d days until full)", | |
math.floor(secsUntilFull / 86400)) | |
elseif avgNum < 0 and count > 0 then | |
local secsUntilEmpty = count / -(avgNum / avgDen) | |
line2 = line2..string.format(" (~%d days until empty)", | |
math.floor(secsUntilEmpty / 86400)) | |
end | |
-- Reset average if AVG_INTERVAL has elapsed | |
if avgDen >= AVG_INTERVAL then | |
avgNum = 0 | |
avgDen = 0 | |
end | |
end | |
-- Clear the screen and write both lines | |
term.clear() | |
writeCentered(line1, 1, 0xFFFFFF) -- White | |
writeCentered(line2, 2, 0x686868) -- Gray | |
-- Update lastCount | |
lastCount = count | |
-- Wait one second between updates | |
if event.pull(1, "interrupted") then | |
-- Break on <C-c> | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment