Class: Gamefic::Mud::Engine
- Inherits:
-
Object
- Object
- Gamefic::Mud::Engine
- 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
- #plot ⇒ Plot readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(plot, start: State::Guest, interval: 1) ⇒ Engine
constructor
A new instance of Engine.
-
#run ⇒ void
Start the engine.
-
#stop ⇒ void
Stop the engine.
-
#will_accept(type: :tcpsocket, host: '0.0.0.0', port: 4342) ⇒ void
Tell the engine to run a TCP or WebSocket server.
-
#will_accept_tcpsocket(host: '0.0.0.0', port: 4342) ⇒ void
Tell the engine to run a TCP server.
-
#will_accept_websocket(host: '0.0.0.0', port: 4343) ⇒ void
Tell the engine to run a WebSocket server.
Constructor Details
#initialize(plot, start: State::Guest, interval: 1) ⇒ Engine
Returns a new instance of Engine.
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
#plot ⇒ Plot (readonly)
11 12 13 |
# File 'lib/gamefic-mud/engine.rb', line 11 def plot @plot end |
Class Method Details
.start(plot, **options) {|engine| ... } ⇒ Object
84 85 86 87 88 |
# File 'lib/gamefic-mud/engine.rb', line 84 def self.start plot, ** engine = new(plot, **) yield(engine) if block_given? engine.run end |
Instance Method Details
#run ⇒ void
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 |
#stop ⇒ void
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.
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.
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.
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 |