Fews

Server

Jul 24th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. rednet.open("top")
  2. delayed_message ={}
  3. connected_device ={}
  4. index_message =0
  5. index_connected =0
  6. while true do
  7. print("Listening ...")
  8. id,message = rednet.receive(1)
  9. if message ~=nil then
  10. mode = string.find(message,"internet")
  11. if mode ~=nil then
  12. print(message)
  13. print("id : "..id)
  14. first = string.find(message,";")
  15. firstmode = string.sub(message,tonumber(first)+1) -- get first param
  16. second = string.find(message,";",tonumber(first)+1)
  17. if second ~= nil then -- if there second param
  18. firstmode = string.sub(message, tonumber(first)+1, tonumber(second)-1)
  19. end
  20. print(firstmode)
  21. if firstmode == "connect" then -- connect
  22. if tonumber(index_connected)>0 then --check connected or not
  23. connected=false
  24. for i=0,tonumber(index_connected)-1 do
  25. first = string.find(connected_device[tonumber(i)],",")
  26. ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
  27. ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
  28. print("id: "..tostring(i+1)..ids)
  29. if id == tonumber(ids) then
  30. rednet.send(id,"You Already Connected to internet \n your IP Adress : "..ip_adress)
  31. connected = true
  32. end
  33. end
  34. if connected == false then
  35. if tonumber(index_connected) <255 then
  36. ip_adress = "192.168.1."..tostring(index_connected+1)
  37. end
  38. connected_device[tonumber(index_connected)] = id..","..ip_adress
  39. index_connected = tonumber(index_connected)+1
  40. rednet.send(id,"Succes Connect to internet \n your IP Adress : "..ip_adress)
  41. end
  42. else
  43. if tonumber(index_connected) <255 then
  44. ip_adress = "192.168.1."..tostring(index_connected+1)
  45. end
  46. connected_device[tonumber(index_connected)] = id..","..ip_adress
  47. index_connected = tonumber(index_connected)+1
  48. rednet.send(id,"Succes Connect to internet \n your IP Adress : "..ip_adress)
  49. end
  50.  
  51. elseif firstmode =="getmyip" then -- get my ip
  52. print("getmyip")
  53. local gotip=false
  54. for i=0,tonumber(index_connected)-1 do
  55. first = string.find(connected_device[tonumber(i)],",")
  56. ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
  57. ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
  58. if id == tonumber(ids) then
  59. rednet.send(id,"Your IP Adress : "..ip_adress)
  60. gotip = true
  61. end
  62. end
  63. if gotip == false then
  64. rednet.send(id,"You are not connected to internet ")
  65. end
  66.  
  67. elseif firstmode == "seedevice" then -- see device
  68. for i=0,tonumber(index_connected)-1 do
  69. first = string.find(connected_device[tonumber(i)],",")
  70. ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
  71. ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
  72. print("index : "..i)
  73. print(first)
  74. print(ip_adress)
  75. if i == 0 then
  76. list_ip = ids.." | "..ip_adress
  77. else
  78. list_ip = list_ip.."\n"..ids.." | "..ip_adress
  79. end
  80. end
  81. print(list_ip)
  82. rednet.send(id,list_ip)
  83.  
  84. elseif firstmode == "tictactoe" then --play tictactoe
  85. print("play tictactoe")
  86. if tonumber(index_connected)>0 then --check connected or not
  87. local ip_destination = string.sub(message,second+1)
  88. print("ip destination : "..ip_destination)
  89. connected=false
  90. for i=0,tonumber(index_connected)-1 do
  91. first = string.find(connected_device[tonumber(i)],",")
  92. ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
  93. print("id : "..ids)
  94. ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
  95. print("device "..i.." "..connected_device[i])
  96. if ip_destination == ip_adress and id ~= tonumber(ids) then -- komputer tujuan sudah terkonek
  97. rednet.send(id,"IP Destination found")
  98. rednet.send(tonumber(ids),id.." Request to Play Tic Tac Toe")
  99. connected = true
  100. end
  101. end
  102. if connected == false then
  103. rednet.send(id,"IP Destination not found")
  104. end
  105. end
  106.  
  107. elseif firstmode == "tictactoe_start" then -- start tictactoe
  108. term.clear()
  109. term.setCursorPos(1,1)
  110. print("start tictactoe")
  111. local ip_destination = string.sub(message,second+1)
  112. for i=0,tonumber(index_connected)-1 do
  113. first = string.find(connected_device[tonumber(i)],",")
  114. ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
  115. ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
  116. if ip_destination == ip_adress then -- komputer tujuan sudah terkonek
  117. rednet.send(tonumber(ids),id.." Accept Your Request to Play Tic Tac Toe")
  118. end
  119. end
  120.  
  121. local player1 = tonumber(id)
  122. local player2 = tonumber(ids)
  123. print (player2)
  124. local endgame=false
  125. while endgame == false do
  126. id,message = rednet.receive()
  127. if string.find(message,"movetictactoe") ~=nil then
  128. local turn = string.sub(message,15,15)
  129. local x = string.sub(message,17,17)
  130. local y = string.sub(message,19,19)
  131. print("turn x y : "..turn.." "..x.." "..y)
  132. if id == player1 then -- pengirim adalah player 1
  133. rednet.send(player2,"movetictactoe;"..turn..";"..x..";"..y) -- kirim data ke player 2
  134. else
  135. rednet.send(player1,"movetictactoe;"..turn..";"..x..";"..y) -- kirim data ke player 1
  136. end
  137. elseif string.find(message,"finishtictactoe") ~=nil then
  138. endgame=true
  139. else
  140. print("not tictactoe command")
  141. end
  142.  
  143.  
  144.  
  145. end
  146. end
  147.  
  148. end
  149. end
  150. end
Add Comment
Please, Sign In to add comment