Advertisement
1lann

shop

Feb 13th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. os.loadAPI("libgo")
  2.  
  3. local primaryMon = nil
  4.  
  5. local colors = {
  6.     black     = 0x000000,
  7.     white     = 0xFFFFFF,
  8.     blue      = 0x1E88E5,
  9.     darkBlue  = 0x1565C0,
  10.     faintBlue = 0xE3F2FD,
  11.     blueGrey  = 0x607D8B,
  12.     blackish  = 0x212121,
  13.     red       = 0xF44336
  14. }
  15.  
  16. local theme = {
  17.     headerBg     = colors.blue,
  18.     headerText   = colors.faintBlue,
  19.     headerAccent = colors.darkBlue,
  20.  
  21.     bodyBg     = colors.white,
  22.     bodyText   = colors.blueGrey
  23. }
  24.  
  25. local function setup()
  26.     -- Primary monitor
  27.     for _, side in pairs(redstone.getSides()) do
  28.         if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
  29.             print("Monitor side: " .. side)
  30.             primaryMon = peripheral.wrap(side)
  31.         end
  32.     end
  33.  
  34.     if not primaryMon then
  35.         print("No monitor could be found.")
  36.         return
  37.     end
  38.  
  39.     -- Theming
  40.     local i = 0
  41.     for name, hex in pairs(theme) do
  42.         primaryMon.setPaletteColor(2^i, hex)
  43.         theme[name] = 2^i
  44.         i = i + 1
  45.     end
  46. end
  47.  
  48. local addressWidth = 12
  49. local priceWidth = 11
  50. local stockWidth = 6
  51. local function writeItem(m, item)
  52.     local x, y = m.getCursorPos()
  53.     local w, h = m.getSize()
  54.  
  55.     m.setBackgroundColor(theme.bodyBg)
  56.     m.setTextColor(theme.bodyText)
  57.     m.setCursorPos(3, y)
  58.     m.write(item.name)
  59.  
  60.     m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth - 1 - stockWidth, y)
  61.     m.write(tostring(item.stock))
  62.  
  63.     m.setTextColor(theme.headerBg)
  64.     m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth, y)
  65.     m.write(tostring(item.price) .. " KST/ea")
  66.  
  67.     m.setTextColor(theme.headerAccent)
  68.     m.setCursorPos(w - 1 - addressWidth, y)
  69.     m.write(item.address)
  70. end
  71.  
  72. local function writeHeader(m)
  73.     local x, y = m.getCursorPos()
  74.     local w, h = m.getSize()
  75.  
  76.     m.setBackgroundColor(theme.headerAccent)
  77.     m.setTextColor(theme.bodyBg)
  78.  
  79.     m.clearLine()
  80.     m.setCursorPos(3, y)
  81.     m.write("Item")
  82.  
  83.     m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth - 1 - stockWidth, y)
  84.     m.write("Stock")
  85.  
  86.     m.setCursorPos(w - 1 - addressWidth - 1 - priceWidth, y)
  87.     m.write("Price")
  88.  
  89.     m.setCursorPos(w - 1 - addressWidth, y)
  90.     m.write("Address")
  91. end
  92.  
  93. local function renderShop(m, items)
  94.     m.setTextScale(0.5)
  95.  
  96.     local w, h = m.getSize()
  97.  
  98.     -- Draw header
  99.     local headerHeight = 3
  100.     m.setBackgroundColor(theme.headerBg)
  101.     m.setTextColor(theme.headerText)
  102.     m.setCursorPos(1, 1)
  103.     m.clearLine()
  104.     m.setCursorPos(3, 2)
  105.     m.clearLine()
  106.     m.write("Cloud's Cache")
  107.     m.setCursorPos(1, 3)
  108.     m.clearLine()
  109.  
  110.     m.setCursorPos(1, 4)
  111.     writeHeader(m)
  112.  
  113.     -- Draw table body
  114.     m.setBackgroundColor(theme.bodyBg)
  115.     for i = headerHeight + 2, h do
  116.         m.setCursorPos(1, i)
  117.         m.clearLine()
  118.     end
  119.  
  120.     m.setCursorPos(3, headerHeight + 2)
  121.     m.setTextColor(theme.bodyText)
  122.     for i = headerHeight + 2, headerHeight + 1 + #items do
  123.         m.setCursorPos(1, i)
  124.         writeItem(m, items[i - headerHeight - 1])
  125.     end
  126.  
  127. end
  128.  
  129. local database = {
  130.     ["minecraft:cobblestone"] = {
  131.         name    = "Cobblestone",
  132.         price   = 0.05,
  133.         address = "cbs@klua.kst"
  134.     }
  135. }
  136.  
  137. local itemUpdateChan = "item_update"
  138. local function scanInv()
  139.     local itemMap = {}
  140.  
  141.     for slot = 1, 16 do
  142.         local stock = turtle.getItemCount(slot)
  143.  
  144.         if stock > 0 then
  145.             local item = turtle.getItemDetail(slot)
  146.             if not itemMap[item.name] then
  147.                 itemMap[item.name] = {stock = stock}
  148.             else
  149.                 local current = itemMap[item.name]
  150.                 current.stock = current.stock + stock
  151.             end
  152.         end
  153.     end
  154.  
  155.     local items = {}
  156.     for itemID, item in pairs(itemMap) do
  157.         if database[itemID] then
  158.             local entry = database[itemID]
  159.             table.insert(items, {
  160.                 name    = entry.name,
  161.                 price   = entry.price,
  162.                 address = entry.address,
  163.                 stock   = item.stock
  164.             })
  165.         end
  166.     end
  167.  
  168.     return items
  169. end
  170.  
  171. local function main()
  172.     setup()
  173.  
  174.     go(function()
  175.         while true do
  176.             renderShop(primaryMon, scanInv())
  177.             sleep(5)
  178.         end
  179.     end)
  180. end
  181.  
  182. libgo.runDispatcher(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement