SuperMcBrent

app_energy_storage

Oct 30th, 2025 (edited)
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | Gaming | 0 0
  1. -- Draconic Core Energy Reader app
  2.  
  3. local protocol = "energystorage"
  4. local active = false;
  5.  
  6. -- IDs for buttons
  7. local testBtnId = "test_energy_storage"
  8.  
  9. -- padding for top-left placement
  10. local PADDING_X, PADDING_Y = 3, 4
  11.  
  12. -- state variables
  13. local lastMessage = nil
  14.  
  15. -- views
  16. local function rootView(ctx)
  17.     return {
  18.         draw = function(ctx, mon)
  19.             local x, y = PADDING_X, PADDING_Y
  20.             local W, H = ctx.os.size()
  21.  
  22.             if not lastMessage then
  23.                 return
  24.             end
  25.  
  26.             local percent = math.floor((lastMessage.now / lastMessage.max * 100) + 0.5)
  27.  
  28.             -- circle and big value
  29.             ctx.libs().draw.drawCircle(x + 2, y + 2, 32, colors.purple, true, mon)
  30.             ctx.libs().draw.drawCircle(x + 2, y + 2, 32, ctx.libs().draw.rainbow(0.75), false, mon)
  31.             ctx.libs().draw.drawNumber(x + 8 + (#tostring(percent) - 1) * 3, y + 10, percent, colors.white, 1, false, mon)
  32.  
  33.             -- stats
  34.             ctx.libs().draw.drawTitle(x + 40, y + 2, "Draconic Energy Core Stats", colors.white, colors.black, mon)
  35.             ctx.libs().draw.drawTitle(x + 40, y + 3, string.rep("-", 26), colors.white, colors.black, mon)
  36.  
  37.             ctx.libs().draw.drawTitle(x + 40, y + 5,
  38.                 "Now: (" .. formatNumber(lastMessage.now) .. ") " .. lastMessage.now .. " RF", colors.white, colors
  39.             .black, mon)
  40.             ctx.libs().draw.drawTitle(x + 40, y + 7,
  41.                 "Max: (" .. formatNumber(lastMessage.max) .. ") " .. lastMessage.max .. " RF", colors.white, colors
  42.             .black, mon)
  43.             ctx.libs().draw.drawTitle(x + 40, y + 9,
  44.                 "Change: (" .. formatNumber(lastMessage.rate) .. ") " .. lastMessage.rate .. " RF/t", colors.white,
  45.                 colors.black, mon)
  46.             ctx.libs().draw.drawTitle(x + 40, y + 11, "Tier: " .. lastMessage.tier, colors.white, colors.black, mon)
  47.             ctx.libs().draw.drawTitle(x + 40, y + 13,
  48.                 "Input: (" .. formatNumber(lastMessage.input) .. ") " .. lastMessage.input .. " RF/t", colors.white,
  49.                 colors.black, mon)
  50.             ctx.libs().draw.drawTitle(x + 40, y + 15,
  51.                 "Output: (" .. formatNumber(lastMessage.output) .. ") " .. lastMessage.output .. " RF/t", colors.white,
  52.                 colors.black, mon)
  53.  
  54.             -- line above progress bar
  55.             ctx.libs().draw.drawTitle(1, H - 6, string.rep("-", W), colors.white, colors.black, mon)
  56.  
  57.             -- progress bar
  58.             ctx.libs().draw.drawTitle(2, H - 5, "0%", colors.white, colors.black, mon)
  59.             ctx.libs().draw.drawTitle(W - 4, H - 5, "100%", colors.white, colors.black, mon)
  60.             ctx.libs().draw.drawProg(2, H - 4, W - 2, 4, lastMessage.now, lastMessage.max, colors.lime, colors.gray, mon)
  61.         end,
  62.         touch = function(ctx, x, y)
  63.             if ctx.libs().button.isWithinBoundingBox(x, y, testBtnId) then
  64.                 ctx.os.transmit("provideCoreData", protocol)
  65.             end
  66.         end
  67.     }
  68. end
  69.  
  70. --- TODO move to a better location
  71. function formatNumber(n)
  72.     local suffixes = { "", "K", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc", "No", "Dc" }
  73.     local i = 1
  74.     while n >= 1000 and i < #suffixes do
  75.         n = n / 1000
  76.         i = i + 1
  77.     end
  78.     if n >= 100 then
  79.         return string.format("%.0f%s", n, suffixes[i])
  80.     elseif n >= 10 then
  81.         return string.format("%.1f%s", n, suffixes[i])
  82.     else
  83.         return string.format("%.2f%s", n, suffixes[i])
  84.     end
  85. end
  86.  
  87. local views = {
  88.     root = rootView
  89. }
  90.  
  91. return {
  92.     id = "energystorage",
  93.     name = "Core",
  94.     protocol = protocol,
  95.     receive = function(ctx, sender, message)
  96.         ---print(textutils.serializeJSON(message))
  97.         lastMessage = message
  98.     end,
  99.     create = function(ctx) end,
  100.     destroy = function(ctx) end,
  101.     resume = function(ctx)
  102.         active = true;
  103.     end,
  104.     suspend = function(ctx)
  105.         active = false;
  106.     end,
  107.     update = function(ctx, dt)
  108.         if active then ctx.os.transmit("provideCoreData", protocol) end
  109.     end,
  110.     draw = function(ctx, mon, viewId)
  111.         local v = views[viewId]
  112.         if v and v(ctx).draw then v(ctx).draw(ctx, mon) end
  113.     end,
  114.     touch = function(ctx, x, y, viewId)
  115.         local v = views[viewId]
  116.         if v and v(ctx).touch then v(ctx).touch(ctx, x, y) end
  117.     end
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment