Advertisement
systox

Untitled

Oct 17th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. -- Client
  2.  
  3. local timeout = 0.5
  4.  
  5. rednet.open("bottom")
  6.  
  7. function clear()
  8. term.clear()
  9. term.setCursorPos(1,1)
  10. end
  11.  
  12. function start1()
  13. local file = fs.open("network/Router","r")
  14. IP = file.readLine()
  15. Password = file.readLine()
  16. file.close()
  17.  
  18. rednet.send(tonumber(IP),tonumber(Password))
  19.  
  20. local scrap,message = rednet.receive(timeout)
  21. if message == "connected!" then
  22. print("connected!")
  23. programm()
  24. elseif message == "!" then
  25. fs.delete("network/Router")
  26. os.reboot()
  27. end
  28. end
  29.  
  30. function start()
  31. write("Router IP: ")
  32. local IP = tonumber(io.read())
  33. write("Router Password: ")
  34. local Password = tonumber(io.read())
  35. rednet.send(IP,Password)
  36. local scrap,message = rednet.receive()
  37. if tonumber(scrap) == IP then
  38. if message == "Type!" then
  39. write("Type: ")
  40. rednet.send(IP,io.read())
  41. local scrap,message = rednet.receive()
  42. if message == "connected!" then
  43. clear()
  44. print("Connected")
  45. fs.makeDir("network")
  46. local file = fs.open("network/Router","w")
  47. file.writeLine(IP)
  48. file.writeLine(Password)
  49. file.close()
  50. print("Router-settings saved to network/Router !")
  51. programm()
  52. end
  53. else
  54. print("Message: "..message)
  55. start()
  56. end
  57. else
  58. print("IP: "..scrap)
  59. start()
  60. end
  61. end
  62.  
  63. function getRouter()
  64. local file = fs.open("network/Router","r")
  65. if file then
  66. IP = file.readLine()
  67. Password = file.readLine()
  68. file.close()
  69. end
  70. return IP,Password
  71. end
  72.  
  73. function getData()
  74. IP,Passwort = getRouter()
  75. local scrap,message = rednet.receive()
  76. if tonumber(scrap) == tonumber(IP) then
  77. rednet.send(tonumber(IP),"received!")
  78. return message
  79. end
  80. end
  81.  
  82. function sendData(id,data)
  83. IP,Passwort = getRouter()
  84. rednet.send(tonumber(IP),"SD")
  85. local scrap,message = rednet.receive()
  86. if (message) ~= nil then
  87. if message == "data" then
  88. rednet.send(tonumber(IP),id)
  89. rednet.send(tonumber(IP),data)
  90. local scrap,message = rednet.receive()
  91. if (message) ~= nil then
  92. if message == "0" then
  93. return false
  94. elseif message == "1" then
  95. return true
  96. end
  97. end
  98. end
  99. end
  100. end
  101.  
  102. function testplayer()
  103. print("TEST!")
  104. os.loadAPI("ocs/apis/sensor")
  105. local prox = sensor.wrap("back")
  106. local targets = prox.getTargets()
  107. for k, v in pairs(targets) do
  108. if ((v.Name) == "Player") then
  109. targetsDetail = prox.getTargetDetails(k)
  110. if not (targetsDetail == nil) then
  111. playername = targetsDetail.Username
  112. print(playername)
  113. return (playername)
  114. else
  115. print("NIL")
  116. end
  117. else
  118. print("no player")
  119. end
  120. end
  121. end
  122.  
  123. function programm()
  124. -- put your programm in here!
  125. while true do
  126. local message = getData()
  127. print(message)
  128. while true do
  129. if (testplayer() == message) then
  130. rs.setOutput("top",true)
  131. sleep(2)
  132. rs.setOutput("top",false)
  133. sendData(7,"TRUE")
  134. programm()
  135. end
  136. end
  137. end
  138. end
  139.  
  140. if fs.exists("network/Router") then
  141. start1()
  142. else
  143. start()
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement