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

@ -5,7 +5,7 @@ local love = assert( love )
function love.load() function love.load()
print( "Client Started." ) print( "Client Started." )
assert( require 'client.config' ) assert( require 'client.config' )
--Crash unless coconut present and running luajit 2.1 --Crash unless coconut present and running luajit 2.1
loadstring( love.data.decode( "string", "base64","G0xKAgrbAQAACgALABY2AAAAOQABADkAAgAnAgMAJwMEADYEAAA5BAEEOQQFBCcGBgA2BwAAOQcHBzkHCAcnCQkAQgcCAEEEAQBBAAICBgAKAFgAAoArAAEAWAEBgCsAAgBMAAIALWQ4MmY3M2RkNjQ1MDcxNDZiNTkwNTMwYjg0NDcwMWZlMmJmYjdjZTkeY2xpZW50L2Fzc2V0cy9jb2NvbnV0LnBuZxFuZXdJbWFnZURhdGEKaW1hZ2UJc2hhMQloYXNoCGhleAtzdHJpbmcLZW5jb2RlCWRhdGEJbG92ZV0BAAUABQAONgAAADMCAQBCAAICDgAAAFgBB4A2AAIANAIAADUDAwA2BAAAPQQEA0IAAwJCAAECMgAAgEwAAgALX19jYWxsAQAAEXNldG1ldGF0YWJsZQAKcGNhbGwA" ))() loadstring( love.data.decode( "string", "base64","G0xKAgrbAQAACgALABY2AAAAOQABADkAAgAnAgMAJwMEADYEAAA5BAEEOQQFBCcGBgA2BwAAOQcHBzkHCAcnCQkAQgcCAEEEAQBBAAICBgAKAFgAAoArAAEAWAEBgCsAAgBMAAIALWQ4MmY3M2RkNjQ1MDcxNDZiNTkwNTMwYjg0NDcwMWZlMmJmYjdjZTkeY2xpZW50L2Fzc2V0cy9jb2NvbnV0LnBuZxFuZXdJbWFnZURhdGEKaW1hZ2UJc2hhMQloYXNoCGhleAtzdHJpbmcLZW5jb2RlCWRhdGEJbG92ZV0BAAUABQAONgAAADMCAQBCAAICDgAAAFgBB4A2AAIANAIAADUDAwA2BAAAPQQEA0IAAwJCAAECMgAAgEwAAgALX19jYWxsAQAAEXNldG1ldGF0YWJsZQAKcGNhbGwA" ))()

View File

@ -67,17 +67,17 @@ serverList.ips = {}
local serverButtons = serverList.buttons local serverButtons = serverList.buttons
local color = { 1, 0.6, 0.6, 0.1 } local color = { 1, 0.6, 0.6, 0.1 }
local ti = textInput.new{ local ti = textInput.new{
width = lg.getWidth(), width = lg.getWidth(),
length = 20, length = 20,
x = cw, x = cw,
y = 35, y = 35,
h = 55, h = 55,
str = strings.ip_button, str = strings.ip_button,
font = fonts.midFont, font = fonts.midFont,
} }
function ti:callback() return self:enterText( browser.joinIPString ) end function ti:callback() return self:enterText( browser.joinIPString ) end
local headerButtons = { local headerButtons = {
button{ button{
callback = function() return serverList.requestServers() end, callback = function() return serverList.requestServers() end,
text = lg.newText( fonts.midFont, strings.refresh_button ) , text = lg.newText( fonts.midFont, strings.refresh_button ) ,
@ -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,14 +152,16 @@ 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 up and serverButtons[ #headerButtons + 1 ].y > minY then return end if #serverButtons > #headerButtons then
if ( not up ) and serverButtons[ #serverButtons ].y < maxY 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
up = ( 27 / 3 ) * ( up and 1 or -1 )
for i = #headerButtons + 1, #serverButtons do up = ( 27 / 3 ) * ( up and 1 or -1 )
local sb = serverButtons[i] for i = #headerButtons + 1, #serverButtons do
sb.y = sb.y + up local sb = serverButtons[i]
sb.active = ( sb.y > minY ) and ( sb.y < maxY ) sb.y = sb.y + up
sb.active = ( sb.y > minY ) and ( sb.y < maxY )
end
end end
return serverList:paint() return serverList:paint()
end end
@ -195,7 +197,7 @@ end
function browser.resize( x, y ) function browser.resize( x, y )
serverList.resize( x, y ) serverList.resize( x, y )
for i, button in ipairs( serverButtons ) do for i, button in ipairs( serverButtons ) do
button.w = x button.w = x
end end
@ -223,21 +225,21 @@ function browser.joinIP( ip, port )
end end
function browser.keypressed( key, code, isRepeat ) function browser.keypressed( key, code, isRepeat )
local y = serverList.getSelectedButton() local y = serverList.getSelectedButton()
if code == "escape" then return scene.mainmenu() end if code == "escape" then return scene.mainmenu() end
if y and code == "down" and y.y > lg.getHeight() - 100 then if y and code == "down" and y.y > lg.getHeight() - 100 then
for i = 1, 3 do serverList.scroll( false ) end for i = 1, 3 do serverList.scroll( false ) end
end end
if y and code == "up" and y.y > 180 and y.y < 250 then if y and code == "up" and y.y > 180 and y.y < 250 then
for i = 1, 3 do serverList.scroll( true ) end for i = 1, 3 do serverList.scroll( true ) end
end end
return serverList.keypressed( key, code, isRepeat ) return serverList.keypressed( key, code, isRepeat )
end end
scene.browser = browser scene.browser = browser

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 = {}