onix331

Bestellung Host

Jul 20th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.60 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. hechtid = 2
  14.  
  15. function clear()--To clear the screen
  16.     term.clear()
  17.     term.setCursorPos(1,1)
  18. end
  19.  
  20. function main()
  21.     print("Initialising...")
  22.     rednet.host("TCiP", "authost")
  23.    
  24.     idok = "0"
  25.     pwok = "0"
  26.    
  27.     ids = {}
  28.     pws = {}
  29.    
  30.     file = fs.open("listedids", "r")
  31.     if file then
  32.         local line = file.readLine()
  33.         while line do
  34.             table.insert(ids, line)
  35.             line = file.readLine()
  36.         end
  37.         file.close()
  38.     end
  39.    
  40.     file1 = fs.open("passwds", "r")
  41.     if file1 then
  42.         local line = file1.readLine()
  43.         while line do
  44.             table.insert(pws, line)
  45.             line = file1.readLine()
  46.         end
  47.         file1.close()
  48.     end
  49.    
  50.     function getclientver()
  51.         clientid, clientvers, protocol = rednet.receive(2)
  52.         if clientvers==nil then
  53.             print ("Version check failed")
  54.             receive()
  55.         elseif clientvers==requiredvers then
  56.             print ("Version check ok")
  57.             rednet.send(tonumber(clientid), "ok")
  58.             sendmessage()
  59.         end
  60.     end
  61.    
  62.     function sendmessage()
  63.         message = fs.open("message", "r")
  64.         if message then
  65.             rednet.send(tonumber(clientid), message.readAll())
  66.             print (message)
  67.             message.close()
  68.         end
  69.         receive()
  70.     end
  71.    
  72.     function idcheck()
  73.         senderid, clientid, protocol = rednet.receive(20)
  74.         for i=1,#ids do
  75.             if clientid==ids[i] then
  76.                 rednet.send(tonumber(clientid), "ok")
  77.                 print("Idcheck ok (", clientid, ")")
  78.                 idok = "1"
  79.             end
  80.         end
  81.         if idok=="1" then
  82.             idok = "0"
  83.             passwordcheck()
  84.         else
  85.             rednet.send(tonumber(clientid), "notexist")
  86.             print("Idcheck not ok (", clientid, ")")
  87.             receive()
  88.         end
  89.     end
  90.            
  91.     function passwordcheck()
  92.         clientid, passwd, protocol = rednet.receive(10)
  93.         if passwd==nil then
  94.             print ("Client waited too long, continuining...")
  95.             receive()
  96.         end
  97.         for i=1,#pws do
  98.             if passwd==pws[i] then
  99.                 rednet.send(tonumber(clientid), "ok")
  100.                 pwok = "1"
  101.                 print("Passwordcheck ok (", passwd, ")")
  102.                 passwd = ""
  103.             end
  104.         end
  105.         if pwok=="1" then
  106.             pwok = "0"
  107.             getclientver()
  108.         else
  109.             rednet.send(tonumber(clientid), "wrong")
  110.             print("Passwordcheck not ok (", passwd, ")")
  111.             receive()
  112.         end
  113.     end
  114.    
  115.     function receive()
  116.         while true do
  117.            
  118.             clientid, message, protocol = rednet.receive(2)
  119.            
  120.             if protocol=="TCiP" then--To pair to the client
  121.                 rednet.send(clientid, "succesfull")
  122.                 print ("Succesfull paired, continuing...")
  123.                 idcheck()
  124.             end
  125.            
  126.             if protocol=="order" then--To receive orders
  127.                 print ("Client ", clientid, " has ordered something!")
  128.                 file = fs.open("orders", "a")
  129.                 file.writeLine(clientid)
  130.                 file.writeLine(message)
  131.                 file.writeLine(" ")
  132.                 file.close()
  133.                 print ("Got order!")
  134.                 rednet.send(clientid, "ok")
  135.             end
  136.            
  137.         end
  138.     end
  139.     clear()
  140.     print ("Irgendwas by onix331")
  141.     print ("Version ", ver)
  142.     print ("Waiting for pair request")
  143.  
  144.     receive()
  145. end
  146.  
  147. local function openRednet()
  148.   for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
  149.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  150.       rednet.open(side)
  151.       return side
  152.     end
  153.   end
  154.   print("no wifi present")
  155. end
  156.  
  157. modemSide = openRednet()
  158. if modemSide == nil then
  159.   print("No modem connected")
  160. else
  161.   print("Opened modem on "..modemSide)
  162. end
  163.  
  164. main()
Advertisement
Add Comment
Please, Sign In to add comment