From 7e45bbe03828ff6e370755cad4bf6ff830ec0dd4 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Fri, 23 Feb 2024 12:22:27 +0800 Subject: [PATCH] Let the server resolve the map path --- lib/config.rb | 20 +++++++++++++++++++- lib/game_server.rb | 25 +++++++++++++++++++++++++ lib/teeworlds_server.rb | 5 ++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/lib/config.rb b/lib/config.rb index 41527d1..b03e53b 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -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]) diff --git a/lib/game_server.rb b/lib/game_server.rb index 9146c9b..c41a49f 100644 --- a/lib/game_server.rb +++ b/lib/game_server.rb @@ -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 # diff --git a/lib/teeworlds_server.rb b/lib/teeworlds_server.rb index 02751c4..93a628b 100644 --- a/lib/teeworlds_server.rb +++ b/lib/teeworlds_server.rb @@ -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] @clients = {} @current_game_tick = 0