Advertisement
Manuzor

elevator server: serve

Dec 4th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. -- pastebin: j1br869P
  2.  
  3. local side = "back"
  4. local senderID = 0
  5.  
  6. local layout = {
  7.   up = {
  8.     name = "up",
  9.     redstoneSide = "left",
  10.     redstonePulses = 10
  11.   },
  12.   down = {
  13.     name = "down",
  14.     redstoneSide = "right",
  15.     redstonePulses = 10
  16.   }
  17. }
  18.  
  19. -- helper functions
  20.  
  21. function directionOK(direction)
  22.   for k,dir in pairs(layout) do
  23.     if direction == dir.name then
  24.       return true
  25.     end
  26.   end
  27.   return false
  28. end
  29.  
  30. function getLayout(direction)
  31.   for key,value in pairs(layout) do
  32.     if value.name == direction then
  33.       return value
  34.     end
  35.   end
  36.   return nil
  37. end
  38.  
  39. -- main functions
  40.  
  41. function work(direction)
  42.  
  43.   if not directionOK(direction) then
  44.     print("Invalid direction: " .. direction)
  45.     return
  46.   end
  47.  
  48.   print("Going " .. direction .. "...")
  49.  
  50.   local theLayout = getLayout(direction)
  51.  
  52.   if layout == nil then
  53.     print("Something terrible happened...")
  54.     return
  55.   end
  56.  
  57.   local redpulseArgs = theLayout.redstoneSide .. " " .. tostring(theLayout.redstonePulses)
  58.  
  59.   shell.run("redpulse", redpulseArgs)
  60.  
  61.   print("Done!")
  62. end
  63.  
  64. function repl()
  65.   while true do
  66.     print("Waiting for input from remote client...")
  67.     local id, message = rednet.receive()
  68.     if id == senderID then
  69.       work(message)
  70.     end
  71.   end
  72. end
  73.  
  74. function main()
  75.   term.clear()
  76.   term.setCursorPos(1, 1)
  77.  
  78.   rednet.open(side)
  79.   repl()
  80.   rednet.close(side)
  81. end
  82.  
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement