Class: Gamefic::Mud::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic-mud/engine.rb

Overview

The MUD server engine. Responsible for handling client connections and updating the game state.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plot, start: State::Guest, interval: 1) ⇒ Engine

Returns a new instance of Engine.

Parameters:

  • plot (Plot)

    The game plot

  • start (Class<State::Base>) (defaults to: State::Guest)

    The initial state for new connections

  • interval (Numeric) (defaults to: 1)

    The number of seconds between updates



16
17
18
19
20
21
22
23
# File 'lib/gamefic-mud/engine.rb', line 16

def initialize plot, start: State::Guest, interval: 1
  @plot = plot
  @start = start
  @interval = interval
  @web_connections = {}
  @accepts = []
  @connections = []
end

Instance Attribute Details

#plotPlot (readonly)

Returns:

  • (Plot)


11
12
13
# File 'lib/gamefic-mud/engine.rb', line 11

def plot
  @plot
end

Class Method Details

.start(plot, **options) {|engine| ... } ⇒ Object

Yields:

  • (engine)


84
85
86
87
88
# File 'lib/gamefic-mud/engine.rb', line 84

def self.start plot, **options
  engine = new(plot, **options)
  yield(engine) if block_given?
  engine.run
end

Instance Method Details

#runvoid

This method returns an undefined value.

Start the engine.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gamefic-mud/engine.rb', line 56

def run
  EM.epoll
  EM.run do
    trap("TERM") { stop }
    trap("INT")  { stop }

    # Start a default TCP server if none are configured
    will_accept if @accepts.empty?

    @accepts.each do |a|
      if a[:type] == :websocket
        start_websocket host: a[:host], port: a[:port]
      else
        start_tcpsocket host: a[:host], port: a[:port]
      end
    end

    EventMachine.add_periodic_timer @interval do
      plot.update
      plot.ready
      @connections.each do |conn|
        next unless conn.character
        conn.update conn.character.output
      end
    end
  end
end

#stopvoid

This method returns an undefined value.

Stop the engine.



93
94
95
96
# File 'lib/gamefic-mud/engine.rb', line 93

def stop
  puts "Terminating server"
  EventMachine.stop
end

#will_accept(type: :tcpsocket, host: '0.0.0.0', port: 4342) ⇒ void

This method returns an undefined value.

Tell the engine to run a TCP or WebSocket server.

Parameters:

  • type (Symbol) (defaults to: :tcpsocket)

    :tcpsocket or :websocket

  • host (String) (defaults to: '0.0.0.0')

    The host name

  • port (Integer) (defaults to: 4342)

    The port number



31
32
33
# File 'lib/gamefic-mud/engine.rb', line 31

def will_accept type: :tcpsocket, host: '0.0.0.0', port: 4342
  @accepts.push({ type: type, host: host, port: port })
end

#will_accept_tcpsocket(host: '0.0.0.0', port: 4342) ⇒ void

This method returns an undefined value.

Tell the engine to run a TCP server.

Parameters:

  • host (String) (defaults to: '0.0.0.0')

    The host name

  • port (Integer) (defaults to: 4342)

    The port number



40
41
42
# File 'lib/gamefic-mud/engine.rb', line 40

def will_accept_tcpsocket host: '0.0.0.0', port: 4342
  will_accept type: :tcpsocket, host: host, port: port
end

#will_accept_websocket(host: '0.0.0.0', port: 4343) ⇒ void

This method returns an undefined value.

Tell the engine to run a WebSocket server.

Parameters:

  • host (String) (defaults to: '0.0.0.0')

    The host name

  • port (Integer) (defaults to: 4343)

    The port number



49
50
51
# File 'lib/gamefic-mud/engine.rb', line 49

def will_accept_websocket host: '0.0.0.0', port: 4343
  will_accept type: :websocket, host: host, port: port
end