Browse Source

Server Detection

tags/crossplatform
Madiwka 4 years ago
parent
commit
5af7f23dbe
2 changed files with 29 additions and 22 deletions
  1. +27
    -20
      main.lua
  2. +2
    -2
      src/baseGame.lua

+ 27
- 20
main.lua View File

@@ -51,7 +51,7 @@ rotation = 0
TEXT = "Nuclear Pong" TEXT = "Nuclear Pong"
currentKey = " " currentKey = " "
ptw = 10 ptw = 10
checkrate = 0.5
--CHECKING IF CONTROLS ARE TAKEN --CHECKING IF CONTROLS ARE TAKEN
danger = "none" danger = "none"
danger2 = "none" danger2 = "none"
@@ -751,6 +751,7 @@ function love.update(dt)


end end
serverinit = false serverinit = false
dserverinit = false
datawaspassedtimer = 0 datawaspassedtimer = 0
clientinit = false clientinit = false
function love.textinput(t) function love.textinput(t)
@@ -1650,32 +1651,38 @@ function split(s, delimiter)
end end
return result return result
end end
address, port = IP, 12345
function checkCurrentServer(dt) function checkCurrentServer(dt)

if dserverinit == false then
local socket = require "socket"
local address, port = IP, 12345
print(address)
udp = socket.udp()
udp:setpeername(address, port)
udp:settimeout(0)
dserverinit = true
end
if IP ~= address then dserverinit = false print(IP .. " " .. address)
end
ts = ts + dt ts = ts + dt
local socket = require "socket"
local address, port = IP, 12345
udp = socket.udp()
udp:setpeername(address, port)
udp:settimeout(0)
if ts > updaterate then
--print(ts)
if ts > checkrate then
status = "offline"
print("sent ping") print("sent ping")
udp:send("HELLO") udp:send("HELLO")
ts = 0
end
status = "offline"
local data local data
local datanumtest = 0
local datawaspassed = false
data = udp:receive() data = udp:receive()
repeat
datanumtest = datanumtest + 1
print("LATENCY: " .. tostring(datanumtest))
data = udp:receive()
if data then
datawaspassed = true
print("ReceivedINFO: " .. data)
if data then
print("got answer!")
local p = split(data, '|') local p = split(data, '|')
status = p[1]
print("answer is " .. status)
else
print("no response!")
end
ts = 0
end end
until not data


end end

+ 2
- 2
src/baseGame.lua View File

@@ -688,7 +688,7 @@ function menuDraw()
end end
if gameState == "chooseIP" then if gameState == "chooseIP" then
IPselect = {} IPselect = {}
if status == "offine" then
if status == "offline" then
love.graphics.printf("UNABLE TO CONNECT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") love.graphics.printf("UNABLE TO CONNECT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center")
elseif status == "nettest" then elseif status == "nettest" then
table.insert( table.insert(
@@ -708,7 +708,7 @@ function menuDraw()
table.insert( table.insert(
IPselect, IPselect,
newButton( newButton(
"Guest",
"Connect as Guest",
function() function()
globalState = "clienttest" globalState = "clienttest"
AGAINST_AI = 0 AGAINST_AI = 0


Loading…
Cancel
Save