Advertisement
Guest User

prog

a guest
Dec 21st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.62 KB | None | 0 0
  1. mon = peripheral.wrap("right")
  2. mon.setBackgroundColor(colors.black)
  3. mon.clear()
  4. w,h = mon.getSize()
  5. tw, th = term.getSize()
  6.  
  7. items = {}
  8.  
  9. print(tw)
  10. changeCostButton = {id = "changeCost", color = colors.green, text1 = {text = "Change", ydiff = 1}, text2 = {text = "Cost", ydiff = 2}, x1 = (tw/2)-(8/2)+1, y1 = 5, x2 = (tw/2)-(8/2)+1+7, y2 = 8}
  11. changeNameButton = {id = "changeName", color = colors.blue, text1 = {text = "Change", ydiff = 1}, text2 = {text = "Name", ydiff = 2}, x1 = (tw/2)-(8/2)+1, y1 = 11, x2 = (tw/2)-(8/2)+1+7, y2 = 14}
  12.  
  13. tempMoving = 0
  14.  
  15. function updateItems()
  16.   items = {}
  17.   file = io.open("items", "r")
  18.   for line in file:lines() do
  19.     if line ~= "" then
  20.       itemN, priceN = line:match("([^:]+):([^:]+)")
  21.       table.insert(items, {item = itemN, price = tonumber(priceN)})
  22.     end
  23.   end
  24.   file:close()
  25. end
  26. function updateMonitor()
  27.   mon.setBackgroundColor(colors.black)
  28.   mon.clear()
  29.   for i, v in pairs(items) do
  30.     mon.setCursorPos(1,i)
  31.     if i%2 == 0 then
  32.       mon.setBackgroundColor(colors.gray)
  33.       mon.clearLine()
  34.     else
  35.       mon.setBackgroundColor(colors.lightGray)
  36.       mon.clearLine()
  37.     end
  38.     mon.write(string.sub(v["item"],1,(w-string.len(" - $"..v["price"]))).." - $"..v["price"])
  39.   end
  40. end
  41. function showPassword()
  42.   term.setBackgroundColor(colors.black)
  43.   term.clear()
  44.   term.setCursorPos(1,1)
  45.   term.write("Password: ")
  46.   pass = read("*")
  47.   if pass == "temp" then
  48.     print("Access Granted")
  49.     sleep(0.5)
  50.     return true
  51.   elseif pass == "term" then
  52.     error("Terminating")
  53.   else
  54.     print("Wrong Password!")
  55.     sleep(3)
  56.     return false
  57.   end
  58. end
  59. function updateScreen()
  60.   term.setBackgroundColor(colors.black)
  61.   term.clear()
  62.   last = 0
  63.   for i, v in pairs(items) do
  64.     term.setCursorPos(1,i)
  65.     if i%2 == 0 then
  66.       term.setBackgroundColor(colors.gray)
  67.       term.clearLine()
  68.     else
  69.       term.setBackgroundColor(colors.lightGray)
  70.       term.clearLine()
  71.     end
  72.     term.write(v["item"].." - $"..v["price"])
  73.     last = i
  74.   end
  75.   if last < th then
  76.     term.setCursorPos(1, last+1)
  77.     term.setBackgroundColor(colors.green)
  78.     term.clearLine()
  79.     term.write("ADD ITEM")
  80.   end
  81. end
  82. function showDetails(num)
  83.   term.setBackgroundColor(colors.black)
  84.   term.clear()
  85.   item = items[num]
  86.   term.setCursorPos((tw/2)-(string.len(item["item"])/2)+1, 2)
  87.   term.write(item["item"])
  88.   term.setCursorPos((tw/2)-(string.len("$"..item["price"])/2)+1, 3)
  89.   term.write("$"..item["price"])
  90.   for i, v in pairs({changeCostButton, changeNameButton}) do
  91.     term.setBackgroundColor(v["color"])
  92.     for x = v["x1"], v["x2"] do
  93.       for y = v["y1"], v["y2"] do
  94.         term.setCursorPos(x,y)
  95.         term.write(" ")
  96.       end
  97.     end
  98.     term.setCursorPos(v["x1"]+(v["x2"]-v["x1"])/2-string.len(v["text1"]["text"])/2,v["y1"]+v["text1"]["ydiff"])
  99.     term.write(v["text1"]["text"])
  100.     term.setCursorPos(v["x1"]+(v["x2"]-v["x1"])/2-string.len(v["text2"]["text"])/2,v["y1"]+v["text2"]["ydiff"])
  101.     term.write(v["text2"]["text"])
  102.   end        
  103. end
  104. function changeDetail(det, num)
  105.   if det == "changeName" then
  106.     term.setBackgroundColor(colors.black)
  107.     term.clear()
  108.     term.setCursorPos(1,1)
  109.     term.write("New Name: ")
  110.     newName = read()
  111.     items[num]["item"] = newName
  112.   elseif det == "changeCost" then
  113.     term.setBackgroundColor(colors.black)
  114.     term.clear()
  115.     term.setCursorPos(1,1)
  116.     term.write("New Price: $")
  117.     newPrice = read()
  118.     if tonumber(newPrice) ~= nil then
  119.       items[num]["price"] = tonumber(newPrice)
  120.     end
  121.   end
  122.   saveItems()
  123. end
  124. function saveItems()
  125.   file = io.open("items", "w")
  126.   for i, v in pairs(items) do
  127.     file:write(v["item"]..":"..v["price"].."\n")
  128.   end
  129.  
  130.   file:close()
  131.  
  132.   updateItems()
  133.   updateMonitor()
  134. end
  135. function addItem()
  136.   term.setBackgroundColor(colors.black)
  137.   term.clear()
  138.   term.setCursorPos(1,1)
  139.   term.write("Item Name: ")
  140.   newName = read()
  141.   term.write("Item Price: $")
  142.   newPrice = read()
  143.   if tonumber(newPrice) ~= nil then
  144.     table.insert(items, {item = newName, price = tonumber(newPrice)})
  145.   end
  146.  
  147.   saveItems()
  148. end
  149. updateItems()
  150. updateMonitor()
  151. --updateScreen()
  152.  
  153. while true do
  154.   if showPassword() then
  155.     ev = ""
  156.     while ev ~= "key" do
  157.       updateScreen()
  158.       ev, d1, d2, d3 = os.pullEvent()
  159.       if ev == "key" then
  160.       elseif ev == "mouse_click" then
  161.         if d1 == 1 then
  162.           if d3 == #items+1 then
  163.             addItem()
  164.           elseif d3 <= #items then
  165.             showDetails(d3)
  166.             subev, subd1, subd2, subd3 = os.pullEvent()
  167.             if subev == "key" then
  168.             elseif subev == "mouse_click" then
  169.               for i, v in pairs({changeCostButton, changeNameButton}) do
  170.                 if subd2 > v["x1"] and subd2 < v["x2"] then
  171.                   if subd3 > v["y1"] and subd3 < v["y2"] then
  172.                     changeDetail(v["id"], d3)
  173.                     print("Changed")
  174.                     sleep(1)
  175.                   end
  176.                 end
  177.               end
  178.             end
  179.           end
  180.         elseif d1 == 2 then
  181.           if d3 <= #items then
  182.             table.remove(items, d3)
  183.             saveItems()
  184.           end
  185.         elseif d1 == 3 then
  186.           if d3 <= #items then
  187.             tempMoving = d3
  188.             subbd3 = 0
  189.             while subbd3 == 0 or subbd3 > #items or subbd3 == tempMoving do
  190.               subbev, subbd1, subbd2, subbd3 = os.pullEvent("mouse_click")
  191.             end
  192.             temp = items[tempMoving]
  193.             items[tempMoving] = items[subbd3]
  194.             items[subbd3] = temp
  195.             saveItems()
  196.           end
  197.         end
  198.       end  
  199.     end
  200.   end
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement