Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local applicationName = "Essence"
- local rootViewName = "mainView"
- local protocol = "essenceFarmLogs"
- local active = false
- local testBtnId = "test_essence_request"
- local sortButtons = {
- name = "sort_name",
- total = "sort_total",
- avg = "sort_avg"
- }
- local PADDING_X, PADDING_Y = 3, 4
- local lastMessage = nil
- local snapshot = { table = nil, time = 0 }
- local lastAverages = {}
- local currentSortColumn, currentSortOrder = "total", "desc"
- local function SortTable(column, order)
- local sorted = {}
- for k, v in pairs(lastMessage or {}) do
- table.insert(sorted, { name = k, value = v, avg = lastAverages[k] or 0 })
- end
- table.sort(sorted, function(a, b)
- if column == "name" then
- if order == "asc" then
- return a.name:lower() < b.name:lower()
- else
- return a.name:lower() > b.name:lower()
- end
- elseif column == "total" then
- if order == "asc" then
- return a.value < b.value
- else
- return a.value > b.value
- end
- elseif column == "avg" then
- if order == "asc" then
- return a.avg < b.avg
- else
- return a.avg > b.avg
- end
- else
- return false
- end
- end)
- return sorted
- end
- local function HasAverages()
- for _ in pairs(lastAverages or {}) do return true end
- return false
- end
- local function SetSortMode(ctx, column, order)
- if column == "avg" and not HasAverages() then
- column, order = "total", "desc"
- end
- currentSortColumn, currentSortOrder = column, order
- for key, id in pairs(sortButtons) do
- local label = key:gsub("^%l", string.upper)
- local state, arrow = false, ""
- if key == column then
- state = true
- arrow = (order == "asc") and "^" or "v"
- end
- local visible = not (key == "avg" and not HasAverages())
- ctx.libs().button.update(id, {
- state = state,
- visible = visible,
- textOn = label .. arrow,
- textOff = label
- })
- end
- end
- local function rootView(ctx)
- return {
- name = rootViewName,
- init = function()
- local x, y = PADDING_X, PADDING_Y
- local W, H = ctx.os.size()
- ctx.libs().button.create({
- app = applicationName,
- view = rootViewName,
- name = testBtnId,
- x = W - 13,
- y = y,
- w = 8,
- h = 3,
- colorOn = colors.cyan,
- state = true,
- textOn = "Test",
- textOff = "60",
- textX = W - 12,
- textY = y + 1
- })
- local group = "sortButtons"
- local colW, colH, gapX = 8, 2, 1
- local startX, startY = x, y + 4
- local function addSortButton(id, label, offsetX)
- ctx.libs().button.create({
- app = applicationName,
- view = rootViewName,
- name = id,
- group = group,
- x = startX + offsetX,
- y = startY,
- w = colW,
- h = colH,
- colorOn = colors.orange,
- colorOff = colors.gray,
- state = false,
- textOn = label,
- textOff = label,
- textX = startX + offsetX + 1,
- textY = startY + 1
- })
- end
- addSortButton(sortButtons.name, "Name", 0)
- addSortButton(sortButtons.total, "Total", (colW + gapX))
- addSortButton(sortButtons.avg, "Avg", (colW + gapX) * 2)
- SetSortMode(ctx, currentSortColumn, currentSortOrder)
- end,
- draw = function(mon)
- local x, y = PADDING_X, PADDING_Y
- local prepared = ctx.libs().tablebuilder.prepare(
- lastMessage or {},
- { lastAverages, {}, {} },
- { "Name", "Total", "Avg/min" }
- )
- ctx.libs().button.update(testBtnId, {
- state = snapshot.table == nil,
- textOff = SnapshotCountdown()
- })
- ctx.libs().button.draw(testBtnId, mon)
- for key, id in pairs(sortButtons) do
- local visible = not (key == "avg" and not HasAverages())
- ctx.libs().button.update(id, { visible = visible })
- ctx.libs().button.draw(id, mon)
- end
- if not lastMessage then return end
- ctx.libs().draw.drawTitle(x + 3, y + 7, ctx.libs().tablebuilder.getRow(prepared, "HEADER"), colors.white,
- colors.black, mon)
- ctx.libs().draw.drawTitle(x + 3, y + 8, ctx.libs().tablebuilder.getRow(prepared, "SPACER"), colors.white,
- colors.black, mon)
- local linesPerPage, index = 15, 0
- local sorted = SortTable(currentSortColumn, currentSortOrder)
- for _, entry in ipairs(sorted) do
- local key = entry.name
- ctx.libs().draw.drawTitle(
- x + 3,
- y + 9 + index,
- ctx.libs().tablebuilder.getRow(prepared, key),
- colors.white,
- colors.black,
- mon
- )
- index = index + 1
- if index >= linesPerPage then break end
- end
- end,
- touch = function(x, y)
- if ctx.libs().button.isWithinBoundingBox(x, y, testBtnId) then
- TakeSnapshot(lastMessage)
- return
- end
- for key, id in pairs(sortButtons) do
- if ctx.libs().button.isWithinBoundingBox(x, y, id) then
- if key == "avg" and not HasAverages() then
- return -- Ignore touches on hidden avg buttons
- end
- if currentSortColumn == key then
- local newOrder = (currentSortOrder == "asc") and "desc" or "asc"
- SetSortMode(ctx, key, newOrder)
- else
- SetSortMode(ctx, key, "asc")
- end
- return
- end
- end
- end
- }
- end
- function SnapshotCountdown()
- if not snapshot.table then return nil end
- local elapsed = os.clock() - snapshot.time
- local remaining = 60 - elapsed
- return remaining > 0 and remaining or 0
- end
- function TakeSnapshot(currentTable)
- snapshot.table = {}
- for k, v in pairs(currentTable) do snapshot.table[k] = v end
- snapshot.time = os.clock()
- end
- function SnapshotReady()
- return snapshot.table and (os.clock() - snapshot.time >= 60)
- end
- function GetDifferences(currentTable)
- if not snapshot.table then return nil end
- local differences = {}
- for k, oldValue in pairs(snapshot.table) do
- differences[k] = (currentTable[k] or 0) - oldValue
- end
- return differences
- end
- function ClearSnapshot()
- snapshot.table = nil
- snapshot.time = 0
- end
- local views = { root = rootView }
- return {
- id = "essenceFarmLogs",
- name = applicationName,
- protocol = protocol,
- receive = function(ctx, sender, message)
- lastMessage = message
- if SnapshotReady() then
- lastAverages = GetDifferences(lastMessage) or {}
- ClearSnapshot()
- end
- end,
- create = function(ctx)
- for k, v in pairs(views) do
- local view = v(ctx)
- if view and view.init then view.init() end
- end
- end,
- destroy = function(ctx) ClearSnapshot() end,
- resume = function(ctx) active = true end,
- suspend = function(ctx)
- active = false
- ClearSnapshot()
- end,
- update = function(ctx, dt)
- if active then ctx.os.transmit("provideEssenceCount", protocol) end
- end,
- draw = function(ctx, mon, viewId)
- local v = views[viewId]
- if v and v(ctx).draw then v(ctx).draw(mon) end
- end,
- touch = function(ctx, x, y, viewId)
- local v = views[viewId]
- if v and v(ctx).touch then v(ctx).touch(x, y) end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment