Advertisement
kurruk

Request Monitor

May 21st, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. p = peripheral.wrap("top")
  2. m = peripheral.wrap("right")
  3. m.open(33880)
  4. p.setTextScale(1.5)
  5. p.setBackgroundColor(colors.black)
  6. p.setTextColor(colors.white)
  7. button={}
  8. amount = 0
  9.  
  10. function split(str, delim, maxNb)
  11.  
  12.   if string.find(str, delim) == nil then
  13.     return { str }
  14.   end
  15.  
  16.   if maxNb == nil or maxNb < 1 then
  17.     maxNb = 0
  18.   end
  19.  
  20.   local result = {}
  21.   local pat = "(.-)" ..delim.. "()"
  22.   local nb = 0
  23.   local lastPos
  24.  
  25.   for part, pos in string.gmatch(str, pat) do
  26.     nb = nb + 1
  27.     result[nb] = part
  28.     lastPos = pos
  29.     if nb == maxNb then
  30.       break
  31.     end
  32.   end
  33.  
  34.   if nb ~= maxNb then
  35.     result[nb + 1] = string.sub(str,lastPos)
  36.   end
  37.  
  38.   return result
  39.  
  40. end
  41.    
  42. function setTable(name, func, xmin, xmax, ymin, ymax)
  43.    button[name] = {}
  44.    button[name]["func"] = func
  45.    button[name]["active"] = false
  46.    button[name]["xmin"] = xmin
  47.    button[name]["ymin"] = ymin
  48.    button[name]["xmax"] = xmax
  49.    button[name]["ymax"] = ymax
  50. end
  51.  
  52. function hit()
  53.   amount = 0
  54.   p.setBackgroundColor(colors.blue)
  55.   p.clear()
  56.   button={}
  57.   setTable("1", save, 4, 10, 3, 7)
  58.   setTable("2", save, 12, 18, 3, 7)
  59.   setTable("3", save, 20, 26, 3, 7)
  60.   setTable("4", save, 4, 10, 9, 13)
  61.   setTable("5", save, 12, 18, 9, 13)
  62.   setTable("6", save, 20, 26, 9, 13)
  63.   setTable("7", save, 4, 10, 15, 19)
  64.   setTable("8", save, 12, 18, 15, 19)
  65.   setTable("9", save, 20, 26, 15, 19)
  66.   setTable("0", save, 28, 34, 9, 13)
  67.   setTable("Get Items", send, 28, 42, 15, 19)
  68.   setTable("Stack", stack, 28, 42, 3, 7)
  69.   setTable("reset", reset, 43, 48, 22, 22)
  70.   screenNum() --line 214
  71.   herp_der_derp() --line 76
  72.   event, side, x, y = os.pullEvent("monitor_touch")
  73.   checkxy2(x,y) --line 176
  74. end
  75.  
  76. function reset() --a reset button for the selection screen
  77.     hit()
  78. end
  79.  
  80. function herp_der_derp()
  81.     amount = tonumber(amount)
  82.     amount = tostring(amount)
  83.     p.setBackgroundColor(colors.lime)
  84.     p.setTextColor(colors.blue)
  85.     size = math.floor((42 - 36 - string.len(amount)) /2) + 1
  86.     place = math.floor(41 - string.len(amount) - 36) /2
  87.     p.setCursorPos(36,9)
  88.     for i=1, 7 do
  89.         p.write(" ")
  90.     end
  91.     p.setCursorPos(36,10)
  92.     for i=1, 7 do
  93.         p.write(" ")
  94.     end
  95.     p.setCursorPos(36,11)
  96.     for i=1, size do
  97.         p.write(" ")
  98.     end
  99.     p.write(amount)
  100.     p.write(" ")
  101.     for i=1, place do
  102.         p.write(" ")
  103.     end
  104.     p.setCursorPos(36,12)
  105.     for i=1, 7 do
  106.         p.write(" ")
  107.     end
  108.     p.setCursorPos(36,13)
  109.     for i=1, 7 do
  110.         p.write(" ")
  111.     end
  112.     p.setBackgroundColor(colors.black)
  113.     p.setTextColor(colors.white)
  114. end
  115.  
  116. function stack() -- button for selecting to add a stack
  117.     amountStack = 0
  118.     amount = tonumber(amount)
  119.     amountStack = hey
  120.     amountStack = 63
  121.     amount = amount + amountStack + 1
  122.     amount = tostring(amount)
  123.     herp_der_derp()
  124.     event, side, x, y = os.pullEvent("monitor_touch")
  125.     checkxy2(x,y)
  126. end
  127.  
  128. function save() -- a function to save the amount that has been entered
  129.    hey = tonumber(hey)
  130.    amount = amount..hey
  131.    amount = tostring(amount)
  132.    herp_der_derp()
  133.    event, side, x, y = os.pullEvent("monitor_touch")
  134.    checkxy2(x,y)
  135. end
  136.  
  137. function send() -- the requesting function
  138.     sent = 0
  139.     input = 0
  140.     amount = tonumber(amount)
  141.     if amount >= 64 then
  142.         sent = amount / 64
  143.         sent = math.floor(sent)
  144.         for i = 1, sent do
  145.             m.transmit(49794, 33880, thing..":64")
  146.             print("64")
  147.         end
  148.         sent = sent * 64
  149.         input = amount - sent
  150.         input = tostring(input)
  151.         m.transmit(49794, 33880, thing..":"..input)
  152.         print(input)
  153.         print (thing..":"..amount)
  154.     else
  155.         amount = tostring(amount)
  156.         m.transmit(49794, 33880, thing..":"..amount)
  157.         print (thing..":"..amount)
  158.     end
  159. end
  160.  
  161. function checkxy1(x, y)
  162.    for name, data in pairs(button) do
  163.       if y >=data["ymin"] and  y <= data["ymax"] then
  164.          if x >=data["xmin"] and x <= data["xmax"] then
  165.             mess = {}
  166.             mess = split(name, ":") --line 9 just splits it on the : and returns the first half
  167.             thing = tostring(mess[1])
  168.             data["func"]() --line 46
  169.          end
  170.       end
  171.    end
  172. end
  173.  
  174. function checkxy2(x, y)
  175.    for name, data in pairs(button) do
  176.       if y >=data["ymin"] and  y <= data["ymax"] then
  177.          if x >=data["xmin"] and x <= data["xmax"] then
  178.             hey = name
  179.             data["func"]()
  180.          end
  181.       end
  182.    end
  183.    
  184. end
  185.  
  186. function fillNum(text, color, bData)
  187.     p.setBackgroundColor(colors.lime)
  188.   p.setTextColor(colors.blue)
  189.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  190.   local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  191.   for j = bData["ymin"], bData["ymax"] do
  192.     p.setCursorPos(bData["xmin"], j)
  193.     if j == yspot then
  194.       for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  195.         if k == xspot then
  196.           p.write(text)
  197.         else
  198.           p.write(" ")
  199.         end
  200.       end
  201.     else
  202.       for i = bData["xmin"], bData["xmax"] do
  203.          p.write(" ")
  204.       end
  205.     end
  206.   end
  207.   p.setBackgroundColor(colors.black)
  208.   p.setTextColor(colors.white)
  209. end
  210.  
  211. function screenNum()
  212.   local currColor
  213.   for name,data in pairs(button) do
  214.     currColor = colors.lime
  215.     fillNum(name, currColor, data) --line 189
  216.   end
  217. end
  218.  
  219. function fill(text, color, bData)
  220.     p.setCursorPos(bData["xmin"], bData["ymin"])
  221.     p.write(text)
  222. end
  223.  
  224. function screen()
  225.    for name,data in pairs(button) do
  226.       fill(name, currColor, data) --line 222
  227.    end
  228. end
  229.  
  230. function hi()
  231.   print("working")
  232. end
  233.  
  234. function1 = function()
  235.   button={}
  236.   line = 2
  237.   item = 1
  238.   p.clear()
  239.   for i=1, 68 do
  240.  
  241.     m.transmit(32036, 33880, i..":XgFzwMe0")
  242.     local timerID1=os.startTimer(0.05)
  243.     event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  244.    
  245.     if message == nil then
  246.         print("nothing to display")
  247.     elseif line <=21 then
  248.         length = string.len(message) + 3
  249.         setTable(message, hit, 3, length, line, line) --line 36
  250.         screen()
  251.     elseif line <= 41 then
  252.         l = line - 20
  253.         length = string.len(message) + 3
  254.         setTable(message, hi, 20, length, l, l) --line 36
  255.         screen()
  256.     else
  257.         l = line - 40
  258.         length = string.len(message) + 3
  259.         setTable(message, hit, 33, length, l, l) --line 36
  260.         screen()
  261.     end
  262.     line = line + 1
  263.     sleep(0.05)
  264.   end
  265.   setTable("reset", hit, 44, 49, 22, 22)
  266.   screen()
  267.   local timerID = os.startTimer(300)
  268.   while true do
  269.     event, side, x, y = os.pullEvent()
  270.     if event == "monitor_touch" then
  271.       print(x..", "..y)
  272.       checkxy1(x,y) --line 161
  273.       break
  274.     elseif event=="timer" and side==timerID then
  275.         print("Refreshing")
  276.         break
  277.     end
  278.   end
  279. end
  280.  
  281. while true do
  282.   sleep(1)
  283.   function1() -- line 235
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement