Moving metaserver and server to LOVE (since it's got a working LuaSocket installation OOTB

This commit is contained in:
wan-may 2023-09-17 19:12:13 -03:00
parent ea5b39ebb7
commit 1e144775a5
12 changed files with 68 additions and 76 deletions

6
all.bat Normal file
View File

@ -0,0 +1,6 @@
call build.bat
pushd "%~dp0\build\"
start server.bat
start client.bat
start metaserver.bat
popd

3
build.bat Normal file
View File

@ -0,0 +1,3 @@
@echo on
robocopy src build /E
pause

View File

@ -1,33 +0,0 @@
#get locations of scripts, libraries, engine
SRC_DIR="src"
LIB_DIR="lib"
LOVE_DIR="../../love"
BUILD_DIR="build"
#build LuaJIT for target platform
#build LuaSocket shared library for target platform
#get appropriate copy of love
#zip client scripts into .love file
cp -r $SRC_DIR/ $BUILD_DIR/
cd $BUILD_DIR/
zip -9 -r vision.love
#zip .love file with LOVE and dependencies (and license!)
#zip server scripts with LuaJIT and dependencies
#zip metaserver with LuaJIT and dependencies
#WINDOWS: make fused client executable
cat ../../love/love.exe vision.love > vision.exe
#LINUX: get official LOVE AppImage

View File

@ -113,7 +113,7 @@ for j, headerButton in ipairs( headerButtons ) do
end end
function serverList.requestServers() function serverList.requestServers()
for i = 1, 5 do metaserver.requestServerList() end return metaserver.requestServerList()
end end
function serverList.clear( ) function serverList.clear( )
@ -152,6 +152,7 @@ local metaServerHandlers = setmetatable(
function serverList.scroll( up ) function serverList.scroll( up )
local minY = 170 local minY = 170
local maxY = lg.getHeight() + 40 local maxY = lg.getHeight() + 40
if #serverButtons > #headerButtons then
if up and serverButtons[ #headerButtons + 1 ].y > minY then return end if up and serverButtons[ #headerButtons + 1 ].y > minY then return end
if ( not up ) and serverButtons[ #serverButtons ].y < maxY then return end if ( not up ) and serverButtons[ #serverButtons ].y < maxY then return end
@ -161,6 +162,7 @@ function serverList.scroll( up )
sb.y = sb.y + up sb.y = sb.y + up
sb.active = ( sb.y > minY ) and ( sb.y < maxY ) sb.active = ( sb.y > minY ) and ( sb.y < maxY )
end end
end
return serverList:paint() return serverList:paint()
end end

View File

@ -42,11 +42,13 @@ return menu.new{
}, },
bg = lg.newMesh{ bg = lg.newMesh{
{ 0, 0, 0, 0, 0.4, 0.05, 0.0, 0.1 }, { 0, 0, 0, 0, 1, 1, 1, 0.01 },
{ 1, 0, 1, 0, 0.8, 0.3, 0.1, 0.8 }, { 1, 0, 1, 0, 1, 1, 1, 0.1 },
{ 1, 1, 1, 1, 0.7, 0.4, 0.1, 0.8 }, { 1, 1, 1, 1, 0, 0, 0, 0.1 },
{ 0, 1, 0, 1, 0.4, 0.05, 0.05, 0.1 }, { 0, 1, 0, 1, 0, 0, 0, 0.01 },
}, },
font = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 48 ) font = font,
wheelmoved = function( x, y ) return ( y ~= 0 ) and love.keypressed( nil, (y > 0) and "up" or "down" ) end
} }

View File

@ -62,7 +62,6 @@ local optionsMenu = menu.new{
{ 0, 1, 0, 1, 0.4, 0.1, 0.03, 0.0 }, { 0, 1, 0, 1, 0.4, 0.1, 0.03, 0.0 },
}, },
bg = lg.newMesh{ bg = lg.newMesh{
{ 0, 0, 0, 0, 1, 1, 1, 0.01 }, { 0, 0, 0, 0, 1, 1, 1, 0.01 },
{ 1, 0, 1, 0, 1, 1, 1, 0.1 }, { 1, 0, 1, 0, 1, 1, 1, 0.1 },

View File

@ -24,9 +24,7 @@ local handlers = setmetatable({
if not servers[ip] then servers[ip] = { ip = ip, port = port, info = svInfo } end if not servers[ip] then servers[ip] = { ip = ip, port = port, info = svInfo } end
servers[ip].time = t servers[ip].time = t
packet.advertised{ time = t } packet.advertised{ time = t }
return udp:sendto( packet.get(), ip, port )
local p = packet.get()
for i = 1, 10 do udp:sendto( p, ip, port ) end
end, end,
default = function( s, ip, port ) default = function( s, ip, port )
@ -42,7 +40,7 @@ local handlers = setmetatable({
return print( ip, port, "Client IP mismatch:", clientInfo.ip ) return print( ip, port, "Client IP mismatch:", clientInfo.ip )
end]] end]]
print( "Client requested server list: ", ip, port ) print( "Server List:", ip, port )
clients[ip] = clients[ip] or {} clients[ip] = clients[ip] or {}
@ -50,12 +48,12 @@ local handlers = setmetatable({
clients[ip].time = t clients[ip].time = t
packet.heartbeat{ tick = tick } packet.heartbeat{ tick = tick }
for ip, server in pairs( servers ) do for svIP, server in pairs( servers ) do
print( "", svIP, packet.getString( server.info.svname ))
packet.serverInfo( server.info ) packet.serverInfo( server.info )
end end
local p = packet.get() return udp:sendto( packet.get(), ip, port )
for i = 1, 10 do udp:sendto( p, ip, port ) end
end end
}, },

View File

@ -16,6 +16,10 @@ local svInfo = packet.serverInfo{ version = 13,
local server = { local server = {
tick = 0, tick = 0,
advertised = function()
print( "Advertised." )
end,
} }
local clients = {} local clients = {}
@ -27,6 +31,7 @@ end
function server.Advertise() function server.Advertise()
print( "Advertise." ) print( "Advertise." )
packet.get()
packet.metaServer() packet.metaServer()
packet.serverInfo( svInfo ) packet.serverInfo( svInfo )
udp:sendto( packet.get() , ms.ip, ms.port ) udp:sendto( packet.get() , ms.ip, ms.port )
@ -59,18 +64,26 @@ end
function server.SetIP( ipString, port ) function server.SetIP( ipString, port )
svInfo.ip = shared.ip.fromString( ipString ) svInfo.ip = shared.ip.fromString( ipString )
svInfo.port = port svInfo.port = port
if udp:setsockname( ipString, port ) then
print( ipString, port )
--Find another port.
elseif port < 65536 then
return server.SetIP( ipString, port + 1 )
else
print( "Could not use IP: "..ipString )
return error( "Connection failed." )
end
end end
function server.Start() function server.Start()
udp = assert( socket.udp() ) udp = assert( socket.udp() )
udp:settimeout(0) udp:settimeout(0)
server.SetIP( socket.dns.toip(socket.dns.gethostname()), 51312 ) server.SetIP( shared.myip, 51312 )
assert( udp:setsockname( tostring( svInfo.ip ), svInfo.port ))
print( "Server started:", udp:getsockname() ) print( "Server started:", udp:getsockname() )
repeat repeat
server.Parse( udp:receivefrom() ) server.Parse( udp:receivefrom() )
server.Advance() server.Advance()
if server.tick % 20 == 0 then if server.tick % 50 == 0 then
server.Advertise() server.Advertise()
end end
until socket.sleep( 0.1 ) until socket.sleep( 0.1 )

1
src/shared/getip.lua Normal file
View File

@ -0,0 +1 @@
return assert(assert( require 'socket.http' ).request 'https://api.ipify.org' )

View File

@ -6,10 +6,11 @@ shared.ip = assert( require 'shared.ipstring' )
shared.packet = assert( require 'shared.packet' ) shared.packet = assert( require 'shared.packet' )
shared.print = assert( require 'shared.print' ) shared.print = assert( require 'shared.print' )
shared.metaserver = assert( require 'shared.metaserver' ) shared.metaserver = assert( require 'shared.metaserver' )
shared.myip = assert( require 'shared.getip' )
--Turn on logging? --Turn on logging?
print = shared.print print = shared.print
print( "My IP: ", shared.myip )
--World state. --World state.
local world = {} local world = {}