Let the server resolve the map path

This commit is contained in:
ChillerDragon 2024-02-23 12:22:27 +08:00
parent aa5b755204
commit 7e45bbe038
3 changed files with 48 additions and 2 deletions

View file

@ -3,11 +3,12 @@
class Config
def initialize(options = {})
filepath = options[:file] || 'autoexec.cfg'
@type = options[:type] || :client
init_configs
load_cfg(filepath)
end
def init_configs
def init_client_configs
@configs = {
password: { help: 'Password to the server', default: '' }
}
@ -15,6 +16,23 @@ class Config
echo: { help: 'Echo the text', callback: proc { |arg| puts arg } },
quit: { help: 'Quit', callback: proc { |_| exit } }
}
end
def init_server_configs
@configs = {
sv_map: { help: 'map', default: 'dm1' }
}
@commands = {
shutdown: { help: 'shutdown server', callback: proc { |_| exit } }
}
end
def init_configs
if @type == :client
init_client_configs
else
init_server_configs
end
@configs.each do |cfg, data|
self.class.send(:attr_accessor, cfg)
instance_variable_set("@#{cfg}", data[:default])

View file

@ -18,6 +18,8 @@ class GameServer
def initialize(server)
@server = server
@config = server.config
@map_path = nil
@ack_game_tick = -1
@pred_game_tick = 0
@map = Map.new(
@ -28,6 +30,29 @@ class GameServer
)
end
def load_map
puts "loading map '#{@config.sv_map}' ..."
map_path = nil
if File.exist? "data/#{@config.sv_map}.map"
map_path = "data/#{@config.sv_map}.map"
elsif File.exist? "data/maps/#{@config.sv_map}.map"
map_path = "data/maps/#{@config.sv_map}.map"
elsif File.exist? "maps/#{@config.sv_map}.map"
map_path = "maps/#{@config.sv_map}.map"
elsif File.exist? "#{Dir.home}/.teeworlds/maps/#{@config.sv_map}.map"
map_path = "#{Dir.home}/.teeworlds/maps/#{@config.sv_map}.map"
end
if map_path.nil?
puts "map not found '#{@config.sv_map}'"
# TODO: this should error when the feature is done
# exit 1
else
puts "found at #{map_path}"
@map_path = map_path
end
end
##
# call_hook
#

View file

@ -11,6 +11,7 @@ require_relative 'chunk'
require_relative 'net_base'
require_relative 'models/net_addr'
require_relative 'packer'
require_relative 'config'
require_relative 'game_server'
require_relative 'models/token'
require_relative 'messages/sv_emoticon'
@ -65,14 +66,16 @@ class Client
end
class TeeworldsServer
attr_accessor :clients
attr_accessor :clients, :config
attr_reader :hooks, :shutdown_reason, :current_game_tick
def initialize(options = {})
@verbose = options[:verbose] || false
@ip = '127.0.0.1'
@port = 8303
@config = Config.new(file: options[:config], type: :server)
@game_server = GameServer.new(self)
@game_server.load_map
# @type clients [Hash<Integer, Client>]
@clients = {}
@current_game_tick = 0