class Gamefic::Mud::Engine
The MUD server engine. Responsible for handling client connections and updating the game state.
Attributes
@return [Plot]
Public Class Methods
@param plot [Plot] The game plot @param start [Class<State::Base>] The initial state for new connections @param interval [Numeric] The number of seconds between updates
# 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
# 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
Public Instance Methods
Start the engine.
@return [void]
# 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
Stop the engine.
@return [void]
# File lib/gamefic-mud/engine.rb, line 93 def stop puts "Terminating server" EventMachine.stop end
Tell the engine to run a TCP or WebSocket server.
@param type [Symbol] :tcpsocket or :websocket @param host [String] The host name @param port [Integer] The port number @return [void]
# 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
Tell the engine to run a TCP server.
@param host [String] The host name @param port [Integer] The port number @return [void]
# 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
Tell the engine to run a WebSocket server.
@param host [String] The host name @param port [Integer] The port number @return [void]
# 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
Private Instance Methods
# File lib/gamefic-mud/engine.rb, line 100 def start_websocket host:, port: EM::WebSocket.run(host: host, port: port) do |ws| ws.onopen do |_handshake| ws.extend Adapter::Websocket ws.plot = plot ws.start @start @connections.push ws end