Advertisement
Guest User

startup

a guest
Oct 25th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. modem = peripheral.wrap("left")
  2.  
  3. function EditDestinations()
  4. while true do
  5.   local lsDestinations
  6.   if fs.exists("destinations.ls") then
  7.     --Load the existing destinations
  8.     local file = fs.open("destinations.ls","r")
  9.     local data = file.readAll()
  10.     file.close()
  11.     lsDestinations = textutils.unserialize(data)
  12.   else
  13.     lsDestinations = {}
  14.   end
  15.  
  16.   --Display existing destinations
  17.   print("\n")
  18.   if #lsDestinations == 0 then
  19.     print("No destinations exist\n")
  20.   else
  21.     for i=1,#lsDestinations do
  22.       print(tostring(i)..". "..lsDestinations[i]["Display"])
  23.     end
  24.   end
  25.   --Display option
  26.   print("(a. Add)")
  27.   --Receive input
  28.   write("Your selection: ")
  29.   local selection = read()
  30.  
  31.   --Decode selection
  32.   if selection == "a" then
  33.     write("\n\nEnter display name: ")
  34.     local sName = read()
  35.     write("Enter ticket: ")
  36.     local sTicket = read()
  37.     --Box this up
  38.     local destination = {}
  39.     destination["Display"] = sName
  40.     destination["Ticket"] = sTicket
  41.     --Add to the list
  42.     table.insert(lsDestinations, destination)
  43.   else
  44.     local iSelection = tonumber(selection)
  45.     if iSelection ~= null then
  46.       if (iSelection <= #lsDestinations) then
  47.         print("\n\nDisplay: " .. lsDestinations[iSelection]["Display"])
  48.         print("Ticket: " .. lsDestinations[iSelection]["Ticket"])
  49.         --Give options
  50.         print("(d. Delete)")
  51.         print("(e. Edit Display Name)")
  52.         print("(t. Edit Ticket)")
  53.         write("Your selection: ")
  54.         selection = read()
  55.         --Decode selection
  56.         if selection == "d" then
  57.           table.remove(lsDestinations,iSelection)
  58.         elseif selection == "e" then
  59.           write("New Display: ")
  60.           local newName = read()
  61.           lsDestinations[iSelection]["Display"] = newName
  62.         elseif selection == "t" then
  63.           write("New Ticket: ")
  64.           local newTicket = read()
  65.           lsDestinations[iSelection]["Ticket"] = newTicket
  66.         else
  67.           print("\nInvalid selection")
  68.         end
  69.       end
  70.     else
  71.       print("\nInvalid selection")
  72.     end
  73.   end
  74.  
  75.   --Save any edits to the list
  76.   file = fs.open("destinations.ls","w")
  77.   file.write(textutils.serialize(lsDestinations))
  78.   file.close()
  79. end
  80. end
  81.  
  82. function ServeTickets()
  83. while true do
  84.   local event,side,freq,refreq,message = os.pullEvent("modem_message")
  85.   if (message == "List") then
  86.     --Load the list
  87.     local data
  88.     if fs.exists("destinations.ls") then
  89.       local file = fs.open("destinations.ls","r")
  90.       data = file.readAll()
  91.       file.close()
  92.     else
  93.       local lsEmpty = {}
  94.       data = textutils.serialize(lsEmpty)
  95.     end
  96.     --Send the list back on refreq
  97.     modem.transmit(refreq,refreq,data)
  98.   end
  99. end
  100. end
  101.  
  102. --Start the server
  103. print("Starting server...")
  104. modem.open(10)
  105. print("Server listening on frequency 10.\n\n")
  106. --Run EditDestinations and ServeTickets in parallel
  107. parallel.waitForAll(EditDestinations,ServeTickets)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement