Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- delayed_message ={}
- connected_device ={}
- index_message =0
- index_connected =0
- while true do
- print("Listening ...")
- id,message = rednet.receive(1)
- if message ~=nil then
- mode = string.find(message,"internet")
- if mode ~=nil then
- print(message)
- print("id : "..id)
- first = string.find(message,";")
- firstmode = string.sub(message,tonumber(first)+1) -- get first param
- second = string.find(message,";",tonumber(first)+1)
- if second ~= nil then -- if there second param
- firstmode = string.sub(message, tonumber(first)+1, tonumber(second)-1)
- end
- print(firstmode)
- if firstmode == "connect" then -- connect
- if tonumber(index_connected)>0 then --check connected or not
- connected=false
- for i=0,tonumber(index_connected)-1 do
- first = string.find(connected_device[tonumber(i)],",")
- ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
- ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
- print("id: "..tostring(i+1)..ids)
- if id == tonumber(ids) then
- rednet.send(id,"You Already Connected to internet \n your IP Adress : "..ip_adress)
- connected = true
- end
- end
- if connected == false then
- if tonumber(index_connected) <255 then
- ip_adress = "192.168.1."..tostring(index_connected+1)
- end
- connected_device[tonumber(index_connected)] = id..","..ip_adress
- index_connected = tonumber(index_connected)+1
- rednet.send(id,"Succes Connect to internet \n your IP Adress : "..ip_adress)
- end
- else
- if tonumber(index_connected) <255 then
- ip_adress = "192.168.1."..tostring(index_connected+1)
- end
- connected_device[tonumber(index_connected)] = id..","..ip_adress
- index_connected = tonumber(index_connected)+1
- rednet.send(id,"Succes Connect to internet \n your IP Adress : "..ip_adress)
- end
- elseif firstmode =="getmyip" then -- get my ip
- print("getmyip")
- local gotip=false
- for i=0,tonumber(index_connected)-1 do
- first = string.find(connected_device[tonumber(i)],",")
- ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
- ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
- if id == tonumber(ids) then
- rednet.send(id,"Your IP Adress : "..ip_adress)
- gotip = true
- end
- end
- if gotip == false then
- rednet.send(id,"You are not connected to internet ")
- end
- elseif firstmode == "seedevice" then -- see device
- for i=0,tonumber(index_connected)-1 do
- first = string.find(connected_device[tonumber(i)],",")
- ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
- ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
- print("index : "..i)
- print(first)
- print(ip_adress)
- if i == 0 then
- list_ip = ids.." | "..ip_adress
- else
- list_ip = list_ip.."\n"..ids.." | "..ip_adress
- end
- end
- print(list_ip)
- rednet.send(id,list_ip)
- elseif firstmode == "tictactoe" then --play tictactoe
- print("play tictactoe")
- if tonumber(index_connected)>0 then --check connected or not
- local ip_destination = string.sub(message,second+1)
- print("ip destination : "..ip_destination)
- connected=false
- for i=0,tonumber(index_connected)-1 do
- first = string.find(connected_device[tonumber(i)],",")
- ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
- print("id : "..ids)
- ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
- print("device "..i.." "..connected_device[i])
- if ip_destination == ip_adress and id ~= tonumber(ids) then -- komputer tujuan sudah terkonek
- rednet.send(id,"IP Destination found")
- rednet.send(tonumber(ids),id.." Request to Play Tic Tac Toe")
- connected = true
- end
- end
- if connected == false then
- rednet.send(id,"IP Destination not found")
- end
- end
- elseif firstmode == "tictactoe_start" then -- start tictactoe
- term.clear()
- term.setCursorPos(1,1)
- print("start tictactoe")
- local ip_destination = string.sub(message,second+1)
- for i=0,tonumber(index_connected)-1 do
- first = string.find(connected_device[tonumber(i)],",")
- ids = string.sub(connected_device[tonumber(i)],0,tonumber(first)-1)
- ip_adress = string.sub(connected_device[tonumber(i)],tonumber(first)+1)
- if ip_destination == ip_adress then -- komputer tujuan sudah terkonek
- rednet.send(tonumber(ids),id.." Accept Your Request to Play Tic Tac Toe")
- end
- end
- local player1 = tonumber(id)
- local player2 = tonumber(ids)
- print (player2)
- local endgame=false
- while endgame == false do
- id,message = rednet.receive()
- if string.find(message,"movetictactoe") ~=nil then
- local turn = string.sub(message,15,15)
- local x = string.sub(message,17,17)
- local y = string.sub(message,19,19)
- print("turn x y : "..turn.." "..x.." "..y)
- if id == player1 then -- pengirim adalah player 1
- rednet.send(player2,"movetictactoe;"..turn..";"..x..";"..y) -- kirim data ke player 2
- else
- rednet.send(player1,"movetictactoe;"..turn..";"..x..";"..y) -- kirim data ke player 1
- end
- elseif string.find(message,"finishtictactoe") ~=nil then
- endgame=true
- else
- print("not tictactoe command")
- end
- end
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment