Class: Gamefic::Cgi::Engine

Inherits:
Engine
  • Object
show all
Defined in:
lib/gamefic/engine/cgi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#turn

Constructor Details

#initialize(plot, args = {}) ⇒ Engine

Returns a new instance of Engine.



30
31
32
33
34
35
36
37
38
# File 'lib/gamefic/engine/cgi.rb', line 30

def initialize(plot, args = {})
  super(plot)
  @session_file = args[:session_file] || 'save.dat'
  @message_format = args[:message_format] || :html
  @response_format = args[:response_format] || :json
  @new_game = args[:new_game] || false
  @entity_keys = Hash.new
  @introducing = true
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



29
30
31
# File 'lib/gamefic/engine/cgi.rb', line 29

def user
  @user
end

Instance Method Details

#begin_sessionObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gamefic/engine/cgi.rb', line 42

def begin_session
  # Initialize keys for all entities
  @plot.entities.each { |e|
    @entity_keys[e.cgi_key] = e
  }
  @entity_keys['yourself'] = @user.character
  if !@new_game and @session_file != nil
    if File.exist?(@session_file)
      load @session_file
      @introducing = false
    end
  end
end

#end_sessionObject



68
69
70
# File 'lib/gamefic/engine/cgi.rb', line 68

def end_session
  save @session_file
end

#post_initializeObject



39
40
41
# File 'lib/gamefic/engine/cgi.rb', line 39

def post_initialize
  @user = Cgi::User.new @plot
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gamefic/engine/cgi.rb', line 55

def run
  if @introducing == true
    @plot.introduce @user.character
  else
    @plot.instance_variable_get(:@players).push @user.character
    tick
  end
  response = Hash.new
  response[:output] = @user.stream.flush
  response[:prompt] = @user.character.state.prompt
  response[:state] = @user.character.state.class.to_s.split('::').last
  return JSON.generate(response)
end