Advertisement
imposiblaa

elevServer

Oct 20th, 2023 (edited)
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. modem = peripheral.find("modem")
  2. modem.open(5296)
  3. db = {}
  4.  
  5.    
  6.  
  7. commands = {
  8.     alert = function(input)
  9.         assert(type(input) == "string", "helloWorld expects a string for its input")
  10.         print("ALERT: " .. input)
  11.         return true
  12.     end,
  13.    
  14.     updateStatus = function(id, dataObj)
  15.         assert(type(id) == "number", "updateStatus expects an int for \"id\"")
  16.         assert(type(dataObj) == "table", "updateStatus expects a table for \"dataObj\"")
  17.         print(id)
  18.         db[id] = dataObj
  19.     end,
  20.  
  21.     requestTransport = function(departure, destination)
  22.     assert(type(departure) == "number", "requestTransport expects an int for \"departure\"")
  23.     assert(type(destination) == "number", "requestTransport expects an int for \"destination\"")
  24.  
  25.     commandRunner("prep", destination, {})
  26.     tTime = os.startTimer(3)
  27.     goForTransport = false
  28.     repeat
  29.         print("repeating")
  30.         eventData = {os.pullEvent()}
  31.         event = eventData[1]
  32.         if event == "timer" and eventData[2] == tTime then
  33.             print("Could not contact destination floor (timed out)")
  34.             return -1
  35.         end
  36.         if event == "modem_message" and eventData[5].cmd == "error" then return end
  37.     until event == "modem_message" and eventData[5].cmd == "ready"
  38.  
  39.     db[destination]["expected"] = db[departure]["cur"]
  40.     db[departure]["expected"] = db[destination]["cur"]
  41.     commandRunner("updateDb", -1, {db})
  42.     end,
  43.  
  44.     getFloors = function()
  45.     commandRunner("updateDb", -1, {db})
  46.     end
  47. }
  48.  
  49.  
  50. function commandParser(cList)
  51.     event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  52.     print(textutils.serialise(message))
  53.     if commands[message.cmd] ~= nil and message.args ~= nil then
  54.         ran, err = pcall(commands[message.cmd], table.unpack(message.args))
  55.         if not ran then
  56.             print(("Error in \"%s\": %s"):format(message.cmd, err))
  57.         end
  58.     else
  59.         print(("No command named \"%s\" found"):format(message.cmd))
  60.     end
  61. end
  62.  
  63. function commandRunner(command, target, arguments)
  64.     assert(type(command) == "string", "commandRunner expects a string for its input \"command\"")
  65.     assert(type(target) == "number", "commandRunner expects a number for its input \"target\"")
  66.     assert(type(arguments) == "table", "commandRunner expects a table for its input \"arguments\"")
  67.     modem.transmit(5296,5296, {cmd=command, tgt=target, args=arguments})
  68. end
  69.  
  70.    
  71.  
  72. commandRunner("init", -1, {os.computerID()})
  73. while true do
  74.     commandParser(commands)
  75.     print(textutils.serialise(db))
  76.            
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement