onix331

Bestellung Client

Jul 20th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. --This code is developed by me (onix331) and is available under the GNU General Public License (Version 3)
  2. --For all CC-Versions Universal
  3. --See http://opensource.org/licenses/GPL-3.0
  4. --Have fun
  5.  
  6. ver = "0.1 Alpha"--For updates
  7. status = "---"--You can modify the init-status that will be transferred to the host at the beginning
  8.  
  9. --It is usually not necessary to modify the next variables
  10.  
  11. requiredvers = "0.1"
  12. isvers = "0.1"
  13.  
  14. function clear()--To clear the screen
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17. end
  18.  
  19. function main()
  20.     clear()
  21.     print ("Initialising...")
  22.    
  23.     clientid = tostring(os.getComputerID())
  24.     clear()
  25.    
  26.     print("Please enter your password!")
  27.     passwd = read()
  28.    
  29.     hostid = rednet.lookup("TCiP")
  30.     if hostid then
  31.         print("Server found! continuining!")
  32.     else
  33.         print("Server not found! Might be down right now, please try again later!")
  34.         error()
  35.     end
  36.    
  37.     function pair()
  38.         clear()
  39.         print ("Trying to pair with host")
  40.         hostid = rednet.lookup("TCiP")
  41.         rednet.send(hostid, "pair", "TCiP")
  42.         hostid, message, protocol = rednet.receive(0.5)
  43.         if message=="succesfull" then
  44.             print("Succesfully paired, continuing...")
  45.         else
  46.             print("Pairing not successfull, trying again...")
  47.             pair()
  48.         end
  49.     end
  50.  
  51.     function getinfo()
  52.         rednet.send(hostid, isvers, "version")
  53.         hostid, message, version = rednet.receive(0.5)
  54.         if message==nil then
  55.             print("Error, host did not respond in time!")
  56.             error()
  57.         elseif message=="ok" then
  58.             print ("Versioncheck ok")
  59.             getmessage()
  60.         else
  61.             print ("Versioncheck not ok, please get the latest version!")
  62.             error()
  63.         end
  64.     end
  65.    
  66.     function getmessage()
  67.         hostid, message, TCiP = rednet.receive(1)
  68.         if message==nil then
  69.             print("Error, host did not respond in time!")
  70.             error()
  71.         else
  72.             print (message)
  73.             orderinput()
  74.         end
  75.     end
  76.     function sendinitial()--Send the initial data
  77.         rednet.send(hostid, clientid)
  78.         hostid, message, TCiP = rednet.receive(0.5)
  79.         if message=="ok" then
  80.             clear()
  81.             rednet.send(hostid, passwd, "pass")
  82.             hostid, response, TCiP = rednet.receive(0.5)
  83.             if response=="ok" then
  84.                 getinfo()
  85.             else
  86.                 print ("An error occured. Please reconnect!")
  87.                 error()
  88.             end
  89.         else
  90.             print ("Error! Your Computer is not registered!")
  91.             error()
  92.         end
  93.     end
  94.  
  95.     function orderinput()
  96.         print ("Wie viele Items sollen bestellt werden?")
  97.         number = read()
  98.        
  99.         if fs.exists("temporaer") then
  100.             shell.run ("rm temporaer")
  101.         end
  102.        
  103.         order = fs.open("temporaer", "a")
  104.        
  105.         for i = 1, number do
  106.             orderitems = read()
  107.             order.writeLine(orderitems)
  108.         end
  109.         order.close()
  110.         order = fs.open("temporaer", "r")
  111.         rednet.send(hostid, order.readAll(), "order")
  112.         order.close()
  113.         hostid, orderok = rednet.receive(0.5)
  114.         if orderok=="ok"
  115.         then
  116.             print ("Bestellung erfolgreich angenommen!")
  117.         else
  118.             print ("Fehler bei der Bestellung, bitte spaeter erneut Versuchen!")
  119.         end
  120.     end
  121.    
  122.     --Print header
  123.     clear()
  124.     print ("Irgendwas by onix331")
  125.     print ("Version ", ver)
  126.     os.sleep(2)
  127.    
  128.     --Fire up the main program
  129.     pair()
  130.     sendinitial()
  131.  
  132. end
  133.  
  134. local function openRednet()
  135.   for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
  136.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  137.       rednet.open(side)
  138.       return side
  139.     end
  140.   end
  141.   print("no wifi present")
  142. end
  143.  
  144. modemSide = openRednet()
  145. if modemSide == nil then
  146.   print("No modem connected")
  147. else
  148.   print("Opened modem on "..modemSide)
  149. end
  150.  
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment