Advertisement
WhiteFire_Sondergaar

Bean Counter Display

May 17th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. -- Bean Counter Scoreboard
  2. -- by Fenthis
  3.  
  4. local pdirs = { "top", "bottom", "front", "back", "left", "right" }
  5. local monitor
  6. local modem
  7. -- Status channel
  8. local status_channel = 21231
  9. local monitor_scale = 1.0
  10.  
  11.  
  12. for i = 1, #pdirs do
  13.   p = peripheral.wrap(pdirs[i])
  14.   if p then
  15.     if p.getSize then
  16.       monitor = p
  17.     end
  18.     if p.transmit then
  19.       modem = p
  20.     end
  21.   end
  22. end
  23.  
  24. if not monitor then
  25.   error("Can not find the monitor!")
  26. end
  27.  
  28. if not modem then
  29.   print("WARNING: No modem found, unable to broadcast updates.")
  30. end
  31.  
  32. if not item_map then
  33.     dofile("item_map.lua")
  34. end
  35.  
  36. -- Initialize the monitor
  37. function center(str, y)
  38.     monitor.setCursorPos((mx - #str) / 2, y)
  39.     monitor.write(str)
  40. end
  41.  
  42. function setup_monitor()
  43.     monitor.clear()
  44.     monitor.setTextScale(monitor_scale)
  45.     mx, my = monitor.getSize()
  46.     cols = math.floor(mx / 25)
  47.     col_width = math.floor(mx / cols)
  48.     rows = my - 2
  49.     center("Incoming Mining Results", 1)
  50.     monitor.setCursorPos(1,2)
  51.     monitor.write(string.rep("-", mx))
  52.     line = ""
  53.     for i = 1, cols do
  54.         line = line .. string.rep(" ", col_width - 1)
  55.         if i ~= cols then
  56.             line = line .. "|"
  57.         end
  58.     end
  59.     for i = 3, my do
  60.         monitor.setCursorPos(1,i)
  61.         monitor.write(line)
  62.     end
  63. end
  64.  
  65. --- Pads str to length len with char from right
  66. -- Because string.format("%-10s", ...) is broken in Minecraft QQ
  67. string.lpad = function(str, len, char)
  68.     if char == nil then char = ' ' end
  69.     return string.sub(str .. string.rep(char, len - #str), 1, len)
  70. end
  71.  
  72. string.rpad = function(str, len, char)
  73.     if char == nil then char = ' ' end
  74.     return string.sub(string.rep(char, len - #str) .. str, 1, len)
  75. end
  76.  
  77. function sorter(a, b)
  78.     return sortlist[a]
  79. end
  80.  
  81. function display(data)
  82.     local sortlist = {}
  83.  
  84.     function sorter(a, b)
  85.         return data[sortlist[a]] < data[sortlist[b]]
  86.     end
  87.  
  88.     for k, v in pairs(data) do
  89.         table.insert(sortlist, k)
  90.     end
  91.  
  92.     table.sort(sortlist, sorter)
  93.  
  94.     for i = 1, #sortlist do
  95.         k = sortlist[i]
  96.         v = data[sortlist[i]]
  97.         col = math.floor((i - 1) / rows)
  98.         row = ((i - 1) % rows)
  99.         monitor.setCursorPos(
  100.             1 + (col * col_width),
  101.             3 + row)
  102.         monitor.write(
  103.             string.lpad(item_map:get_name_by_uuid(k), col_width - 7) ..
  104.             " " ..
  105.             string.rpad(tostring(v), 5)
  106.             )
  107.         i = i + 1
  108.     end
  109. end
  110.  
  111. setup_monitor()
  112.  
  113. -- Initialize the Modem
  114. modem.open(status_channel)
  115.  
  116. --
  117. print("Starting... press Q to exit.")
  118.  
  119. -- Main Loop
  120. while true do
  121.     e, modem, chan, reply, text = os.pullEvent()
  122.     if e == "modem_message" then
  123.         data = textutils.unserialize(text)
  124.         display(data)
  125.     elseif e == "char" and (modem == "Q" or modem == "q") then
  126.         return
  127.     end
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement