Advertisement
fusty

DoubleTradeos

Jan 3rd, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.93 KB | None | 0 0
  1. isFirst = true
  2. colors = {
  3.   2,
  4.   4,
  5.   8,
  6.   16,
  7.   32,
  8.   64,
  9.   256,
  10.   512,
  11.   1024,
  12.   4096,
  13.   8192}
  14.  
  15. function getItems(invName)
  16.   --print("Reading "..invName)
  17.   inv = peripheral.wrap(invName)
  18.  
  19.   if(inv.getInventorySize()) then
  20.     invSize = inv.getInventorySize()
  21.     contents = {}
  22.    
  23.     i = 1
  24.     while i<=invSize do
  25.       if inv.getStackInSlot(i) then
  26.         thisStack = inv.getStackInSlot(i)
  27.         thisName = thisStack["name"]
  28.         if contents[thisName] then
  29.           contents[thisName] = tonumber(contents[thisName])+tonumber(thisStack["qty"])
  30.         else
  31.           contents[thisName] = tonumber(thisStack["qty"])
  32.         end
  33.       end
  34.       i=i+1
  35.     end
  36.  
  37.     return contents
  38.   else
  39.     return false
  40.   end
  41. end
  42.  
  43. function getTrade(tradeName)
  44.   --print("Reading "..tradeName)
  45.   trader = peripheral.wrap(tradeName)
  46.   trade = {}
  47.   trade["want"] = {}
  48.   trade["offer"] = {}
  49.   --Ensure it exists
  50.   if(trader.getInventorySize()) then
  51.  
  52.     want = trader.getStackInSlot(1)
  53.     if(want) then
  54.       trade["want"]["name"] = want["name"]
  55.       trade["want"]["qty"] = want["qty"]
  56.     else
  57.       trade["want"]["name"] = "-----"
  58.       trade["want"]["qty"] = 0
  59.     end
  60.      
  61.     offer = trader.getStackInSlot(2)
  62.     if(offer) then
  63.       trade["offer"]["name"] = offer["name"]
  64.       trade["offer"]["qty"] = offer["qty"]
  65.     else
  66.       trade["offer"]["name"] = "-----"
  67.       trade["offer"]["qty"] = 0
  68.     end
  69.  
  70.   end
  71.  
  72.   return trade
  73.  
  74. end
  75.  
  76. function monSetCol(col)
  77.   x,y = mon.getCursorPos()
  78.   mon.setCursorPos(col*35+1,y)
  79. end
  80.  
  81. function monInc()
  82.   x,y = mon.getCursorPos()
  83.   mon.setCursorPos(1,y+1)
  84. end
  85.  
  86. while true do
  87.   local invs = {}
  88.   local trades = {}
  89.   local i = 1
  90.   local j = 1
  91.   --Get inventories and trade o mats and monitor
  92.   for k,v in pairs(peripheral.getNames()) do
  93.     if string.match(v,'iron') then
  94.       invs[i] = v
  95.       i = i+1
  96.     end
  97.     if string.match(v,'container') then
  98.       invs[i] = v
  99.       i = i+1
  100.     end
  101.     if string.match(v,'ender_chest') then
  102.       invs[i] = v
  103.       i = i+1
  104.     end
  105.     if string.match(v,'trade') then
  106.       trades[j] = v
  107.       j = j+1
  108.     end
  109.     if string.match(v,'monitor') then
  110.       mon = peripheral.wrap(v)
  111.       mon.setTextScale(1)
  112.       mon.setCursorPos(1,1)
  113.       if(isFirst) then
  114.         mon.setBackgroundColor(32768)
  115.         mon.clear()
  116.         isFirst = false
  117.       end
  118.     end
  119.   end
  120.  
  121.   table.sort(invs)
  122.   table.sort(trades)
  123.   data = {}
  124.  
  125.   --Combine into mega table
  126.  
  127.   for k,v in pairs(invs) do
  128.     data[k] = {}
  129.     data[k]["inv"] = v
  130.  
  131.     //Get pairs of Trade-O-Mats now
  132.     data[k]["trade1"] = trades[k]
  133.     data[k]["trade2"] = trades[k+9]
  134.   end
  135.  
  136.  
  137.   col = 0
  138.   dispString = {}
  139.   for k,v in pairs(data) do
  140.     inv = v["inv"]
  141.     trade1Name = v["trade1"]
  142.     trade2Name = v["trade2"]
  143.     items = getItems(inv)
  144.     trade1 = getTrade(trade1Name)
  145.     trade2 = getTrade(trade2Name)
  146.     if(items) then
  147.       if(trade1) then
  148.         wantStock = 0
  149.         offerStock = 0
  150.         for i,j in pairs(items) do
  151.           if trade1["offer"]["name"] == i then
  152.             offerStock = j
  153.           end
  154.  
  155.           if trade1["want"]["name"] == i then
  156.             wantStock = j
  157.           end
  158.  
  159.         end
  160.         wantName = string.gsub(string.gsub(string.gsub(string.gsub(trade1["want"]["name"],'item.',''),'.name',''),'tile.',''),'extrautils:','')
  161.         offerName = string.gsub(string.gsub(string.gsub(string.gsub(trade1["offer"]["name"],'item.',''),'.name',''),'tile.',''),'extrautils:','')
  162.         color = colors[k%11+1]
  163.         mon.setBackgroundColor(32768)
  164.         mon.clearLine()
  165.         mon.setTextColor(color) --Black
  166.         monSetCol(0)
  167.     mon.write("["..k.."]")
  168.         if(trade1["offer"]["name"] == "Empty") then
  169.           mon.setBackgroundColor(16384)
  170.           mon.setTextColor(1) --White
  171.         end      
  172.         if(offerStock <= 0) then
  173.           mon.setBackgroundColor(16384) --Red
  174.           mon.setTextColor(1) --White
  175.           offerStock = "EMPTY!"
  176.         end
  177.  
  178.         mon.write(" Offer: "..string.sub(offerName,1,18).." x"..trade1["offer"]["qty"])
  179.         monSetCol(1)
  180.         mon.setBackgroundColor(32768)
  181.         mon.setTextColor(color) --Black
  182.         mon.write('Want: '..string.sub(wantName,1,18).." x"..trade1["want"]["qty"])
  183.         monSetCol(2)
  184.         mon.setBackgroundColor(32768)
  185.         mon.setTextColor(color) --Black
  186.         monInc()
  187.       else
  188.       if(trade2) then
  189.         wantStock = 0
  190.         offerStock = 0
  191.         for i,j in pairs(items) do
  192.           if trade2["offer"]["name"] == i then
  193.             offerStock = j
  194.           end
  195.  
  196.           if trade2["want"]["name"] == i then
  197.             wantStock = j
  198.           end
  199.  
  200.         end
  201.         wantName = string.gsub(string.gsub(string.gsub(string.gsub(trade2["want"]["name"],'item.',''),'.name',''),'tile.',''),'extrautils:','')
  202.         offerName = string.gsub(string.gsub(string.gsub(string.gsub(trade2["offer"]["name"],'item.',''),'.name',''),'tile.',''),'extrautils:','')
  203.         color = colors[k%11+1]
  204.         mon.setBackgroundColor(32768)
  205.         mon.clearLine()
  206.         mon.setTextColor(color) --Black
  207.         monSetCol(0)
  208.     mon.write("["..k+9.."]")
  209.         if(trade2["offer"]["name"] == "Empty") then
  210.           mon.setBackgroundColor(16384)
  211.           mon.setTextColor(1) --White
  212.         end      
  213.         if(offerStock <= 0) then
  214.           mon.setBackgroundColor(16384) --Red
  215.           mon.setTextColor(1) --White
  216.           offerStock = "EMPTY!"
  217.         end
  218.  
  219.         mon.write(" Offer: "..string.sub(offerName,1,18).." x"..trade2["offer"]["qty"])
  220.         monSetCol(1)
  221.         mon.setBackgroundColor(32768)
  222.         mon.setTextColor(color) --Black
  223.         mon.write('Want: '..string.sub(wantName,1,18).." x"..trade2["want"]["qty"])
  224.         monSetCol(2)
  225.         mon.setBackgroundColor(32768)
  226.         mon.setTextColor(color) --Black
  227.         monInc()
  228.       end
  229.     end
  230.   end
  231.   print('Resting . . .')
  232.   sleep(5)
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement