muding 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ *** 0.1.1 / 2006-07-25
2
+
3
+ + 2 bug fixes
4
+ +fixed some issues with Controller::Base
5
+ +removed redirect_to method due to it's non-functioning
6
+
1
7
  *** 0.1.0 / 2006-07-23
2
8
 
3
9
  + 3 major enhancements
@@ -24,7 +24,7 @@ module Acts #:nodoc:
24
24
  # s.destroy
25
25
  #end
26
26
 
27
- puts Time.now.to_s
27
+ #puts Time.now.to_s
28
28
 
29
29
  self.find(:all, :conditions => ["spawn_at <= ? and expires_at >= ?", Time.now, Time.now]).each do |expireable|
30
30
  expireable.spawn
@@ -2,7 +2,6 @@
2
2
 
3
3
  log = Log4r::Logger.new 'main'
4
4
  log.outputters = Log4r::Outputter.stdout
5
-
6
5
  begin
7
6
  server = TCPServer.new('localhost', 4000)
8
7
  threads = []
data/lib/controller.rb CHANGED
@@ -1,8 +1,13 @@
1
1
  module Controller
2
2
  Log = Log4r::Logger.new 'controller'
3
3
  Log.outputters = Log4r::Outputter.stdout
4
+
5
+ class NoCommandException < Exception;end
6
+
7
+
4
8
 
5
9
  class Base
10
+ alias :route :class
6
11
  attr_accessor :input, :body, :rendered
7
12
  attr_accessor :new_route, :default_command, :prompt
8
13
 
@@ -12,6 +17,7 @@ module Controller
12
17
  #next command controller, or default command.
13
18
  #
14
19
  # route_to :route =>StartController, :command=>:begin, :prompt=>">"
20
+ #
15
21
  # This will make the user's next input goto the StartController
16
22
  # with the command "begin". After rendering the command
17
23
  # it will output ">" to the user.
@@ -28,59 +34,82 @@ module Controller
28
34
 
29
35
  self.new_route = config[:controller]
30
36
  self.default_command = config[:command]
37
+
31
38
  self.prompt = config[:prompt] if config[:prompt] != ""
32
39
  self.prompt = render_template(before_view_file) if config[:view] == :default && @@before_view[config[:command]]
33
40
  end
34
41
 
35
42
  #TODO: Implement this.
36
- #Should jump to next controller method right away, instead of waiting for new input.
37
- #r(302,'','Location'=>URL(*a))
38
- #by right away I mean after the command finishes running.
39
- def redirect_to(options={})
40
- config = {:route => self.class, :command => nil, :prompt => "", :view => :default}
41
- config.update options if options.is_a?(Hash)
42
-
43
- end
44
-
43
+ #Should jump to next controller method right after this one finishes
44
+ #def redirect_to(options={})
45
+ # config = {:route => self.class, :command => nil, :prompt => "", :view => :default}
46
+ # config.update options if options.is_a?(Hash)
47
+ #end
48
+
49
+ #render sets the text that will be sent back as a response to this command
50
+ #you can only render once per controller. This is in place to avoid magical
51
+ #renderings from happening out of the developers control.
52
+ #
53
+ #render called with no options yields the view file rendered if one exists.
54
+ #
55
+ # render #=> renders /mud/views/controller/view.rhtml
56
+ #
57
+ # render :text => "send this to them" #=> send this to them
45
58
  def render(options = {})
46
59
  raise "Double Render Error" if self.rendered
47
-
48
60
  self.rendered = true
49
61
  return if options[:nothing]
50
62
  if options[:text]
51
63
  @body = options[:text]
52
64
  return
53
65
  end
54
- @body = render_template(view_file)
55
-
56
- @body += self.prompt if self.prompt
66
+ @body = render_template(view_file(@method))
57
67
  end
58
-
68
+
59
69
  def initialize(command, s = {}) #:nodoc:
60
70
  @method = command.downcase
61
71
  @session = s
62
72
  end
63
73
 
64
- def route
65
- return self.class
74
+ #service is the main method that controlls the flow through the controller.
75
+ #first it runs the method,
76
+ #then it renders the view if something hasn't already been rendered
77
+ #then it tacks the prompt onto the end of the command.
78
+ def service(*a)
79
+ if respond_to? @method
80
+ if self.method(@method).arity >= 1
81
+ send(@method, *a)
82
+ else
83
+ send(@method)
84
+ end
85
+ end
86
+ if !self.rendered
87
+ render
88
+ end
89
+
90
+ @body += self.prompt if self.prompt
91
+
92
+ session[:handle].puts(@body)
93
+
94
+ self
66
95
  end
67
-
68
- def view_file
69
- command_file = @method + ".rhtml"
96
+
97
+ def view_file(method)
98
+ command_file = method + ".rhtml"
70
99
  load_and_read_file(command_file)
71
100
  end
72
101
 
73
102
  def before_view_file
74
- command_file = @@before_view[default_command].to_s + ".rhtml"
75
- load_and_read_file(command_file)
103
+ view_file @@before_view[default_command].to_s
76
104
  end
77
105
 
106
+ #this method uses view_dir...
78
107
  def load_and_read_file(file_name)
79
108
  file = File.open(File.join(view_dir, file_name))
80
109
  file.read
81
- rescue Errno::ENOENT
82
- puts $!
83
- return "Could not find the proper view for this command"
110
+ rescue Errno::ENOENT
111
+ #if we haven't at least rendered something, we are going to through a nocommand exception here..
112
+ raise Controller::NoCommandException
84
113
  end
85
114
 
86
115
  def view_dir
@@ -91,14 +120,6 @@ module Controller
91
120
  ERB.new(vf, nil, '-').result(binding)
92
121
  end
93
122
 
94
- def service(*a)
95
- send(@method, *a) if respond_to? @method
96
- if !self.rendered
97
- render
98
- end
99
- self
100
- end
101
-
102
123
  def session()
103
124
  return @session
104
125
  end
@@ -113,8 +134,8 @@ module Controller
113
134
  end
114
135
 
115
136
  class NotFound < Base
116
- def get(p)
117
- render :text=>"#{p} Could not be found."
137
+ def get
138
+ render :text=>"There is no command for that."
118
139
  end
119
140
  end
120
141
 
@@ -124,9 +145,7 @@ module Controller
124
145
  end
125
146
  end
126
147
 
127
- #Load all the controllers.
128
- load "./mud/controllers/mud.rb"
129
- Dir["./mud/controllers/*_controller.rb"].sort.each { |ext| load ext.to_s }
148
+ #Load all the controllers.
149
+ load "./mud/controllers/mud.rb"
150
+ Dir["./mud/controllers/*_controller.rb"].sort.each { |ext| load ext.to_s }
130
151
  end
131
-
132
-
data/lib/handle.rb CHANGED
@@ -25,18 +25,31 @@ class Handle
25
25
  response = Muding.run("begin", self)
26
26
 
27
27
  while true
28
- peer.puts response.body
29
- self.route = response.new_route if response.new_route
30
-
31
- self.default_command = nil
32
- self.default_command = response.default_command if response.default_command
33
- response = Muding.run(peer.gets, self)
28
+ update_route(response) if response
29
+ response = Muding.run(peer.gets, self) #blocks on peer.gets
34
30
  end
31
+
35
32
  rescue Errno::EPIPE
36
33
  Log.info "Connection " + self.object_id.to_s + " Severed"
37
34
  end
38
35
 
36
+ def update_route(response)
37
+ self.route = response.new_route if response.new_route
38
+ self.default_command = nil
39
+ self.default_command = response.default_command if response.default_command
40
+ end
41
+
39
42
  def message(message_string)
40
43
  peer.puts message_string
41
44
  end
45
+
46
+ def puts(string)
47
+ peer.puts(string)
48
+ rescue Error
49
+ end
50
+
51
+ def shutdown()
52
+ read_and_write = 2
53
+ peer.shutdown(read_and_write)
54
+ end
42
55
  end
data/lib/muding.rb CHANGED
@@ -30,13 +30,19 @@ module Muding
30
30
  args = input
31
31
  end
32
32
 
33
+ if command == nil
34
+ raise Controller::NoCommandException
35
+ end
36
+
33
37
  return route.new(command, session).service(*args)
34
38
 
39
+ rescue Controller::NoCommandException
40
+ return Controller::NotFound.new("get",session).service()
35
41
  rescue Errno::EPIPE
36
42
  raise
37
43
  rescue Exception => x
38
44
  Log.warn x
39
- return Controllers::ServerError.new("get",session).service()
45
+ return Controller::ServerError.new("get",session).service()
40
46
  end
41
47
  def reload_controller_code
42
48
  load "./mud/controllers/mud.rb"
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: muding
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-07-23 00:00:00 -05:00
6
+ version: 0.1.1
7
+ date: 2006-07-25 00:00:00 -05:00
8
8
  summary: an application framework for building multi-user environments.
9
9
  require_paths:
10
10
  - lib