Skip to content

Commit 7e45bbe

Browse files
committed
Let the server resolve the map path
1 parent aa5b755 commit 7e45bbe

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

lib/config.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@
33
class Config
44
def initialize(options = {})
55
filepath = options[:file] || 'autoexec.cfg'
6+
@type = options[:type] || :client
67
init_configs
78
load_cfg(filepath)
89
end
910

10-
def init_configs
11+
def init_client_configs
1112
@configs = {
1213
password: { help: 'Password to the server', default: '' }
1314
}
1415
@commands = {
1516
echo: { help: 'Echo the text', callback: proc { |arg| puts arg } },
1617
quit: { help: 'Quit', callback: proc { |_| exit } }
1718
}
19+
end
20+
21+
def init_server_configs
22+
@configs = {
23+
sv_map: { help: 'map', default: 'dm1' }
24+
}
25+
@commands = {
26+
shutdown: { help: 'shutdown server', callback: proc { |_| exit } }
27+
}
28+
end
29+
30+
def init_configs
31+
if @type == :client
32+
init_client_configs
33+
else
34+
init_server_configs
35+
end
1836
@configs.each do |cfg, data|
1937
self.class.send(:attr_accessor, cfg)
2038
instance_variable_set("@#{cfg}", data[:default])

lib/game_server.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class GameServer
1818

1919
def initialize(server)
2020
@server = server
21+
@config = server.config
22+
@map_path = nil
2123
@ack_game_tick = -1
2224
@pred_game_tick = 0
2325
@map = Map.new(
@@ -28,6 +30,29 @@ def initialize(server)
2830
)
2931
end
3032

33+
def load_map
34+
puts "loading map '#{@config.sv_map}' ..."
35+
map_path = nil
36+
if File.exist? "data/#{@config.sv_map}.map"
37+
map_path = "data/#{@config.sv_map}.map"
38+
elsif File.exist? "data/maps/#{@config.sv_map}.map"
39+
map_path = "data/maps/#{@config.sv_map}.map"
40+
elsif File.exist? "maps/#{@config.sv_map}.map"
41+
map_path = "maps/#{@config.sv_map}.map"
42+
elsif File.exist? "#{Dir.home}/.teeworlds/maps/#{@config.sv_map}.map"
43+
map_path = "#{Dir.home}/.teeworlds/maps/#{@config.sv_map}.map"
44+
end
45+
46+
if map_path.nil?
47+
puts "map not found '#{@config.sv_map}'"
48+
# TODO: this should error when the feature is done
49+
# exit 1
50+
else
51+
puts "found at #{map_path}"
52+
@map_path = map_path
53+
end
54+
end
55+
3156
##
3257
# call_hook
3358
#

lib/teeworlds_server.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require_relative 'net_base'
1212
require_relative 'models/net_addr'
1313
require_relative 'packer'
14+
require_relative 'config'
1415
require_relative 'game_server'
1516
require_relative 'models/token'
1617
require_relative 'messages/sv_emoticon'
@@ -65,14 +66,16 @@ def seq
6566
end
6667

6768
class TeeworldsServer
68-
attr_accessor :clients
69+
attr_accessor :clients, :config
6970
attr_reader :hooks, :shutdown_reason, :current_game_tick
7071

7172
def initialize(options = {})
7273
@verbose = options[:verbose] || false
7374
@ip = '127.0.0.1'
7475
@port = 8303
76+
@config = Config.new(file: options[:config], type: :server)
7577
@game_server = GameServer.new(self)
78+
@game_server.load_map
7679
# @type clients [Hash<Integer, Client>]
7780
@clients = {}
7881
@current_game_tick = 0

0 commit comments

Comments
 (0)