Class: Settlers::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/settlers/application.rb

Defined Under Namespace

Classes: NullObject

Constant Summary collapse

DEFAULT_PORT =
7777

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/settlers/application.rb', line 7

def initialize
  @collector = Collector.new
  @ui        = UI.new

  @options = OptionParser.with_defaults do |opts|
    opts.banner  = "Usage: #{File.basename($0)} [--client]"
    opts.defaults = %w(--server)
    opts.separator ''
    opts.version = Settlers::VERSION

    opts.on('-c', '--client', 'Discover and connect to a game server.') do
      @announcer  = NullObject.new
      @discoverer = Bonjour.new
      @server     = NullObject.new
    end

    opts.on('-s', '--server[=PORT]', Integer, 'Launch, announce, and connect to a game server.', ". PORT defaults to #{DEFAULT_PORT}.") do |port|
      @announcer  = Bonjour.new
      @discoverer = NullObject.new
      @server     = Server.new(port || DEFAULT_PORT)
    end
  end
end

Instance Method Details

#run(args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/settlers/application.rb', line 31

def run(args)
  @options.order(args)

  @server.add_observer(@announcer)
  @server.add_observer(@collector)
  @server.start

  @discoverer.add_observer(@collector)
  @discoverer.start

  @ui.choose_server(@collector) do |address|
    Client.new(address).start
  end
end