ruby-mud 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9acab5713021f56b21844f932f356c3375fc5b0a
4
- data.tar.gz: fae204f94afedf57dbc37a7b832161d589382f0f
3
+ metadata.gz: 21f775f532d3ecb4f1fe785b2bf43375a333f50d
4
+ data.tar.gz: c5adb0167b1f4c3e085a523d97b0af5265515f80
5
5
  SHA512:
6
- metadata.gz: 5c565c5be311e2694a21ad8681a5eb47ddfff26dde8c0034760430c563560fe8e791442f3a617913a3f2485c1a694035eb203ea3a9c66de5f5fffbfe8b0a8a3f
7
- data.tar.gz: 85493ba37834c7591bd628148cc274c88036fba0a2ec7f3315b3a86fa12ebc915de0d03cb17700b4bd274ec4cef69a1431689a6b7340b7872b7b149ba915b24d
6
+ metadata.gz: 9c57d647655acb4d75b3050e8cee7398e6ba6b24b7808e45c4f0462cc04bc51880b7cb84755b935ec4ac44de8f3ddb7f568b0c7ef70c289185eb849993b1009b
7
+ data.tar.gz: 07c224d01d9d1840e7a1c1d692749a6b1ec0427b36a969b117115de9b9e5c0b350bf6a3246b3fc07b8545671d43d5c9261183dfdda8f941b554b6c216469e339
@@ -0,0 +1,103 @@
1
+ # Transfers input/output from the Telnet world into the Ruby world while
2
+ # abstracting away all the rough spots.
3
+ # All controllers should inherit from here
4
+ class MudServer::AbstractController
5
+
6
+ attr_accessor :session, :params
7
+
8
+ # Instantiates a new controller instance. It is highly recomended that derived
9
+ # controller classes either not modify this method, call super, or proxy via a
10
+ # helper method (such as create() or buil(), etc).
11
+ #
12
+ # * +session+ - (Session) The connection session that the controller will
13
+ # communicate with.
14
+ def initialize(session)
15
+ @session = session
16
+ on_start
17
+ true
18
+ end
19
+
20
+ # User definable callback called after instantiation.
21
+ def on_start
22
+ end
23
+
24
+ # Sends a string to remote client over the TCP socket connection.
25
+ def send_text(command)
26
+ @session.connection.puts(command)
27
+ end
28
+
29
+ # Void paramaterless controller action that closes TCP socket to client.
30
+ # You must whitelist this command in allowed_methods() in order for it to be
31
+ # usable by players.
32
+ def quit
33
+ session.connection.close
34
+ end
35
+
36
+ # Parses arbitrary user input into a format usable by the interpreter. Strips
37
+ # all input after initial command and stores it in `params`.
38
+ #
39
+ # * +command+ - (String) A string of space seperated commands and arguments
40
+ #
41
+ # parse_command('login user_x password123')
42
+ # # => True
43
+ # params() # Note: side effects
44
+ # # => 'user_x password123'
45
+ def get_text(command)
46
+ command = command.split(' ')
47
+ head = command.shift
48
+ @params = command.join(' ')
49
+ interpret_command head.to_s.downcase
50
+ end
51
+
52
+ # Interprets a controller command (first argument of user input)
53
+ #
54
+ # * +head+ - (String) The command to execute. Will only execute the command if
55
+ # it is within the 'allowed_methods' dynamic array.
56
+ #
57
+ # interpret_command('add') # Assumes the current controller has
58
+ # # an add() method defined and within
59
+ # # => 10 # the allowed_methods Array.
60
+ def interpret_command(head)
61
+ if allowed_methods.include? head
62
+ self.send(head)
63
+ else
64
+ send_error
65
+ end
66
+ rescue => error
67
+ send_text 'You just broke something. Please tell the admins about this.'
68
+ send_text error.message
69
+ end
70
+
71
+ # A whimsical way of telling the user they input an unknown /
72
+ # unauthorized command. Override this if you want a '404 page' on
73
+ # your controller
74
+ def send_error
75
+ send_text funny_responses.sample
76
+ end
77
+
78
+ # Parameterless method used for generating quirky messages when the
79
+ # user attempts to use a non-existant / forbidden controller action.
80
+ # Returns String.
81
+ def funny_responses
82
+ [
83
+ 'Huh?',
84
+ "What's that you say?",
85
+ 'come again?',
86
+ "Sorry, I don't know that command."
87
+ ]
88
+ end
89
+
90
+ # Dynamically generated list of user accesible controller methods. By default
91
+ # (and when used in conjugation with +super+ in derived classes), will return
92
+ # ['quit'] as its only accessible method. Returns Array of Strings.
93
+ def allowed_methods
94
+ ['quit']
95
+ end
96
+
97
+ # Transfers control from on controller to another. Eg: Move from login
98
+ # controler to main game.
99
+ def transfer_to(controller_name)
100
+ @session.controller = controller_name.new(@session)
101
+ end
102
+
103
+ end
@@ -0,0 +1,8 @@
1
+ # This is a default implementation and entry point for all Mud activity.
2
+ # Override this controller in Your implementation. See AbstractController for
3
+ # better documentation.
4
+ class MudServer::DefaultController < MudServer::AbstractController
5
+ def on_start
6
+ send_text 'It works! Type `quit` to end'
7
+ end
8
+ end
@@ -0,0 +1,37 @@
1
+ #The server class creates a single instance of a mud server.
2
+ class MudServer
3
+
4
+ attr_accessor :connection_pool, :tcp_socket,
5
+ :connection_acceptor, :port, :ip, :environment
6
+
7
+ def initialize(ip = "0.0.0.0", port = 4000, environment = 'development')
8
+ bootstrap_settings(ip, port, environment)
9
+ end
10
+
11
+ def bootstrap_settings(ip, port, environment)
12
+ @port = port
13
+ @ip = ip
14
+ @environment = environment
15
+ @connection_pool = [] # This is where we keep reference to all game
16
+ # connections
17
+ end
18
+
19
+ def start
20
+ @tcp_socket = TCPServer.new @ip , @port
21
+ @connection_acceptor = Thread.new do
22
+ while connection = @tcp_socket.accept
23
+ @connection_pool << MudServer::Session.new(connection)
24
+ end
25
+ end
26
+ return true
27
+ end
28
+
29
+ # You probably won't need this one in production, but it's a must for testing.
30
+ def stop
31
+ @tcp_socket.close
32
+ @connection_acceptor.kill
33
+ @connection_acceptor = nil
34
+ return true
35
+ end
36
+
37
+ end
@@ -0,0 +1,24 @@
1
+ class MudServer::Session
2
+
3
+ attr_accessor :connection, :ip_addr, :input_thread, :controller
4
+
5
+ def initialize(connection, controller = MudServer::DefaultController)
6
+ bootstrap_settings(connection, controller)
7
+ start
8
+ end
9
+
10
+ def bootstrap_settings(connection, controller)
11
+ @connection = connection
12
+ @ip_addr = @connection.peeraddr[-1]
13
+ @controller = controller.new(self)
14
+ end
15
+
16
+ def start
17
+ @input_thread = Thread.new(@connection) do |client|
18
+ while (client_input = client.gets.chomp)
19
+ @controller.get_text client_input
20
+ end
21
+ end
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-mud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Carlino
@@ -59,6 +59,10 @@ extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
61
  - lib/ruby_mud.rb
62
+ - lib/mud_server.rb
63
+ - lib/default_controller.rb
64
+ - lib/abstract_controller.rb
65
+ - lib/session.rb
62
66
  homepage: https://github.com/rickcarlino/mud
63
67
  licenses:
64
68
  - MIT