Advertisement
joebodo

miningStatus.lua

Apr 25th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 KB | None | 0 0
  1. os.loadAPI('apis.lua')
  2.  
  3. Peripheral.wrap('wireless_modem')
  4.  
  5. local bossId
  6. local state
  7. local status
  8.  
  9. Logger.disable()
  10.  
  11. local terminal = UI.term
  12.  
  13. if Peripheral.isPresent({ type = 'openperipheral_glassesbridge' }) then
  14.   terminal = UI.Glasses({
  15.     --height = 30,
  16.     --width = 40,
  17.     textScale = .5,
  18.   })
  19. elseif Peripheral.isPresent({ type = 'monitor' }) then
  20.   terminal = UI.Device({
  21.     deviceType = 'monitor',
  22.     textScale = .5
  23.   })
  24. end
  25.  
  26. local window = UI.Window({ parent = terminal })
  27. window:clear()
  28.  
  29. statusPage = UI.Page({
  30.   parent = window,
  31.   titleBar = UI.TitleBar({
  32.     title = 'Mining Status'
  33.   }),
  34.   statusInfo = UI.Window({
  35.     totalHolesProgressBar = UI.ProgressBar({
  36.       y = 7,
  37.       x = 2,
  38.       width = window.width - 2
  39.     }),
  40.     chunkHolesProgressBar = UI.ProgressBar({
  41.       y = 10,
  42.       x = 2,
  43.       width = window.width - 2
  44.     }),
  45.   }),
  46.   statusBar = UI.StatusBar({
  47.     backgroundColor = colors.blue
  48.   })
  49. })
  50.  
  51. --statusPage.titleBar:draw()
  52.  
  53. if terminal.height > 17 then
  54.   statusPage:add({
  55.     logTitleBar = UI.TitleBar({
  56.       title = 'Log Messages',
  57.       y = 13
  58.     }),
  59.     scrollingText = UI.ScrollingText({
  60.       backgroundColor = colors.green,
  61.       y = 14,
  62.       height = terminal.height - 13
  63.     })
  64.   })
  65.  
  66.   Message.addHandler('log', function(h, id, msg)
  67.     if bossId and id == bossId then
  68.       statusPage.scrollingText:write(os.clock() .. ' ' .. msg.contents)
  69.     end
  70.   end)
  71.  
  72.   Message.addHandler('logClient', function(h, id)
  73.     Message.send(id, 'logServer')
  74.   end)
  75. end
  76.  
  77. function statusPage.statusBar:draw()
  78.   if state then
  79.     self:setValue('status', state.status)
  80.   end
  81.   UI.StatusBar.draw(self)
  82. end
  83.  
  84. function statusPage.statusInfo:draw()
  85.   if not state or not status then
  86.     return
  87.   end
  88.   if state.chunks ~= -1 then
  89.     local totalHoles = state.chunks * 52 -- roughly
  90.     local percentDone = state.holes * 100 / totalHoles
  91.     self.totalHolesProgressBar:setProgress(percentDone)
  92.     self:centeredWrite(8, string.format('Total: %d of ~%d holes (%d%%)',
  93.       state.holes, totalHoles, percentDone))
  94.   else
  95.     self:centeredWrite(8, string.format('Total holes: %d', state.holes))
  96.     self.totalHolesProgressBar:setProgress(Util.random(100))
  97.   end
  98.  
  99.   local currentChunk = math.pow(state.diameter-2, 2) + state.chunkIndex + 1
  100.   if state.diameter == 1 then
  101.     currentChunk = 1
  102.   end
  103.   if state.chunks == -1 then
  104.     self:write(2, 3, string.format('Chunk   %d', currentChunk))
  105.   else
  106.     self:write(2, 3, string.format('Chunk   %d of %d', currentChunk, state.chunks))
  107.   end
  108.   self:write(2, 4, string.format('Miners  %d', status.count))
  109.  
  110.   if state.depth ~= 9000 then
  111.     self:write(2, 5, string.format('Depth   %d', state.depth))
  112.   end
  113.  
  114.   local totalHoles = 52 -- roughly
  115.   local holesRemaining = #state.locations
  116.   if holesRemaining > totalHoles then
  117.     totalHoles = holesRemaining
  118.   end
  119.   local percentDone = 100 - (holesRemaining * 100 / totalHoles)
  120.   self.chunkHolesProgressBar:setProgress(percentDone)
  121.   self:centeredWrite(11, string.format('Current Chunk: %d%%', percentDone))
  122.  
  123.   self.chunkHolesProgressBar:draw()
  124.   self.totalHolesProgressBar:draw()
  125. end
  126.  
  127. Message.addHandler('miningStatus', function(h, id, msg)
  128.   bossId = id
  129.   state = msg.contents.state
  130.   status = msg.contents.status
  131.   statusPage.statusInfo:draw()
  132.   statusPage.statusBar:draw()
  133. end)
  134.  
  135. Event.addHandler('heartbeat', function()
  136.   Message.send(bossId, 'getMiningStatus')
  137. end)
  138.  
  139. Event.addHandler('char', function()
  140.   Event.exitPullEvents()
  141. end)
  142.  
  143. statusPage:draw()
  144. Message.broadcast('getMiningStatus')
  145.  
  146. Event.heartbeat(5)
  147.  
  148. window:clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement