From d636b73b9b5dc8b8a2de7ee85471296ce3806948 Mon Sep 17 00:00:00 2001 From: Madiwka Date: Tue, 25 Aug 2020 13:49:40 +0600 Subject: [PATCH] Added server verification --- main.lua | 5 ++++- src/baseGame.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/main.lua b/main.lua index 18593f3..6c27e17 100644 --- a/main.lua +++ b/main.lua @@ -1690,7 +1690,10 @@ function split(s, delimiter) end address, port = IP, 12345 function checkCurrentServer(dt) - if IP ~= "" then + if GetIPType(IP) ~= 1 then + status = "offline" + end + if GetIPType(IP) == 1 then if dserverinit == false then print("Switching IP") socket = require "socket" diff --git a/src/baseGame.lua b/src/baseGame.lua index 836f6d8..73b4573 100644 --- a/src/baseGame.lua +++ b/src/baseGame.lua @@ -1,3 +1,4 @@ +local counter = 0 function basegame(dt) if gameMode == "reversegame" then reversegame(dt) @@ -706,12 +707,13 @@ function menuDraw() "Check Server", function() IP = IPnew + counter = 0 end ) ) if status == "offline" then - love.graphics.printf("UNABLE TO CONNECT", 0, VIRTUAL_HEIGHT / 2, VIRTUAL_WIDTH, "center") - elseif status == "nettest" then + animateConnection() + elseif status == "nettest" and IP == IPnew then table.insert( IPselect, newButton( @@ -725,7 +727,7 @@ function menuDraw() ) ) - elseif status == "clienttest" then + elseif status == "clienttest" and IP == IPnew then table.insert( IPselect, newButton( @@ -1313,4 +1315,49 @@ function clientsBaseGame(dt) powerAvailability() player1:update(dt) player2:update(dt) -end \ No newline at end of file +end + +function animateConnection() + if GetIPType(IP) ~= 1 then + love.graphics.printf("WRONG SYNTAX", 0, VIRTUAL_HEIGHT-80, VIRTUAL_WIDTH, "center") + else + counter = counter + 1 / love.timer.getFPS() + if counter < 0.8 then + love.graphics.printf("TRYING TO CONNECT.", 0, VIRTUAL_HEIGHT-80, VIRTUAL_WIDTH, "center") + elseif counter < 1.6 then + love.graphics.printf("TRYING TO CONNECT..", 0, VIRTUAL_HEIGHT-80, VIRTUAL_WIDTH, "center") + elseif counter < 2.4 then + love.graphics.printf("TRYING TO CONNECT...", 0, VIRTUAL_HEIGHT-80, VIRTUAL_WIDTH, "center") + else + love.graphics.printf("NO CONNECTION!", 0, VIRTUAL_HEIGHT-80, VIRTUAL_WIDTH, "center") + end +end +end +function GetIPType(ip) + -- must pass in a string value + if ip == nil or type(ip) ~= "string" then + return 0 + end + + -- check for format 1.11.111.111 for ipv4 + local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")} + if (#chunks == 4) then + for _,v in pairs(chunks) do + if (tonumber(v) < 0 or tonumber(v) > 255) then + return 0 + end + end + return 1 + else + return 0 + end + + -- check for ipv6 format, should be 8 'chunks' of numbers/letters + local _, chunks = ip:gsub("[%a%d]+%:?", "") + if chunks == 8 then + return 2 + end + + -- if we get here, assume we've been given a random string + return 3 +end \ No newline at end of file