Server browser functionality.

This commit is contained in:
wan-may 2023-09-13 00:22:31 -03:00
parent dc6cdf123e
commit e6b9e7aa13
8 changed files with 86 additions and 33 deletions

View File

@ -1,10 +1,33 @@
#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

@ -11,7 +11,7 @@ return {
chat = "t",
love = "q",
hate = "e",
}
},
serverIP = "192.168.2.15",
serverPort = 51312,
}

View File

@ -6,7 +6,7 @@ local connecting = {}
local time, ip, port = 0
function connecting.draw()
lg.print( "Connecting to server:\t"..time, 0, 0, 0 )
lg.print( ("CONNECTING:\nTIME: %1.1fs\nADDRESS: %s:%d"):format(time, ip, port), 0, 0, 0 )
return false
end
@ -20,8 +20,10 @@ function connecting.update(dt)
end
function connecting:onLoad( params )
params = params or { ip = "127.0.0.0", port = 8 }
local ip, port = params.ip, params.port
print( "connecting:", params, self.ip, self.port )
for k, v in pairs( self ) do print( k,v ) end
params = params or { ip = "8.8.8.8", port = 8 }
ip, port = params.ip, params.port
return server.connect( ip, port )
end

View File

@ -41,7 +41,6 @@ function game.disconnect( )
end
function game.onLoad( params )
end
function game.keypressed( key, code, isRepeat )

View File

@ -4,6 +4,7 @@ local textInput = assert( require 'client.ui.textinput' )
local button = assert( require 'client.ui.button' )
local packet = assert( require 'shared.packet' )
local ipString = assert( require 'shared.ipstring' )
local menu = assert( require 'client.ui.menu' )
local browser = {}
local font = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 20 )
@ -20,7 +21,7 @@ local function populateTestServers()
map = "testMap_01",
}
packet.serverInfo{
players = 24,
players = 8,
capacity = 255,
ip = ipString.new{ 123, 456, 789, 101 },
port = 51312,
@ -29,8 +30,8 @@ local function populateTestServers()
map = "testMap_02",
}
packet.serverInfo{
players = 24,
capacity = 255,
players = 14,
capacity = 64,
ip = ipString.new{ 150, 645, 151, 67 },
port = 51312,
version = 25,
@ -48,7 +49,24 @@ local function populateTestServers()
}
return packet.deserialise( packet.get() )
end
local serverList = {}
local function joinServerCallback( button )
if button.ip and button.port then return browser.joinIP( button.ip, button.port ) end
end
local serverButtons = populateTestServers()
for i, server in ipairs( serverButtons ) do
serverButtons[i] = button{
ip = tostring( server.ip ),
port = server.port,
text = tostring( server.ip ),
callback = joinServerCallback}
end
local serverList = menu.new( "serverList",
serverButtons,
nil,
nil,
nil,
true )
serverList.servers = populateTestServers()
serverList.offsets = {
15,
@ -65,7 +83,7 @@ serverList.x = 25
serverList.y = 220
serverList.h = 36
function serverList.draw()
--[[function serverList.draw()
local gs = packet.getString
local x, y, h = serverList.x, serverList.y, serverList.h
local oldFont = lg.getFont()
@ -89,7 +107,7 @@ function serverList.draw()
y = y + h + 5
end
lg.setFont( oldFont )
end
end]]
function serverList.select()
@ -110,6 +128,8 @@ local ti = textInput.new{
y = 175
}
browser.selected = false
function browser.draw()
lg.setColor( 1, 1, 1, 1 )
lg.print( "Server Browser", 15, 115 )
@ -122,26 +142,29 @@ function browser.update( dt )
end
function browser.onLoad( )
serverList:onLoad()
lg.setColor( 1, 1, 1, 1 )
end
function browser.mousepressed(x, y, button, istouch, pressed)
if ti:contains( x, y ) then return ti:enterText( browser.joinIPString ) end
return serverList.mousepressed( x, y, button, istouch, pressed )
end
function browser.joinIPString( s )
--Parse IP address and port from string. If it's valid, join the server.
print( "browser: Attempting to join server", s )
print( "browser: entered IP and port", s )
if not s then return end
ti:clear()
local valid, ip, port
local ip, port = s:match '(%d+%.%d+%.%d+%.%d+)', s:match ':(%d+)'
print( "browser:", "ip:", ip, port )
if valid then return browser.joinIP( ip, port ) end
if ip and port then return browser.joinIP( ip, port ) end
end
function browser.joinIP( ip, port )
print( "Joining server:", ip, port )
return scene.game{ serverIP = ip, serverPort = port }
return scene.loadScene( scene.connecting, { ip = ip, port = port } )
end
function browser.keypressed( key, code, isRepeat )

View File

@ -14,7 +14,7 @@ return menu.new(
x = 15, w = lg.getWidth(), y = 115, h = 72,
text = strings.newgame_button,
color = { 0.6, 0.6, 0.6, 0.9 },
callback = function() return scene.connecting() end },
callback = function() return scene.connecting{ip = "127.0.0.0", port = 8} end },
button{
text = strings.join_button,

View File

@ -10,10 +10,13 @@ local canvas
local wWidth, wHeight
local currentMenu
function menu.new( name, buttons, fg, bg, font )
function menu.new( name, buttons, fg, bg, font, subScene )
local t = { buttons = buttons, fg = fg, bg = bg, font = font or lg.getFont() }
scene[name] = t
print( 'scene', scene )
if subScene then setmetatable( t, t )
else
scene[name] = t
print( 'scene', scene )
end
getmetatable( t ).__index = menu
return t
end
@ -21,7 +24,7 @@ end
function menu:onLoad()
print( 'loading:', self.name )
currentMenu = self
lg.setFont( self.font )
if self.font then lg.setFont( self.font ) end
return menu.resize( lg.getDimensions() )
end
@ -117,7 +120,9 @@ function menu:paint()
--bg
lg.setColor( 1, 1, 1, 1 )
lg.draw( self.bg, 0, 0, 0, wWidth, wHeight )
if self.bg then
lg.draw( self.bg, 0, 0, 0, wWidth, wHeight )
end
--buttons
for i = #self.buttons, 1, -1 do self.buttons[i]:draw( ) end
@ -127,12 +132,14 @@ function menu:paint()
local r = math.random
local v = r() * 0.4 + 0.6
local fg = self.fg
fg:setVertexAttribute( 2, 3, v * (0.7 + 0.2 * r()), v * (0.1 + 0.4 * r()), v * (0.1 * r()), 1 )
fg:setVertexAttribute( 1, 3, 0.3 * ( 1 - v ), 0, 0, 0.4 * (1 - v) )
fg:setVertexAttribute( 4, 3, 0.3 * ( 1 - v ), 0, 0, 0.4 * (1 - v) )
fg:setVertexAttribute( 3, 3, v * (0.7 + 0.2 * r()), v * (0.1 + 0.4 * r()), v * (0.1 * r()), 1 )
lg.draw( fg, 0, 0, 0, wWidth, wHeight )
if self.fg then
local fg = self.fg
fg:setVertexAttribute( 2, 3, v * (0.7 + 0.2 * r()), v * (0.1 + 0.4 * r()), v * (0.1 * r()), 1 )
fg:setVertexAttribute( 1, 3, 0.3 * ( 1 - v ), 0, 0, 0.4 * (1 - v) )
fg:setVertexAttribute( 4, 3, 0.3 * ( 1 - v ), 0, 0, 0.4 * (1 - v) )
fg:setVertexAttribute( 3, 3, v * (0.7 + 0.2 * r()), v * (0.1 + 0.4 * r()), v * (0.1 * r()), 1 )
lg.draw( fg, 0, 0, 0, wWidth, wHeight )
end
lg.setCanvas()
end

View File

@ -64,7 +64,6 @@ function server.Start()
udp:settimeout(0)
server.SetIP( socket.dns.toip(socket.dns.gethostname()), 51312 )
assert( udp:setsockname( tostring( svInfo.ip ), svInfo.port ))
server.StartLocalClient()
print( socket.gettime(), "Start." )
repeat
server.Parse( udp:receivefrom() )