Advertisement
PeachGaming

ticketapp

Mar 24th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. local mside = "back" --NFC Modem Side
  2. local channel = 4021--NFC Send Channel(must match with the NFC Receive Channel of NFC Receiver Device)
  3. local rchannel = 4022--NFC Response Channel
  4. local menu_options = { --This is our menu table. It contains basic data about the menu
  5.   [1] = {text="Pay and Open Gate", color=colors.green},
  6.   [2] = {text="Exit", color=colors.red},
  7.  
  8. }
  9. term.clear()
  10. term.setBackgroundColor(colors.white)
  11. term.clear()
  12. local termX, termY = term.getSize() --The x/y size of the terminal
  13. local function menuDraw(selected) --Our main draw function
  14.   local yPos = termY/2 - #menu_options/2 --The initial y position
  15.   for index, data in pairs(menu_options) do
  16.     menu_options[index].bounds = { --Create a new table in each option with the boundary data
  17.       x1 = termX/2 - (#data.text+4)/2;
  18.       x2 = termX/2 + (#data.text+4)/2;
  19.       y = yPos;
  20.     }
  21.    
  22.    
  23.     term.setTextColor(data.color)
  24.     term.setCursorPos(data.bounds.x1, data.bounds.y)
  25.  
  26.     local text =
  27.       index==selected and "[ "..data.text.." ]" or
  28.       "  "..data.text.."  " --Essentially an if statement, but in a contracted form
  29.     term.write(text)
  30.     yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  31.   end
  32. end
  33.  
  34. local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  35.   for index, data in pairs(menu_options) do
  36.     if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
  37.       return index --Returns the index of the clicked option
  38.     end
  39.   end
  40.   return false --If it went through the entire for loop without success, return false
  41. end
  42.  
  43.  
  44.  
  45. local selector = 1 --Our selector
  46. while true do --The main loop. I would generally put this inside of a function for a program.
  47.   menuDraw(selector) --Draw the menu first
  48.   local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  49.   if e[1] == "key" then --If it's a key...
  50.     if e[2] == keys.down then -- ... and it's the down arrow
  51.       selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
  52.     elseif e[2] == keys.up then
  53.       selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
  54.     elseif e[2] == keys.enter then
  55.       break --Break out of the loop
  56.     end
  57.   elseif e[1] == "mouse_click" then
  58.     local value = checkClick(e[3], e[4]) --Check the mouse click
  59.     if value then --If checkClick returns a value and not false
  60.       selector = value --Set the selector to that value and break out of the loop
  61.       break --um what was this
  62.     end
  63.   end
  64. end
  65.  
  66.  
  67. term.setTextColor(colors.green)
  68. term.setBackgroundColor(colors.cyan)
  69.  
  70. term.clear()
  71.  
  72.  
  73. if selector == 1 then
  74. local modem = peripheral.wrap("back")
  75.  
  76. modem.open(rchannel)
  77. -- Asking how far it is
  78. local printcentred =
  79. function(text)
  80. local maxw, maxh = term.getSize() --screen dimensions
  81. local curx, cury = term.getCursorPos() --cursor pos
  82. term.setCursorPos((maxw-#text)/2,cury) --set it to the point where write will make the text centred
  83. term.write(text) --write the text
  84. term.setCursorPos(curx,cury+1) --return the cursor to the same position on the next line
  85. end
  86. print("Put your device over the terminal.")
  87.  
  88.  --Wait for reply
  89. while true do
  90. modem.transmit(channel, rchannel, "hey")
  91.  
  92.  
  93.  local checkevent, dmside, drschannel, drchannel, dmessage, ddistance = os.pullEvent("modem_message")
  94.  
  95.  if ddistance < 3 and dmessage == "okay fam" then
  96.  --Send that ship
  97.  modem.transmit(channel, rchannel, "open plz")
  98. end
  99. if dmessage == "failure" then
  100.  
  101. sleep(0.5)
  102. end
  103. end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement