SuperMcBrent

app_essence_farm_logs

Oct 31st, 2025 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.42 KB | Gaming | 0 0
  1. local applicationName = "Essence"
  2. local rootViewName = "mainView"
  3. local protocol = "essenceFarmLogs"
  4. local active = false
  5.  
  6. local testBtnId = "test_essence_request"
  7.  
  8. local sortButtons = {
  9.     name = "sort_name",
  10.     total = "sort_total",
  11.     avg = "sort_avg"
  12. }
  13.  
  14. local PADDING_X, PADDING_Y = 3, 4
  15. local lastMessage = nil
  16. local snapshot = { table = nil, time = 0 }
  17. local lastAverages = {}
  18.  
  19. local currentSortColumn, currentSortOrder = "total", "desc"
  20.  
  21. local function SortTable(column, order)
  22.     local sorted = {}
  23.     for k, v in pairs(lastMessage or {}) do
  24.         table.insert(sorted, { name = k, value = v, avg = lastAverages[k] or 0 })
  25.     end
  26.  
  27.     table.sort(sorted, function(a, b)
  28.         if column == "name" then
  29.             if order == "asc" then
  30.                 return a.name:lower() < b.name:lower()
  31.             else
  32.                 return a.name:lower() > b.name:lower()
  33.             end
  34.         elseif column == "total" then
  35.             if order == "asc" then
  36.                 return a.value < b.value
  37.             else
  38.                 return a.value > b.value
  39.             end
  40.         elseif column == "avg" then
  41.             if order == "asc" then
  42.                 return a.avg < b.avg
  43.             else
  44.                 return a.avg > b.avg
  45.             end
  46.         else
  47.             return false
  48.         end
  49.     end)
  50.  
  51.     return sorted
  52. end
  53.  
  54.  
  55. local function HasAverages()
  56.     for _ in pairs(lastAverages or {}) do return true end
  57.     return false
  58. end
  59.  
  60. local function SetSortMode(ctx, column, order)
  61.     if column == "avg" and not HasAverages() then
  62.         column, order = "total", "desc"
  63.     end
  64.  
  65.     currentSortColumn, currentSortOrder = column, order
  66.  
  67.     for key, id in pairs(sortButtons) do
  68.         local label = key:gsub("^%l", string.upper)
  69.         local state, arrow = false, ""
  70.         if key == column then
  71.             state = true
  72.             arrow = (order == "asc") and "^" or "v"
  73.         end
  74.         local visible = not (key == "avg" and not HasAverages())
  75.         ctx.libs().button.update(id, {
  76.             state = state,
  77.             visible = visible,
  78.             textOn = label .. arrow,
  79.             textOff = label
  80.         })
  81.     end
  82. end
  83.  
  84. local function rootView(ctx)
  85.     return {
  86.         name = rootViewName,
  87.         init = function()
  88.             local x, y = PADDING_X, PADDING_Y
  89.             local W, H = ctx.os.size()
  90.  
  91.             ctx.libs().button.create({
  92.                 app = applicationName,
  93.                 view = rootViewName,
  94.                 name = testBtnId,
  95.                 x = W - 13,
  96.                 y = y,
  97.                 w = 8,
  98.                 h = 3,
  99.                 colorOn = colors.cyan,
  100.                 state = true,
  101.                 textOn = "Test",
  102.                 textOff = "60",
  103.                 textX = W - 12,
  104.                 textY = y + 1
  105.             })
  106.  
  107.             local group = "sortButtons"
  108.             local colW, colH, gapX = 8, 2, 1
  109.             local startX, startY = x, y + 4
  110.  
  111.             local function addSortButton(id, label, offsetX)
  112.                 ctx.libs().button.create({
  113.                     app = applicationName,
  114.                     view = rootViewName,
  115.                     name = id,
  116.                     group = group,
  117.                     x = startX + offsetX,
  118.                     y = startY,
  119.                     w = colW,
  120.                     h = colH,
  121.                     colorOn = colors.orange,
  122.                     colorOff = colors.gray,
  123.                     state = false,
  124.                     textOn = label,
  125.                     textOff = label,
  126.                     textX = startX + offsetX + 1,
  127.                     textY = startY + 1
  128.                 })
  129.             end
  130.  
  131.             addSortButton(sortButtons.name, "Name", 0)
  132.             addSortButton(sortButtons.total, "Total", (colW + gapX))
  133.             addSortButton(sortButtons.avg, "Avg", (colW + gapX) * 2)
  134.  
  135.             SetSortMode(ctx, currentSortColumn, currentSortOrder)
  136.         end,
  137.  
  138.         draw = function(mon)
  139.             local x, y = PADDING_X, PADDING_Y
  140.             local prepared = ctx.libs().tablebuilder.prepare(
  141.                 lastMessage or {},
  142.                 { lastAverages, {}, {} },
  143.                 { "Name", "Total", "Avg/min" }
  144.             )
  145.  
  146.             ctx.libs().button.update(testBtnId, {
  147.                 state = snapshot.table == nil,
  148.                 textOff = SnapshotCountdown()
  149.             })
  150.  
  151.             ctx.libs().button.draw(testBtnId, mon)
  152.  
  153.             for key, id in pairs(sortButtons) do
  154.                 local visible = not (key == "avg" and not HasAverages())
  155.                 ctx.libs().button.update(id, { visible = visible })
  156.                 ctx.libs().button.draw(id, mon)
  157.             end
  158.  
  159.             if not lastMessage then return end
  160.  
  161.             ctx.libs().draw.drawTitle(x + 3, y + 7, ctx.libs().tablebuilder.getRow(prepared, "HEADER"), colors.white,
  162.                 colors.black, mon)
  163.             ctx.libs().draw.drawTitle(x + 3, y + 8, ctx.libs().tablebuilder.getRow(prepared, "SPACER"), colors.white,
  164.                 colors.black, mon)
  165.  
  166.             local linesPerPage, index = 15, 0
  167.             local sorted = SortTable(currentSortColumn, currentSortOrder)
  168.             for _, entry in ipairs(sorted) do
  169.                 local key = entry.name
  170.                 ctx.libs().draw.drawTitle(
  171.                     x + 3,
  172.                     y + 9 + index,
  173.                     ctx.libs().tablebuilder.getRow(prepared, key),
  174.                     colors.white,
  175.                     colors.black,
  176.                     mon
  177.                 )
  178.                 index = index + 1
  179.                 if index >= linesPerPage then break end
  180.             end
  181.         end,
  182.  
  183.         touch = function(x, y)
  184.             if ctx.libs().button.isWithinBoundingBox(x, y, testBtnId) then
  185.                 TakeSnapshot(lastMessage)
  186.                 return
  187.             end
  188.  
  189.             for key, id in pairs(sortButtons) do
  190.                 if ctx.libs().button.isWithinBoundingBox(x, y, id) then
  191.                     if key == "avg" and not HasAverages() then
  192.                         return -- Ignore touches on hidden avg buttons
  193.                     end
  194.                     if currentSortColumn == key then
  195.                         local newOrder = (currentSortOrder == "asc") and "desc" or "asc"
  196.                         SetSortMode(ctx, key, newOrder)
  197.                     else
  198.                         SetSortMode(ctx, key, "asc")
  199.                     end
  200.                     return
  201.                 end
  202.             end
  203.         end
  204.     }
  205. end
  206.  
  207. function SnapshotCountdown()
  208.     if not snapshot.table then return nil end
  209.     local elapsed = os.clock() - snapshot.time
  210.     local remaining = 60 - elapsed
  211.     return remaining > 0 and remaining or 0
  212. end
  213.  
  214. function TakeSnapshot(currentTable)
  215.     snapshot.table = {}
  216.     for k, v in pairs(currentTable) do snapshot.table[k] = v end
  217.     snapshot.time = os.clock()
  218. end
  219.  
  220. function SnapshotReady()
  221.     return snapshot.table and (os.clock() - snapshot.time >= 60)
  222. end
  223.  
  224. function GetDifferences(currentTable)
  225.     if not snapshot.table then return nil end
  226.     local differences = {}
  227.     for k, oldValue in pairs(snapshot.table) do
  228.         differences[k] = (currentTable[k] or 0) - oldValue
  229.     end
  230.     return differences
  231. end
  232.  
  233. function ClearSnapshot()
  234.     snapshot.table = nil
  235.     snapshot.time = 0
  236. end
  237.  
  238. local views = { root = rootView }
  239.  
  240. return {
  241.     id = "essenceFarmLogs",
  242.     name = applicationName,
  243.     protocol = protocol,
  244.     receive = function(ctx, sender, message)
  245.         lastMessage = message
  246.         if SnapshotReady() then
  247.             lastAverages = GetDifferences(lastMessage) or {}
  248.             ClearSnapshot()
  249.         end
  250.     end,
  251.     create = function(ctx)
  252.         for k, v in pairs(views) do
  253.             local view = v(ctx)
  254.             if view and view.init then view.init() end
  255.         end
  256.     end,
  257.     destroy = function(ctx) ClearSnapshot() end,
  258.     resume = function(ctx) active = true end,
  259.     suspend = function(ctx)
  260.         active = false
  261.         ClearSnapshot()
  262.     end,
  263.     update = function(ctx, dt)
  264.         if active then ctx.os.transmit("provideEssenceCount", protocol) end
  265.     end,
  266.     draw = function(ctx, mon, viewId)
  267.         local v = views[viewId]
  268.         if v and v(ctx).draw then v(ctx).draw(mon) end
  269.     end,
  270.     touch = function(ctx, x, y, viewId)
  271.         local v = views[viewId]
  272.         if v and v(ctx).touch then v(ctx).touch(x, y) end
  273.     end
  274. }
  275.  
Advertisement
Add Comment
Please, Sign In to add comment